lpfc_sli.c 471 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2011 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static IOCB_t *
  65. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  66. {
  67. return &iocbq->iocb;
  68. }
  69. /**
  70. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  71. * @q: The Work Queue to operate on.
  72. * @wqe: The work Queue Entry to put on the Work queue.
  73. *
  74. * This routine will copy the contents of @wqe to the next available entry on
  75. * the @q. This function will then ring the Work Queue Doorbell to signal the
  76. * HBA to start processing the Work Queue Entry. This function returns 0 if
  77. * successful. If no entries are available on @q then this function will return
  78. * -ENOMEM.
  79. * The caller is expected to hold the hbalock when calling this routine.
  80. **/
  81. static uint32_t
  82. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  83. {
  84. union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
  85. struct lpfc_register doorbell;
  86. uint32_t host_index;
  87. /* If the host has not yet processed the next entry then we are done */
  88. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  89. return -ENOMEM;
  90. /* set consumption flag every once in a while */
  91. if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
  92. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  93. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  94. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  95. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  96. /* Update the host index before invoking device */
  97. host_index = q->host_index;
  98. q->host_index = ((q->host_index + 1) % q->entry_count);
  99. /* Ring Doorbell */
  100. doorbell.word0 = 0;
  101. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  102. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  103. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  104. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  105. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  106. return 0;
  107. }
  108. /**
  109. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  110. * @q: The Work Queue to operate on.
  111. * @index: The index to advance the hba index to.
  112. *
  113. * This routine will update the HBA index of a queue to reflect consumption of
  114. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  115. * an entry the host calls this function to update the queue's internal
  116. * pointers. This routine returns the number of entries that were consumed by
  117. * the HBA.
  118. **/
  119. static uint32_t
  120. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  121. {
  122. uint32_t released = 0;
  123. if (q->hba_index == index)
  124. return 0;
  125. do {
  126. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  127. released++;
  128. } while (q->hba_index != index);
  129. return released;
  130. }
  131. /**
  132. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  133. * @q: The Mailbox Queue to operate on.
  134. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  135. *
  136. * This routine will copy the contents of @mqe to the next available entry on
  137. * the @q. This function will then ring the Work Queue Doorbell to signal the
  138. * HBA to start processing the Work Queue Entry. This function returns 0 if
  139. * successful. If no entries are available on @q then this function will return
  140. * -ENOMEM.
  141. * The caller is expected to hold the hbalock when calling this routine.
  142. **/
  143. static uint32_t
  144. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  145. {
  146. struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
  147. struct lpfc_register doorbell;
  148. uint32_t host_index;
  149. /* If the host has not yet processed the next entry then we are done */
  150. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  151. return -ENOMEM;
  152. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  153. /* Save off the mailbox pointer for completion */
  154. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  155. /* Update the host index before invoking device */
  156. host_index = q->host_index;
  157. q->host_index = ((q->host_index + 1) % q->entry_count);
  158. /* Ring Doorbell */
  159. doorbell.word0 = 0;
  160. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  161. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  162. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  163. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  164. return 0;
  165. }
  166. /**
  167. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  168. * @q: The Mailbox Queue to operate on.
  169. *
  170. * This routine will update the HBA index of a queue to reflect consumption of
  171. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  172. * an entry the host calls this function to update the queue's internal
  173. * pointers. This routine returns the number of entries that were consumed by
  174. * the HBA.
  175. **/
  176. static uint32_t
  177. lpfc_sli4_mq_release(struct lpfc_queue *q)
  178. {
  179. /* Clear the mailbox pointer for completion */
  180. q->phba->mbox = NULL;
  181. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  182. return 1;
  183. }
  184. /**
  185. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  186. * @q: The Event Queue to get the first valid EQE from
  187. *
  188. * This routine will get the first valid Event Queue Entry from @q, update
  189. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  190. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  191. * processed, but not popped back to the HBA then this routine will return NULL.
  192. **/
  193. static struct lpfc_eqe *
  194. lpfc_sli4_eq_get(struct lpfc_queue *q)
  195. {
  196. struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
  197. /* If the next EQE is not valid then we are done */
  198. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  199. return NULL;
  200. /* If the host has not yet processed the next entry then we are done */
  201. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  202. return NULL;
  203. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  204. return eqe;
  205. }
  206. /**
  207. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  208. * @q: The Event Queue that the host has completed processing for.
  209. * @arm: Indicates whether the host wants to arms this CQ.
  210. *
  211. * This routine will mark all Event Queue Entries on @q, from the last
  212. * known completed entry to the last entry that was processed, as completed
  213. * by clearing the valid bit for each completion queue entry. Then it will
  214. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  215. * The internal host index in the @q will be updated by this routine to indicate
  216. * that the host has finished processing the entries. The @arm parameter
  217. * indicates that the queue should be rearmed when ringing the doorbell.
  218. *
  219. * This function will return the number of EQEs that were popped.
  220. **/
  221. uint32_t
  222. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  223. {
  224. uint32_t released = 0;
  225. struct lpfc_eqe *temp_eqe;
  226. struct lpfc_register doorbell;
  227. /* while there are valid entries */
  228. while (q->hba_index != q->host_index) {
  229. temp_eqe = q->qe[q->host_index].eqe;
  230. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  231. released++;
  232. q->host_index = ((q->host_index + 1) % q->entry_count);
  233. }
  234. if (unlikely(released == 0 && !arm))
  235. return 0;
  236. /* ring doorbell for number popped */
  237. doorbell.word0 = 0;
  238. if (arm) {
  239. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  240. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  241. }
  242. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  243. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  244. bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
  245. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  246. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  247. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  248. readl(q->phba->sli4_hba.EQCQDBregaddr);
  249. return released;
  250. }
  251. /**
  252. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  253. * @q: The Completion Queue to get the first valid CQE from
  254. *
  255. * This routine will get the first valid Completion Queue Entry from @q, update
  256. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  257. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  258. * processed, but not popped back to the HBA then this routine will return NULL.
  259. **/
  260. static struct lpfc_cqe *
  261. lpfc_sli4_cq_get(struct lpfc_queue *q)
  262. {
  263. struct lpfc_cqe *cqe;
  264. /* If the next CQE is not valid then we are done */
  265. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  266. return NULL;
  267. /* If the host has not yet processed the next entry then we are done */
  268. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  269. return NULL;
  270. cqe = q->qe[q->hba_index].cqe;
  271. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  272. return cqe;
  273. }
  274. /**
  275. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  276. * @q: The Completion Queue that the host has completed processing for.
  277. * @arm: Indicates whether the host wants to arms this CQ.
  278. *
  279. * This routine will mark all Completion queue entries on @q, from the last
  280. * known completed entry to the last entry that was processed, as completed
  281. * by clearing the valid bit for each completion queue entry. Then it will
  282. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  283. * The internal host index in the @q will be updated by this routine to indicate
  284. * that the host has finished processing the entries. The @arm parameter
  285. * indicates that the queue should be rearmed when ringing the doorbell.
  286. *
  287. * This function will return the number of CQEs that were released.
  288. **/
  289. uint32_t
  290. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  291. {
  292. uint32_t released = 0;
  293. struct lpfc_cqe *temp_qe;
  294. struct lpfc_register doorbell;
  295. /* while there are valid entries */
  296. while (q->hba_index != q->host_index) {
  297. temp_qe = q->qe[q->host_index].cqe;
  298. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  299. released++;
  300. q->host_index = ((q->host_index + 1) % q->entry_count);
  301. }
  302. if (unlikely(released == 0 && !arm))
  303. return 0;
  304. /* ring doorbell for number popped */
  305. doorbell.word0 = 0;
  306. if (arm)
  307. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  308. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  309. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  310. bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
  311. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  312. return released;
  313. }
  314. /**
  315. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  316. * @q: The Header Receive Queue to operate on.
  317. * @wqe: The Receive Queue Entry to put on the Receive queue.
  318. *
  319. * This routine will copy the contents of @wqe to the next available entry on
  320. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  321. * HBA to start processing the Receive Queue Entry. This function returns the
  322. * index that the rqe was copied to if successful. If no entries are available
  323. * on @q then this function will return -ENOMEM.
  324. * The caller is expected to hold the hbalock when calling this routine.
  325. **/
  326. static int
  327. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  328. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  329. {
  330. struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
  331. struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
  332. struct lpfc_register doorbell;
  333. int put_index = hq->host_index;
  334. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  335. return -EINVAL;
  336. if (hq->host_index != dq->host_index)
  337. return -EINVAL;
  338. /* If the host has not yet processed the next entry then we are done */
  339. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  340. return -EBUSY;
  341. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  342. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  343. /* Update the host index to point to the next slot */
  344. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  345. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  346. /* Ring The Header Receive Queue Doorbell */
  347. if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
  348. doorbell.word0 = 0;
  349. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  350. LPFC_RQ_POST_BATCH);
  351. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  352. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  353. }
  354. return put_index;
  355. }
  356. /**
  357. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  358. * @q: The Header Receive Queue to operate on.
  359. *
  360. * This routine will update the HBA index of a queue to reflect consumption of
  361. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  362. * consumed an entry the host calls this function to update the queue's
  363. * internal pointers. This routine returns the number of entries that were
  364. * consumed by the HBA.
  365. **/
  366. static uint32_t
  367. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  368. {
  369. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  370. return 0;
  371. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  372. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  373. return 1;
  374. }
  375. /**
  376. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  377. * @phba: Pointer to HBA context object.
  378. * @pring: Pointer to driver SLI ring object.
  379. *
  380. * This function returns pointer to next command iocb entry
  381. * in the command ring. The caller must hold hbalock to prevent
  382. * other threads consume the next command iocb.
  383. * SLI-2/SLI-3 provide different sized iocbs.
  384. **/
  385. static inline IOCB_t *
  386. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  387. {
  388. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  389. pring->cmdidx * phba->iocb_cmd_size);
  390. }
  391. /**
  392. * lpfc_resp_iocb - Get next response iocb entry in the ring
  393. * @phba: Pointer to HBA context object.
  394. * @pring: Pointer to driver SLI ring object.
  395. *
  396. * This function returns pointer to next response iocb entry
  397. * in the response ring. The caller must hold hbalock to make sure
  398. * that no other thread consume the next response iocb.
  399. * SLI-2/SLI-3 provide different sized iocbs.
  400. **/
  401. static inline IOCB_t *
  402. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  403. {
  404. return (IOCB_t *) (((char *) pring->rspringaddr) +
  405. pring->rspidx * phba->iocb_rsp_size);
  406. }
  407. /**
  408. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  409. * @phba: Pointer to HBA context object.
  410. *
  411. * This function is called with hbalock held. This function
  412. * allocates a new driver iocb object from the iocb pool. If the
  413. * allocation is successful, it returns pointer to the newly
  414. * allocated iocb object else it returns NULL.
  415. **/
  416. static struct lpfc_iocbq *
  417. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  418. {
  419. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  420. struct lpfc_iocbq * iocbq = NULL;
  421. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  422. if (iocbq)
  423. phba->iocb_cnt++;
  424. if (phba->iocb_cnt > phba->iocb_max)
  425. phba->iocb_max = phba->iocb_cnt;
  426. return iocbq;
  427. }
  428. /**
  429. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  430. * @phba: Pointer to HBA context object.
  431. * @xritag: XRI value.
  432. *
  433. * This function clears the sglq pointer from the array of acive
  434. * sglq's. The xritag that is passed in is used to index into the
  435. * array. Before the xritag can be used it needs to be adjusted
  436. * by subtracting the xribase.
  437. *
  438. * Returns sglq ponter = success, NULL = Failure.
  439. **/
  440. static struct lpfc_sglq *
  441. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  442. {
  443. struct lpfc_sglq *sglq;
  444. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  445. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  446. return sglq;
  447. }
  448. /**
  449. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  450. * @phba: Pointer to HBA context object.
  451. * @xritag: XRI value.
  452. *
  453. * This function returns the sglq pointer from the array of acive
  454. * sglq's. The xritag that is passed in is used to index into the
  455. * array. Before the xritag can be used it needs to be adjusted
  456. * by subtracting the xribase.
  457. *
  458. * Returns sglq ponter = success, NULL = Failure.
  459. **/
  460. struct lpfc_sglq *
  461. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  462. {
  463. struct lpfc_sglq *sglq;
  464. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  465. return sglq;
  466. }
  467. /**
  468. * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
  469. * @phba: Pointer to HBA context object.
  470. * @ndlp: nodelist pointer for this target.
  471. * @xritag: xri used in this exchange.
  472. * @rxid: Remote Exchange ID.
  473. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  474. *
  475. * This function is called with hbalock held.
  476. * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
  477. * rrq struct and adds it to the active_rrq_list.
  478. *
  479. * returns 0 for rrq slot for this xri
  480. * < 0 Were not able to get rrq mem or invalid parameter.
  481. **/
  482. static int
  483. __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  484. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  485. {
  486. struct lpfc_node_rrq *rrq;
  487. int empty;
  488. uint32_t did = 0;
  489. if (!ndlp)
  490. return -EINVAL;
  491. if (!phba->cfg_enable_rrq)
  492. return -EINVAL;
  493. if (phba->pport->load_flag & FC_UNLOADING) {
  494. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  495. goto out;
  496. }
  497. did = ndlp->nlp_DID;
  498. /*
  499. * set the active bit even if there is no mem available.
  500. */
  501. if (NLP_CHK_FREE_REQ(ndlp))
  502. goto out;
  503. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  504. goto out;
  505. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  506. goto out;
  507. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  508. if (rrq) {
  509. rrq->send_rrq = send_rrq;
  510. rrq->xritag = xritag;
  511. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  512. rrq->ndlp = ndlp;
  513. rrq->nlp_DID = ndlp->nlp_DID;
  514. rrq->vport = ndlp->vport;
  515. rrq->rxid = rxid;
  516. empty = list_empty(&phba->active_rrq_list);
  517. rrq->send_rrq = send_rrq;
  518. list_add_tail(&rrq->list, &phba->active_rrq_list);
  519. if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
  520. phba->hba_flag |= HBA_RRQ_ACTIVE;
  521. if (empty)
  522. lpfc_worker_wake_up(phba);
  523. }
  524. return 0;
  525. }
  526. out:
  527. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  528. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  529. " DID:0x%x Send:%d\n",
  530. xritag, rxid, did, send_rrq);
  531. return -EINVAL;
  532. }
  533. /**
  534. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  535. * @phba: Pointer to HBA context object.
  536. * @xritag: xri used in this exchange.
  537. * @rrq: The RRQ to be cleared.
  538. *
  539. **/
  540. void
  541. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  542. uint16_t xritag,
  543. struct lpfc_node_rrq *rrq)
  544. {
  545. struct lpfc_nodelist *ndlp = NULL;
  546. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  547. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  548. /* The target DID could have been swapped (cable swap)
  549. * we should use the ndlp from the findnode if it is
  550. * available.
  551. */
  552. if ((!ndlp) && rrq->ndlp)
  553. ndlp = rrq->ndlp;
  554. if (!ndlp)
  555. goto out;
  556. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  557. rrq->send_rrq = 0;
  558. rrq->xritag = 0;
  559. rrq->rrq_stop_time = 0;
  560. }
  561. out:
  562. mempool_free(rrq, phba->rrq_pool);
  563. }
  564. /**
  565. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  566. * @phba: Pointer to HBA context object.
  567. *
  568. * This function is called with hbalock held. This function
  569. * Checks if stop_time (ratov from setting rrq active) has
  570. * been reached, if it has and the send_rrq flag is set then
  571. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  572. * then it will just call the routine to clear the rrq and
  573. * free the rrq resource.
  574. * The timer is set to the next rrq that is going to expire before
  575. * leaving the routine.
  576. *
  577. **/
  578. void
  579. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  580. {
  581. struct lpfc_node_rrq *rrq;
  582. struct lpfc_node_rrq *nextrrq;
  583. unsigned long next_time;
  584. unsigned long iflags;
  585. LIST_HEAD(send_rrq);
  586. spin_lock_irqsave(&phba->hbalock, iflags);
  587. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  588. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  589. list_for_each_entry_safe(rrq, nextrrq,
  590. &phba->active_rrq_list, list) {
  591. if (time_after(jiffies, rrq->rrq_stop_time))
  592. list_move(&rrq->list, &send_rrq);
  593. else if (time_before(rrq->rrq_stop_time, next_time))
  594. next_time = rrq->rrq_stop_time;
  595. }
  596. spin_unlock_irqrestore(&phba->hbalock, iflags);
  597. if (!list_empty(&phba->active_rrq_list))
  598. mod_timer(&phba->rrq_tmr, next_time);
  599. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  600. list_del(&rrq->list);
  601. if (!rrq->send_rrq)
  602. /* this call will free the rrq */
  603. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  604. else if (lpfc_send_rrq(phba, rrq)) {
  605. /* if we send the rrq then the completion handler
  606. * will clear the bit in the xribitmap.
  607. */
  608. lpfc_clr_rrq_active(phba, rrq->xritag,
  609. rrq);
  610. }
  611. }
  612. }
  613. /**
  614. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  615. * @vport: Pointer to vport context object.
  616. * @xri: The xri used in the exchange.
  617. * @did: The targets DID for this exchange.
  618. *
  619. * returns NULL = rrq not found in the phba->active_rrq_list.
  620. * rrq = rrq for this xri and target.
  621. **/
  622. struct lpfc_node_rrq *
  623. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  624. {
  625. struct lpfc_hba *phba = vport->phba;
  626. struct lpfc_node_rrq *rrq;
  627. struct lpfc_node_rrq *nextrrq;
  628. unsigned long iflags;
  629. if (phba->sli_rev != LPFC_SLI_REV4)
  630. return NULL;
  631. spin_lock_irqsave(&phba->hbalock, iflags);
  632. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  633. if (rrq->vport == vport && rrq->xritag == xri &&
  634. rrq->nlp_DID == did){
  635. list_del(&rrq->list);
  636. spin_unlock_irqrestore(&phba->hbalock, iflags);
  637. return rrq;
  638. }
  639. }
  640. spin_unlock_irqrestore(&phba->hbalock, iflags);
  641. return NULL;
  642. }
  643. /**
  644. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  645. * @vport: Pointer to vport context object.
  646. * @ndlp: Pointer to the lpfc_node_list structure.
  647. * If ndlp is NULL Remove all active RRQs for this vport from the
  648. * phba->active_rrq_list and clear the rrq.
  649. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  650. **/
  651. void
  652. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  653. {
  654. struct lpfc_hba *phba = vport->phba;
  655. struct lpfc_node_rrq *rrq;
  656. struct lpfc_node_rrq *nextrrq;
  657. unsigned long iflags;
  658. LIST_HEAD(rrq_list);
  659. if (phba->sli_rev != LPFC_SLI_REV4)
  660. return;
  661. if (!ndlp) {
  662. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  663. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  664. }
  665. spin_lock_irqsave(&phba->hbalock, iflags);
  666. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  667. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  668. list_move(&rrq->list, &rrq_list);
  669. spin_unlock_irqrestore(&phba->hbalock, iflags);
  670. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  671. list_del(&rrq->list);
  672. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  673. }
  674. }
  675. /**
  676. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  677. * @phba: Pointer to HBA context object.
  678. *
  679. * Remove all rrqs from the phba->active_rrq_list and free them by
  680. * calling __lpfc_clr_active_rrq
  681. *
  682. **/
  683. void
  684. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  685. {
  686. struct lpfc_node_rrq *rrq;
  687. struct lpfc_node_rrq *nextrrq;
  688. unsigned long next_time;
  689. unsigned long iflags;
  690. LIST_HEAD(rrq_list);
  691. if (phba->sli_rev != LPFC_SLI_REV4)
  692. return;
  693. spin_lock_irqsave(&phba->hbalock, iflags);
  694. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  695. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  696. list_splice_init(&phba->active_rrq_list, &rrq_list);
  697. spin_unlock_irqrestore(&phba->hbalock, iflags);
  698. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  699. list_del(&rrq->list);
  700. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  701. }
  702. if (!list_empty(&phba->active_rrq_list))
  703. mod_timer(&phba->rrq_tmr, next_time);
  704. }
  705. /**
  706. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  707. * @phba: Pointer to HBA context object.
  708. * @ndlp: Targets nodelist pointer for this exchange.
  709. * @xritag the xri in the bitmap to test.
  710. *
  711. * This function is called with hbalock held. This function
  712. * returns 0 = rrq not active for this xri
  713. * 1 = rrq is valid for this xri.
  714. **/
  715. int
  716. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  717. uint16_t xritag)
  718. {
  719. if (!ndlp)
  720. return 0;
  721. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  722. return 1;
  723. else
  724. return 0;
  725. }
  726. /**
  727. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  728. * @phba: Pointer to HBA context object.
  729. * @ndlp: nodelist pointer for this target.
  730. * @xritag: xri used in this exchange.
  731. * @rxid: Remote Exchange ID.
  732. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  733. *
  734. * This function takes the hbalock.
  735. * The active bit is always set in the active rrq xri_bitmap even
  736. * if there is no slot avaiable for the other rrq information.
  737. *
  738. * returns 0 rrq actived for this xri
  739. * < 0 No memory or invalid ndlp.
  740. **/
  741. int
  742. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  743. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  744. {
  745. int ret;
  746. unsigned long iflags;
  747. spin_lock_irqsave(&phba->hbalock, iflags);
  748. ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
  749. spin_unlock_irqrestore(&phba->hbalock, iflags);
  750. return ret;
  751. }
  752. /**
  753. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  754. * @phba: Pointer to HBA context object.
  755. * @piocb: Pointer to the iocbq.
  756. *
  757. * This function is called with hbalock held. This function
  758. * gets a new driver sglq object from the sglq list. If the
  759. * list is not empty then it is successful, it returns pointer to the newly
  760. * allocated sglq object else it returns NULL.
  761. **/
  762. static struct lpfc_sglq *
  763. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  764. {
  765. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  766. struct lpfc_sglq *sglq = NULL;
  767. struct lpfc_sglq *start_sglq = NULL;
  768. struct lpfc_scsi_buf *lpfc_cmd;
  769. struct lpfc_nodelist *ndlp;
  770. int found = 0;
  771. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  772. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  773. ndlp = lpfc_cmd->rdata->pnode;
  774. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  775. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  776. ndlp = piocbq->context_un.ndlp;
  777. else
  778. ndlp = piocbq->context1;
  779. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  780. start_sglq = sglq;
  781. while (!found) {
  782. if (!sglq)
  783. return NULL;
  784. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
  785. /* This xri has an rrq outstanding for this DID.
  786. * put it back in the list and get another xri.
  787. */
  788. list_add_tail(&sglq->list, lpfc_sgl_list);
  789. sglq = NULL;
  790. list_remove_head(lpfc_sgl_list, sglq,
  791. struct lpfc_sglq, list);
  792. if (sglq == start_sglq) {
  793. sglq = NULL;
  794. break;
  795. } else
  796. continue;
  797. }
  798. sglq->ndlp = ndlp;
  799. found = 1;
  800. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  801. sglq->state = SGL_ALLOCATED;
  802. }
  803. return sglq;
  804. }
  805. /**
  806. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  807. * @phba: Pointer to HBA context object.
  808. *
  809. * This function is called with no lock held. This function
  810. * allocates a new driver iocb object from the iocb pool. If the
  811. * allocation is successful, it returns pointer to the newly
  812. * allocated iocb object else it returns NULL.
  813. **/
  814. struct lpfc_iocbq *
  815. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  816. {
  817. struct lpfc_iocbq * iocbq = NULL;
  818. unsigned long iflags;
  819. spin_lock_irqsave(&phba->hbalock, iflags);
  820. iocbq = __lpfc_sli_get_iocbq(phba);
  821. spin_unlock_irqrestore(&phba->hbalock, iflags);
  822. return iocbq;
  823. }
  824. /**
  825. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  826. * @phba: Pointer to HBA context object.
  827. * @iocbq: Pointer to driver iocb object.
  828. *
  829. * This function is called with hbalock held to release driver
  830. * iocb object to the iocb pool. The iotag in the iocb object
  831. * does not change for each use of the iocb object. This function
  832. * clears all other fields of the iocb object when it is freed.
  833. * The sqlq structure that holds the xritag and phys and virtual
  834. * mappings for the scatter gather list is retrieved from the
  835. * active array of sglq. The get of the sglq pointer also clears
  836. * the entry in the array. If the status of the IO indiactes that
  837. * this IO was aborted then the sglq entry it put on the
  838. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  839. * IO has good status or fails for any other reason then the sglq
  840. * entry is added to the free list (lpfc_sgl_list).
  841. **/
  842. static void
  843. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  844. {
  845. struct lpfc_sglq *sglq;
  846. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  847. unsigned long iflag = 0;
  848. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  849. if (iocbq->sli4_xritag == NO_XRI)
  850. sglq = NULL;
  851. else
  852. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  853. if (sglq) {
  854. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  855. (sglq->state != SGL_XRI_ABORTED)) {
  856. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  857. iflag);
  858. list_add(&sglq->list,
  859. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  860. spin_unlock_irqrestore(
  861. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  862. } else {
  863. sglq->state = SGL_FREED;
  864. sglq->ndlp = NULL;
  865. list_add_tail(&sglq->list,
  866. &phba->sli4_hba.lpfc_sgl_list);
  867. /* Check if TXQ queue needs to be serviced */
  868. if (pring->txq_cnt)
  869. lpfc_worker_wake_up(phba);
  870. }
  871. }
  872. /*
  873. * Clean all volatile data fields, preserve iotag and node struct.
  874. */
  875. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  876. iocbq->sli4_lxritag = NO_XRI;
  877. iocbq->sli4_xritag = NO_XRI;
  878. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  879. }
  880. /**
  881. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  882. * @phba: Pointer to HBA context object.
  883. * @iocbq: Pointer to driver iocb object.
  884. *
  885. * This function is called with hbalock held to release driver
  886. * iocb object to the iocb pool. The iotag in the iocb object
  887. * does not change for each use of the iocb object. This function
  888. * clears all other fields of the iocb object when it is freed.
  889. **/
  890. static void
  891. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  892. {
  893. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  894. /*
  895. * Clean all volatile data fields, preserve iotag and node struct.
  896. */
  897. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  898. iocbq->sli4_xritag = NO_XRI;
  899. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  900. }
  901. /**
  902. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  903. * @phba: Pointer to HBA context object.
  904. * @iocbq: Pointer to driver iocb object.
  905. *
  906. * This function is called with hbalock held to release driver
  907. * iocb object to the iocb pool. The iotag in the iocb object
  908. * does not change for each use of the iocb object. This function
  909. * clears all other fields of the iocb object when it is freed.
  910. **/
  911. static void
  912. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  913. {
  914. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  915. phba->iocb_cnt--;
  916. }
  917. /**
  918. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  919. * @phba: Pointer to HBA context object.
  920. * @iocbq: Pointer to driver iocb object.
  921. *
  922. * This function is called with no lock held to release the iocb to
  923. * iocb pool.
  924. **/
  925. void
  926. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  927. {
  928. unsigned long iflags;
  929. /*
  930. * Clean all volatile data fields, preserve iotag and node struct.
  931. */
  932. spin_lock_irqsave(&phba->hbalock, iflags);
  933. __lpfc_sli_release_iocbq(phba, iocbq);
  934. spin_unlock_irqrestore(&phba->hbalock, iflags);
  935. }
  936. /**
  937. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  938. * @phba: Pointer to HBA context object.
  939. * @iocblist: List of IOCBs.
  940. * @ulpstatus: ULP status in IOCB command field.
  941. * @ulpWord4: ULP word-4 in IOCB command field.
  942. *
  943. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  944. * on the list by invoking the complete callback function associated with the
  945. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  946. * fields.
  947. **/
  948. void
  949. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  950. uint32_t ulpstatus, uint32_t ulpWord4)
  951. {
  952. struct lpfc_iocbq *piocb;
  953. while (!list_empty(iocblist)) {
  954. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  955. if (!piocb->iocb_cmpl)
  956. lpfc_sli_release_iocbq(phba, piocb);
  957. else {
  958. piocb->iocb.ulpStatus = ulpstatus;
  959. piocb->iocb.un.ulpWord[4] = ulpWord4;
  960. (piocb->iocb_cmpl) (phba, piocb, piocb);
  961. }
  962. }
  963. return;
  964. }
  965. /**
  966. * lpfc_sli_iocb_cmd_type - Get the iocb type
  967. * @iocb_cmnd: iocb command code.
  968. *
  969. * This function is called by ring event handler function to get the iocb type.
  970. * This function translates the iocb command to an iocb command type used to
  971. * decide the final disposition of each completed IOCB.
  972. * The function returns
  973. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  974. * LPFC_SOL_IOCB if it is a solicited iocb completion
  975. * LPFC_ABORT_IOCB if it is an abort iocb
  976. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  977. *
  978. * The caller is not required to hold any lock.
  979. **/
  980. static lpfc_iocb_type
  981. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  982. {
  983. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  984. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  985. return 0;
  986. switch (iocb_cmnd) {
  987. case CMD_XMIT_SEQUENCE_CR:
  988. case CMD_XMIT_SEQUENCE_CX:
  989. case CMD_XMIT_BCAST_CN:
  990. case CMD_XMIT_BCAST_CX:
  991. case CMD_ELS_REQUEST_CR:
  992. case CMD_ELS_REQUEST_CX:
  993. case CMD_CREATE_XRI_CR:
  994. case CMD_CREATE_XRI_CX:
  995. case CMD_GET_RPI_CN:
  996. case CMD_XMIT_ELS_RSP_CX:
  997. case CMD_GET_RPI_CR:
  998. case CMD_FCP_IWRITE_CR:
  999. case CMD_FCP_IWRITE_CX:
  1000. case CMD_FCP_IREAD_CR:
  1001. case CMD_FCP_IREAD_CX:
  1002. case CMD_FCP_ICMND_CR:
  1003. case CMD_FCP_ICMND_CX:
  1004. case CMD_FCP_TSEND_CX:
  1005. case CMD_FCP_TRSP_CX:
  1006. case CMD_FCP_TRECEIVE_CX:
  1007. case CMD_FCP_AUTO_TRSP_CX:
  1008. case CMD_ADAPTER_MSG:
  1009. case CMD_ADAPTER_DUMP:
  1010. case CMD_XMIT_SEQUENCE64_CR:
  1011. case CMD_XMIT_SEQUENCE64_CX:
  1012. case CMD_XMIT_BCAST64_CN:
  1013. case CMD_XMIT_BCAST64_CX:
  1014. case CMD_ELS_REQUEST64_CR:
  1015. case CMD_ELS_REQUEST64_CX:
  1016. case CMD_FCP_IWRITE64_CR:
  1017. case CMD_FCP_IWRITE64_CX:
  1018. case CMD_FCP_IREAD64_CR:
  1019. case CMD_FCP_IREAD64_CX:
  1020. case CMD_FCP_ICMND64_CR:
  1021. case CMD_FCP_ICMND64_CX:
  1022. case CMD_FCP_TSEND64_CX:
  1023. case CMD_FCP_TRSP64_CX:
  1024. case CMD_FCP_TRECEIVE64_CX:
  1025. case CMD_GEN_REQUEST64_CR:
  1026. case CMD_GEN_REQUEST64_CX:
  1027. case CMD_XMIT_ELS_RSP64_CX:
  1028. case DSSCMD_IWRITE64_CR:
  1029. case DSSCMD_IWRITE64_CX:
  1030. case DSSCMD_IREAD64_CR:
  1031. case DSSCMD_IREAD64_CX:
  1032. type = LPFC_SOL_IOCB;
  1033. break;
  1034. case CMD_ABORT_XRI_CN:
  1035. case CMD_ABORT_XRI_CX:
  1036. case CMD_CLOSE_XRI_CN:
  1037. case CMD_CLOSE_XRI_CX:
  1038. case CMD_XRI_ABORTED_CX:
  1039. case CMD_ABORT_MXRI64_CN:
  1040. case CMD_XMIT_BLS_RSP64_CX:
  1041. type = LPFC_ABORT_IOCB;
  1042. break;
  1043. case CMD_RCV_SEQUENCE_CX:
  1044. case CMD_RCV_ELS_REQ_CX:
  1045. case CMD_RCV_SEQUENCE64_CX:
  1046. case CMD_RCV_ELS_REQ64_CX:
  1047. case CMD_ASYNC_STATUS:
  1048. case CMD_IOCB_RCV_SEQ64_CX:
  1049. case CMD_IOCB_RCV_ELS64_CX:
  1050. case CMD_IOCB_RCV_CONT64_CX:
  1051. case CMD_IOCB_RET_XRI64_CX:
  1052. type = LPFC_UNSOL_IOCB;
  1053. break;
  1054. case CMD_IOCB_XMIT_MSEQ64_CR:
  1055. case CMD_IOCB_XMIT_MSEQ64_CX:
  1056. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1057. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1058. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1059. case CMD_IOCB_ABORT_EXTENDED_CN:
  1060. case CMD_IOCB_RET_HBQE64_CN:
  1061. case CMD_IOCB_FCP_IBIDIR64_CR:
  1062. case CMD_IOCB_FCP_IBIDIR64_CX:
  1063. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1064. case CMD_IOCB_LOGENTRY_CN:
  1065. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1066. printk("%s - Unhandled SLI-3 Command x%x\n",
  1067. __func__, iocb_cmnd);
  1068. type = LPFC_UNKNOWN_IOCB;
  1069. break;
  1070. default:
  1071. type = LPFC_UNKNOWN_IOCB;
  1072. break;
  1073. }
  1074. return type;
  1075. }
  1076. /**
  1077. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1078. * @phba: Pointer to HBA context object.
  1079. *
  1080. * This function is called from SLI initialization code
  1081. * to configure every ring of the HBA's SLI interface. The
  1082. * caller is not required to hold any lock. This function issues
  1083. * a config_ring mailbox command for each ring.
  1084. * This function returns zero if successful else returns a negative
  1085. * error code.
  1086. **/
  1087. static int
  1088. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1089. {
  1090. struct lpfc_sli *psli = &phba->sli;
  1091. LPFC_MBOXQ_t *pmb;
  1092. MAILBOX_t *pmbox;
  1093. int i, rc, ret = 0;
  1094. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1095. if (!pmb)
  1096. return -ENOMEM;
  1097. pmbox = &pmb->u.mb;
  1098. phba->link_state = LPFC_INIT_MBX_CMDS;
  1099. for (i = 0; i < psli->num_rings; i++) {
  1100. lpfc_config_ring(phba, i, pmb);
  1101. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1102. if (rc != MBX_SUCCESS) {
  1103. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1104. "0446 Adapter failed to init (%d), "
  1105. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1106. "ring %d\n",
  1107. rc, pmbox->mbxCommand,
  1108. pmbox->mbxStatus, i);
  1109. phba->link_state = LPFC_HBA_ERROR;
  1110. ret = -ENXIO;
  1111. break;
  1112. }
  1113. }
  1114. mempool_free(pmb, phba->mbox_mem_pool);
  1115. return ret;
  1116. }
  1117. /**
  1118. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1119. * @phba: Pointer to HBA context object.
  1120. * @pring: Pointer to driver SLI ring object.
  1121. * @piocb: Pointer to the driver iocb object.
  1122. *
  1123. * This function is called with hbalock held. The function adds the
  1124. * new iocb to txcmplq of the given ring. This function always returns
  1125. * 0. If this function is called for ELS ring, this function checks if
  1126. * there is a vport associated with the ELS command. This function also
  1127. * starts els_tmofunc timer if this is an ELS command.
  1128. **/
  1129. static int
  1130. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1131. struct lpfc_iocbq *piocb)
  1132. {
  1133. list_add_tail(&piocb->list, &pring->txcmplq);
  1134. piocb->iocb_flag |= LPFC_IO_ON_Q;
  1135. pring->txcmplq_cnt++;
  1136. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1137. pring->txcmplq_max = pring->txcmplq_cnt;
  1138. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1139. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1140. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1141. if (!piocb->vport)
  1142. BUG();
  1143. else
  1144. mod_timer(&piocb->vport->els_tmofunc,
  1145. jiffies + HZ * (phba->fc_ratov << 1));
  1146. }
  1147. return 0;
  1148. }
  1149. /**
  1150. * lpfc_sli_ringtx_get - Get first element of the txq
  1151. * @phba: Pointer to HBA context object.
  1152. * @pring: Pointer to driver SLI ring object.
  1153. *
  1154. * This function is called with hbalock held to get next
  1155. * iocb in txq of the given ring. If there is any iocb in
  1156. * the txq, the function returns first iocb in the list after
  1157. * removing the iocb from the list, else it returns NULL.
  1158. **/
  1159. struct lpfc_iocbq *
  1160. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1161. {
  1162. struct lpfc_iocbq *cmd_iocb;
  1163. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1164. if (cmd_iocb != NULL)
  1165. pring->txq_cnt--;
  1166. return cmd_iocb;
  1167. }
  1168. /**
  1169. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1170. * @phba: Pointer to HBA context object.
  1171. * @pring: Pointer to driver SLI ring object.
  1172. *
  1173. * This function is called with hbalock held and the caller must post the
  1174. * iocb without releasing the lock. If the caller releases the lock,
  1175. * iocb slot returned by the function is not guaranteed to be available.
  1176. * The function returns pointer to the next available iocb slot if there
  1177. * is available slot in the ring, else it returns NULL.
  1178. * If the get index of the ring is ahead of the put index, the function
  1179. * will post an error attention event to the worker thread to take the
  1180. * HBA to offline state.
  1181. **/
  1182. static IOCB_t *
  1183. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1184. {
  1185. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1186. uint32_t max_cmd_idx = pring->numCiocb;
  1187. if ((pring->next_cmdidx == pring->cmdidx) &&
  1188. (++pring->next_cmdidx >= max_cmd_idx))
  1189. pring->next_cmdidx = 0;
  1190. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1191. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1192. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1193. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1194. "0315 Ring %d issue: portCmdGet %d "
  1195. "is bigger than cmd ring %d\n",
  1196. pring->ringno,
  1197. pring->local_getidx, max_cmd_idx);
  1198. phba->link_state = LPFC_HBA_ERROR;
  1199. /*
  1200. * All error attention handlers are posted to
  1201. * worker thread
  1202. */
  1203. phba->work_ha |= HA_ERATT;
  1204. phba->work_hs = HS_FFER3;
  1205. lpfc_worker_wake_up(phba);
  1206. return NULL;
  1207. }
  1208. if (pring->local_getidx == pring->next_cmdidx)
  1209. return NULL;
  1210. }
  1211. return lpfc_cmd_iocb(phba, pring);
  1212. }
  1213. /**
  1214. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1215. * @phba: Pointer to HBA context object.
  1216. * @iocbq: Pointer to driver iocb object.
  1217. *
  1218. * This function gets an iotag for the iocb. If there is no unused iotag and
  1219. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1220. * array and assigns a new iotag.
  1221. * The function returns the allocated iotag if successful, else returns zero.
  1222. * Zero is not a valid iotag.
  1223. * The caller is not required to hold any lock.
  1224. **/
  1225. uint16_t
  1226. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1227. {
  1228. struct lpfc_iocbq **new_arr;
  1229. struct lpfc_iocbq **old_arr;
  1230. size_t new_len;
  1231. struct lpfc_sli *psli = &phba->sli;
  1232. uint16_t iotag;
  1233. spin_lock_irq(&phba->hbalock);
  1234. iotag = psli->last_iotag;
  1235. if(++iotag < psli->iocbq_lookup_len) {
  1236. psli->last_iotag = iotag;
  1237. psli->iocbq_lookup[iotag] = iocbq;
  1238. spin_unlock_irq(&phba->hbalock);
  1239. iocbq->iotag = iotag;
  1240. return iotag;
  1241. } else if (psli->iocbq_lookup_len < (0xffff
  1242. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1243. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1244. spin_unlock_irq(&phba->hbalock);
  1245. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1246. GFP_KERNEL);
  1247. if (new_arr) {
  1248. spin_lock_irq(&phba->hbalock);
  1249. old_arr = psli->iocbq_lookup;
  1250. if (new_len <= psli->iocbq_lookup_len) {
  1251. /* highly unprobable case */
  1252. kfree(new_arr);
  1253. iotag = psli->last_iotag;
  1254. if(++iotag < psli->iocbq_lookup_len) {
  1255. psli->last_iotag = iotag;
  1256. psli->iocbq_lookup[iotag] = iocbq;
  1257. spin_unlock_irq(&phba->hbalock);
  1258. iocbq->iotag = iotag;
  1259. return iotag;
  1260. }
  1261. spin_unlock_irq(&phba->hbalock);
  1262. return 0;
  1263. }
  1264. if (psli->iocbq_lookup)
  1265. memcpy(new_arr, old_arr,
  1266. ((psli->last_iotag + 1) *
  1267. sizeof (struct lpfc_iocbq *)));
  1268. psli->iocbq_lookup = new_arr;
  1269. psli->iocbq_lookup_len = new_len;
  1270. psli->last_iotag = iotag;
  1271. psli->iocbq_lookup[iotag] = iocbq;
  1272. spin_unlock_irq(&phba->hbalock);
  1273. iocbq->iotag = iotag;
  1274. kfree(old_arr);
  1275. return iotag;
  1276. }
  1277. } else
  1278. spin_unlock_irq(&phba->hbalock);
  1279. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1280. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1281. psli->last_iotag);
  1282. return 0;
  1283. }
  1284. /**
  1285. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1286. * @phba: Pointer to HBA context object.
  1287. * @pring: Pointer to driver SLI ring object.
  1288. * @iocb: Pointer to iocb slot in the ring.
  1289. * @nextiocb: Pointer to driver iocb object which need to be
  1290. * posted to firmware.
  1291. *
  1292. * This function is called with hbalock held to post a new iocb to
  1293. * the firmware. This function copies the new iocb to ring iocb slot and
  1294. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1295. * a completion call back for this iocb else the function will free the
  1296. * iocb object.
  1297. **/
  1298. static void
  1299. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1300. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1301. {
  1302. /*
  1303. * Set up an iotag
  1304. */
  1305. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1306. if (pring->ringno == LPFC_ELS_RING) {
  1307. lpfc_debugfs_slow_ring_trc(phba,
  1308. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1309. *(((uint32_t *) &nextiocb->iocb) + 4),
  1310. *(((uint32_t *) &nextiocb->iocb) + 6),
  1311. *(((uint32_t *) &nextiocb->iocb) + 7));
  1312. }
  1313. /*
  1314. * Issue iocb command to adapter
  1315. */
  1316. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1317. wmb();
  1318. pring->stats.iocb_cmd++;
  1319. /*
  1320. * If there is no completion routine to call, we can release the
  1321. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1322. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1323. */
  1324. if (nextiocb->iocb_cmpl)
  1325. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1326. else
  1327. __lpfc_sli_release_iocbq(phba, nextiocb);
  1328. /*
  1329. * Let the HBA know what IOCB slot will be the next one the
  1330. * driver will put a command into.
  1331. */
  1332. pring->cmdidx = pring->next_cmdidx;
  1333. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1334. }
  1335. /**
  1336. * lpfc_sli_update_full_ring - Update the chip attention register
  1337. * @phba: Pointer to HBA context object.
  1338. * @pring: Pointer to driver SLI ring object.
  1339. *
  1340. * The caller is not required to hold any lock for calling this function.
  1341. * This function updates the chip attention bits for the ring to inform firmware
  1342. * that there are pending work to be done for this ring and requests an
  1343. * interrupt when there is space available in the ring. This function is
  1344. * called when the driver is unable to post more iocbs to the ring due
  1345. * to unavailability of space in the ring.
  1346. **/
  1347. static void
  1348. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1349. {
  1350. int ringno = pring->ringno;
  1351. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1352. wmb();
  1353. /*
  1354. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1355. * The HBA will tell us when an IOCB entry is available.
  1356. */
  1357. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1358. readl(phba->CAregaddr); /* flush */
  1359. pring->stats.iocb_cmd_full++;
  1360. }
  1361. /**
  1362. * lpfc_sli_update_ring - Update chip attention register
  1363. * @phba: Pointer to HBA context object.
  1364. * @pring: Pointer to driver SLI ring object.
  1365. *
  1366. * This function updates the chip attention register bit for the
  1367. * given ring to inform HBA that there is more work to be done
  1368. * in this ring. The caller is not required to hold any lock.
  1369. **/
  1370. static void
  1371. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1372. {
  1373. int ringno = pring->ringno;
  1374. /*
  1375. * Tell the HBA that there is work to do in this ring.
  1376. */
  1377. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1378. wmb();
  1379. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1380. readl(phba->CAregaddr); /* flush */
  1381. }
  1382. }
  1383. /**
  1384. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1385. * @phba: Pointer to HBA context object.
  1386. * @pring: Pointer to driver SLI ring object.
  1387. *
  1388. * This function is called with hbalock held to post pending iocbs
  1389. * in the txq to the firmware. This function is called when driver
  1390. * detects space available in the ring.
  1391. **/
  1392. static void
  1393. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1394. {
  1395. IOCB_t *iocb;
  1396. struct lpfc_iocbq *nextiocb;
  1397. /*
  1398. * Check to see if:
  1399. * (a) there is anything on the txq to send
  1400. * (b) link is up
  1401. * (c) link attention events can be processed (fcp ring only)
  1402. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1403. */
  1404. if (pring->txq_cnt &&
  1405. lpfc_is_link_up(phba) &&
  1406. (pring->ringno != phba->sli.fcp_ring ||
  1407. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1408. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1409. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1410. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1411. if (iocb)
  1412. lpfc_sli_update_ring(phba, pring);
  1413. else
  1414. lpfc_sli_update_full_ring(phba, pring);
  1415. }
  1416. return;
  1417. }
  1418. /**
  1419. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1420. * @phba: Pointer to HBA context object.
  1421. * @hbqno: HBQ number.
  1422. *
  1423. * This function is called with hbalock held to get the next
  1424. * available slot for the given HBQ. If there is free slot
  1425. * available for the HBQ it will return pointer to the next available
  1426. * HBQ entry else it will return NULL.
  1427. **/
  1428. static struct lpfc_hbq_entry *
  1429. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1430. {
  1431. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1432. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1433. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1434. hbqp->next_hbqPutIdx = 0;
  1435. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1436. uint32_t raw_index = phba->hbq_get[hbqno];
  1437. uint32_t getidx = le32_to_cpu(raw_index);
  1438. hbqp->local_hbqGetIdx = getidx;
  1439. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1440. lpfc_printf_log(phba, KERN_ERR,
  1441. LOG_SLI | LOG_VPORT,
  1442. "1802 HBQ %d: local_hbqGetIdx "
  1443. "%u is > than hbqp->entry_count %u\n",
  1444. hbqno, hbqp->local_hbqGetIdx,
  1445. hbqp->entry_count);
  1446. phba->link_state = LPFC_HBA_ERROR;
  1447. return NULL;
  1448. }
  1449. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1450. return NULL;
  1451. }
  1452. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1453. hbqp->hbqPutIdx;
  1454. }
  1455. /**
  1456. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1457. * @phba: Pointer to HBA context object.
  1458. *
  1459. * This function is called with no lock held to free all the
  1460. * hbq buffers while uninitializing the SLI interface. It also
  1461. * frees the HBQ buffers returned by the firmware but not yet
  1462. * processed by the upper layers.
  1463. **/
  1464. void
  1465. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1466. {
  1467. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1468. struct hbq_dmabuf *hbq_buf;
  1469. unsigned long flags;
  1470. int i, hbq_count;
  1471. uint32_t hbqno;
  1472. hbq_count = lpfc_sli_hbq_count();
  1473. /* Return all memory used by all HBQs */
  1474. spin_lock_irqsave(&phba->hbalock, flags);
  1475. for (i = 0; i < hbq_count; ++i) {
  1476. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1477. &phba->hbqs[i].hbq_buffer_list, list) {
  1478. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1479. list_del(&hbq_buf->dbuf.list);
  1480. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1481. }
  1482. phba->hbqs[i].buffer_count = 0;
  1483. }
  1484. /* Return all HBQ buffer that are in-fly */
  1485. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1486. list) {
  1487. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1488. list_del(&hbq_buf->dbuf.list);
  1489. if (hbq_buf->tag == -1) {
  1490. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1491. (phba, hbq_buf);
  1492. } else {
  1493. hbqno = hbq_buf->tag >> 16;
  1494. if (hbqno >= LPFC_MAX_HBQS)
  1495. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1496. (phba, hbq_buf);
  1497. else
  1498. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1499. hbq_buf);
  1500. }
  1501. }
  1502. /* Mark the HBQs not in use */
  1503. phba->hbq_in_use = 0;
  1504. spin_unlock_irqrestore(&phba->hbalock, flags);
  1505. }
  1506. /**
  1507. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1508. * @phba: Pointer to HBA context object.
  1509. * @hbqno: HBQ number.
  1510. * @hbq_buf: Pointer to HBQ buffer.
  1511. *
  1512. * This function is called with the hbalock held to post a
  1513. * hbq buffer to the firmware. If the function finds an empty
  1514. * slot in the HBQ, it will post the buffer. The function will return
  1515. * pointer to the hbq entry if it successfully post the buffer
  1516. * else it will return NULL.
  1517. **/
  1518. static int
  1519. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1520. struct hbq_dmabuf *hbq_buf)
  1521. {
  1522. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1523. }
  1524. /**
  1525. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1526. * @phba: Pointer to HBA context object.
  1527. * @hbqno: HBQ number.
  1528. * @hbq_buf: Pointer to HBQ buffer.
  1529. *
  1530. * This function is called with the hbalock held to post a hbq buffer to the
  1531. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1532. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1533. * it successfully post the buffer else it will return an error.
  1534. **/
  1535. static int
  1536. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1537. struct hbq_dmabuf *hbq_buf)
  1538. {
  1539. struct lpfc_hbq_entry *hbqe;
  1540. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1541. /* Get next HBQ entry slot to use */
  1542. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1543. if (hbqe) {
  1544. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1545. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1546. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1547. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1548. hbqe->bde.tus.f.bdeFlags = 0;
  1549. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1550. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1551. /* Sync SLIM */
  1552. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1553. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1554. /* flush */
  1555. readl(phba->hbq_put + hbqno);
  1556. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1557. return 0;
  1558. } else
  1559. return -ENOMEM;
  1560. }
  1561. /**
  1562. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1563. * @phba: Pointer to HBA context object.
  1564. * @hbqno: HBQ number.
  1565. * @hbq_buf: Pointer to HBQ buffer.
  1566. *
  1567. * This function is called with the hbalock held to post an RQE to the SLI4
  1568. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1569. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1570. **/
  1571. static int
  1572. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1573. struct hbq_dmabuf *hbq_buf)
  1574. {
  1575. int rc;
  1576. struct lpfc_rqe hrqe;
  1577. struct lpfc_rqe drqe;
  1578. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1579. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1580. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1581. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1582. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1583. &hrqe, &drqe);
  1584. if (rc < 0)
  1585. return rc;
  1586. hbq_buf->tag = rc;
  1587. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1588. return 0;
  1589. }
  1590. /* HBQ for ELS and CT traffic. */
  1591. static struct lpfc_hbq_init lpfc_els_hbq = {
  1592. .rn = 1,
  1593. .entry_count = 256,
  1594. .mask_count = 0,
  1595. .profile = 0,
  1596. .ring_mask = (1 << LPFC_ELS_RING),
  1597. .buffer_count = 0,
  1598. .init_count = 40,
  1599. .add_count = 40,
  1600. };
  1601. /* HBQ for the extra ring if needed */
  1602. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1603. .rn = 1,
  1604. .entry_count = 200,
  1605. .mask_count = 0,
  1606. .profile = 0,
  1607. .ring_mask = (1 << LPFC_EXTRA_RING),
  1608. .buffer_count = 0,
  1609. .init_count = 0,
  1610. .add_count = 5,
  1611. };
  1612. /* Array of HBQs */
  1613. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1614. &lpfc_els_hbq,
  1615. &lpfc_extra_hbq,
  1616. };
  1617. /**
  1618. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1619. * @phba: Pointer to HBA context object.
  1620. * @hbqno: HBQ number.
  1621. * @count: Number of HBQ buffers to be posted.
  1622. *
  1623. * This function is called with no lock held to post more hbq buffers to the
  1624. * given HBQ. The function returns the number of HBQ buffers successfully
  1625. * posted.
  1626. **/
  1627. static int
  1628. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1629. {
  1630. uint32_t i, posted = 0;
  1631. unsigned long flags;
  1632. struct hbq_dmabuf *hbq_buffer;
  1633. LIST_HEAD(hbq_buf_list);
  1634. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1635. return 0;
  1636. if ((phba->hbqs[hbqno].buffer_count + count) >
  1637. lpfc_hbq_defs[hbqno]->entry_count)
  1638. count = lpfc_hbq_defs[hbqno]->entry_count -
  1639. phba->hbqs[hbqno].buffer_count;
  1640. if (!count)
  1641. return 0;
  1642. /* Allocate HBQ entries */
  1643. for (i = 0; i < count; i++) {
  1644. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1645. if (!hbq_buffer)
  1646. break;
  1647. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1648. }
  1649. /* Check whether HBQ is still in use */
  1650. spin_lock_irqsave(&phba->hbalock, flags);
  1651. if (!phba->hbq_in_use)
  1652. goto err;
  1653. while (!list_empty(&hbq_buf_list)) {
  1654. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1655. dbuf.list);
  1656. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1657. (hbqno << 16));
  1658. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1659. phba->hbqs[hbqno].buffer_count++;
  1660. posted++;
  1661. } else
  1662. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1663. }
  1664. spin_unlock_irqrestore(&phba->hbalock, flags);
  1665. return posted;
  1666. err:
  1667. spin_unlock_irqrestore(&phba->hbalock, flags);
  1668. while (!list_empty(&hbq_buf_list)) {
  1669. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1670. dbuf.list);
  1671. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1672. }
  1673. return 0;
  1674. }
  1675. /**
  1676. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1677. * @phba: Pointer to HBA context object.
  1678. * @qno: HBQ number.
  1679. *
  1680. * This function posts more buffers to the HBQ. This function
  1681. * is called with no lock held. The function returns the number of HBQ entries
  1682. * successfully allocated.
  1683. **/
  1684. int
  1685. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1686. {
  1687. if (phba->sli_rev == LPFC_SLI_REV4)
  1688. return 0;
  1689. else
  1690. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1691. lpfc_hbq_defs[qno]->add_count);
  1692. }
  1693. /**
  1694. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1695. * @phba: Pointer to HBA context object.
  1696. * @qno: HBQ queue number.
  1697. *
  1698. * This function is called from SLI initialization code path with
  1699. * no lock held to post initial HBQ buffers to firmware. The
  1700. * function returns the number of HBQ entries successfully allocated.
  1701. **/
  1702. static int
  1703. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1704. {
  1705. if (phba->sli_rev == LPFC_SLI_REV4)
  1706. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1707. lpfc_hbq_defs[qno]->entry_count);
  1708. else
  1709. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1710. lpfc_hbq_defs[qno]->init_count);
  1711. }
  1712. /**
  1713. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1714. * @phba: Pointer to HBA context object.
  1715. * @hbqno: HBQ number.
  1716. *
  1717. * This function removes the first hbq buffer on an hbq list and returns a
  1718. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1719. **/
  1720. static struct hbq_dmabuf *
  1721. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1722. {
  1723. struct lpfc_dmabuf *d_buf;
  1724. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1725. if (!d_buf)
  1726. return NULL;
  1727. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1728. }
  1729. /**
  1730. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1731. * @phba: Pointer to HBA context object.
  1732. * @tag: Tag of the hbq buffer.
  1733. *
  1734. * This function is called with hbalock held. This function searches
  1735. * for the hbq buffer associated with the given tag in the hbq buffer
  1736. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1737. * it returns NULL.
  1738. **/
  1739. static struct hbq_dmabuf *
  1740. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1741. {
  1742. struct lpfc_dmabuf *d_buf;
  1743. struct hbq_dmabuf *hbq_buf;
  1744. uint32_t hbqno;
  1745. hbqno = tag >> 16;
  1746. if (hbqno >= LPFC_MAX_HBQS)
  1747. return NULL;
  1748. spin_lock_irq(&phba->hbalock);
  1749. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1750. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1751. if (hbq_buf->tag == tag) {
  1752. spin_unlock_irq(&phba->hbalock);
  1753. return hbq_buf;
  1754. }
  1755. }
  1756. spin_unlock_irq(&phba->hbalock);
  1757. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1758. "1803 Bad hbq tag. Data: x%x x%x\n",
  1759. tag, phba->hbqs[tag >> 16].buffer_count);
  1760. return NULL;
  1761. }
  1762. /**
  1763. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1764. * @phba: Pointer to HBA context object.
  1765. * @hbq_buffer: Pointer to HBQ buffer.
  1766. *
  1767. * This function is called with hbalock. This function gives back
  1768. * the hbq buffer to firmware. If the HBQ does not have space to
  1769. * post the buffer, it will free the buffer.
  1770. **/
  1771. void
  1772. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1773. {
  1774. uint32_t hbqno;
  1775. if (hbq_buffer) {
  1776. hbqno = hbq_buffer->tag >> 16;
  1777. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1778. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1779. }
  1780. }
  1781. /**
  1782. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1783. * @mbxCommand: mailbox command code.
  1784. *
  1785. * This function is called by the mailbox event handler function to verify
  1786. * that the completed mailbox command is a legitimate mailbox command. If the
  1787. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1788. * and the mailbox event handler will take the HBA offline.
  1789. **/
  1790. static int
  1791. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1792. {
  1793. uint8_t ret;
  1794. switch (mbxCommand) {
  1795. case MBX_LOAD_SM:
  1796. case MBX_READ_NV:
  1797. case MBX_WRITE_NV:
  1798. case MBX_WRITE_VPARMS:
  1799. case MBX_RUN_BIU_DIAG:
  1800. case MBX_INIT_LINK:
  1801. case MBX_DOWN_LINK:
  1802. case MBX_CONFIG_LINK:
  1803. case MBX_CONFIG_RING:
  1804. case MBX_RESET_RING:
  1805. case MBX_READ_CONFIG:
  1806. case MBX_READ_RCONFIG:
  1807. case MBX_READ_SPARM:
  1808. case MBX_READ_STATUS:
  1809. case MBX_READ_RPI:
  1810. case MBX_READ_XRI:
  1811. case MBX_READ_REV:
  1812. case MBX_READ_LNK_STAT:
  1813. case MBX_REG_LOGIN:
  1814. case MBX_UNREG_LOGIN:
  1815. case MBX_CLEAR_LA:
  1816. case MBX_DUMP_MEMORY:
  1817. case MBX_DUMP_CONTEXT:
  1818. case MBX_RUN_DIAGS:
  1819. case MBX_RESTART:
  1820. case MBX_UPDATE_CFG:
  1821. case MBX_DOWN_LOAD:
  1822. case MBX_DEL_LD_ENTRY:
  1823. case MBX_RUN_PROGRAM:
  1824. case MBX_SET_MASK:
  1825. case MBX_SET_VARIABLE:
  1826. case MBX_UNREG_D_ID:
  1827. case MBX_KILL_BOARD:
  1828. case MBX_CONFIG_FARP:
  1829. case MBX_BEACON:
  1830. case MBX_LOAD_AREA:
  1831. case MBX_RUN_BIU_DIAG64:
  1832. case MBX_CONFIG_PORT:
  1833. case MBX_READ_SPARM64:
  1834. case MBX_READ_RPI64:
  1835. case MBX_REG_LOGIN64:
  1836. case MBX_READ_TOPOLOGY:
  1837. case MBX_WRITE_WWN:
  1838. case MBX_SET_DEBUG:
  1839. case MBX_LOAD_EXP_ROM:
  1840. case MBX_ASYNCEVT_ENABLE:
  1841. case MBX_REG_VPI:
  1842. case MBX_UNREG_VPI:
  1843. case MBX_HEARTBEAT:
  1844. case MBX_PORT_CAPABILITIES:
  1845. case MBX_PORT_IOV_CONTROL:
  1846. case MBX_SLI4_CONFIG:
  1847. case MBX_SLI4_REQ_FTRS:
  1848. case MBX_REG_FCFI:
  1849. case MBX_UNREG_FCFI:
  1850. case MBX_REG_VFI:
  1851. case MBX_UNREG_VFI:
  1852. case MBX_INIT_VPI:
  1853. case MBX_INIT_VFI:
  1854. case MBX_RESUME_RPI:
  1855. case MBX_READ_EVENT_LOG_STATUS:
  1856. case MBX_READ_EVENT_LOG:
  1857. case MBX_SECURITY_MGMT:
  1858. case MBX_AUTH_PORT:
  1859. ret = mbxCommand;
  1860. break;
  1861. default:
  1862. ret = MBX_SHUTDOWN;
  1863. break;
  1864. }
  1865. return ret;
  1866. }
  1867. /**
  1868. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1869. * @phba: Pointer to HBA context object.
  1870. * @pmboxq: Pointer to mailbox command.
  1871. *
  1872. * This is completion handler function for mailbox commands issued from
  1873. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1874. * mailbox event handler function with no lock held. This function
  1875. * will wake up thread waiting on the wait queue pointed by context1
  1876. * of the mailbox.
  1877. **/
  1878. void
  1879. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1880. {
  1881. wait_queue_head_t *pdone_q;
  1882. unsigned long drvr_flag;
  1883. /*
  1884. * If pdone_q is empty, the driver thread gave up waiting and
  1885. * continued running.
  1886. */
  1887. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1888. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1889. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1890. if (pdone_q)
  1891. wake_up_interruptible(pdone_q);
  1892. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1893. return;
  1894. }
  1895. /**
  1896. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1897. * @phba: Pointer to HBA context object.
  1898. * @pmb: Pointer to mailbox object.
  1899. *
  1900. * This function is the default mailbox completion handler. It
  1901. * frees the memory resources associated with the completed mailbox
  1902. * command. If the completed command is a REG_LOGIN mailbox command,
  1903. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1904. **/
  1905. void
  1906. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1907. {
  1908. struct lpfc_vport *vport = pmb->vport;
  1909. struct lpfc_dmabuf *mp;
  1910. struct lpfc_nodelist *ndlp;
  1911. struct Scsi_Host *shost;
  1912. uint16_t rpi, vpi;
  1913. int rc;
  1914. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1915. if (mp) {
  1916. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1917. kfree(mp);
  1918. }
  1919. /*
  1920. * If a REG_LOGIN succeeded after node is destroyed or node
  1921. * is in re-discovery driver need to cleanup the RPI.
  1922. */
  1923. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1924. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1925. !pmb->u.mb.mbxStatus) {
  1926. rpi = pmb->u.mb.un.varWords[0];
  1927. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1928. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1929. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1930. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1931. if (rc != MBX_NOT_FINISHED)
  1932. return;
  1933. }
  1934. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1935. !(phba->pport->load_flag & FC_UNLOADING) &&
  1936. !pmb->u.mb.mbxStatus) {
  1937. shost = lpfc_shost_from_vport(vport);
  1938. spin_lock_irq(shost->host_lock);
  1939. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1940. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1941. spin_unlock_irq(shost->host_lock);
  1942. }
  1943. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1944. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1945. lpfc_nlp_put(ndlp);
  1946. pmb->context2 = NULL;
  1947. }
  1948. /* Check security permission status on INIT_LINK mailbox command */
  1949. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1950. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1951. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1952. "2860 SLI authentication is required "
  1953. "for INIT_LINK but has not done yet\n");
  1954. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1955. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1956. else
  1957. mempool_free(pmb, phba->mbox_mem_pool);
  1958. }
  1959. /**
  1960. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1961. * @phba: Pointer to HBA context object.
  1962. *
  1963. * This function is called with no lock held. This function processes all
  1964. * the completed mailbox commands and gives it to upper layers. The interrupt
  1965. * service routine processes mailbox completion interrupt and adds completed
  1966. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1967. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1968. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1969. * function returns the mailbox commands to the upper layer by calling the
  1970. * completion handler function of each mailbox.
  1971. **/
  1972. int
  1973. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1974. {
  1975. MAILBOX_t *pmbox;
  1976. LPFC_MBOXQ_t *pmb;
  1977. int rc;
  1978. LIST_HEAD(cmplq);
  1979. phba->sli.slistat.mbox_event++;
  1980. /* Get all completed mailboxe buffers into the cmplq */
  1981. spin_lock_irq(&phba->hbalock);
  1982. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  1983. spin_unlock_irq(&phba->hbalock);
  1984. /* Get a Mailbox buffer to setup mailbox commands for callback */
  1985. do {
  1986. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  1987. if (pmb == NULL)
  1988. break;
  1989. pmbox = &pmb->u.mb;
  1990. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  1991. if (pmb->vport) {
  1992. lpfc_debugfs_disc_trc(pmb->vport,
  1993. LPFC_DISC_TRC_MBOX_VPORT,
  1994. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  1995. (uint32_t)pmbox->mbxCommand,
  1996. pmbox->un.varWords[0],
  1997. pmbox->un.varWords[1]);
  1998. }
  1999. else {
  2000. lpfc_debugfs_disc_trc(phba->pport,
  2001. LPFC_DISC_TRC_MBOX,
  2002. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2003. (uint32_t)pmbox->mbxCommand,
  2004. pmbox->un.varWords[0],
  2005. pmbox->un.varWords[1]);
  2006. }
  2007. }
  2008. /*
  2009. * It is a fatal error if unknown mbox command completion.
  2010. */
  2011. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2012. MBX_SHUTDOWN) {
  2013. /* Unknown mailbox command compl */
  2014. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2015. "(%d):0323 Unknown Mailbox command "
  2016. "x%x (x%x) Cmpl\n",
  2017. pmb->vport ? pmb->vport->vpi : 0,
  2018. pmbox->mbxCommand,
  2019. lpfc_sli4_mbox_opcode_get(phba, pmb));
  2020. phba->link_state = LPFC_HBA_ERROR;
  2021. phba->work_hs = HS_FFER3;
  2022. lpfc_handle_eratt(phba);
  2023. continue;
  2024. }
  2025. if (pmbox->mbxStatus) {
  2026. phba->sli.slistat.mbox_stat_err++;
  2027. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2028. /* Mbox cmd cmpl error - RETRYing */
  2029. lpfc_printf_log(phba, KERN_INFO,
  2030. LOG_MBOX | LOG_SLI,
  2031. "(%d):0305 Mbox cmd cmpl "
  2032. "error - RETRYing Data: x%x "
  2033. "(x%x) x%x x%x x%x\n",
  2034. pmb->vport ? pmb->vport->vpi :0,
  2035. pmbox->mbxCommand,
  2036. lpfc_sli4_mbox_opcode_get(phba,
  2037. pmb),
  2038. pmbox->mbxStatus,
  2039. pmbox->un.varWords[0],
  2040. pmb->vport->port_state);
  2041. pmbox->mbxStatus = 0;
  2042. pmbox->mbxOwner = OWN_HOST;
  2043. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2044. if (rc != MBX_NOT_FINISHED)
  2045. continue;
  2046. }
  2047. }
  2048. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2049. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2050. "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
  2051. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2052. pmb->vport ? pmb->vport->vpi : 0,
  2053. pmbox->mbxCommand,
  2054. lpfc_sli4_mbox_opcode_get(phba, pmb),
  2055. pmb->mbox_cmpl,
  2056. *((uint32_t *) pmbox),
  2057. pmbox->un.varWords[0],
  2058. pmbox->un.varWords[1],
  2059. pmbox->un.varWords[2],
  2060. pmbox->un.varWords[3],
  2061. pmbox->un.varWords[4],
  2062. pmbox->un.varWords[5],
  2063. pmbox->un.varWords[6],
  2064. pmbox->un.varWords[7]);
  2065. if (pmb->mbox_cmpl)
  2066. pmb->mbox_cmpl(phba,pmb);
  2067. } while (1);
  2068. return 0;
  2069. }
  2070. /**
  2071. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2072. * @phba: Pointer to HBA context object.
  2073. * @pring: Pointer to driver SLI ring object.
  2074. * @tag: buffer tag.
  2075. *
  2076. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2077. * is set in the tag the buffer is posted for a particular exchange,
  2078. * the function will return the buffer without replacing the buffer.
  2079. * If the buffer is for unsolicited ELS or CT traffic, this function
  2080. * returns the buffer and also posts another buffer to the firmware.
  2081. **/
  2082. static struct lpfc_dmabuf *
  2083. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2084. struct lpfc_sli_ring *pring,
  2085. uint32_t tag)
  2086. {
  2087. struct hbq_dmabuf *hbq_entry;
  2088. if (tag & QUE_BUFTAG_BIT)
  2089. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2090. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2091. if (!hbq_entry)
  2092. return NULL;
  2093. return &hbq_entry->dbuf;
  2094. }
  2095. /**
  2096. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2097. * @phba: Pointer to HBA context object.
  2098. * @pring: Pointer to driver SLI ring object.
  2099. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2100. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2101. * @fch_type: the type for the first frame of the sequence.
  2102. *
  2103. * This function is called with no lock held. This function uses the r_ctl and
  2104. * type of the received sequence to find the correct callback function to call
  2105. * to process the sequence.
  2106. **/
  2107. static int
  2108. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2109. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2110. uint32_t fch_type)
  2111. {
  2112. int i;
  2113. /* unSolicited Responses */
  2114. if (pring->prt[0].profile) {
  2115. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2116. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2117. saveq);
  2118. return 1;
  2119. }
  2120. /* We must search, based on rctl / type
  2121. for the right routine */
  2122. for (i = 0; i < pring->num_mask; i++) {
  2123. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2124. (pring->prt[i].type == fch_type)) {
  2125. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2126. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2127. (phba, pring, saveq);
  2128. return 1;
  2129. }
  2130. }
  2131. return 0;
  2132. }
  2133. /**
  2134. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2135. * @phba: Pointer to HBA context object.
  2136. * @pring: Pointer to driver SLI ring object.
  2137. * @saveq: Pointer to the unsolicited iocb.
  2138. *
  2139. * This function is called with no lock held by the ring event handler
  2140. * when there is an unsolicited iocb posted to the response ring by the
  2141. * firmware. This function gets the buffer associated with the iocbs
  2142. * and calls the event handler for the ring. This function handles both
  2143. * qring buffers and hbq buffers.
  2144. * When the function returns 1 the caller can free the iocb object otherwise
  2145. * upper layer functions will free the iocb objects.
  2146. **/
  2147. static int
  2148. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2149. struct lpfc_iocbq *saveq)
  2150. {
  2151. IOCB_t * irsp;
  2152. WORD5 * w5p;
  2153. uint32_t Rctl, Type;
  2154. uint32_t match;
  2155. struct lpfc_iocbq *iocbq;
  2156. struct lpfc_dmabuf *dmzbuf;
  2157. match = 0;
  2158. irsp = &(saveq->iocb);
  2159. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2160. if (pring->lpfc_sli_rcv_async_status)
  2161. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2162. else
  2163. lpfc_printf_log(phba,
  2164. KERN_WARNING,
  2165. LOG_SLI,
  2166. "0316 Ring %d handler: unexpected "
  2167. "ASYNC_STATUS iocb received evt_code "
  2168. "0x%x\n",
  2169. pring->ringno,
  2170. irsp->un.asyncstat.evt_code);
  2171. return 1;
  2172. }
  2173. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2174. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2175. if (irsp->ulpBdeCount > 0) {
  2176. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2177. irsp->un.ulpWord[3]);
  2178. lpfc_in_buf_free(phba, dmzbuf);
  2179. }
  2180. if (irsp->ulpBdeCount > 1) {
  2181. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2182. irsp->unsli3.sli3Words[3]);
  2183. lpfc_in_buf_free(phba, dmzbuf);
  2184. }
  2185. if (irsp->ulpBdeCount > 2) {
  2186. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2187. irsp->unsli3.sli3Words[7]);
  2188. lpfc_in_buf_free(phba, dmzbuf);
  2189. }
  2190. return 1;
  2191. }
  2192. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2193. if (irsp->ulpBdeCount != 0) {
  2194. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2195. irsp->un.ulpWord[3]);
  2196. if (!saveq->context2)
  2197. lpfc_printf_log(phba,
  2198. KERN_ERR,
  2199. LOG_SLI,
  2200. "0341 Ring %d Cannot find buffer for "
  2201. "an unsolicited iocb. tag 0x%x\n",
  2202. pring->ringno,
  2203. irsp->un.ulpWord[3]);
  2204. }
  2205. if (irsp->ulpBdeCount == 2) {
  2206. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2207. irsp->unsli3.sli3Words[7]);
  2208. if (!saveq->context3)
  2209. lpfc_printf_log(phba,
  2210. KERN_ERR,
  2211. LOG_SLI,
  2212. "0342 Ring %d Cannot find buffer for an"
  2213. " unsolicited iocb. tag 0x%x\n",
  2214. pring->ringno,
  2215. irsp->unsli3.sli3Words[7]);
  2216. }
  2217. list_for_each_entry(iocbq, &saveq->list, list) {
  2218. irsp = &(iocbq->iocb);
  2219. if (irsp->ulpBdeCount != 0) {
  2220. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2221. irsp->un.ulpWord[3]);
  2222. if (!iocbq->context2)
  2223. lpfc_printf_log(phba,
  2224. KERN_ERR,
  2225. LOG_SLI,
  2226. "0343 Ring %d Cannot find "
  2227. "buffer for an unsolicited iocb"
  2228. ". tag 0x%x\n", pring->ringno,
  2229. irsp->un.ulpWord[3]);
  2230. }
  2231. if (irsp->ulpBdeCount == 2) {
  2232. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2233. irsp->unsli3.sli3Words[7]);
  2234. if (!iocbq->context3)
  2235. lpfc_printf_log(phba,
  2236. KERN_ERR,
  2237. LOG_SLI,
  2238. "0344 Ring %d Cannot find "
  2239. "buffer for an unsolicited "
  2240. "iocb. tag 0x%x\n",
  2241. pring->ringno,
  2242. irsp->unsli3.sli3Words[7]);
  2243. }
  2244. }
  2245. }
  2246. if (irsp->ulpBdeCount != 0 &&
  2247. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2248. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2249. int found = 0;
  2250. /* search continue save q for same XRI */
  2251. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2252. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2253. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2254. list_add_tail(&saveq->list, &iocbq->list);
  2255. found = 1;
  2256. break;
  2257. }
  2258. }
  2259. if (!found)
  2260. list_add_tail(&saveq->clist,
  2261. &pring->iocb_continue_saveq);
  2262. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2263. list_del_init(&iocbq->clist);
  2264. saveq = iocbq;
  2265. irsp = &(saveq->iocb);
  2266. } else
  2267. return 0;
  2268. }
  2269. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2270. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2271. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2272. Rctl = FC_RCTL_ELS_REQ;
  2273. Type = FC_TYPE_ELS;
  2274. } else {
  2275. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2276. Rctl = w5p->hcsw.Rctl;
  2277. Type = w5p->hcsw.Type;
  2278. /* Firmware Workaround */
  2279. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2280. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2281. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2282. Rctl = FC_RCTL_ELS_REQ;
  2283. Type = FC_TYPE_ELS;
  2284. w5p->hcsw.Rctl = Rctl;
  2285. w5p->hcsw.Type = Type;
  2286. }
  2287. }
  2288. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2289. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2290. "0313 Ring %d handler: unexpected Rctl x%x "
  2291. "Type x%x received\n",
  2292. pring->ringno, Rctl, Type);
  2293. return 1;
  2294. }
  2295. /**
  2296. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2297. * @phba: Pointer to HBA context object.
  2298. * @pring: Pointer to driver SLI ring object.
  2299. * @prspiocb: Pointer to response iocb object.
  2300. *
  2301. * This function looks up the iocb_lookup table to get the command iocb
  2302. * corresponding to the given response iocb using the iotag of the
  2303. * response iocb. This function is called with the hbalock held.
  2304. * This function returns the command iocb object if it finds the command
  2305. * iocb else returns NULL.
  2306. **/
  2307. static struct lpfc_iocbq *
  2308. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2309. struct lpfc_sli_ring *pring,
  2310. struct lpfc_iocbq *prspiocb)
  2311. {
  2312. struct lpfc_iocbq *cmd_iocb = NULL;
  2313. uint16_t iotag;
  2314. iotag = prspiocb->iocb.ulpIoTag;
  2315. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2316. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2317. list_del_init(&cmd_iocb->list);
  2318. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2319. pring->txcmplq_cnt--;
  2320. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2321. }
  2322. return cmd_iocb;
  2323. }
  2324. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2325. "0317 iotag x%x is out off "
  2326. "range: max iotag x%x wd0 x%x\n",
  2327. iotag, phba->sli.last_iotag,
  2328. *(((uint32_t *) &prspiocb->iocb) + 7));
  2329. return NULL;
  2330. }
  2331. /**
  2332. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2333. * @phba: Pointer to HBA context object.
  2334. * @pring: Pointer to driver SLI ring object.
  2335. * @iotag: IOCB tag.
  2336. *
  2337. * This function looks up the iocb_lookup table to get the command iocb
  2338. * corresponding to the given iotag. This function is called with the
  2339. * hbalock held.
  2340. * This function returns the command iocb object if it finds the command
  2341. * iocb else returns NULL.
  2342. **/
  2343. static struct lpfc_iocbq *
  2344. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2345. struct lpfc_sli_ring *pring, uint16_t iotag)
  2346. {
  2347. struct lpfc_iocbq *cmd_iocb;
  2348. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2349. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2350. list_del_init(&cmd_iocb->list);
  2351. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2352. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2353. pring->txcmplq_cnt--;
  2354. }
  2355. return cmd_iocb;
  2356. }
  2357. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2358. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2359. iotag, phba->sli.last_iotag);
  2360. return NULL;
  2361. }
  2362. /**
  2363. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2364. * @phba: Pointer to HBA context object.
  2365. * @pring: Pointer to driver SLI ring object.
  2366. * @saveq: Pointer to the response iocb to be processed.
  2367. *
  2368. * This function is called by the ring event handler for non-fcp
  2369. * rings when there is a new response iocb in the response ring.
  2370. * The caller is not required to hold any locks. This function
  2371. * gets the command iocb associated with the response iocb and
  2372. * calls the completion handler for the command iocb. If there
  2373. * is no completion handler, the function will free the resources
  2374. * associated with command iocb. If the response iocb is for
  2375. * an already aborted command iocb, the status of the completion
  2376. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2377. * This function always returns 1.
  2378. **/
  2379. static int
  2380. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2381. struct lpfc_iocbq *saveq)
  2382. {
  2383. struct lpfc_iocbq *cmdiocbp;
  2384. int rc = 1;
  2385. unsigned long iflag;
  2386. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2387. spin_lock_irqsave(&phba->hbalock, iflag);
  2388. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2389. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2390. if (cmdiocbp) {
  2391. if (cmdiocbp->iocb_cmpl) {
  2392. /*
  2393. * If an ELS command failed send an event to mgmt
  2394. * application.
  2395. */
  2396. if (saveq->iocb.ulpStatus &&
  2397. (pring->ringno == LPFC_ELS_RING) &&
  2398. (cmdiocbp->iocb.ulpCommand ==
  2399. CMD_ELS_REQUEST64_CR))
  2400. lpfc_send_els_failure_event(phba,
  2401. cmdiocbp, saveq);
  2402. /*
  2403. * Post all ELS completions to the worker thread.
  2404. * All other are passed to the completion callback.
  2405. */
  2406. if (pring->ringno == LPFC_ELS_RING) {
  2407. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2408. (cmdiocbp->iocb_flag &
  2409. LPFC_DRIVER_ABORTED)) {
  2410. spin_lock_irqsave(&phba->hbalock,
  2411. iflag);
  2412. cmdiocbp->iocb_flag &=
  2413. ~LPFC_DRIVER_ABORTED;
  2414. spin_unlock_irqrestore(&phba->hbalock,
  2415. iflag);
  2416. saveq->iocb.ulpStatus =
  2417. IOSTAT_LOCAL_REJECT;
  2418. saveq->iocb.un.ulpWord[4] =
  2419. IOERR_SLI_ABORTED;
  2420. /* Firmware could still be in progress
  2421. * of DMAing payload, so don't free data
  2422. * buffer till after a hbeat.
  2423. */
  2424. spin_lock_irqsave(&phba->hbalock,
  2425. iflag);
  2426. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2427. spin_unlock_irqrestore(&phba->hbalock,
  2428. iflag);
  2429. }
  2430. if (phba->sli_rev == LPFC_SLI_REV4) {
  2431. if (saveq->iocb_flag &
  2432. LPFC_EXCHANGE_BUSY) {
  2433. /* Set cmdiocb flag for the
  2434. * exchange busy so sgl (xri)
  2435. * will not be released until
  2436. * the abort xri is received
  2437. * from hba.
  2438. */
  2439. spin_lock_irqsave(
  2440. &phba->hbalock, iflag);
  2441. cmdiocbp->iocb_flag |=
  2442. LPFC_EXCHANGE_BUSY;
  2443. spin_unlock_irqrestore(
  2444. &phba->hbalock, iflag);
  2445. }
  2446. if (cmdiocbp->iocb_flag &
  2447. LPFC_DRIVER_ABORTED) {
  2448. /*
  2449. * Clear LPFC_DRIVER_ABORTED
  2450. * bit in case it was driver
  2451. * initiated abort.
  2452. */
  2453. spin_lock_irqsave(
  2454. &phba->hbalock, iflag);
  2455. cmdiocbp->iocb_flag &=
  2456. ~LPFC_DRIVER_ABORTED;
  2457. spin_unlock_irqrestore(
  2458. &phba->hbalock, iflag);
  2459. cmdiocbp->iocb.ulpStatus =
  2460. IOSTAT_LOCAL_REJECT;
  2461. cmdiocbp->iocb.un.ulpWord[4] =
  2462. IOERR_ABORT_REQUESTED;
  2463. /*
  2464. * For SLI4, irsiocb contains
  2465. * NO_XRI in sli_xritag, it
  2466. * shall not affect releasing
  2467. * sgl (xri) process.
  2468. */
  2469. saveq->iocb.ulpStatus =
  2470. IOSTAT_LOCAL_REJECT;
  2471. saveq->iocb.un.ulpWord[4] =
  2472. IOERR_SLI_ABORTED;
  2473. spin_lock_irqsave(
  2474. &phba->hbalock, iflag);
  2475. saveq->iocb_flag |=
  2476. LPFC_DELAY_MEM_FREE;
  2477. spin_unlock_irqrestore(
  2478. &phba->hbalock, iflag);
  2479. }
  2480. }
  2481. }
  2482. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2483. } else
  2484. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2485. } else {
  2486. /*
  2487. * Unknown initiating command based on the response iotag.
  2488. * This could be the case on the ELS ring because of
  2489. * lpfc_els_abort().
  2490. */
  2491. if (pring->ringno != LPFC_ELS_RING) {
  2492. /*
  2493. * Ring <ringno> handler: unexpected completion IoTag
  2494. * <IoTag>
  2495. */
  2496. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2497. "0322 Ring %d handler: "
  2498. "unexpected completion IoTag x%x "
  2499. "Data: x%x x%x x%x x%x\n",
  2500. pring->ringno,
  2501. saveq->iocb.ulpIoTag,
  2502. saveq->iocb.ulpStatus,
  2503. saveq->iocb.un.ulpWord[4],
  2504. saveq->iocb.ulpCommand,
  2505. saveq->iocb.ulpContext);
  2506. }
  2507. }
  2508. return rc;
  2509. }
  2510. /**
  2511. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2512. * @phba: Pointer to HBA context object.
  2513. * @pring: Pointer to driver SLI ring object.
  2514. *
  2515. * This function is called from the iocb ring event handlers when
  2516. * put pointer is ahead of the get pointer for a ring. This function signal
  2517. * an error attention condition to the worker thread and the worker
  2518. * thread will transition the HBA to offline state.
  2519. **/
  2520. static void
  2521. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2522. {
  2523. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2524. /*
  2525. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2526. * rsp ring <portRspMax>
  2527. */
  2528. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2529. "0312 Ring %d handler: portRspPut %d "
  2530. "is bigger than rsp ring %d\n",
  2531. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2532. pring->numRiocb);
  2533. phba->link_state = LPFC_HBA_ERROR;
  2534. /*
  2535. * All error attention handlers are posted to
  2536. * worker thread
  2537. */
  2538. phba->work_ha |= HA_ERATT;
  2539. phba->work_hs = HS_FFER3;
  2540. lpfc_worker_wake_up(phba);
  2541. return;
  2542. }
  2543. /**
  2544. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2545. * @ptr: Pointer to address of HBA context object.
  2546. *
  2547. * This function is invoked by the Error Attention polling timer when the
  2548. * timer times out. It will check the SLI Error Attention register for
  2549. * possible attention events. If so, it will post an Error Attention event
  2550. * and wake up worker thread to process it. Otherwise, it will set up the
  2551. * Error Attention polling timer for the next poll.
  2552. **/
  2553. void lpfc_poll_eratt(unsigned long ptr)
  2554. {
  2555. struct lpfc_hba *phba;
  2556. uint32_t eratt = 0;
  2557. phba = (struct lpfc_hba *)ptr;
  2558. /* Check chip HA register for error event */
  2559. eratt = lpfc_sli_check_eratt(phba);
  2560. if (eratt)
  2561. /* Tell the worker thread there is work to do */
  2562. lpfc_worker_wake_up(phba);
  2563. else
  2564. /* Restart the timer for next eratt poll */
  2565. mod_timer(&phba->eratt_poll, jiffies +
  2566. HZ * LPFC_ERATT_POLL_INTERVAL);
  2567. return;
  2568. }
  2569. /**
  2570. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2571. * @phba: Pointer to HBA context object.
  2572. * @pring: Pointer to driver SLI ring object.
  2573. * @mask: Host attention register mask for this ring.
  2574. *
  2575. * This function is called from the interrupt context when there is a ring
  2576. * event for the fcp ring. The caller does not hold any lock.
  2577. * The function processes each response iocb in the response ring until it
  2578. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2579. * LE bit set. The function will call the completion handler of the command iocb
  2580. * if the response iocb indicates a completion for a command iocb or it is
  2581. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2582. * function if this is an unsolicited iocb.
  2583. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2584. * to check it explicitly.
  2585. */
  2586. int
  2587. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2588. struct lpfc_sli_ring *pring, uint32_t mask)
  2589. {
  2590. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2591. IOCB_t *irsp = NULL;
  2592. IOCB_t *entry = NULL;
  2593. struct lpfc_iocbq *cmdiocbq = NULL;
  2594. struct lpfc_iocbq rspiocbq;
  2595. uint32_t status;
  2596. uint32_t portRspPut, portRspMax;
  2597. int rc = 1;
  2598. lpfc_iocb_type type;
  2599. unsigned long iflag;
  2600. uint32_t rsp_cmpl = 0;
  2601. spin_lock_irqsave(&phba->hbalock, iflag);
  2602. pring->stats.iocb_event++;
  2603. /*
  2604. * The next available response entry should never exceed the maximum
  2605. * entries. If it does, treat it as an adapter hardware error.
  2606. */
  2607. portRspMax = pring->numRiocb;
  2608. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2609. if (unlikely(portRspPut >= portRspMax)) {
  2610. lpfc_sli_rsp_pointers_error(phba, pring);
  2611. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2612. return 1;
  2613. }
  2614. if (phba->fcp_ring_in_use) {
  2615. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2616. return 1;
  2617. } else
  2618. phba->fcp_ring_in_use = 1;
  2619. rmb();
  2620. while (pring->rspidx != portRspPut) {
  2621. /*
  2622. * Fetch an entry off the ring and copy it into a local data
  2623. * structure. The copy involves a byte-swap since the
  2624. * network byte order and pci byte orders are different.
  2625. */
  2626. entry = lpfc_resp_iocb(phba, pring);
  2627. phba->last_completion_time = jiffies;
  2628. if (++pring->rspidx >= portRspMax)
  2629. pring->rspidx = 0;
  2630. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2631. (uint32_t *) &rspiocbq.iocb,
  2632. phba->iocb_rsp_size);
  2633. INIT_LIST_HEAD(&(rspiocbq.list));
  2634. irsp = &rspiocbq.iocb;
  2635. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2636. pring->stats.iocb_rsp++;
  2637. rsp_cmpl++;
  2638. if (unlikely(irsp->ulpStatus)) {
  2639. /*
  2640. * If resource errors reported from HBA, reduce
  2641. * queuedepths of the SCSI device.
  2642. */
  2643. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2644. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2645. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2646. phba->lpfc_rampdown_queue_depth(phba);
  2647. spin_lock_irqsave(&phba->hbalock, iflag);
  2648. }
  2649. /* Rsp ring <ringno> error: IOCB */
  2650. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2651. "0336 Rsp Ring %d error: IOCB Data: "
  2652. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2653. pring->ringno,
  2654. irsp->un.ulpWord[0],
  2655. irsp->un.ulpWord[1],
  2656. irsp->un.ulpWord[2],
  2657. irsp->un.ulpWord[3],
  2658. irsp->un.ulpWord[4],
  2659. irsp->un.ulpWord[5],
  2660. *(uint32_t *)&irsp->un1,
  2661. *((uint32_t *)&irsp->un1 + 1));
  2662. }
  2663. switch (type) {
  2664. case LPFC_ABORT_IOCB:
  2665. case LPFC_SOL_IOCB:
  2666. /*
  2667. * Idle exchange closed via ABTS from port. No iocb
  2668. * resources need to be recovered.
  2669. */
  2670. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2671. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2672. "0333 IOCB cmd 0x%x"
  2673. " processed. Skipping"
  2674. " completion\n",
  2675. irsp->ulpCommand);
  2676. break;
  2677. }
  2678. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2679. &rspiocbq);
  2680. if (unlikely(!cmdiocbq))
  2681. break;
  2682. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2683. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2684. if (cmdiocbq->iocb_cmpl) {
  2685. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2686. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2687. &rspiocbq);
  2688. spin_lock_irqsave(&phba->hbalock, iflag);
  2689. }
  2690. break;
  2691. case LPFC_UNSOL_IOCB:
  2692. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2693. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2694. spin_lock_irqsave(&phba->hbalock, iflag);
  2695. break;
  2696. default:
  2697. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2698. char adaptermsg[LPFC_MAX_ADPTMSG];
  2699. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2700. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2701. MAX_MSG_DATA);
  2702. dev_warn(&((phba->pcidev)->dev),
  2703. "lpfc%d: %s\n",
  2704. phba->brd_no, adaptermsg);
  2705. } else {
  2706. /* Unknown IOCB command */
  2707. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2708. "0334 Unknown IOCB command "
  2709. "Data: x%x, x%x x%x x%x x%x\n",
  2710. type, irsp->ulpCommand,
  2711. irsp->ulpStatus,
  2712. irsp->ulpIoTag,
  2713. irsp->ulpContext);
  2714. }
  2715. break;
  2716. }
  2717. /*
  2718. * The response IOCB has been processed. Update the ring
  2719. * pointer in SLIM. If the port response put pointer has not
  2720. * been updated, sync the pgp->rspPutInx and fetch the new port
  2721. * response put pointer.
  2722. */
  2723. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2724. if (pring->rspidx == portRspPut)
  2725. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2726. }
  2727. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2728. pring->stats.iocb_rsp_full++;
  2729. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2730. writel(status, phba->CAregaddr);
  2731. readl(phba->CAregaddr);
  2732. }
  2733. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2734. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2735. pring->stats.iocb_cmd_empty++;
  2736. /* Force update of the local copy of cmdGetInx */
  2737. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2738. lpfc_sli_resume_iocb(phba, pring);
  2739. if ((pring->lpfc_sli_cmd_available))
  2740. (pring->lpfc_sli_cmd_available) (phba, pring);
  2741. }
  2742. phba->fcp_ring_in_use = 0;
  2743. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2744. return rc;
  2745. }
  2746. /**
  2747. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2748. * @phba: Pointer to HBA context object.
  2749. * @pring: Pointer to driver SLI ring object.
  2750. * @rspiocbp: Pointer to driver response IOCB object.
  2751. *
  2752. * This function is called from the worker thread when there is a slow-path
  2753. * response IOCB to process. This function chains all the response iocbs until
  2754. * seeing the iocb with the LE bit set. The function will call
  2755. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2756. * completion of a command iocb. The function will call the
  2757. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2758. * The function frees the resources or calls the completion handler if this
  2759. * iocb is an abort completion. The function returns NULL when the response
  2760. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2761. * this function shall chain the iocb on to the iocb_continueq and return the
  2762. * response iocb passed in.
  2763. **/
  2764. static struct lpfc_iocbq *
  2765. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2766. struct lpfc_iocbq *rspiocbp)
  2767. {
  2768. struct lpfc_iocbq *saveq;
  2769. struct lpfc_iocbq *cmdiocbp;
  2770. struct lpfc_iocbq *next_iocb;
  2771. IOCB_t *irsp = NULL;
  2772. uint32_t free_saveq;
  2773. uint8_t iocb_cmd_type;
  2774. lpfc_iocb_type type;
  2775. unsigned long iflag;
  2776. int rc;
  2777. spin_lock_irqsave(&phba->hbalock, iflag);
  2778. /* First add the response iocb to the countinueq list */
  2779. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2780. pring->iocb_continueq_cnt++;
  2781. /* Now, determine whether the list is completed for processing */
  2782. irsp = &rspiocbp->iocb;
  2783. if (irsp->ulpLe) {
  2784. /*
  2785. * By default, the driver expects to free all resources
  2786. * associated with this iocb completion.
  2787. */
  2788. free_saveq = 1;
  2789. saveq = list_get_first(&pring->iocb_continueq,
  2790. struct lpfc_iocbq, list);
  2791. irsp = &(saveq->iocb);
  2792. list_del_init(&pring->iocb_continueq);
  2793. pring->iocb_continueq_cnt = 0;
  2794. pring->stats.iocb_rsp++;
  2795. /*
  2796. * If resource errors reported from HBA, reduce
  2797. * queuedepths of the SCSI device.
  2798. */
  2799. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2800. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2801. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2802. phba->lpfc_rampdown_queue_depth(phba);
  2803. spin_lock_irqsave(&phba->hbalock, iflag);
  2804. }
  2805. if (irsp->ulpStatus) {
  2806. /* Rsp ring <ringno> error: IOCB */
  2807. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2808. "0328 Rsp Ring %d error: "
  2809. "IOCB Data: "
  2810. "x%x x%x x%x x%x "
  2811. "x%x x%x x%x x%x "
  2812. "x%x x%x x%x x%x "
  2813. "x%x x%x x%x x%x\n",
  2814. pring->ringno,
  2815. irsp->un.ulpWord[0],
  2816. irsp->un.ulpWord[1],
  2817. irsp->un.ulpWord[2],
  2818. irsp->un.ulpWord[3],
  2819. irsp->un.ulpWord[4],
  2820. irsp->un.ulpWord[5],
  2821. *(((uint32_t *) irsp) + 6),
  2822. *(((uint32_t *) irsp) + 7),
  2823. *(((uint32_t *) irsp) + 8),
  2824. *(((uint32_t *) irsp) + 9),
  2825. *(((uint32_t *) irsp) + 10),
  2826. *(((uint32_t *) irsp) + 11),
  2827. *(((uint32_t *) irsp) + 12),
  2828. *(((uint32_t *) irsp) + 13),
  2829. *(((uint32_t *) irsp) + 14),
  2830. *(((uint32_t *) irsp) + 15));
  2831. }
  2832. /*
  2833. * Fetch the IOCB command type and call the correct completion
  2834. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2835. * get freed back to the lpfc_iocb_list by the discovery
  2836. * kernel thread.
  2837. */
  2838. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2839. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2840. switch (type) {
  2841. case LPFC_SOL_IOCB:
  2842. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2843. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2844. spin_lock_irqsave(&phba->hbalock, iflag);
  2845. break;
  2846. case LPFC_UNSOL_IOCB:
  2847. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2848. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2849. spin_lock_irqsave(&phba->hbalock, iflag);
  2850. if (!rc)
  2851. free_saveq = 0;
  2852. break;
  2853. case LPFC_ABORT_IOCB:
  2854. cmdiocbp = NULL;
  2855. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2856. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2857. saveq);
  2858. if (cmdiocbp) {
  2859. /* Call the specified completion routine */
  2860. if (cmdiocbp->iocb_cmpl) {
  2861. spin_unlock_irqrestore(&phba->hbalock,
  2862. iflag);
  2863. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2864. saveq);
  2865. spin_lock_irqsave(&phba->hbalock,
  2866. iflag);
  2867. } else
  2868. __lpfc_sli_release_iocbq(phba,
  2869. cmdiocbp);
  2870. }
  2871. break;
  2872. case LPFC_UNKNOWN_IOCB:
  2873. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2874. char adaptermsg[LPFC_MAX_ADPTMSG];
  2875. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2876. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2877. MAX_MSG_DATA);
  2878. dev_warn(&((phba->pcidev)->dev),
  2879. "lpfc%d: %s\n",
  2880. phba->brd_no, adaptermsg);
  2881. } else {
  2882. /* Unknown IOCB command */
  2883. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2884. "0335 Unknown IOCB "
  2885. "command Data: x%x "
  2886. "x%x x%x x%x\n",
  2887. irsp->ulpCommand,
  2888. irsp->ulpStatus,
  2889. irsp->ulpIoTag,
  2890. irsp->ulpContext);
  2891. }
  2892. break;
  2893. }
  2894. if (free_saveq) {
  2895. list_for_each_entry_safe(rspiocbp, next_iocb,
  2896. &saveq->list, list) {
  2897. list_del(&rspiocbp->list);
  2898. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2899. }
  2900. __lpfc_sli_release_iocbq(phba, saveq);
  2901. }
  2902. rspiocbp = NULL;
  2903. }
  2904. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2905. return rspiocbp;
  2906. }
  2907. /**
  2908. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2909. * @phba: Pointer to HBA context object.
  2910. * @pring: Pointer to driver SLI ring object.
  2911. * @mask: Host attention register mask for this ring.
  2912. *
  2913. * This routine wraps the actual slow_ring event process routine from the
  2914. * API jump table function pointer from the lpfc_hba struct.
  2915. **/
  2916. void
  2917. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2918. struct lpfc_sli_ring *pring, uint32_t mask)
  2919. {
  2920. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2921. }
  2922. /**
  2923. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2924. * @phba: Pointer to HBA context object.
  2925. * @pring: Pointer to driver SLI ring object.
  2926. * @mask: Host attention register mask for this ring.
  2927. *
  2928. * This function is called from the worker thread when there is a ring event
  2929. * for non-fcp rings. The caller does not hold any lock. The function will
  2930. * remove each response iocb in the response ring and calls the handle
  2931. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2932. **/
  2933. static void
  2934. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2935. struct lpfc_sli_ring *pring, uint32_t mask)
  2936. {
  2937. struct lpfc_pgp *pgp;
  2938. IOCB_t *entry;
  2939. IOCB_t *irsp = NULL;
  2940. struct lpfc_iocbq *rspiocbp = NULL;
  2941. uint32_t portRspPut, portRspMax;
  2942. unsigned long iflag;
  2943. uint32_t status;
  2944. pgp = &phba->port_gp[pring->ringno];
  2945. spin_lock_irqsave(&phba->hbalock, iflag);
  2946. pring->stats.iocb_event++;
  2947. /*
  2948. * The next available response entry should never exceed the maximum
  2949. * entries. If it does, treat it as an adapter hardware error.
  2950. */
  2951. portRspMax = pring->numRiocb;
  2952. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2953. if (portRspPut >= portRspMax) {
  2954. /*
  2955. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2956. * rsp ring <portRspMax>
  2957. */
  2958. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2959. "0303 Ring %d handler: portRspPut %d "
  2960. "is bigger than rsp ring %d\n",
  2961. pring->ringno, portRspPut, portRspMax);
  2962. phba->link_state = LPFC_HBA_ERROR;
  2963. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2964. phba->work_hs = HS_FFER3;
  2965. lpfc_handle_eratt(phba);
  2966. return;
  2967. }
  2968. rmb();
  2969. while (pring->rspidx != portRspPut) {
  2970. /*
  2971. * Build a completion list and call the appropriate handler.
  2972. * The process is to get the next available response iocb, get
  2973. * a free iocb from the list, copy the response data into the
  2974. * free iocb, insert to the continuation list, and update the
  2975. * next response index to slim. This process makes response
  2976. * iocb's in the ring available to DMA as fast as possible but
  2977. * pays a penalty for a copy operation. Since the iocb is
  2978. * only 32 bytes, this penalty is considered small relative to
  2979. * the PCI reads for register values and a slim write. When
  2980. * the ulpLe field is set, the entire Command has been
  2981. * received.
  2982. */
  2983. entry = lpfc_resp_iocb(phba, pring);
  2984. phba->last_completion_time = jiffies;
  2985. rspiocbp = __lpfc_sli_get_iocbq(phba);
  2986. if (rspiocbp == NULL) {
  2987. printk(KERN_ERR "%s: out of buffers! Failing "
  2988. "completion.\n", __func__);
  2989. break;
  2990. }
  2991. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  2992. phba->iocb_rsp_size);
  2993. irsp = &rspiocbp->iocb;
  2994. if (++pring->rspidx >= portRspMax)
  2995. pring->rspidx = 0;
  2996. if (pring->ringno == LPFC_ELS_RING) {
  2997. lpfc_debugfs_slow_ring_trc(phba,
  2998. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  2999. *(((uint32_t *) irsp) + 4),
  3000. *(((uint32_t *) irsp) + 6),
  3001. *(((uint32_t *) irsp) + 7));
  3002. }
  3003. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3004. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3005. /* Handle the response IOCB */
  3006. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3007. spin_lock_irqsave(&phba->hbalock, iflag);
  3008. /*
  3009. * If the port response put pointer has not been updated, sync
  3010. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3011. * response put pointer.
  3012. */
  3013. if (pring->rspidx == portRspPut) {
  3014. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3015. }
  3016. } /* while (pring->rspidx != portRspPut) */
  3017. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3018. /* At least one response entry has been freed */
  3019. pring->stats.iocb_rsp_full++;
  3020. /* SET RxRE_RSP in Chip Att register */
  3021. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3022. writel(status, phba->CAregaddr);
  3023. readl(phba->CAregaddr); /* flush */
  3024. }
  3025. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3026. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3027. pring->stats.iocb_cmd_empty++;
  3028. /* Force update of the local copy of cmdGetInx */
  3029. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3030. lpfc_sli_resume_iocb(phba, pring);
  3031. if ((pring->lpfc_sli_cmd_available))
  3032. (pring->lpfc_sli_cmd_available) (phba, pring);
  3033. }
  3034. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3035. return;
  3036. }
  3037. /**
  3038. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3039. * @phba: Pointer to HBA context object.
  3040. * @pring: Pointer to driver SLI ring object.
  3041. * @mask: Host attention register mask for this ring.
  3042. *
  3043. * This function is called from the worker thread when there is a pending
  3044. * ELS response iocb on the driver internal slow-path response iocb worker
  3045. * queue. The caller does not hold any lock. The function will remove each
  3046. * response iocb from the response worker queue and calls the handle
  3047. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3048. **/
  3049. static void
  3050. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3051. struct lpfc_sli_ring *pring, uint32_t mask)
  3052. {
  3053. struct lpfc_iocbq *irspiocbq;
  3054. struct hbq_dmabuf *dmabuf;
  3055. struct lpfc_cq_event *cq_event;
  3056. unsigned long iflag;
  3057. spin_lock_irqsave(&phba->hbalock, iflag);
  3058. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3059. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3060. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3061. /* Get the response iocb from the head of work queue */
  3062. spin_lock_irqsave(&phba->hbalock, iflag);
  3063. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3064. cq_event, struct lpfc_cq_event, list);
  3065. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3066. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3067. case CQE_CODE_COMPL_WQE:
  3068. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3069. cq_event);
  3070. /* Translate ELS WCQE to response IOCBQ */
  3071. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3072. irspiocbq);
  3073. if (irspiocbq)
  3074. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3075. irspiocbq);
  3076. break;
  3077. case CQE_CODE_RECEIVE:
  3078. case CQE_CODE_RECEIVE_V1:
  3079. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3080. cq_event);
  3081. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3082. break;
  3083. default:
  3084. break;
  3085. }
  3086. }
  3087. }
  3088. /**
  3089. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3090. * @phba: Pointer to HBA context object.
  3091. * @pring: Pointer to driver SLI ring object.
  3092. *
  3093. * This function aborts all iocbs in the given ring and frees all the iocb
  3094. * objects in txq. This function issues an abort iocb for all the iocb commands
  3095. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3096. * the return of this function. The caller is not required to hold any locks.
  3097. **/
  3098. void
  3099. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3100. {
  3101. LIST_HEAD(completions);
  3102. struct lpfc_iocbq *iocb, *next_iocb;
  3103. if (pring->ringno == LPFC_ELS_RING) {
  3104. lpfc_fabric_abort_hba(phba);
  3105. }
  3106. /* Error everything on txq and txcmplq
  3107. * First do the txq.
  3108. */
  3109. spin_lock_irq(&phba->hbalock);
  3110. list_splice_init(&pring->txq, &completions);
  3111. pring->txq_cnt = 0;
  3112. /* Next issue ABTS for everything on the txcmplq */
  3113. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3114. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3115. spin_unlock_irq(&phba->hbalock);
  3116. /* Cancel all the IOCBs from the completions list */
  3117. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3118. IOERR_SLI_ABORTED);
  3119. }
  3120. /**
  3121. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3122. * @phba: Pointer to HBA context object.
  3123. *
  3124. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3125. * objects in txq and txcmplq. This function will not issue abort iocbs
  3126. * for all the iocb commands in txcmplq, they will just be returned with
  3127. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3128. * slot has been permanently disabled.
  3129. **/
  3130. void
  3131. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3132. {
  3133. LIST_HEAD(txq);
  3134. LIST_HEAD(txcmplq);
  3135. struct lpfc_sli *psli = &phba->sli;
  3136. struct lpfc_sli_ring *pring;
  3137. /* Currently, only one fcp ring */
  3138. pring = &psli->ring[psli->fcp_ring];
  3139. spin_lock_irq(&phba->hbalock);
  3140. /* Retrieve everything on txq */
  3141. list_splice_init(&pring->txq, &txq);
  3142. pring->txq_cnt = 0;
  3143. /* Retrieve everything on the txcmplq */
  3144. list_splice_init(&pring->txcmplq, &txcmplq);
  3145. pring->txcmplq_cnt = 0;
  3146. spin_unlock_irq(&phba->hbalock);
  3147. /* Flush the txq */
  3148. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3149. IOERR_SLI_DOWN);
  3150. /* Flush the txcmpq */
  3151. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3152. IOERR_SLI_DOWN);
  3153. }
  3154. /**
  3155. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3156. * @phba: Pointer to HBA context object.
  3157. * @mask: Bit mask to be checked.
  3158. *
  3159. * This function reads the host status register and compares
  3160. * with the provided bit mask to check if HBA completed
  3161. * the restart. This function will wait in a loop for the
  3162. * HBA to complete restart. If the HBA does not restart within
  3163. * 15 iterations, the function will reset the HBA again. The
  3164. * function returns 1 when HBA fail to restart otherwise returns
  3165. * zero.
  3166. **/
  3167. static int
  3168. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3169. {
  3170. uint32_t status;
  3171. int i = 0;
  3172. int retval = 0;
  3173. /* Read the HBA Host Status Register */
  3174. if (lpfc_readl(phba->HSregaddr, &status))
  3175. return 1;
  3176. /*
  3177. * Check status register every 100ms for 5 retries, then every
  3178. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3179. * every 2.5 sec for 4.
  3180. * Break our of the loop if errors occurred during init.
  3181. */
  3182. while (((status & mask) != mask) &&
  3183. !(status & HS_FFERM) &&
  3184. i++ < 20) {
  3185. if (i <= 5)
  3186. msleep(10);
  3187. else if (i <= 10)
  3188. msleep(500);
  3189. else
  3190. msleep(2500);
  3191. if (i == 15) {
  3192. /* Do post */
  3193. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3194. lpfc_sli_brdrestart(phba);
  3195. }
  3196. /* Read the HBA Host Status Register */
  3197. if (lpfc_readl(phba->HSregaddr, &status)) {
  3198. retval = 1;
  3199. break;
  3200. }
  3201. }
  3202. /* Check to see if any errors occurred during init */
  3203. if ((status & HS_FFERM) || (i >= 20)) {
  3204. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3205. "2751 Adapter failed to restart, "
  3206. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3207. status,
  3208. readl(phba->MBslimaddr + 0xa8),
  3209. readl(phba->MBslimaddr + 0xac));
  3210. phba->link_state = LPFC_HBA_ERROR;
  3211. retval = 1;
  3212. }
  3213. return retval;
  3214. }
  3215. /**
  3216. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3217. * @phba: Pointer to HBA context object.
  3218. * @mask: Bit mask to be checked.
  3219. *
  3220. * This function checks the host status register to check if HBA is
  3221. * ready. This function will wait in a loop for the HBA to be ready
  3222. * If the HBA is not ready , the function will will reset the HBA PCI
  3223. * function again. The function returns 1 when HBA fail to be ready
  3224. * otherwise returns zero.
  3225. **/
  3226. static int
  3227. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3228. {
  3229. uint32_t status;
  3230. int retval = 0;
  3231. /* Read the HBA Host Status Register */
  3232. status = lpfc_sli4_post_status_check(phba);
  3233. if (status) {
  3234. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3235. lpfc_sli_brdrestart(phba);
  3236. status = lpfc_sli4_post_status_check(phba);
  3237. }
  3238. /* Check to see if any errors occurred during init */
  3239. if (status) {
  3240. phba->link_state = LPFC_HBA_ERROR;
  3241. retval = 1;
  3242. } else
  3243. phba->sli4_hba.intr_enable = 0;
  3244. return retval;
  3245. }
  3246. /**
  3247. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3248. * @phba: Pointer to HBA context object.
  3249. * @mask: Bit mask to be checked.
  3250. *
  3251. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3252. * from the API jump table function pointer from the lpfc_hba struct.
  3253. **/
  3254. int
  3255. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3256. {
  3257. return phba->lpfc_sli_brdready(phba, mask);
  3258. }
  3259. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3260. /**
  3261. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3262. * @phba: Pointer to HBA context object.
  3263. *
  3264. * This function is called before resetting an HBA. This
  3265. * function requests HBA to quiesce DMAs before a reset.
  3266. **/
  3267. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3268. {
  3269. uint32_t __iomem *resp_buf;
  3270. uint32_t __iomem *mbox_buf;
  3271. volatile uint32_t mbox;
  3272. uint32_t hc_copy, ha_copy, resp_data;
  3273. int i;
  3274. uint8_t hdrtype;
  3275. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3276. if (hdrtype != 0x80 ||
  3277. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3278. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3279. return;
  3280. /*
  3281. * Tell the other part of the chip to suspend temporarily all
  3282. * its DMA activity.
  3283. */
  3284. resp_buf = phba->MBslimaddr;
  3285. /* Disable the error attention */
  3286. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3287. return;
  3288. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3289. readl(phba->HCregaddr); /* flush */
  3290. phba->link_flag |= LS_IGNORE_ERATT;
  3291. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3292. return;
  3293. if (ha_copy & HA_ERATT) {
  3294. /* Clear Chip error bit */
  3295. writel(HA_ERATT, phba->HAregaddr);
  3296. phba->pport->stopped = 1;
  3297. }
  3298. mbox = 0;
  3299. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3300. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3301. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3302. mbox_buf = phba->MBslimaddr;
  3303. writel(mbox, mbox_buf);
  3304. for (i = 0; i < 50; i++) {
  3305. if (lpfc_readl((resp_buf + 1), &resp_data))
  3306. return;
  3307. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3308. mdelay(1);
  3309. else
  3310. break;
  3311. }
  3312. resp_data = 0;
  3313. if (lpfc_readl((resp_buf + 1), &resp_data))
  3314. return;
  3315. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3316. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3317. phba->pport->stopped)
  3318. goto restore_hc;
  3319. else
  3320. goto clear_errat;
  3321. }
  3322. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3323. resp_data = 0;
  3324. for (i = 0; i < 500; i++) {
  3325. if (lpfc_readl(resp_buf, &resp_data))
  3326. return;
  3327. if (resp_data != mbox)
  3328. mdelay(1);
  3329. else
  3330. break;
  3331. }
  3332. clear_errat:
  3333. while (++i < 500) {
  3334. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3335. return;
  3336. if (!(ha_copy & HA_ERATT))
  3337. mdelay(1);
  3338. else
  3339. break;
  3340. }
  3341. if (readl(phba->HAregaddr) & HA_ERATT) {
  3342. writel(HA_ERATT, phba->HAregaddr);
  3343. phba->pport->stopped = 1;
  3344. }
  3345. restore_hc:
  3346. phba->link_flag &= ~LS_IGNORE_ERATT;
  3347. writel(hc_copy, phba->HCregaddr);
  3348. readl(phba->HCregaddr); /* flush */
  3349. }
  3350. /**
  3351. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3352. * @phba: Pointer to HBA context object.
  3353. *
  3354. * This function issues a kill_board mailbox command and waits for
  3355. * the error attention interrupt. This function is called for stopping
  3356. * the firmware processing. The caller is not required to hold any
  3357. * locks. This function calls lpfc_hba_down_post function to free
  3358. * any pending commands after the kill. The function will return 1 when it
  3359. * fails to kill the board else will return 0.
  3360. **/
  3361. int
  3362. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3363. {
  3364. struct lpfc_sli *psli;
  3365. LPFC_MBOXQ_t *pmb;
  3366. uint32_t status;
  3367. uint32_t ha_copy;
  3368. int retval;
  3369. int i = 0;
  3370. psli = &phba->sli;
  3371. /* Kill HBA */
  3372. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3373. "0329 Kill HBA Data: x%x x%x\n",
  3374. phba->pport->port_state, psli->sli_flag);
  3375. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3376. if (!pmb)
  3377. return 1;
  3378. /* Disable the error attention */
  3379. spin_lock_irq(&phba->hbalock);
  3380. if (lpfc_readl(phba->HCregaddr, &status)) {
  3381. spin_unlock_irq(&phba->hbalock);
  3382. mempool_free(pmb, phba->mbox_mem_pool);
  3383. return 1;
  3384. }
  3385. status &= ~HC_ERINT_ENA;
  3386. writel(status, phba->HCregaddr);
  3387. readl(phba->HCregaddr); /* flush */
  3388. phba->link_flag |= LS_IGNORE_ERATT;
  3389. spin_unlock_irq(&phba->hbalock);
  3390. lpfc_kill_board(phba, pmb);
  3391. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3392. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3393. if (retval != MBX_SUCCESS) {
  3394. if (retval != MBX_BUSY)
  3395. mempool_free(pmb, phba->mbox_mem_pool);
  3396. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3397. "2752 KILL_BOARD command failed retval %d\n",
  3398. retval);
  3399. spin_lock_irq(&phba->hbalock);
  3400. phba->link_flag &= ~LS_IGNORE_ERATT;
  3401. spin_unlock_irq(&phba->hbalock);
  3402. return 1;
  3403. }
  3404. spin_lock_irq(&phba->hbalock);
  3405. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3406. spin_unlock_irq(&phba->hbalock);
  3407. mempool_free(pmb, phba->mbox_mem_pool);
  3408. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3409. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3410. * 3 seconds we still set HBA_ERROR state because the status of the
  3411. * board is now undefined.
  3412. */
  3413. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3414. return 1;
  3415. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3416. mdelay(100);
  3417. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3418. return 1;
  3419. }
  3420. del_timer_sync(&psli->mbox_tmo);
  3421. if (ha_copy & HA_ERATT) {
  3422. writel(HA_ERATT, phba->HAregaddr);
  3423. phba->pport->stopped = 1;
  3424. }
  3425. spin_lock_irq(&phba->hbalock);
  3426. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3427. psli->mbox_active = NULL;
  3428. phba->link_flag &= ~LS_IGNORE_ERATT;
  3429. spin_unlock_irq(&phba->hbalock);
  3430. lpfc_hba_down_post(phba);
  3431. phba->link_state = LPFC_HBA_ERROR;
  3432. return ha_copy & HA_ERATT ? 0 : 1;
  3433. }
  3434. /**
  3435. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3436. * @phba: Pointer to HBA context object.
  3437. *
  3438. * This function resets the HBA by writing HC_INITFF to the control
  3439. * register. After the HBA resets, this function resets all the iocb ring
  3440. * indices. This function disables PCI layer parity checking during
  3441. * the reset.
  3442. * This function returns 0 always.
  3443. * The caller is not required to hold any locks.
  3444. **/
  3445. int
  3446. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3447. {
  3448. struct lpfc_sli *psli;
  3449. struct lpfc_sli_ring *pring;
  3450. uint16_t cfg_value;
  3451. int i;
  3452. psli = &phba->sli;
  3453. /* Reset HBA */
  3454. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3455. "0325 Reset HBA Data: x%x x%x\n",
  3456. phba->pport->port_state, psli->sli_flag);
  3457. /* perform board reset */
  3458. phba->fc_eventTag = 0;
  3459. phba->link_events = 0;
  3460. phba->pport->fc_myDID = 0;
  3461. phba->pport->fc_prevDID = 0;
  3462. /* Turn off parity checking and serr during the physical reset */
  3463. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3464. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3465. (cfg_value &
  3466. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3467. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3468. /* Now toggle INITFF bit in the Host Control Register */
  3469. writel(HC_INITFF, phba->HCregaddr);
  3470. mdelay(1);
  3471. readl(phba->HCregaddr); /* flush */
  3472. writel(0, phba->HCregaddr);
  3473. readl(phba->HCregaddr); /* flush */
  3474. /* Restore PCI cmd register */
  3475. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3476. /* Initialize relevant SLI info */
  3477. for (i = 0; i < psli->num_rings; i++) {
  3478. pring = &psli->ring[i];
  3479. pring->flag = 0;
  3480. pring->rspidx = 0;
  3481. pring->next_cmdidx = 0;
  3482. pring->local_getidx = 0;
  3483. pring->cmdidx = 0;
  3484. pring->missbufcnt = 0;
  3485. }
  3486. phba->link_state = LPFC_WARM_START;
  3487. return 0;
  3488. }
  3489. /**
  3490. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3491. * @phba: Pointer to HBA context object.
  3492. *
  3493. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3494. * checking during resets the device. The caller is not required to hold
  3495. * any locks.
  3496. *
  3497. * This function returns 0 always.
  3498. **/
  3499. int
  3500. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3501. {
  3502. struct lpfc_sli *psli = &phba->sli;
  3503. uint16_t cfg_value;
  3504. uint8_t qindx;
  3505. /* Reset HBA */
  3506. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3507. "0295 Reset HBA Data: x%x x%x\n",
  3508. phba->pport->port_state, psli->sli_flag);
  3509. /* perform board reset */
  3510. phba->fc_eventTag = 0;
  3511. phba->link_events = 0;
  3512. phba->pport->fc_myDID = 0;
  3513. phba->pport->fc_prevDID = 0;
  3514. spin_lock_irq(&phba->hbalock);
  3515. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3516. phba->fcf.fcf_flag = 0;
  3517. /* Clean up the child queue list for the CQs */
  3518. list_del_init(&phba->sli4_hba.mbx_wq->list);
  3519. list_del_init(&phba->sli4_hba.els_wq->list);
  3520. list_del_init(&phba->sli4_hba.hdr_rq->list);
  3521. list_del_init(&phba->sli4_hba.dat_rq->list);
  3522. list_del_init(&phba->sli4_hba.mbx_cq->list);
  3523. list_del_init(&phba->sli4_hba.els_cq->list);
  3524. for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
  3525. list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
  3526. qindx = 0;
  3527. do
  3528. list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
  3529. while (++qindx < phba->cfg_fcp_eq_count);
  3530. spin_unlock_irq(&phba->hbalock);
  3531. /* Now physically reset the device */
  3532. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3533. "0389 Performing PCI function reset!\n");
  3534. /* Turn off parity checking and serr during the physical reset */
  3535. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3536. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3537. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3538. /* Perform FCoE PCI function reset */
  3539. lpfc_pci_function_reset(phba);
  3540. /* Restore PCI cmd register */
  3541. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3542. return 0;
  3543. }
  3544. /**
  3545. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3546. * @phba: Pointer to HBA context object.
  3547. *
  3548. * This function is called in the SLI initialization code path to
  3549. * restart the HBA. The caller is not required to hold any lock.
  3550. * This function writes MBX_RESTART mailbox command to the SLIM and
  3551. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3552. * function to free any pending commands. The function enables
  3553. * POST only during the first initialization. The function returns zero.
  3554. * The function does not guarantee completion of MBX_RESTART mailbox
  3555. * command before the return of this function.
  3556. **/
  3557. static int
  3558. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3559. {
  3560. MAILBOX_t *mb;
  3561. struct lpfc_sli *psli;
  3562. volatile uint32_t word0;
  3563. void __iomem *to_slim;
  3564. uint32_t hba_aer_enabled;
  3565. spin_lock_irq(&phba->hbalock);
  3566. /* Take PCIe device Advanced Error Reporting (AER) state */
  3567. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3568. psli = &phba->sli;
  3569. /* Restart HBA */
  3570. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3571. "0337 Restart HBA Data: x%x x%x\n",
  3572. phba->pport->port_state, psli->sli_flag);
  3573. word0 = 0;
  3574. mb = (MAILBOX_t *) &word0;
  3575. mb->mbxCommand = MBX_RESTART;
  3576. mb->mbxHc = 1;
  3577. lpfc_reset_barrier(phba);
  3578. to_slim = phba->MBslimaddr;
  3579. writel(*(uint32_t *) mb, to_slim);
  3580. readl(to_slim); /* flush */
  3581. /* Only skip post after fc_ffinit is completed */
  3582. if (phba->pport->port_state)
  3583. word0 = 1; /* This is really setting up word1 */
  3584. else
  3585. word0 = 0; /* This is really setting up word1 */
  3586. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3587. writel(*(uint32_t *) mb, to_slim);
  3588. readl(to_slim); /* flush */
  3589. lpfc_sli_brdreset(phba);
  3590. phba->pport->stopped = 0;
  3591. phba->link_state = LPFC_INIT_START;
  3592. phba->hba_flag = 0;
  3593. spin_unlock_irq(&phba->hbalock);
  3594. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3595. psli->stats_start = get_seconds();
  3596. /* Give the INITFF and Post time to settle. */
  3597. mdelay(100);
  3598. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3599. if (hba_aer_enabled)
  3600. pci_disable_pcie_error_reporting(phba->pcidev);
  3601. lpfc_hba_down_post(phba);
  3602. return 0;
  3603. }
  3604. /**
  3605. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3606. * @phba: Pointer to HBA context object.
  3607. *
  3608. * This function is called in the SLI initialization code path to restart
  3609. * a SLI4 HBA. The caller is not required to hold any lock.
  3610. * At the end of the function, it calls lpfc_hba_down_post function to
  3611. * free any pending commands.
  3612. **/
  3613. static int
  3614. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3615. {
  3616. struct lpfc_sli *psli = &phba->sli;
  3617. uint32_t hba_aer_enabled;
  3618. /* Restart HBA */
  3619. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3620. "0296 Restart HBA Data: x%x x%x\n",
  3621. phba->pport->port_state, psli->sli_flag);
  3622. /* Take PCIe device Advanced Error Reporting (AER) state */
  3623. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3624. lpfc_sli4_brdreset(phba);
  3625. spin_lock_irq(&phba->hbalock);
  3626. phba->pport->stopped = 0;
  3627. phba->link_state = LPFC_INIT_START;
  3628. phba->hba_flag = 0;
  3629. spin_unlock_irq(&phba->hbalock);
  3630. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3631. psli->stats_start = get_seconds();
  3632. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3633. if (hba_aer_enabled)
  3634. pci_disable_pcie_error_reporting(phba->pcidev);
  3635. lpfc_hba_down_post(phba);
  3636. return 0;
  3637. }
  3638. /**
  3639. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3640. * @phba: Pointer to HBA context object.
  3641. *
  3642. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3643. * API jump table function pointer from the lpfc_hba struct.
  3644. **/
  3645. int
  3646. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3647. {
  3648. return phba->lpfc_sli_brdrestart(phba);
  3649. }
  3650. /**
  3651. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3652. * @phba: Pointer to HBA context object.
  3653. *
  3654. * This function is called after a HBA restart to wait for successful
  3655. * restart of the HBA. Successful restart of the HBA is indicated by
  3656. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3657. * iteration, the function will restart the HBA again. The function returns
  3658. * zero if HBA successfully restarted else returns negative error code.
  3659. **/
  3660. static int
  3661. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3662. {
  3663. uint32_t status, i = 0;
  3664. /* Read the HBA Host Status Register */
  3665. if (lpfc_readl(phba->HSregaddr, &status))
  3666. return -EIO;
  3667. /* Check status register to see what current state is */
  3668. i = 0;
  3669. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3670. /* Check every 10ms for 10 retries, then every 100ms for 90
  3671. * retries, then every 1 sec for 50 retires for a total of
  3672. * ~60 seconds before reset the board again and check every
  3673. * 1 sec for 50 retries. The up to 60 seconds before the
  3674. * board ready is required by the Falcon FIPS zeroization
  3675. * complete, and any reset the board in between shall cause
  3676. * restart of zeroization, further delay the board ready.
  3677. */
  3678. if (i++ >= 200) {
  3679. /* Adapter failed to init, timeout, status reg
  3680. <status> */
  3681. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3682. "0436 Adapter failed to init, "
  3683. "timeout, status reg x%x, "
  3684. "FW Data: A8 x%x AC x%x\n", status,
  3685. readl(phba->MBslimaddr + 0xa8),
  3686. readl(phba->MBslimaddr + 0xac));
  3687. phba->link_state = LPFC_HBA_ERROR;
  3688. return -ETIMEDOUT;
  3689. }
  3690. /* Check to see if any errors occurred during init */
  3691. if (status & HS_FFERM) {
  3692. /* ERROR: During chipset initialization */
  3693. /* Adapter failed to init, chipset, status reg
  3694. <status> */
  3695. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3696. "0437 Adapter failed to init, "
  3697. "chipset, status reg x%x, "
  3698. "FW Data: A8 x%x AC x%x\n", status,
  3699. readl(phba->MBslimaddr + 0xa8),
  3700. readl(phba->MBslimaddr + 0xac));
  3701. phba->link_state = LPFC_HBA_ERROR;
  3702. return -EIO;
  3703. }
  3704. if (i <= 10)
  3705. msleep(10);
  3706. else if (i <= 100)
  3707. msleep(100);
  3708. else
  3709. msleep(1000);
  3710. if (i == 150) {
  3711. /* Do post */
  3712. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3713. lpfc_sli_brdrestart(phba);
  3714. }
  3715. /* Read the HBA Host Status Register */
  3716. if (lpfc_readl(phba->HSregaddr, &status))
  3717. return -EIO;
  3718. }
  3719. /* Check to see if any errors occurred during init */
  3720. if (status & HS_FFERM) {
  3721. /* ERROR: During chipset initialization */
  3722. /* Adapter failed to init, chipset, status reg <status> */
  3723. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3724. "0438 Adapter failed to init, chipset, "
  3725. "status reg x%x, "
  3726. "FW Data: A8 x%x AC x%x\n", status,
  3727. readl(phba->MBslimaddr + 0xa8),
  3728. readl(phba->MBslimaddr + 0xac));
  3729. phba->link_state = LPFC_HBA_ERROR;
  3730. return -EIO;
  3731. }
  3732. /* Clear all interrupt enable conditions */
  3733. writel(0, phba->HCregaddr);
  3734. readl(phba->HCregaddr); /* flush */
  3735. /* setup host attn register */
  3736. writel(0xffffffff, phba->HAregaddr);
  3737. readl(phba->HAregaddr); /* flush */
  3738. return 0;
  3739. }
  3740. /**
  3741. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3742. *
  3743. * This function calculates and returns the number of HBQs required to be
  3744. * configured.
  3745. **/
  3746. int
  3747. lpfc_sli_hbq_count(void)
  3748. {
  3749. return ARRAY_SIZE(lpfc_hbq_defs);
  3750. }
  3751. /**
  3752. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3753. *
  3754. * This function adds the number of hbq entries in every HBQ to get
  3755. * the total number of hbq entries required for the HBA and returns
  3756. * the total count.
  3757. **/
  3758. static int
  3759. lpfc_sli_hbq_entry_count(void)
  3760. {
  3761. int hbq_count = lpfc_sli_hbq_count();
  3762. int count = 0;
  3763. int i;
  3764. for (i = 0; i < hbq_count; ++i)
  3765. count += lpfc_hbq_defs[i]->entry_count;
  3766. return count;
  3767. }
  3768. /**
  3769. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3770. *
  3771. * This function calculates amount of memory required for all hbq entries
  3772. * to be configured and returns the total memory required.
  3773. **/
  3774. int
  3775. lpfc_sli_hbq_size(void)
  3776. {
  3777. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3778. }
  3779. /**
  3780. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3781. * @phba: Pointer to HBA context object.
  3782. *
  3783. * This function is called during the SLI initialization to configure
  3784. * all the HBQs and post buffers to the HBQ. The caller is not
  3785. * required to hold any locks. This function will return zero if successful
  3786. * else it will return negative error code.
  3787. **/
  3788. static int
  3789. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3790. {
  3791. int hbq_count = lpfc_sli_hbq_count();
  3792. LPFC_MBOXQ_t *pmb;
  3793. MAILBOX_t *pmbox;
  3794. uint32_t hbqno;
  3795. uint32_t hbq_entry_index;
  3796. /* Get a Mailbox buffer to setup mailbox
  3797. * commands for HBA initialization
  3798. */
  3799. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3800. if (!pmb)
  3801. return -ENOMEM;
  3802. pmbox = &pmb->u.mb;
  3803. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3804. phba->link_state = LPFC_INIT_MBX_CMDS;
  3805. phba->hbq_in_use = 1;
  3806. hbq_entry_index = 0;
  3807. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3808. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3809. phba->hbqs[hbqno].hbqPutIdx = 0;
  3810. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3811. phba->hbqs[hbqno].entry_count =
  3812. lpfc_hbq_defs[hbqno]->entry_count;
  3813. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3814. hbq_entry_index, pmb);
  3815. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3816. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3817. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3818. mbxStatus <status>, ring <num> */
  3819. lpfc_printf_log(phba, KERN_ERR,
  3820. LOG_SLI | LOG_VPORT,
  3821. "1805 Adapter failed to init. "
  3822. "Data: x%x x%x x%x\n",
  3823. pmbox->mbxCommand,
  3824. pmbox->mbxStatus, hbqno);
  3825. phba->link_state = LPFC_HBA_ERROR;
  3826. mempool_free(pmb, phba->mbox_mem_pool);
  3827. return -ENXIO;
  3828. }
  3829. }
  3830. phba->hbq_count = hbq_count;
  3831. mempool_free(pmb, phba->mbox_mem_pool);
  3832. /* Initially populate or replenish the HBQs */
  3833. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3834. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3835. return 0;
  3836. }
  3837. /**
  3838. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3839. * @phba: Pointer to HBA context object.
  3840. *
  3841. * This function is called during the SLI initialization to configure
  3842. * all the HBQs and post buffers to the HBQ. The caller is not
  3843. * required to hold any locks. This function will return zero if successful
  3844. * else it will return negative error code.
  3845. **/
  3846. static int
  3847. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3848. {
  3849. phba->hbq_in_use = 1;
  3850. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3851. phba->hbq_count = 1;
  3852. /* Initially populate or replenish the HBQs */
  3853. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3854. return 0;
  3855. }
  3856. /**
  3857. * lpfc_sli_config_port - Issue config port mailbox command
  3858. * @phba: Pointer to HBA context object.
  3859. * @sli_mode: sli mode - 2/3
  3860. *
  3861. * This function is called by the sli intialization code path
  3862. * to issue config_port mailbox command. This function restarts the
  3863. * HBA firmware and issues a config_port mailbox command to configure
  3864. * the SLI interface in the sli mode specified by sli_mode
  3865. * variable. The caller is not required to hold any locks.
  3866. * The function returns 0 if successful, else returns negative error
  3867. * code.
  3868. **/
  3869. int
  3870. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3871. {
  3872. LPFC_MBOXQ_t *pmb;
  3873. uint32_t resetcount = 0, rc = 0, done = 0;
  3874. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3875. if (!pmb) {
  3876. phba->link_state = LPFC_HBA_ERROR;
  3877. return -ENOMEM;
  3878. }
  3879. phba->sli_rev = sli_mode;
  3880. while (resetcount < 2 && !done) {
  3881. spin_lock_irq(&phba->hbalock);
  3882. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3883. spin_unlock_irq(&phba->hbalock);
  3884. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3885. lpfc_sli_brdrestart(phba);
  3886. rc = lpfc_sli_chipset_init(phba);
  3887. if (rc)
  3888. break;
  3889. spin_lock_irq(&phba->hbalock);
  3890. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3891. spin_unlock_irq(&phba->hbalock);
  3892. resetcount++;
  3893. /* Call pre CONFIG_PORT mailbox command initialization. A
  3894. * value of 0 means the call was successful. Any other
  3895. * nonzero value is a failure, but if ERESTART is returned,
  3896. * the driver may reset the HBA and try again.
  3897. */
  3898. rc = lpfc_config_port_prep(phba);
  3899. if (rc == -ERESTART) {
  3900. phba->link_state = LPFC_LINK_UNKNOWN;
  3901. continue;
  3902. } else if (rc)
  3903. break;
  3904. phba->link_state = LPFC_INIT_MBX_CMDS;
  3905. lpfc_config_port(phba, pmb);
  3906. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3907. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3908. LPFC_SLI3_HBQ_ENABLED |
  3909. LPFC_SLI3_CRP_ENABLED |
  3910. LPFC_SLI3_BG_ENABLED |
  3911. LPFC_SLI3_DSS_ENABLED);
  3912. if (rc != MBX_SUCCESS) {
  3913. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3914. "0442 Adapter failed to init, mbxCmd x%x "
  3915. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3916. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3917. spin_lock_irq(&phba->hbalock);
  3918. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3919. spin_unlock_irq(&phba->hbalock);
  3920. rc = -ENXIO;
  3921. } else {
  3922. /* Allow asynchronous mailbox command to go through */
  3923. spin_lock_irq(&phba->hbalock);
  3924. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3925. spin_unlock_irq(&phba->hbalock);
  3926. done = 1;
  3927. }
  3928. }
  3929. if (!done) {
  3930. rc = -EINVAL;
  3931. goto do_prep_failed;
  3932. }
  3933. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3934. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3935. rc = -ENXIO;
  3936. goto do_prep_failed;
  3937. }
  3938. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3939. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3940. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3941. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3942. phba->max_vpi : phba->max_vports;
  3943. } else
  3944. phba->max_vpi = 0;
  3945. phba->fips_level = 0;
  3946. phba->fips_spec_rev = 0;
  3947. if (pmb->u.mb.un.varCfgPort.gdss) {
  3948. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3949. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3950. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3951. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3952. "2850 Security Crypto Active. FIPS x%d "
  3953. "(Spec Rev: x%d)",
  3954. phba->fips_level, phba->fips_spec_rev);
  3955. }
  3956. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3957. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3958. "2856 Config Port Security Crypto "
  3959. "Error: x%x ",
  3960. pmb->u.mb.un.varCfgPort.sec_err);
  3961. }
  3962. if (pmb->u.mb.un.varCfgPort.gerbm)
  3963. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3964. if (pmb->u.mb.un.varCfgPort.gcrp)
  3965. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3966. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3967. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3968. if (phba->cfg_enable_bg) {
  3969. if (pmb->u.mb.un.varCfgPort.gbg)
  3970. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3971. else
  3972. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3973. "0443 Adapter did not grant "
  3974. "BlockGuard\n");
  3975. }
  3976. } else {
  3977. phba->hbq_get = NULL;
  3978. phba->port_gp = phba->mbox->us.s2.port;
  3979. phba->max_vpi = 0;
  3980. }
  3981. do_prep_failed:
  3982. mempool_free(pmb, phba->mbox_mem_pool);
  3983. return rc;
  3984. }
  3985. /**
  3986. * lpfc_sli_hba_setup - SLI intialization function
  3987. * @phba: Pointer to HBA context object.
  3988. *
  3989. * This function is the main SLI intialization function. This function
  3990. * is called by the HBA intialization code, HBA reset code and HBA
  3991. * error attention handler code. Caller is not required to hold any
  3992. * locks. This function issues config_port mailbox command to configure
  3993. * the SLI, setup iocb rings and HBQ rings. In the end the function
  3994. * calls the config_port_post function to issue init_link mailbox
  3995. * command and to start the discovery. The function will return zero
  3996. * if successful, else it will return negative error code.
  3997. **/
  3998. int
  3999. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4000. {
  4001. uint32_t rc;
  4002. int mode = 3, i;
  4003. int longs;
  4004. switch (lpfc_sli_mode) {
  4005. case 2:
  4006. if (phba->cfg_enable_npiv) {
  4007. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4008. "1824 NPIV enabled: Override lpfc_sli_mode "
  4009. "parameter (%d) to auto (0).\n",
  4010. lpfc_sli_mode);
  4011. break;
  4012. }
  4013. mode = 2;
  4014. break;
  4015. case 0:
  4016. case 3:
  4017. break;
  4018. default:
  4019. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4020. "1819 Unrecognized lpfc_sli_mode "
  4021. "parameter: %d.\n", lpfc_sli_mode);
  4022. break;
  4023. }
  4024. rc = lpfc_sli_config_port(phba, mode);
  4025. if (rc && lpfc_sli_mode == 3)
  4026. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4027. "1820 Unable to select SLI-3. "
  4028. "Not supported by adapter.\n");
  4029. if (rc && mode != 2)
  4030. rc = lpfc_sli_config_port(phba, 2);
  4031. if (rc)
  4032. goto lpfc_sli_hba_setup_error;
  4033. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4034. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4035. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4036. if (!rc) {
  4037. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4038. "2709 This device supports "
  4039. "Advanced Error Reporting (AER)\n");
  4040. spin_lock_irq(&phba->hbalock);
  4041. phba->hba_flag |= HBA_AER_ENABLED;
  4042. spin_unlock_irq(&phba->hbalock);
  4043. } else {
  4044. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4045. "2708 This device does not support "
  4046. "Advanced Error Reporting (AER)\n");
  4047. phba->cfg_aer_support = 0;
  4048. }
  4049. }
  4050. if (phba->sli_rev == 3) {
  4051. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4052. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4053. } else {
  4054. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4055. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4056. phba->sli3_options = 0;
  4057. }
  4058. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4059. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4060. phba->sli_rev, phba->max_vpi);
  4061. rc = lpfc_sli_ring_map(phba);
  4062. if (rc)
  4063. goto lpfc_sli_hba_setup_error;
  4064. /* Initialize VPIs. */
  4065. if (phba->sli_rev == LPFC_SLI_REV3) {
  4066. /*
  4067. * The VPI bitmask and physical ID array are allocated
  4068. * and initialized once only - at driver load. A port
  4069. * reset doesn't need to reinitialize this memory.
  4070. */
  4071. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4072. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4073. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4074. GFP_KERNEL);
  4075. if (!phba->vpi_bmask) {
  4076. rc = -ENOMEM;
  4077. goto lpfc_sli_hba_setup_error;
  4078. }
  4079. phba->vpi_ids = kzalloc(
  4080. (phba->max_vpi+1) * sizeof(uint16_t),
  4081. GFP_KERNEL);
  4082. if (!phba->vpi_ids) {
  4083. kfree(phba->vpi_bmask);
  4084. rc = -ENOMEM;
  4085. goto lpfc_sli_hba_setup_error;
  4086. }
  4087. for (i = 0; i < phba->max_vpi; i++)
  4088. phba->vpi_ids[i] = i;
  4089. }
  4090. }
  4091. /* Init HBQs */
  4092. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4093. rc = lpfc_sli_hbq_setup(phba);
  4094. if (rc)
  4095. goto lpfc_sli_hba_setup_error;
  4096. }
  4097. spin_lock_irq(&phba->hbalock);
  4098. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4099. spin_unlock_irq(&phba->hbalock);
  4100. rc = lpfc_config_port_post(phba);
  4101. if (rc)
  4102. goto lpfc_sli_hba_setup_error;
  4103. return rc;
  4104. lpfc_sli_hba_setup_error:
  4105. phba->link_state = LPFC_HBA_ERROR;
  4106. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4107. "0445 Firmware initialization failed\n");
  4108. return rc;
  4109. }
  4110. /**
  4111. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4112. * @phba: Pointer to HBA context object.
  4113. * @mboxq: mailbox pointer.
  4114. * This function issue a dump mailbox command to read config region
  4115. * 23 and parse the records in the region and populate driver
  4116. * data structure.
  4117. **/
  4118. static int
  4119. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
  4120. LPFC_MBOXQ_t *mboxq)
  4121. {
  4122. struct lpfc_dmabuf *mp;
  4123. struct lpfc_mqe *mqe;
  4124. uint32_t data_length;
  4125. int rc;
  4126. /* Program the default value of vlan_id and fc_map */
  4127. phba->valid_vlan = 0;
  4128. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4129. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4130. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4131. mqe = &mboxq->u.mqe;
  4132. if (lpfc_dump_fcoe_param(phba, mboxq))
  4133. return -ENOMEM;
  4134. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4135. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4136. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4137. "(%d):2571 Mailbox cmd x%x Status x%x "
  4138. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4139. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4140. "CQ: x%x x%x x%x x%x\n",
  4141. mboxq->vport ? mboxq->vport->vpi : 0,
  4142. bf_get(lpfc_mqe_command, mqe),
  4143. bf_get(lpfc_mqe_status, mqe),
  4144. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4145. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4146. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4147. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4148. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4149. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4150. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4151. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4152. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4153. mboxq->mcqe.word0,
  4154. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4155. mboxq->mcqe.trailer);
  4156. if (rc) {
  4157. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4158. kfree(mp);
  4159. return -EIO;
  4160. }
  4161. data_length = mqe->un.mb_words[5];
  4162. if (data_length > DMP_RGN23_SIZE) {
  4163. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4164. kfree(mp);
  4165. return -EIO;
  4166. }
  4167. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4168. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4169. kfree(mp);
  4170. return 0;
  4171. }
  4172. /**
  4173. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4174. * @phba: pointer to lpfc hba data structure.
  4175. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4176. * @vpd: pointer to the memory to hold resulting port vpd data.
  4177. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4178. * On output, the number of data bytes in @vpd.
  4179. *
  4180. * This routine executes a READ_REV SLI4 mailbox command. In
  4181. * addition, this routine gets the port vpd data.
  4182. *
  4183. * Return codes
  4184. * 0 - successful
  4185. * -ENOMEM - could not allocated memory.
  4186. **/
  4187. static int
  4188. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4189. uint8_t *vpd, uint32_t *vpd_size)
  4190. {
  4191. int rc = 0;
  4192. uint32_t dma_size;
  4193. struct lpfc_dmabuf *dmabuf;
  4194. struct lpfc_mqe *mqe;
  4195. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4196. if (!dmabuf)
  4197. return -ENOMEM;
  4198. /*
  4199. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4200. * mailbox command.
  4201. */
  4202. dma_size = *vpd_size;
  4203. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4204. dma_size,
  4205. &dmabuf->phys,
  4206. GFP_KERNEL);
  4207. if (!dmabuf->virt) {
  4208. kfree(dmabuf);
  4209. return -ENOMEM;
  4210. }
  4211. memset(dmabuf->virt, 0, dma_size);
  4212. /*
  4213. * The SLI4 implementation of READ_REV conflicts at word1,
  4214. * bits 31:16 and SLI4 adds vpd functionality not present
  4215. * in SLI3. This code corrects the conflicts.
  4216. */
  4217. lpfc_read_rev(phba, mboxq);
  4218. mqe = &mboxq->u.mqe;
  4219. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4220. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4221. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4222. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4223. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4224. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4225. if (rc) {
  4226. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4227. dmabuf->virt, dmabuf->phys);
  4228. kfree(dmabuf);
  4229. return -EIO;
  4230. }
  4231. /*
  4232. * The available vpd length cannot be bigger than the
  4233. * DMA buffer passed to the port. Catch the less than
  4234. * case and update the caller's size.
  4235. */
  4236. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4237. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4238. memcpy(vpd, dmabuf->virt, *vpd_size);
  4239. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4240. dmabuf->virt, dmabuf->phys);
  4241. kfree(dmabuf);
  4242. return 0;
  4243. }
  4244. /**
  4245. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4246. * @phba: pointer to lpfc hba data structure.
  4247. *
  4248. * This routine is called to explicitly arm the SLI4 device's completion and
  4249. * event queues
  4250. **/
  4251. static void
  4252. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4253. {
  4254. uint8_t fcp_eqidx;
  4255. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4256. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4257. fcp_eqidx = 0;
  4258. do
  4259. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4260. LPFC_QUEUE_REARM);
  4261. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4262. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4263. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
  4264. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4265. LPFC_QUEUE_REARM);
  4266. }
  4267. /**
  4268. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4269. * @phba: Pointer to HBA context object.
  4270. * @type: The resource extent type.
  4271. * @extnt_count: buffer to hold port available extent count.
  4272. * @extnt_size: buffer to hold element count per extent.
  4273. *
  4274. * This function calls the port and retrievs the number of available
  4275. * extents and their size for a particular extent type.
  4276. *
  4277. * Returns: 0 if successful. Nonzero otherwise.
  4278. **/
  4279. int
  4280. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4281. uint16_t *extnt_count, uint16_t *extnt_size)
  4282. {
  4283. int rc = 0;
  4284. uint32_t length;
  4285. uint32_t mbox_tmo;
  4286. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4287. LPFC_MBOXQ_t *mbox;
  4288. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4289. if (!mbox)
  4290. return -ENOMEM;
  4291. /* Find out how many extents are available for this resource type */
  4292. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4293. sizeof(struct lpfc_sli4_cfg_mhdr));
  4294. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4295. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4296. length, LPFC_SLI4_MBX_EMBED);
  4297. /* Send an extents count of 0 - the GET doesn't use it. */
  4298. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4299. LPFC_SLI4_MBX_EMBED);
  4300. if (unlikely(rc)) {
  4301. rc = -EIO;
  4302. goto err_exit;
  4303. }
  4304. if (!phba->sli4_hba.intr_enable)
  4305. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4306. else {
  4307. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  4308. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4309. }
  4310. if (unlikely(rc)) {
  4311. rc = -EIO;
  4312. goto err_exit;
  4313. }
  4314. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4315. if (bf_get(lpfc_mbox_hdr_status,
  4316. &rsrc_info->header.cfg_shdr.response)) {
  4317. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4318. "2930 Failed to get resource extents "
  4319. "Status 0x%x Add'l Status 0x%x\n",
  4320. bf_get(lpfc_mbox_hdr_status,
  4321. &rsrc_info->header.cfg_shdr.response),
  4322. bf_get(lpfc_mbox_hdr_add_status,
  4323. &rsrc_info->header.cfg_shdr.response));
  4324. rc = -EIO;
  4325. goto err_exit;
  4326. }
  4327. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4328. &rsrc_info->u.rsp);
  4329. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4330. &rsrc_info->u.rsp);
  4331. err_exit:
  4332. mempool_free(mbox, phba->mbox_mem_pool);
  4333. return rc;
  4334. }
  4335. /**
  4336. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4337. * @phba: Pointer to HBA context object.
  4338. * @type: The extent type to check.
  4339. *
  4340. * This function reads the current available extents from the port and checks
  4341. * if the extent count or extent size has changed since the last access.
  4342. * Callers use this routine post port reset to understand if there is a
  4343. * extent reprovisioning requirement.
  4344. *
  4345. * Returns:
  4346. * -Error: error indicates problem.
  4347. * 1: Extent count or size has changed.
  4348. * 0: No changes.
  4349. **/
  4350. static int
  4351. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4352. {
  4353. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4354. uint16_t size_diff, rsrc_ext_size;
  4355. int rc = 0;
  4356. struct lpfc_rsrc_blks *rsrc_entry;
  4357. struct list_head *rsrc_blk_list = NULL;
  4358. size_diff = 0;
  4359. curr_ext_cnt = 0;
  4360. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4361. &rsrc_ext_cnt,
  4362. &rsrc_ext_size);
  4363. if (unlikely(rc))
  4364. return -EIO;
  4365. switch (type) {
  4366. case LPFC_RSC_TYPE_FCOE_RPI:
  4367. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4368. break;
  4369. case LPFC_RSC_TYPE_FCOE_VPI:
  4370. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4371. break;
  4372. case LPFC_RSC_TYPE_FCOE_XRI:
  4373. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4374. break;
  4375. case LPFC_RSC_TYPE_FCOE_VFI:
  4376. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4377. break;
  4378. default:
  4379. break;
  4380. }
  4381. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4382. curr_ext_cnt++;
  4383. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4384. size_diff++;
  4385. }
  4386. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4387. rc = 1;
  4388. return rc;
  4389. }
  4390. /**
  4391. * lpfc_sli4_cfg_post_extnts -
  4392. * @phba: Pointer to HBA context object.
  4393. * @extnt_cnt - number of available extents.
  4394. * @type - the extent type (rpi, xri, vfi, vpi).
  4395. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4396. * @mbox - pointer to the caller's allocated mailbox structure.
  4397. *
  4398. * This function executes the extents allocation request. It also
  4399. * takes care of the amount of memory needed to allocate or get the
  4400. * allocated extents. It is the caller's responsibility to evaluate
  4401. * the response.
  4402. *
  4403. * Returns:
  4404. * -Error: Error value describes the condition found.
  4405. * 0: if successful
  4406. **/
  4407. static int
  4408. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
  4409. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4410. {
  4411. int rc = 0;
  4412. uint32_t req_len;
  4413. uint32_t emb_len;
  4414. uint32_t alloc_len, mbox_tmo;
  4415. /* Calculate the total requested length of the dma memory */
  4416. req_len = *extnt_cnt * sizeof(uint16_t);
  4417. /*
  4418. * Calculate the size of an embedded mailbox. The uint32_t
  4419. * accounts for extents-specific word.
  4420. */
  4421. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4422. sizeof(uint32_t);
  4423. /*
  4424. * Presume the allocation and response will fit into an embedded
  4425. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4426. */
  4427. *emb = LPFC_SLI4_MBX_EMBED;
  4428. if (req_len > emb_len) {
  4429. req_len = *extnt_cnt * sizeof(uint16_t) +
  4430. sizeof(union lpfc_sli4_cfg_shdr) +
  4431. sizeof(uint32_t);
  4432. *emb = LPFC_SLI4_MBX_NEMBED;
  4433. }
  4434. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4435. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4436. req_len, *emb);
  4437. if (alloc_len < req_len) {
  4438. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4439. "2982 Allocated DMA memory size (x%x) is "
  4440. "less than the requested DMA memory "
  4441. "size (x%x)\n", alloc_len, req_len);
  4442. return -ENOMEM;
  4443. }
  4444. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
  4445. if (unlikely(rc))
  4446. return -EIO;
  4447. if (!phba->sli4_hba.intr_enable)
  4448. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4449. else {
  4450. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  4451. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4452. }
  4453. if (unlikely(rc))
  4454. rc = -EIO;
  4455. return rc;
  4456. }
  4457. /**
  4458. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4459. * @phba: Pointer to HBA context object.
  4460. * @type: The resource extent type to allocate.
  4461. *
  4462. * This function allocates the number of elements for the specified
  4463. * resource type.
  4464. **/
  4465. static int
  4466. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4467. {
  4468. bool emb = false;
  4469. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4470. uint16_t rsrc_id, rsrc_start, j, k;
  4471. uint16_t *ids;
  4472. int i, rc;
  4473. unsigned long longs;
  4474. unsigned long *bmask;
  4475. struct lpfc_rsrc_blks *rsrc_blks;
  4476. LPFC_MBOXQ_t *mbox;
  4477. uint32_t length;
  4478. struct lpfc_id_range *id_array = NULL;
  4479. void *virtaddr = NULL;
  4480. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4481. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4482. struct list_head *ext_blk_list;
  4483. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4484. &rsrc_cnt,
  4485. &rsrc_size);
  4486. if (unlikely(rc))
  4487. return -EIO;
  4488. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4489. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4490. "3009 No available Resource Extents "
  4491. "for resource type 0x%x: Count: 0x%x, "
  4492. "Size 0x%x\n", type, rsrc_cnt,
  4493. rsrc_size);
  4494. return -ENOMEM;
  4495. }
  4496. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
  4497. "2903 Available Resource Extents "
  4498. "for resource type 0x%x: Count: 0x%x, "
  4499. "Size 0x%x\n", type, rsrc_cnt,
  4500. rsrc_size);
  4501. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4502. if (!mbox)
  4503. return -ENOMEM;
  4504. rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
  4505. if (unlikely(rc)) {
  4506. rc = -EIO;
  4507. goto err_exit;
  4508. }
  4509. /*
  4510. * Figure out where the response is located. Then get local pointers
  4511. * to the response data. The port does not guarantee to respond to
  4512. * all extents counts request so update the local variable with the
  4513. * allocated count from the port.
  4514. */
  4515. if (emb == LPFC_SLI4_MBX_EMBED) {
  4516. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4517. id_array = &rsrc_ext->u.rsp.id[0];
  4518. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4519. } else {
  4520. virtaddr = mbox->sge_array->addr[0];
  4521. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4522. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4523. id_array = &n_rsrc->id;
  4524. }
  4525. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4526. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4527. /*
  4528. * Based on the resource size and count, correct the base and max
  4529. * resource values.
  4530. */
  4531. length = sizeof(struct lpfc_rsrc_blks);
  4532. switch (type) {
  4533. case LPFC_RSC_TYPE_FCOE_RPI:
  4534. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4535. sizeof(unsigned long),
  4536. GFP_KERNEL);
  4537. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4538. rc = -ENOMEM;
  4539. goto err_exit;
  4540. }
  4541. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4542. sizeof(uint16_t),
  4543. GFP_KERNEL);
  4544. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4545. kfree(phba->sli4_hba.rpi_bmask);
  4546. rc = -ENOMEM;
  4547. goto err_exit;
  4548. }
  4549. /*
  4550. * The next_rpi was initialized with the maximum available
  4551. * count but the port may allocate a smaller number. Catch
  4552. * that case and update the next_rpi.
  4553. */
  4554. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4555. /* Initialize local ptrs for common extent processing later. */
  4556. bmask = phba->sli4_hba.rpi_bmask;
  4557. ids = phba->sli4_hba.rpi_ids;
  4558. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4559. break;
  4560. case LPFC_RSC_TYPE_FCOE_VPI:
  4561. phba->vpi_bmask = kzalloc(longs *
  4562. sizeof(unsigned long),
  4563. GFP_KERNEL);
  4564. if (unlikely(!phba->vpi_bmask)) {
  4565. rc = -ENOMEM;
  4566. goto err_exit;
  4567. }
  4568. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4569. sizeof(uint16_t),
  4570. GFP_KERNEL);
  4571. if (unlikely(!phba->vpi_ids)) {
  4572. kfree(phba->vpi_bmask);
  4573. rc = -ENOMEM;
  4574. goto err_exit;
  4575. }
  4576. /* Initialize local ptrs for common extent processing later. */
  4577. bmask = phba->vpi_bmask;
  4578. ids = phba->vpi_ids;
  4579. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4580. break;
  4581. case LPFC_RSC_TYPE_FCOE_XRI:
  4582. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4583. sizeof(unsigned long),
  4584. GFP_KERNEL);
  4585. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4586. rc = -ENOMEM;
  4587. goto err_exit;
  4588. }
  4589. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4590. sizeof(uint16_t),
  4591. GFP_KERNEL);
  4592. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4593. kfree(phba->sli4_hba.xri_bmask);
  4594. rc = -ENOMEM;
  4595. goto err_exit;
  4596. }
  4597. /* Initialize local ptrs for common extent processing later. */
  4598. bmask = phba->sli4_hba.xri_bmask;
  4599. ids = phba->sli4_hba.xri_ids;
  4600. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4601. break;
  4602. case LPFC_RSC_TYPE_FCOE_VFI:
  4603. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4604. sizeof(unsigned long),
  4605. GFP_KERNEL);
  4606. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4607. rc = -ENOMEM;
  4608. goto err_exit;
  4609. }
  4610. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4611. sizeof(uint16_t),
  4612. GFP_KERNEL);
  4613. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4614. kfree(phba->sli4_hba.vfi_bmask);
  4615. rc = -ENOMEM;
  4616. goto err_exit;
  4617. }
  4618. /* Initialize local ptrs for common extent processing later. */
  4619. bmask = phba->sli4_hba.vfi_bmask;
  4620. ids = phba->sli4_hba.vfi_ids;
  4621. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4622. break;
  4623. default:
  4624. /* Unsupported Opcode. Fail call. */
  4625. id_array = NULL;
  4626. bmask = NULL;
  4627. ids = NULL;
  4628. ext_blk_list = NULL;
  4629. goto err_exit;
  4630. }
  4631. /*
  4632. * Complete initializing the extent configuration with the
  4633. * allocated ids assigned to this function. The bitmask serves
  4634. * as an index into the array and manages the available ids. The
  4635. * array just stores the ids communicated to the port via the wqes.
  4636. */
  4637. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4638. if ((i % 2) == 0)
  4639. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4640. &id_array[k]);
  4641. else
  4642. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4643. &id_array[k]);
  4644. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4645. if (unlikely(!rsrc_blks)) {
  4646. rc = -ENOMEM;
  4647. kfree(bmask);
  4648. kfree(ids);
  4649. goto err_exit;
  4650. }
  4651. rsrc_blks->rsrc_start = rsrc_id;
  4652. rsrc_blks->rsrc_size = rsrc_size;
  4653. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4654. rsrc_start = rsrc_id;
  4655. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4656. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4657. lpfc_sli4_get_els_iocb_cnt(phba);
  4658. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4659. ids[j] = rsrc_id;
  4660. rsrc_id++;
  4661. j++;
  4662. }
  4663. /* Entire word processed. Get next word.*/
  4664. if ((i % 2) == 1)
  4665. k++;
  4666. }
  4667. err_exit:
  4668. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4669. return rc;
  4670. }
  4671. /**
  4672. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4673. * @phba: Pointer to HBA context object.
  4674. * @type: the extent's type.
  4675. *
  4676. * This function deallocates all extents of a particular resource type.
  4677. * SLI4 does not allow for deallocating a particular extent range. It
  4678. * is the caller's responsibility to release all kernel memory resources.
  4679. **/
  4680. static int
  4681. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4682. {
  4683. int rc;
  4684. uint32_t length, mbox_tmo = 0;
  4685. LPFC_MBOXQ_t *mbox;
  4686. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4687. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4688. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4689. if (!mbox)
  4690. return -ENOMEM;
  4691. /*
  4692. * This function sends an embedded mailbox because it only sends the
  4693. * the resource type. All extents of this type are released by the
  4694. * port.
  4695. */
  4696. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4697. sizeof(struct lpfc_sli4_cfg_mhdr));
  4698. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4699. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4700. length, LPFC_SLI4_MBX_EMBED);
  4701. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4702. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4703. LPFC_SLI4_MBX_EMBED);
  4704. if (unlikely(rc)) {
  4705. rc = -EIO;
  4706. goto out_free_mbox;
  4707. }
  4708. if (!phba->sli4_hba.intr_enable)
  4709. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4710. else {
  4711. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox_tmo);
  4712. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4713. }
  4714. if (unlikely(rc)) {
  4715. rc = -EIO;
  4716. goto out_free_mbox;
  4717. }
  4718. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4719. if (bf_get(lpfc_mbox_hdr_status,
  4720. &dealloc_rsrc->header.cfg_shdr.response)) {
  4721. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4722. "2919 Failed to release resource extents "
  4723. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4724. "Resource memory not released.\n",
  4725. type,
  4726. bf_get(lpfc_mbox_hdr_status,
  4727. &dealloc_rsrc->header.cfg_shdr.response),
  4728. bf_get(lpfc_mbox_hdr_add_status,
  4729. &dealloc_rsrc->header.cfg_shdr.response));
  4730. rc = -EIO;
  4731. goto out_free_mbox;
  4732. }
  4733. /* Release kernel memory resources for the specific type. */
  4734. switch (type) {
  4735. case LPFC_RSC_TYPE_FCOE_VPI:
  4736. kfree(phba->vpi_bmask);
  4737. kfree(phba->vpi_ids);
  4738. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4739. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4740. &phba->lpfc_vpi_blk_list, list) {
  4741. list_del_init(&rsrc_blk->list);
  4742. kfree(rsrc_blk);
  4743. }
  4744. break;
  4745. case LPFC_RSC_TYPE_FCOE_XRI:
  4746. kfree(phba->sli4_hba.xri_bmask);
  4747. kfree(phba->sli4_hba.xri_ids);
  4748. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4749. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4750. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4751. list_del_init(&rsrc_blk->list);
  4752. kfree(rsrc_blk);
  4753. }
  4754. break;
  4755. case LPFC_RSC_TYPE_FCOE_VFI:
  4756. kfree(phba->sli4_hba.vfi_bmask);
  4757. kfree(phba->sli4_hba.vfi_ids);
  4758. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4759. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4760. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4761. list_del_init(&rsrc_blk->list);
  4762. kfree(rsrc_blk);
  4763. }
  4764. break;
  4765. case LPFC_RSC_TYPE_FCOE_RPI:
  4766. /* RPI bitmask and physical id array are cleaned up earlier. */
  4767. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4768. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4769. list_del_init(&rsrc_blk->list);
  4770. kfree(rsrc_blk);
  4771. }
  4772. break;
  4773. default:
  4774. break;
  4775. }
  4776. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4777. out_free_mbox:
  4778. mempool_free(mbox, phba->mbox_mem_pool);
  4779. return rc;
  4780. }
  4781. /**
  4782. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4783. * @phba: Pointer to HBA context object.
  4784. *
  4785. * This function allocates all SLI4 resource identifiers.
  4786. **/
  4787. int
  4788. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4789. {
  4790. int i, rc, error = 0;
  4791. uint16_t count, base;
  4792. unsigned long longs;
  4793. if (phba->sli4_hba.extents_in_use) {
  4794. /*
  4795. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4796. * resource extent count must be read and allocated before
  4797. * provisioning the resource id arrays.
  4798. */
  4799. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4800. LPFC_IDX_RSRC_RDY) {
  4801. /*
  4802. * Extent-based resources are set - the driver could
  4803. * be in a port reset. Figure out if any corrective
  4804. * actions need to be taken.
  4805. */
  4806. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4807. LPFC_RSC_TYPE_FCOE_VFI);
  4808. if (rc != 0)
  4809. error++;
  4810. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4811. LPFC_RSC_TYPE_FCOE_VPI);
  4812. if (rc != 0)
  4813. error++;
  4814. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4815. LPFC_RSC_TYPE_FCOE_XRI);
  4816. if (rc != 0)
  4817. error++;
  4818. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4819. LPFC_RSC_TYPE_FCOE_RPI);
  4820. if (rc != 0)
  4821. error++;
  4822. /*
  4823. * It's possible that the number of resources
  4824. * provided to this port instance changed between
  4825. * resets. Detect this condition and reallocate
  4826. * resources. Otherwise, there is no action.
  4827. */
  4828. if (error) {
  4829. lpfc_printf_log(phba, KERN_INFO,
  4830. LOG_MBOX | LOG_INIT,
  4831. "2931 Detected extent resource "
  4832. "change. Reallocating all "
  4833. "extents.\n");
  4834. rc = lpfc_sli4_dealloc_extent(phba,
  4835. LPFC_RSC_TYPE_FCOE_VFI);
  4836. rc = lpfc_sli4_dealloc_extent(phba,
  4837. LPFC_RSC_TYPE_FCOE_VPI);
  4838. rc = lpfc_sli4_dealloc_extent(phba,
  4839. LPFC_RSC_TYPE_FCOE_XRI);
  4840. rc = lpfc_sli4_dealloc_extent(phba,
  4841. LPFC_RSC_TYPE_FCOE_RPI);
  4842. } else
  4843. return 0;
  4844. }
  4845. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  4846. if (unlikely(rc))
  4847. goto err_exit;
  4848. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  4849. if (unlikely(rc))
  4850. goto err_exit;
  4851. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  4852. if (unlikely(rc))
  4853. goto err_exit;
  4854. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  4855. if (unlikely(rc))
  4856. goto err_exit;
  4857. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  4858. LPFC_IDX_RSRC_RDY);
  4859. return rc;
  4860. } else {
  4861. /*
  4862. * The port does not support resource extents. The XRI, VPI,
  4863. * VFI, RPI resource ids were determined from READ_CONFIG.
  4864. * Just allocate the bitmasks and provision the resource id
  4865. * arrays. If a port reset is active, the resources don't
  4866. * need any action - just exit.
  4867. */
  4868. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4869. LPFC_IDX_RSRC_RDY)
  4870. return 0;
  4871. /* RPIs. */
  4872. count = phba->sli4_hba.max_cfg_param.max_rpi;
  4873. base = phba->sli4_hba.max_cfg_param.rpi_base;
  4874. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4875. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4876. sizeof(unsigned long),
  4877. GFP_KERNEL);
  4878. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4879. rc = -ENOMEM;
  4880. goto err_exit;
  4881. }
  4882. phba->sli4_hba.rpi_ids = kzalloc(count *
  4883. sizeof(uint16_t),
  4884. GFP_KERNEL);
  4885. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4886. rc = -ENOMEM;
  4887. goto free_rpi_bmask;
  4888. }
  4889. for (i = 0; i < count; i++)
  4890. phba->sli4_hba.rpi_ids[i] = base + i;
  4891. /* VPIs. */
  4892. count = phba->sli4_hba.max_cfg_param.max_vpi;
  4893. base = phba->sli4_hba.max_cfg_param.vpi_base;
  4894. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4895. phba->vpi_bmask = kzalloc(longs *
  4896. sizeof(unsigned long),
  4897. GFP_KERNEL);
  4898. if (unlikely(!phba->vpi_bmask)) {
  4899. rc = -ENOMEM;
  4900. goto free_rpi_ids;
  4901. }
  4902. phba->vpi_ids = kzalloc(count *
  4903. sizeof(uint16_t),
  4904. GFP_KERNEL);
  4905. if (unlikely(!phba->vpi_ids)) {
  4906. rc = -ENOMEM;
  4907. goto free_vpi_bmask;
  4908. }
  4909. for (i = 0; i < count; i++)
  4910. phba->vpi_ids[i] = base + i;
  4911. /* XRIs. */
  4912. count = phba->sli4_hba.max_cfg_param.max_xri;
  4913. base = phba->sli4_hba.max_cfg_param.xri_base;
  4914. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4915. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4916. sizeof(unsigned long),
  4917. GFP_KERNEL);
  4918. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4919. rc = -ENOMEM;
  4920. goto free_vpi_ids;
  4921. }
  4922. phba->sli4_hba.xri_ids = kzalloc(count *
  4923. sizeof(uint16_t),
  4924. GFP_KERNEL);
  4925. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4926. rc = -ENOMEM;
  4927. goto free_xri_bmask;
  4928. }
  4929. for (i = 0; i < count; i++)
  4930. phba->sli4_hba.xri_ids[i] = base + i;
  4931. /* VFIs. */
  4932. count = phba->sli4_hba.max_cfg_param.max_vfi;
  4933. base = phba->sli4_hba.max_cfg_param.vfi_base;
  4934. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4935. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4936. sizeof(unsigned long),
  4937. GFP_KERNEL);
  4938. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4939. rc = -ENOMEM;
  4940. goto free_xri_ids;
  4941. }
  4942. phba->sli4_hba.vfi_ids = kzalloc(count *
  4943. sizeof(uint16_t),
  4944. GFP_KERNEL);
  4945. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4946. rc = -ENOMEM;
  4947. goto free_vfi_bmask;
  4948. }
  4949. for (i = 0; i < count; i++)
  4950. phba->sli4_hba.vfi_ids[i] = base + i;
  4951. /*
  4952. * Mark all resources ready. An HBA reset doesn't need
  4953. * to reset the initialization.
  4954. */
  4955. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  4956. LPFC_IDX_RSRC_RDY);
  4957. return 0;
  4958. }
  4959. free_vfi_bmask:
  4960. kfree(phba->sli4_hba.vfi_bmask);
  4961. free_xri_ids:
  4962. kfree(phba->sli4_hba.xri_ids);
  4963. free_xri_bmask:
  4964. kfree(phba->sli4_hba.xri_bmask);
  4965. free_vpi_ids:
  4966. kfree(phba->vpi_ids);
  4967. free_vpi_bmask:
  4968. kfree(phba->vpi_bmask);
  4969. free_rpi_ids:
  4970. kfree(phba->sli4_hba.rpi_ids);
  4971. free_rpi_bmask:
  4972. kfree(phba->sli4_hba.rpi_bmask);
  4973. err_exit:
  4974. return rc;
  4975. }
  4976. /**
  4977. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  4978. * @phba: Pointer to HBA context object.
  4979. *
  4980. * This function allocates the number of elements for the specified
  4981. * resource type.
  4982. **/
  4983. int
  4984. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  4985. {
  4986. if (phba->sli4_hba.extents_in_use) {
  4987. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  4988. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  4989. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  4990. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  4991. } else {
  4992. kfree(phba->vpi_bmask);
  4993. kfree(phba->vpi_ids);
  4994. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4995. kfree(phba->sli4_hba.xri_bmask);
  4996. kfree(phba->sli4_hba.xri_ids);
  4997. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4998. kfree(phba->sli4_hba.vfi_bmask);
  4999. kfree(phba->sli4_hba.vfi_ids);
  5000. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5001. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5002. }
  5003. return 0;
  5004. }
  5005. /**
  5006. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5007. * @phba: Pointer to HBA context object.
  5008. * @type: The resource extent type.
  5009. * @extnt_count: buffer to hold port extent count response
  5010. * @extnt_size: buffer to hold port extent size response.
  5011. *
  5012. * This function calls the port to read the host allocated extents
  5013. * for a particular type.
  5014. **/
  5015. int
  5016. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5017. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5018. {
  5019. bool emb;
  5020. int rc = 0;
  5021. uint16_t curr_blks = 0;
  5022. uint32_t req_len, emb_len;
  5023. uint32_t alloc_len, mbox_tmo;
  5024. struct list_head *blk_list_head;
  5025. struct lpfc_rsrc_blks *rsrc_blk;
  5026. LPFC_MBOXQ_t *mbox;
  5027. void *virtaddr = NULL;
  5028. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5029. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5030. union lpfc_sli4_cfg_shdr *shdr;
  5031. switch (type) {
  5032. case LPFC_RSC_TYPE_FCOE_VPI:
  5033. blk_list_head = &phba->lpfc_vpi_blk_list;
  5034. break;
  5035. case LPFC_RSC_TYPE_FCOE_XRI:
  5036. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5037. break;
  5038. case LPFC_RSC_TYPE_FCOE_VFI:
  5039. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5040. break;
  5041. case LPFC_RSC_TYPE_FCOE_RPI:
  5042. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5043. break;
  5044. default:
  5045. return -EIO;
  5046. }
  5047. /* Count the number of extents currently allocatd for this type. */
  5048. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5049. if (curr_blks == 0) {
  5050. /*
  5051. * The GET_ALLOCATED mailbox does not return the size,
  5052. * just the count. The size should be just the size
  5053. * stored in the current allocated block and all sizes
  5054. * for an extent type are the same so set the return
  5055. * value now.
  5056. */
  5057. *extnt_size = rsrc_blk->rsrc_size;
  5058. }
  5059. curr_blks++;
  5060. }
  5061. /* Calculate the total requested length of the dma memory. */
  5062. req_len = curr_blks * sizeof(uint16_t);
  5063. /*
  5064. * Calculate the size of an embedded mailbox. The uint32_t
  5065. * accounts for extents-specific word.
  5066. */
  5067. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5068. sizeof(uint32_t);
  5069. /*
  5070. * Presume the allocation and response will fit into an embedded
  5071. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5072. */
  5073. emb = LPFC_SLI4_MBX_EMBED;
  5074. req_len = emb_len;
  5075. if (req_len > emb_len) {
  5076. req_len = curr_blks * sizeof(uint16_t) +
  5077. sizeof(union lpfc_sli4_cfg_shdr) +
  5078. sizeof(uint32_t);
  5079. emb = LPFC_SLI4_MBX_NEMBED;
  5080. }
  5081. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5082. if (!mbox)
  5083. return -ENOMEM;
  5084. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5085. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5086. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5087. req_len, emb);
  5088. if (alloc_len < req_len) {
  5089. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5090. "2983 Allocated DMA memory size (x%x) is "
  5091. "less than the requested DMA memory "
  5092. "size (x%x)\n", alloc_len, req_len);
  5093. rc = -ENOMEM;
  5094. goto err_exit;
  5095. }
  5096. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5097. if (unlikely(rc)) {
  5098. rc = -EIO;
  5099. goto err_exit;
  5100. }
  5101. if (!phba->sli4_hba.intr_enable)
  5102. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5103. else {
  5104. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  5105. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5106. }
  5107. if (unlikely(rc)) {
  5108. rc = -EIO;
  5109. goto err_exit;
  5110. }
  5111. /*
  5112. * Figure out where the response is located. Then get local pointers
  5113. * to the response data. The port does not guarantee to respond to
  5114. * all extents counts request so update the local variable with the
  5115. * allocated count from the port.
  5116. */
  5117. if (emb == LPFC_SLI4_MBX_EMBED) {
  5118. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5119. shdr = &rsrc_ext->header.cfg_shdr;
  5120. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5121. } else {
  5122. virtaddr = mbox->sge_array->addr[0];
  5123. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5124. shdr = &n_rsrc->cfg_shdr;
  5125. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5126. }
  5127. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5128. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5129. "2984 Failed to read allocated resources "
  5130. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5131. type,
  5132. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5133. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5134. rc = -EIO;
  5135. goto err_exit;
  5136. }
  5137. err_exit:
  5138. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5139. return rc;
  5140. }
  5141. /**
  5142. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5143. * @phba: Pointer to HBA context object.
  5144. *
  5145. * This function is the main SLI4 device intialization PCI function. This
  5146. * function is called by the HBA intialization code, HBA reset code and
  5147. * HBA error attention handler code. Caller is not required to hold any
  5148. * locks.
  5149. **/
  5150. int
  5151. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5152. {
  5153. int rc;
  5154. LPFC_MBOXQ_t *mboxq;
  5155. struct lpfc_mqe *mqe;
  5156. uint8_t *vpd;
  5157. uint32_t vpd_size;
  5158. uint32_t ftr_rsp = 0;
  5159. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5160. struct lpfc_vport *vport = phba->pport;
  5161. struct lpfc_dmabuf *mp;
  5162. /* Perform a PCI function reset to start from clean */
  5163. rc = lpfc_pci_function_reset(phba);
  5164. if (unlikely(rc))
  5165. return -ENODEV;
  5166. /* Check the HBA Host Status Register for readyness */
  5167. rc = lpfc_sli4_post_status_check(phba);
  5168. if (unlikely(rc))
  5169. return -ENODEV;
  5170. else {
  5171. spin_lock_irq(&phba->hbalock);
  5172. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5173. spin_unlock_irq(&phba->hbalock);
  5174. }
  5175. /*
  5176. * Allocate a single mailbox container for initializing the
  5177. * port.
  5178. */
  5179. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5180. if (!mboxq)
  5181. return -ENOMEM;
  5182. /*
  5183. * Continue initialization with default values even if driver failed
  5184. * to read FCoE param config regions
  5185. */
  5186. if (lpfc_sli4_read_fcoe_params(phba, mboxq))
  5187. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5188. "2570 Failed to read FCoE parameters\n");
  5189. /* Issue READ_REV to collect vpd and FW information. */
  5190. vpd_size = SLI4_PAGE_SIZE;
  5191. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5192. if (!vpd) {
  5193. rc = -ENOMEM;
  5194. goto out_free_mbox;
  5195. }
  5196. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5197. if (unlikely(rc)) {
  5198. kfree(vpd);
  5199. goto out_free_mbox;
  5200. }
  5201. mqe = &mboxq->u.mqe;
  5202. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5203. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5204. phba->hba_flag |= HBA_FCOE_MODE;
  5205. else
  5206. phba->hba_flag &= ~HBA_FCOE_MODE;
  5207. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5208. LPFC_DCBX_CEE_MODE)
  5209. phba->hba_flag |= HBA_FIP_SUPPORT;
  5210. else
  5211. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5212. if (phba->sli_rev != LPFC_SLI_REV4) {
  5213. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5214. "0376 READ_REV Error. SLI Level %d "
  5215. "FCoE enabled %d\n",
  5216. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5217. rc = -EIO;
  5218. kfree(vpd);
  5219. goto out_free_mbox;
  5220. }
  5221. /*
  5222. * Evaluate the read rev and vpd data. Populate the driver
  5223. * state with the results. If this routine fails, the failure
  5224. * is not fatal as the driver will use generic values.
  5225. */
  5226. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5227. if (unlikely(!rc)) {
  5228. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5229. "0377 Error %d parsing vpd. "
  5230. "Using defaults.\n", rc);
  5231. rc = 0;
  5232. }
  5233. kfree(vpd);
  5234. /* Save information as VPD data */
  5235. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5236. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5237. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5238. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5239. &mqe->un.read_rev);
  5240. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5241. &mqe->un.read_rev);
  5242. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5243. &mqe->un.read_rev);
  5244. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5245. &mqe->un.read_rev);
  5246. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5247. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5248. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5249. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5250. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5251. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5252. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5253. "(%d):0380 READ_REV Status x%x "
  5254. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5255. mboxq->vport ? mboxq->vport->vpi : 0,
  5256. bf_get(lpfc_mqe_status, mqe),
  5257. phba->vpd.rev.opFwName,
  5258. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5259. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5260. /*
  5261. * Discover the port's supported feature set and match it against the
  5262. * hosts requests.
  5263. */
  5264. lpfc_request_features(phba, mboxq);
  5265. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5266. if (unlikely(rc)) {
  5267. rc = -EIO;
  5268. goto out_free_mbox;
  5269. }
  5270. /*
  5271. * The port must support FCP initiator mode as this is the
  5272. * only mode running in the host.
  5273. */
  5274. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5275. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5276. "0378 No support for fcpi mode.\n");
  5277. ftr_rsp++;
  5278. }
  5279. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5280. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5281. else
  5282. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5283. /*
  5284. * If the port cannot support the host's requested features
  5285. * then turn off the global config parameters to disable the
  5286. * feature in the driver. This is not a fatal error.
  5287. */
  5288. if ((phba->cfg_enable_bg) &&
  5289. !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5290. ftr_rsp++;
  5291. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5292. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5293. ftr_rsp++;
  5294. if (ftr_rsp) {
  5295. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5296. "0379 Feature Mismatch Data: x%08x %08x "
  5297. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5298. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5299. phba->cfg_enable_npiv, phba->max_vpi);
  5300. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5301. phba->cfg_enable_bg = 0;
  5302. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5303. phba->cfg_enable_npiv = 0;
  5304. }
  5305. /* These SLI3 features are assumed in SLI4 */
  5306. spin_lock_irq(&phba->hbalock);
  5307. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5308. spin_unlock_irq(&phba->hbalock);
  5309. /*
  5310. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5311. * calls depends on these resources to complete port setup.
  5312. */
  5313. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5314. if (rc) {
  5315. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5316. "2920 Failed to alloc Resource IDs "
  5317. "rc = x%x\n", rc);
  5318. goto out_free_mbox;
  5319. }
  5320. /* Read the port's service parameters. */
  5321. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5322. if (rc) {
  5323. phba->link_state = LPFC_HBA_ERROR;
  5324. rc = -ENOMEM;
  5325. goto out_free_mbox;
  5326. }
  5327. mboxq->vport = vport;
  5328. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5329. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5330. if (rc == MBX_SUCCESS) {
  5331. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5332. rc = 0;
  5333. }
  5334. /*
  5335. * This memory was allocated by the lpfc_read_sparam routine. Release
  5336. * it to the mbuf pool.
  5337. */
  5338. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5339. kfree(mp);
  5340. mboxq->context1 = NULL;
  5341. if (unlikely(rc)) {
  5342. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5343. "0382 READ_SPARAM command failed "
  5344. "status %d, mbxStatus x%x\n",
  5345. rc, bf_get(lpfc_mqe_status, mqe));
  5346. phba->link_state = LPFC_HBA_ERROR;
  5347. rc = -EIO;
  5348. goto out_free_mbox;
  5349. }
  5350. lpfc_update_vport_wwn(vport);
  5351. /* Update the fc_host data structures with new wwn. */
  5352. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5353. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5354. /* Register SGL pool to the device using non-embedded mailbox command */
  5355. if (!phba->sli4_hba.extents_in_use) {
  5356. rc = lpfc_sli4_post_els_sgl_list(phba);
  5357. if (unlikely(rc)) {
  5358. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5359. "0582 Error %d during els sgl post "
  5360. "operation\n", rc);
  5361. rc = -ENODEV;
  5362. goto out_free_mbox;
  5363. }
  5364. } else {
  5365. rc = lpfc_sli4_post_els_sgl_list_ext(phba);
  5366. if (unlikely(rc)) {
  5367. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5368. "2560 Error %d during els sgl post "
  5369. "operation\n", rc);
  5370. rc = -ENODEV;
  5371. goto out_free_mbox;
  5372. }
  5373. }
  5374. /* Register SCSI SGL pool to the device */
  5375. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5376. if (unlikely(rc)) {
  5377. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5378. "0383 Error %d during scsi sgl post "
  5379. "operation\n", rc);
  5380. /* Some Scsi buffers were moved to the abort scsi list */
  5381. /* A pci function reset will repost them */
  5382. rc = -ENODEV;
  5383. goto out_free_mbox;
  5384. }
  5385. /* Post the rpi header region to the device. */
  5386. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5387. if (unlikely(rc)) {
  5388. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5389. "0393 Error %d during rpi post operation\n",
  5390. rc);
  5391. rc = -ENODEV;
  5392. goto out_free_mbox;
  5393. }
  5394. /* Set up all the queues to the device */
  5395. rc = lpfc_sli4_queue_setup(phba);
  5396. if (unlikely(rc)) {
  5397. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5398. "0381 Error %d during queue setup.\n ", rc);
  5399. goto out_stop_timers;
  5400. }
  5401. /* Arm the CQs and then EQs on device */
  5402. lpfc_sli4_arm_cqeq_intr(phba);
  5403. /* Indicate device interrupt mode */
  5404. phba->sli4_hba.intr_enable = 1;
  5405. /* Allow asynchronous mailbox command to go through */
  5406. spin_lock_irq(&phba->hbalock);
  5407. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5408. spin_unlock_irq(&phba->hbalock);
  5409. /* Post receive buffers to the device */
  5410. lpfc_sli4_rb_setup(phba);
  5411. /* Reset HBA FCF states after HBA reset */
  5412. phba->fcf.fcf_flag = 0;
  5413. phba->fcf.current_rec.flag = 0;
  5414. /* Start the ELS watchdog timer */
  5415. mod_timer(&vport->els_tmofunc,
  5416. jiffies + HZ * (phba->fc_ratov * 2));
  5417. /* Start heart beat timer */
  5418. mod_timer(&phba->hb_tmofunc,
  5419. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5420. phba->hb_outstanding = 0;
  5421. phba->last_completion_time = jiffies;
  5422. /* Start error attention (ERATT) polling timer */
  5423. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5424. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5425. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5426. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5427. if (!rc) {
  5428. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5429. "2829 This device supports "
  5430. "Advanced Error Reporting (AER)\n");
  5431. spin_lock_irq(&phba->hbalock);
  5432. phba->hba_flag |= HBA_AER_ENABLED;
  5433. spin_unlock_irq(&phba->hbalock);
  5434. } else {
  5435. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5436. "2830 This device does not support "
  5437. "Advanced Error Reporting (AER)\n");
  5438. phba->cfg_aer_support = 0;
  5439. }
  5440. rc = 0;
  5441. }
  5442. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5443. /*
  5444. * The FC Port needs to register FCFI (index 0)
  5445. */
  5446. lpfc_reg_fcfi(phba, mboxq);
  5447. mboxq->vport = phba->pport;
  5448. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5449. if (rc != MBX_SUCCESS)
  5450. goto out_unset_queue;
  5451. rc = 0;
  5452. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5453. &mboxq->u.mqe.un.reg_fcfi);
  5454. }
  5455. /*
  5456. * The port is ready, set the host's link state to LINK_DOWN
  5457. * in preparation for link interrupts.
  5458. */
  5459. spin_lock_irq(&phba->hbalock);
  5460. phba->link_state = LPFC_LINK_DOWN;
  5461. spin_unlock_irq(&phba->hbalock);
  5462. if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
  5463. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5464. out_unset_queue:
  5465. /* Unset all the queues set up in this routine when error out */
  5466. if (rc)
  5467. lpfc_sli4_queue_unset(phba);
  5468. out_stop_timers:
  5469. if (rc)
  5470. lpfc_stop_hba_timers(phba);
  5471. out_free_mbox:
  5472. mempool_free(mboxq, phba->mbox_mem_pool);
  5473. return rc;
  5474. }
  5475. /**
  5476. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5477. * @ptr: context object - pointer to hba structure.
  5478. *
  5479. * This is the callback function for mailbox timer. The mailbox
  5480. * timer is armed when a new mailbox command is issued and the timer
  5481. * is deleted when the mailbox complete. The function is called by
  5482. * the kernel timer code when a mailbox does not complete within
  5483. * expected time. This function wakes up the worker thread to
  5484. * process the mailbox timeout and returns. All the processing is
  5485. * done by the worker thread function lpfc_mbox_timeout_handler.
  5486. **/
  5487. void
  5488. lpfc_mbox_timeout(unsigned long ptr)
  5489. {
  5490. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5491. unsigned long iflag;
  5492. uint32_t tmo_posted;
  5493. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5494. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5495. if (!tmo_posted)
  5496. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5497. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5498. if (!tmo_posted)
  5499. lpfc_worker_wake_up(phba);
  5500. return;
  5501. }
  5502. /**
  5503. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5504. * @phba: Pointer to HBA context object.
  5505. *
  5506. * This function is called from worker thread when a mailbox command times out.
  5507. * The caller is not required to hold any locks. This function will reset the
  5508. * HBA and recover all the pending commands.
  5509. **/
  5510. void
  5511. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5512. {
  5513. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5514. MAILBOX_t *mb = &pmbox->u.mb;
  5515. struct lpfc_sli *psli = &phba->sli;
  5516. struct lpfc_sli_ring *pring;
  5517. /* Check the pmbox pointer first. There is a race condition
  5518. * between the mbox timeout handler getting executed in the
  5519. * worklist and the mailbox actually completing. When this
  5520. * race condition occurs, the mbox_active will be NULL.
  5521. */
  5522. spin_lock_irq(&phba->hbalock);
  5523. if (pmbox == NULL) {
  5524. lpfc_printf_log(phba, KERN_WARNING,
  5525. LOG_MBOX | LOG_SLI,
  5526. "0353 Active Mailbox cleared - mailbox timeout "
  5527. "exiting\n");
  5528. spin_unlock_irq(&phba->hbalock);
  5529. return;
  5530. }
  5531. /* Mbox cmd <mbxCommand> timeout */
  5532. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5533. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5534. mb->mbxCommand,
  5535. phba->pport->port_state,
  5536. phba->sli.sli_flag,
  5537. phba->sli.mbox_active);
  5538. spin_unlock_irq(&phba->hbalock);
  5539. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5540. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5541. * it to fail all outstanding SCSI IO.
  5542. */
  5543. spin_lock_irq(&phba->pport->work_port_lock);
  5544. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5545. spin_unlock_irq(&phba->pport->work_port_lock);
  5546. spin_lock_irq(&phba->hbalock);
  5547. phba->link_state = LPFC_LINK_UNKNOWN;
  5548. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5549. spin_unlock_irq(&phba->hbalock);
  5550. pring = &psli->ring[psli->fcp_ring];
  5551. lpfc_sli_abort_iocb_ring(phba, pring);
  5552. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5553. "0345 Resetting board due to mailbox timeout\n");
  5554. /* Reset the HBA device */
  5555. lpfc_reset_hba(phba);
  5556. }
  5557. /**
  5558. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5559. * @phba: Pointer to HBA context object.
  5560. * @pmbox: Pointer to mailbox object.
  5561. * @flag: Flag indicating how the mailbox need to be processed.
  5562. *
  5563. * This function is called by discovery code and HBA management code
  5564. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5565. * function gets the hbalock to protect the data structures.
  5566. * The mailbox command can be submitted in polling mode, in which case
  5567. * this function will wait in a polling loop for the completion of the
  5568. * mailbox.
  5569. * If the mailbox is submitted in no_wait mode (not polling) the
  5570. * function will submit the command and returns immediately without waiting
  5571. * for the mailbox completion. The no_wait is supported only when HBA
  5572. * is in SLI2/SLI3 mode - interrupts are enabled.
  5573. * The SLI interface allows only one mailbox pending at a time. If the
  5574. * mailbox is issued in polling mode and there is already a mailbox
  5575. * pending, then the function will return an error. If the mailbox is issued
  5576. * in NO_WAIT mode and there is a mailbox pending already, the function
  5577. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5578. * The sli layer owns the mailbox object until the completion of mailbox
  5579. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5580. * return codes the caller owns the mailbox command after the return of
  5581. * the function.
  5582. **/
  5583. static int
  5584. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5585. uint32_t flag)
  5586. {
  5587. MAILBOX_t *mb;
  5588. struct lpfc_sli *psli = &phba->sli;
  5589. uint32_t status, evtctr;
  5590. uint32_t ha_copy, hc_copy;
  5591. int i;
  5592. unsigned long timeout;
  5593. unsigned long drvr_flag = 0;
  5594. uint32_t word0, ldata;
  5595. void __iomem *to_slim;
  5596. int processing_queue = 0;
  5597. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5598. if (!pmbox) {
  5599. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5600. /* processing mbox queue from intr_handler */
  5601. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5602. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5603. return MBX_SUCCESS;
  5604. }
  5605. processing_queue = 1;
  5606. pmbox = lpfc_mbox_get(phba);
  5607. if (!pmbox) {
  5608. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5609. return MBX_SUCCESS;
  5610. }
  5611. }
  5612. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5613. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5614. if(!pmbox->vport) {
  5615. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5616. lpfc_printf_log(phba, KERN_ERR,
  5617. LOG_MBOX | LOG_VPORT,
  5618. "1806 Mbox x%x failed. No vport\n",
  5619. pmbox->u.mb.mbxCommand);
  5620. dump_stack();
  5621. goto out_not_finished;
  5622. }
  5623. }
  5624. /* If the PCI channel is in offline state, do not post mbox. */
  5625. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5626. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5627. goto out_not_finished;
  5628. }
  5629. /* If HBA has a deferred error attention, fail the iocb. */
  5630. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5631. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5632. goto out_not_finished;
  5633. }
  5634. psli = &phba->sli;
  5635. mb = &pmbox->u.mb;
  5636. status = MBX_SUCCESS;
  5637. if (phba->link_state == LPFC_HBA_ERROR) {
  5638. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5639. /* Mbox command <mbxCommand> cannot issue */
  5640. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5641. "(%d):0311 Mailbox command x%x cannot "
  5642. "issue Data: x%x x%x\n",
  5643. pmbox->vport ? pmbox->vport->vpi : 0,
  5644. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5645. goto out_not_finished;
  5646. }
  5647. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  5648. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  5649. !(hc_copy & HC_MBINT_ENA)) {
  5650. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5651. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5652. "(%d):2528 Mailbox command x%x cannot "
  5653. "issue Data: x%x x%x\n",
  5654. pmbox->vport ? pmbox->vport->vpi : 0,
  5655. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5656. goto out_not_finished;
  5657. }
  5658. }
  5659. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  5660. /* Polling for a mbox command when another one is already active
  5661. * is not allowed in SLI. Also, the driver must have established
  5662. * SLI2 mode to queue and process multiple mbox commands.
  5663. */
  5664. if (flag & MBX_POLL) {
  5665. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5666. /* Mbox command <mbxCommand> cannot issue */
  5667. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5668. "(%d):2529 Mailbox command x%x "
  5669. "cannot issue Data: x%x x%x\n",
  5670. pmbox->vport ? pmbox->vport->vpi : 0,
  5671. pmbox->u.mb.mbxCommand,
  5672. psli->sli_flag, flag);
  5673. goto out_not_finished;
  5674. }
  5675. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  5676. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5677. /* Mbox command <mbxCommand> cannot issue */
  5678. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5679. "(%d):2530 Mailbox command x%x "
  5680. "cannot issue Data: x%x x%x\n",
  5681. pmbox->vport ? pmbox->vport->vpi : 0,
  5682. pmbox->u.mb.mbxCommand,
  5683. psli->sli_flag, flag);
  5684. goto out_not_finished;
  5685. }
  5686. /* Another mailbox command is still being processed, queue this
  5687. * command to be processed later.
  5688. */
  5689. lpfc_mbox_put(phba, pmbox);
  5690. /* Mbox cmd issue - BUSY */
  5691. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5692. "(%d):0308 Mbox cmd issue - BUSY Data: "
  5693. "x%x x%x x%x x%x\n",
  5694. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  5695. mb->mbxCommand, phba->pport->port_state,
  5696. psli->sli_flag, flag);
  5697. psli->slistat.mbox_busy++;
  5698. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5699. if (pmbox->vport) {
  5700. lpfc_debugfs_disc_trc(pmbox->vport,
  5701. LPFC_DISC_TRC_MBOX_VPORT,
  5702. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  5703. (uint32_t)mb->mbxCommand,
  5704. mb->un.varWords[0], mb->un.varWords[1]);
  5705. }
  5706. else {
  5707. lpfc_debugfs_disc_trc(phba->pport,
  5708. LPFC_DISC_TRC_MBOX,
  5709. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  5710. (uint32_t)mb->mbxCommand,
  5711. mb->un.varWords[0], mb->un.varWords[1]);
  5712. }
  5713. return MBX_BUSY;
  5714. }
  5715. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  5716. /* If we are not polling, we MUST be in SLI2 mode */
  5717. if (flag != MBX_POLL) {
  5718. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  5719. (mb->mbxCommand != MBX_KILL_BOARD)) {
  5720. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5721. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5722. /* Mbox command <mbxCommand> cannot issue */
  5723. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5724. "(%d):2531 Mailbox command x%x "
  5725. "cannot issue Data: x%x x%x\n",
  5726. pmbox->vport ? pmbox->vport->vpi : 0,
  5727. pmbox->u.mb.mbxCommand,
  5728. psli->sli_flag, flag);
  5729. goto out_not_finished;
  5730. }
  5731. /* timeout active mbox command */
  5732. mod_timer(&psli->mbox_tmo, (jiffies +
  5733. (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
  5734. }
  5735. /* Mailbox cmd <cmd> issue */
  5736. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5737. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  5738. "x%x\n",
  5739. pmbox->vport ? pmbox->vport->vpi : 0,
  5740. mb->mbxCommand, phba->pport->port_state,
  5741. psli->sli_flag, flag);
  5742. if (mb->mbxCommand != MBX_HEARTBEAT) {
  5743. if (pmbox->vport) {
  5744. lpfc_debugfs_disc_trc(pmbox->vport,
  5745. LPFC_DISC_TRC_MBOX_VPORT,
  5746. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  5747. (uint32_t)mb->mbxCommand,
  5748. mb->un.varWords[0], mb->un.varWords[1]);
  5749. }
  5750. else {
  5751. lpfc_debugfs_disc_trc(phba->pport,
  5752. LPFC_DISC_TRC_MBOX,
  5753. "MBOX Send: cmd:x%x mb:x%x x%x",
  5754. (uint32_t)mb->mbxCommand,
  5755. mb->un.varWords[0], mb->un.varWords[1]);
  5756. }
  5757. }
  5758. psli->slistat.mbox_cmd++;
  5759. evtctr = psli->slistat.mbox_event;
  5760. /* next set own bit for the adapter and copy over command word */
  5761. mb->mbxOwner = OWN_CHIP;
  5762. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  5763. /* Populate mbox extension offset word. */
  5764. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  5765. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  5766. = (uint8_t *)phba->mbox_ext
  5767. - (uint8_t *)phba->mbox;
  5768. }
  5769. /* Copy the mailbox extension data */
  5770. if (pmbox->in_ext_byte_len && pmbox->context2) {
  5771. lpfc_sli_pcimem_bcopy(pmbox->context2,
  5772. (uint8_t *)phba->mbox_ext,
  5773. pmbox->in_ext_byte_len);
  5774. }
  5775. /* Copy command data to host SLIM area */
  5776. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  5777. } else {
  5778. /* Populate mbox extension offset word. */
  5779. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  5780. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  5781. = MAILBOX_HBA_EXT_OFFSET;
  5782. /* Copy the mailbox extension data */
  5783. if (pmbox->in_ext_byte_len && pmbox->context2) {
  5784. lpfc_memcpy_to_slim(phba->MBslimaddr +
  5785. MAILBOX_HBA_EXT_OFFSET,
  5786. pmbox->context2, pmbox->in_ext_byte_len);
  5787. }
  5788. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  5789. /* copy command data into host mbox for cmpl */
  5790. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  5791. }
  5792. /* First copy mbox command data to HBA SLIM, skip past first
  5793. word */
  5794. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  5795. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  5796. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  5797. /* Next copy over first word, with mbxOwner set */
  5798. ldata = *((uint32_t *)mb);
  5799. to_slim = phba->MBslimaddr;
  5800. writel(ldata, to_slim);
  5801. readl(to_slim); /* flush */
  5802. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  5803. /* switch over to host mailbox */
  5804. psli->sli_flag |= LPFC_SLI_ACTIVE;
  5805. }
  5806. }
  5807. wmb();
  5808. switch (flag) {
  5809. case MBX_NOWAIT:
  5810. /* Set up reference to mailbox command */
  5811. psli->mbox_active = pmbox;
  5812. /* Interrupt board to do it */
  5813. writel(CA_MBATT, phba->CAregaddr);
  5814. readl(phba->CAregaddr); /* flush */
  5815. /* Don't wait for it to finish, just return */
  5816. break;
  5817. case MBX_POLL:
  5818. /* Set up null reference to mailbox command */
  5819. psli->mbox_active = NULL;
  5820. /* Interrupt board to do it */
  5821. writel(CA_MBATT, phba->CAregaddr);
  5822. readl(phba->CAregaddr); /* flush */
  5823. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  5824. /* First read mbox status word */
  5825. word0 = *((uint32_t *)phba->mbox);
  5826. word0 = le32_to_cpu(word0);
  5827. } else {
  5828. /* First read mbox status word */
  5829. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  5830. spin_unlock_irqrestore(&phba->hbalock,
  5831. drvr_flag);
  5832. goto out_not_finished;
  5833. }
  5834. }
  5835. /* Read the HBA Host Attention Register */
  5836. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  5837. spin_unlock_irqrestore(&phba->hbalock,
  5838. drvr_flag);
  5839. goto out_not_finished;
  5840. }
  5841. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  5842. mb->mbxCommand) *
  5843. 1000) + jiffies;
  5844. i = 0;
  5845. /* Wait for command to complete */
  5846. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  5847. (!(ha_copy & HA_MBATT) &&
  5848. (phba->link_state > LPFC_WARM_START))) {
  5849. if (time_after(jiffies, timeout)) {
  5850. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5851. spin_unlock_irqrestore(&phba->hbalock,
  5852. drvr_flag);
  5853. goto out_not_finished;
  5854. }
  5855. /* Check if we took a mbox interrupt while we were
  5856. polling */
  5857. if (((word0 & OWN_CHIP) != OWN_CHIP)
  5858. && (evtctr != psli->slistat.mbox_event))
  5859. break;
  5860. if (i++ > 10) {
  5861. spin_unlock_irqrestore(&phba->hbalock,
  5862. drvr_flag);
  5863. msleep(1);
  5864. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5865. }
  5866. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  5867. /* First copy command data */
  5868. word0 = *((uint32_t *)phba->mbox);
  5869. word0 = le32_to_cpu(word0);
  5870. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  5871. MAILBOX_t *slimmb;
  5872. uint32_t slimword0;
  5873. /* Check real SLIM for any errors */
  5874. slimword0 = readl(phba->MBslimaddr);
  5875. slimmb = (MAILBOX_t *) & slimword0;
  5876. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  5877. && slimmb->mbxStatus) {
  5878. psli->sli_flag &=
  5879. ~LPFC_SLI_ACTIVE;
  5880. word0 = slimword0;
  5881. }
  5882. }
  5883. } else {
  5884. /* First copy command data */
  5885. word0 = readl(phba->MBslimaddr);
  5886. }
  5887. /* Read the HBA Host Attention Register */
  5888. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  5889. spin_unlock_irqrestore(&phba->hbalock,
  5890. drvr_flag);
  5891. goto out_not_finished;
  5892. }
  5893. }
  5894. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  5895. /* copy results back to user */
  5896. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  5897. /* Copy the mailbox extension data */
  5898. if (pmbox->out_ext_byte_len && pmbox->context2) {
  5899. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  5900. pmbox->context2,
  5901. pmbox->out_ext_byte_len);
  5902. }
  5903. } else {
  5904. /* First copy command data */
  5905. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  5906. MAILBOX_CMD_SIZE);
  5907. /* Copy the mailbox extension data */
  5908. if (pmbox->out_ext_byte_len && pmbox->context2) {
  5909. lpfc_memcpy_from_slim(pmbox->context2,
  5910. phba->MBslimaddr +
  5911. MAILBOX_HBA_EXT_OFFSET,
  5912. pmbox->out_ext_byte_len);
  5913. }
  5914. }
  5915. writel(HA_MBATT, phba->HAregaddr);
  5916. readl(phba->HAregaddr); /* flush */
  5917. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5918. status = mb->mbxStatus;
  5919. }
  5920. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5921. return status;
  5922. out_not_finished:
  5923. if (processing_queue) {
  5924. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  5925. lpfc_mbox_cmpl_put(phba, pmbox);
  5926. }
  5927. return MBX_NOT_FINISHED;
  5928. }
  5929. /**
  5930. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  5931. * @phba: Pointer to HBA context object.
  5932. *
  5933. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  5934. * the driver internal pending mailbox queue. It will then try to wait out the
  5935. * possible outstanding mailbox command before return.
  5936. *
  5937. * Returns:
  5938. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  5939. * the outstanding mailbox command timed out.
  5940. **/
  5941. static int
  5942. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  5943. {
  5944. struct lpfc_sli *psli = &phba->sli;
  5945. uint8_t actcmd = MBX_HEARTBEAT;
  5946. int rc = 0;
  5947. unsigned long timeout;
  5948. /* Mark the asynchronous mailbox command posting as blocked */
  5949. spin_lock_irq(&phba->hbalock);
  5950. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  5951. if (phba->sli.mbox_active)
  5952. actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
  5953. spin_unlock_irq(&phba->hbalock);
  5954. /* Determine how long we might wait for the active mailbox
  5955. * command to be gracefully completed by firmware.
  5956. */
  5957. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
  5958. jiffies;
  5959. /* Wait for the outstnading mailbox command to complete */
  5960. while (phba->sli.mbox_active) {
  5961. /* Check active mailbox complete status every 2ms */
  5962. msleep(2);
  5963. if (time_after(jiffies, timeout)) {
  5964. /* Timeout, marked the outstanding cmd not complete */
  5965. rc = 1;
  5966. break;
  5967. }
  5968. }
  5969. /* Can not cleanly block async mailbox command, fails it */
  5970. if (rc) {
  5971. spin_lock_irq(&phba->hbalock);
  5972. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5973. spin_unlock_irq(&phba->hbalock);
  5974. }
  5975. return rc;
  5976. }
  5977. /**
  5978. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  5979. * @phba: Pointer to HBA context object.
  5980. *
  5981. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  5982. * commands from the driver internal pending mailbox queue. It makes sure
  5983. * that there is no outstanding mailbox command before resuming posting
  5984. * asynchronous mailbox commands. If, for any reason, there is outstanding
  5985. * mailbox command, it will try to wait it out before resuming asynchronous
  5986. * mailbox command posting.
  5987. **/
  5988. static void
  5989. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  5990. {
  5991. struct lpfc_sli *psli = &phba->sli;
  5992. spin_lock_irq(&phba->hbalock);
  5993. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5994. /* Asynchronous mailbox posting is not blocked, do nothing */
  5995. spin_unlock_irq(&phba->hbalock);
  5996. return;
  5997. }
  5998. /* Outstanding synchronous mailbox command is guaranteed to be done,
  5999. * successful or timeout, after timing-out the outstanding mailbox
  6000. * command shall always be removed, so just unblock posting async
  6001. * mailbox command and resume
  6002. */
  6003. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6004. spin_unlock_irq(&phba->hbalock);
  6005. /* wake up worker thread to post asynchronlous mailbox command */
  6006. lpfc_worker_wake_up(phba);
  6007. }
  6008. /**
  6009. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6010. * @phba: Pointer to HBA context object.
  6011. * @mboxq: Pointer to mailbox object.
  6012. *
  6013. * The function posts a mailbox to the port. The mailbox is expected
  6014. * to be comletely filled in and ready for the port to operate on it.
  6015. * This routine executes a synchronous completion operation on the
  6016. * mailbox by polling for its completion.
  6017. *
  6018. * The caller must not be holding any locks when calling this routine.
  6019. *
  6020. * Returns:
  6021. * MBX_SUCCESS - mailbox posted successfully
  6022. * Any of the MBX error values.
  6023. **/
  6024. static int
  6025. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6026. {
  6027. int rc = MBX_SUCCESS;
  6028. unsigned long iflag;
  6029. uint32_t db_ready;
  6030. uint32_t mcqe_status;
  6031. uint32_t mbx_cmnd;
  6032. unsigned long timeout;
  6033. struct lpfc_sli *psli = &phba->sli;
  6034. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6035. struct lpfc_bmbx_create *mbox_rgn;
  6036. struct dma_address *dma_address;
  6037. struct lpfc_register bmbx_reg;
  6038. /*
  6039. * Only one mailbox can be active to the bootstrap mailbox region
  6040. * at a time and there is no queueing provided.
  6041. */
  6042. spin_lock_irqsave(&phba->hbalock, iflag);
  6043. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6044. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6045. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6046. "(%d):2532 Mailbox command x%x (x%x) "
  6047. "cannot issue Data: x%x x%x\n",
  6048. mboxq->vport ? mboxq->vport->vpi : 0,
  6049. mboxq->u.mb.mbxCommand,
  6050. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6051. psli->sli_flag, MBX_POLL);
  6052. return MBXERR_ERROR;
  6053. }
  6054. /* The server grabs the token and owns it until release */
  6055. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6056. phba->sli.mbox_active = mboxq;
  6057. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6058. /*
  6059. * Initialize the bootstrap memory region to avoid stale data areas
  6060. * in the mailbox post. Then copy the caller's mailbox contents to
  6061. * the bmbx mailbox region.
  6062. */
  6063. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6064. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6065. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6066. sizeof(struct lpfc_mqe));
  6067. /* Post the high mailbox dma address to the port and wait for ready. */
  6068. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6069. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6070. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
  6071. * 1000) + jiffies;
  6072. do {
  6073. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6074. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6075. if (!db_ready)
  6076. msleep(2);
  6077. if (time_after(jiffies, timeout)) {
  6078. rc = MBXERR_ERROR;
  6079. goto exit;
  6080. }
  6081. } while (!db_ready);
  6082. /* Post the low mailbox dma address to the port. */
  6083. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6084. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
  6085. * 1000) + jiffies;
  6086. do {
  6087. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6088. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6089. if (!db_ready)
  6090. msleep(2);
  6091. if (time_after(jiffies, timeout)) {
  6092. rc = MBXERR_ERROR;
  6093. goto exit;
  6094. }
  6095. } while (!db_ready);
  6096. /*
  6097. * Read the CQ to ensure the mailbox has completed.
  6098. * If so, update the mailbox status so that the upper layers
  6099. * can complete the request normally.
  6100. */
  6101. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6102. sizeof(struct lpfc_mqe));
  6103. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6104. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6105. sizeof(struct lpfc_mcqe));
  6106. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6107. /*
  6108. * When the CQE status indicates a failure and the mailbox status
  6109. * indicates success then copy the CQE status into the mailbox status
  6110. * (and prefix it with x4000).
  6111. */
  6112. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6113. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6114. bf_set(lpfc_mqe_status, mb,
  6115. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6116. rc = MBXERR_ERROR;
  6117. } else
  6118. lpfc_sli4_swap_str(phba, mboxq);
  6119. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6120. "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
  6121. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6122. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6123. mboxq->vport ? mboxq->vport->vpi : 0,
  6124. mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6125. bf_get(lpfc_mqe_status, mb),
  6126. mb->un.mb_words[0], mb->un.mb_words[1],
  6127. mb->un.mb_words[2], mb->un.mb_words[3],
  6128. mb->un.mb_words[4], mb->un.mb_words[5],
  6129. mb->un.mb_words[6], mb->un.mb_words[7],
  6130. mb->un.mb_words[8], mb->un.mb_words[9],
  6131. mb->un.mb_words[10], mb->un.mb_words[11],
  6132. mb->un.mb_words[12], mboxq->mcqe.word0,
  6133. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6134. mboxq->mcqe.trailer);
  6135. exit:
  6136. /* We are holding the token, no needed for lock when release */
  6137. spin_lock_irqsave(&phba->hbalock, iflag);
  6138. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6139. phba->sli.mbox_active = NULL;
  6140. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6141. return rc;
  6142. }
  6143. /**
  6144. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6145. * @phba: Pointer to HBA context object.
  6146. * @pmbox: Pointer to mailbox object.
  6147. * @flag: Flag indicating how the mailbox need to be processed.
  6148. *
  6149. * This function is called by discovery code and HBA management code to submit
  6150. * a mailbox command to firmware with SLI-4 interface spec.
  6151. *
  6152. * Return codes the caller owns the mailbox command after the return of the
  6153. * function.
  6154. **/
  6155. static int
  6156. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6157. uint32_t flag)
  6158. {
  6159. struct lpfc_sli *psli = &phba->sli;
  6160. unsigned long iflags;
  6161. int rc;
  6162. /* dump from issue mailbox command if setup */
  6163. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6164. rc = lpfc_mbox_dev_check(phba);
  6165. if (unlikely(rc)) {
  6166. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6167. "(%d):2544 Mailbox command x%x (x%x) "
  6168. "cannot issue Data: x%x x%x\n",
  6169. mboxq->vport ? mboxq->vport->vpi : 0,
  6170. mboxq->u.mb.mbxCommand,
  6171. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6172. psli->sli_flag, flag);
  6173. goto out_not_finished;
  6174. }
  6175. /* Detect polling mode and jump to a handler */
  6176. if (!phba->sli4_hba.intr_enable) {
  6177. if (flag == MBX_POLL)
  6178. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6179. else
  6180. rc = -EIO;
  6181. if (rc != MBX_SUCCESS)
  6182. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6183. "(%d):2541 Mailbox command x%x "
  6184. "(x%x) cannot issue Data: x%x x%x\n",
  6185. mboxq->vport ? mboxq->vport->vpi : 0,
  6186. mboxq->u.mb.mbxCommand,
  6187. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6188. psli->sli_flag, flag);
  6189. return rc;
  6190. } else if (flag == MBX_POLL) {
  6191. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6192. "(%d):2542 Try to issue mailbox command "
  6193. "x%x (x%x) synchronously ahead of async"
  6194. "mailbox command queue: x%x x%x\n",
  6195. mboxq->vport ? mboxq->vport->vpi : 0,
  6196. mboxq->u.mb.mbxCommand,
  6197. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6198. psli->sli_flag, flag);
  6199. /* Try to block the asynchronous mailbox posting */
  6200. rc = lpfc_sli4_async_mbox_block(phba);
  6201. if (!rc) {
  6202. /* Successfully blocked, now issue sync mbox cmd */
  6203. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6204. if (rc != MBX_SUCCESS)
  6205. lpfc_printf_log(phba, KERN_ERR,
  6206. LOG_MBOX | LOG_SLI,
  6207. "(%d):2597 Mailbox command "
  6208. "x%x (x%x) cannot issue "
  6209. "Data: x%x x%x\n",
  6210. mboxq->vport ?
  6211. mboxq->vport->vpi : 0,
  6212. mboxq->u.mb.mbxCommand,
  6213. lpfc_sli4_mbox_opcode_get(phba,
  6214. mboxq),
  6215. psli->sli_flag, flag);
  6216. /* Unblock the async mailbox posting afterward */
  6217. lpfc_sli4_async_mbox_unblock(phba);
  6218. }
  6219. return rc;
  6220. }
  6221. /* Now, interrupt mode asynchrous mailbox command */
  6222. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6223. if (rc) {
  6224. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6225. "(%d):2543 Mailbox command x%x (x%x) "
  6226. "cannot issue Data: x%x x%x\n",
  6227. mboxq->vport ? mboxq->vport->vpi : 0,
  6228. mboxq->u.mb.mbxCommand,
  6229. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6230. psli->sli_flag, flag);
  6231. goto out_not_finished;
  6232. }
  6233. /* Put the mailbox command to the driver internal FIFO */
  6234. psli->slistat.mbox_busy++;
  6235. spin_lock_irqsave(&phba->hbalock, iflags);
  6236. lpfc_mbox_put(phba, mboxq);
  6237. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6238. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6239. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6240. "x%x (x%x) x%x x%x x%x\n",
  6241. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6242. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6243. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6244. phba->pport->port_state,
  6245. psli->sli_flag, MBX_NOWAIT);
  6246. /* Wake up worker thread to transport mailbox command from head */
  6247. lpfc_worker_wake_up(phba);
  6248. return MBX_BUSY;
  6249. out_not_finished:
  6250. return MBX_NOT_FINISHED;
  6251. }
  6252. /**
  6253. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6254. * @phba: Pointer to HBA context object.
  6255. *
  6256. * This function is called by worker thread to send a mailbox command to
  6257. * SLI4 HBA firmware.
  6258. *
  6259. **/
  6260. int
  6261. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6262. {
  6263. struct lpfc_sli *psli = &phba->sli;
  6264. LPFC_MBOXQ_t *mboxq;
  6265. int rc = MBX_SUCCESS;
  6266. unsigned long iflags;
  6267. struct lpfc_mqe *mqe;
  6268. uint32_t mbx_cmnd;
  6269. /* Check interrupt mode before post async mailbox command */
  6270. if (unlikely(!phba->sli4_hba.intr_enable))
  6271. return MBX_NOT_FINISHED;
  6272. /* Check for mailbox command service token */
  6273. spin_lock_irqsave(&phba->hbalock, iflags);
  6274. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6275. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6276. return MBX_NOT_FINISHED;
  6277. }
  6278. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6279. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6280. return MBX_NOT_FINISHED;
  6281. }
  6282. if (unlikely(phba->sli.mbox_active)) {
  6283. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6284. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6285. "0384 There is pending active mailbox cmd\n");
  6286. return MBX_NOT_FINISHED;
  6287. }
  6288. /* Take the mailbox command service token */
  6289. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6290. /* Get the next mailbox command from head of queue */
  6291. mboxq = lpfc_mbox_get(phba);
  6292. /* If no more mailbox command waiting for post, we're done */
  6293. if (!mboxq) {
  6294. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6295. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6296. return MBX_SUCCESS;
  6297. }
  6298. phba->sli.mbox_active = mboxq;
  6299. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6300. /* Check device readiness for posting mailbox command */
  6301. rc = lpfc_mbox_dev_check(phba);
  6302. if (unlikely(rc))
  6303. /* Driver clean routine will clean up pending mailbox */
  6304. goto out_not_finished;
  6305. /* Prepare the mbox command to be posted */
  6306. mqe = &mboxq->u.mqe;
  6307. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6308. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6309. mod_timer(&psli->mbox_tmo, (jiffies +
  6310. (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
  6311. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6312. "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
  6313. "x%x x%x\n",
  6314. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6315. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6316. phba->pport->port_state, psli->sli_flag);
  6317. if (mbx_cmnd != MBX_HEARTBEAT) {
  6318. if (mboxq->vport) {
  6319. lpfc_debugfs_disc_trc(mboxq->vport,
  6320. LPFC_DISC_TRC_MBOX_VPORT,
  6321. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6322. mbx_cmnd, mqe->un.mb_words[0],
  6323. mqe->un.mb_words[1]);
  6324. } else {
  6325. lpfc_debugfs_disc_trc(phba->pport,
  6326. LPFC_DISC_TRC_MBOX,
  6327. "MBOX Send: cmd:x%x mb:x%x x%x",
  6328. mbx_cmnd, mqe->un.mb_words[0],
  6329. mqe->un.mb_words[1]);
  6330. }
  6331. }
  6332. psli->slistat.mbox_cmd++;
  6333. /* Post the mailbox command to the port */
  6334. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6335. if (rc != MBX_SUCCESS) {
  6336. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6337. "(%d):2533 Mailbox command x%x (x%x) "
  6338. "cannot issue Data: x%x x%x\n",
  6339. mboxq->vport ? mboxq->vport->vpi : 0,
  6340. mboxq->u.mb.mbxCommand,
  6341. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  6342. psli->sli_flag, MBX_NOWAIT);
  6343. goto out_not_finished;
  6344. }
  6345. return rc;
  6346. out_not_finished:
  6347. spin_lock_irqsave(&phba->hbalock, iflags);
  6348. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6349. __lpfc_mbox_cmpl_put(phba, mboxq);
  6350. /* Release the token */
  6351. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6352. phba->sli.mbox_active = NULL;
  6353. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6354. return MBX_NOT_FINISHED;
  6355. }
  6356. /**
  6357. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6358. * @phba: Pointer to HBA context object.
  6359. * @pmbox: Pointer to mailbox object.
  6360. * @flag: Flag indicating how the mailbox need to be processed.
  6361. *
  6362. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6363. * the API jump table function pointer from the lpfc_hba struct.
  6364. *
  6365. * Return codes the caller owns the mailbox command after the return of the
  6366. * function.
  6367. **/
  6368. int
  6369. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6370. {
  6371. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6372. }
  6373. /**
  6374. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6375. * @phba: The hba struct for which this call is being executed.
  6376. * @dev_grp: The HBA PCI-Device group number.
  6377. *
  6378. * This routine sets up the mbox interface API function jump table in @phba
  6379. * struct.
  6380. * Returns: 0 - success, -ENODEV - failure.
  6381. **/
  6382. int
  6383. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6384. {
  6385. switch (dev_grp) {
  6386. case LPFC_PCI_DEV_LP:
  6387. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6388. phba->lpfc_sli_handle_slow_ring_event =
  6389. lpfc_sli_handle_slow_ring_event_s3;
  6390. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6391. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6392. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6393. break;
  6394. case LPFC_PCI_DEV_OC:
  6395. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6396. phba->lpfc_sli_handle_slow_ring_event =
  6397. lpfc_sli_handle_slow_ring_event_s4;
  6398. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6399. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6400. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6401. break;
  6402. default:
  6403. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6404. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6405. dev_grp);
  6406. return -ENODEV;
  6407. break;
  6408. }
  6409. return 0;
  6410. }
  6411. /**
  6412. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6413. * @phba: Pointer to HBA context object.
  6414. * @pring: Pointer to driver SLI ring object.
  6415. * @piocb: Pointer to address of newly added command iocb.
  6416. *
  6417. * This function is called with hbalock held to add a command
  6418. * iocb to the txq when SLI layer cannot submit the command iocb
  6419. * to the ring.
  6420. **/
  6421. void
  6422. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6423. struct lpfc_iocbq *piocb)
  6424. {
  6425. /* Insert the caller's iocb in the txq tail for later processing. */
  6426. list_add_tail(&piocb->list, &pring->txq);
  6427. pring->txq_cnt++;
  6428. }
  6429. /**
  6430. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6431. * @phba: Pointer to HBA context object.
  6432. * @pring: Pointer to driver SLI ring object.
  6433. * @piocb: Pointer to address of newly added command iocb.
  6434. *
  6435. * This function is called with hbalock held before a new
  6436. * iocb is submitted to the firmware. This function checks
  6437. * txq to flush the iocbs in txq to Firmware before
  6438. * submitting new iocbs to the Firmware.
  6439. * If there are iocbs in the txq which need to be submitted
  6440. * to firmware, lpfc_sli_next_iocb returns the first element
  6441. * of the txq after dequeuing it from txq.
  6442. * If there is no iocb in the txq then the function will return
  6443. * *piocb and *piocb is set to NULL. Caller needs to check
  6444. * *piocb to find if there are more commands in the txq.
  6445. **/
  6446. static struct lpfc_iocbq *
  6447. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6448. struct lpfc_iocbq **piocb)
  6449. {
  6450. struct lpfc_iocbq * nextiocb;
  6451. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6452. if (!nextiocb) {
  6453. nextiocb = *piocb;
  6454. *piocb = NULL;
  6455. }
  6456. return nextiocb;
  6457. }
  6458. /**
  6459. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6460. * @phba: Pointer to HBA context object.
  6461. * @ring_number: SLI ring number to issue iocb on.
  6462. * @piocb: Pointer to command iocb.
  6463. * @flag: Flag indicating if this command can be put into txq.
  6464. *
  6465. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6466. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6467. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6468. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6469. * this function allows only iocbs for posting buffers. This function finds
  6470. * next available slot in the command ring and posts the command to the
  6471. * available slot and writes the port attention register to request HBA start
  6472. * processing new iocb. If there is no slot available in the ring and
  6473. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6474. * the function returns IOCB_BUSY.
  6475. *
  6476. * This function is called with hbalock held. The function will return success
  6477. * after it successfully submit the iocb to firmware or after adding to the
  6478. * txq.
  6479. **/
  6480. static int
  6481. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6482. struct lpfc_iocbq *piocb, uint32_t flag)
  6483. {
  6484. struct lpfc_iocbq *nextiocb;
  6485. IOCB_t *iocb;
  6486. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6487. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6488. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6489. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6490. lpfc_printf_log(phba, KERN_ERR,
  6491. LOG_SLI | LOG_VPORT,
  6492. "1807 IOCB x%x failed. No vport\n",
  6493. piocb->iocb.ulpCommand);
  6494. dump_stack();
  6495. return IOCB_ERROR;
  6496. }
  6497. /* If the PCI channel is in offline state, do not post iocbs. */
  6498. if (unlikely(pci_channel_offline(phba->pcidev)))
  6499. return IOCB_ERROR;
  6500. /* If HBA has a deferred error attention, fail the iocb. */
  6501. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6502. return IOCB_ERROR;
  6503. /*
  6504. * We should never get an IOCB if we are in a < LINK_DOWN state
  6505. */
  6506. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6507. return IOCB_ERROR;
  6508. /*
  6509. * Check to see if we are blocking IOCB processing because of a
  6510. * outstanding event.
  6511. */
  6512. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6513. goto iocb_busy;
  6514. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6515. /*
  6516. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6517. * can be issued if the link is not up.
  6518. */
  6519. switch (piocb->iocb.ulpCommand) {
  6520. case CMD_GEN_REQUEST64_CR:
  6521. case CMD_GEN_REQUEST64_CX:
  6522. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6523. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6524. FC_RCTL_DD_UNSOL_CMD) ||
  6525. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6526. MENLO_TRANSPORT_TYPE))
  6527. goto iocb_busy;
  6528. break;
  6529. case CMD_QUE_RING_BUF_CN:
  6530. case CMD_QUE_RING_BUF64_CN:
  6531. /*
  6532. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6533. * completion, iocb_cmpl MUST be 0.
  6534. */
  6535. if (piocb->iocb_cmpl)
  6536. piocb->iocb_cmpl = NULL;
  6537. /*FALLTHROUGH*/
  6538. case CMD_CREATE_XRI_CR:
  6539. case CMD_CLOSE_XRI_CN:
  6540. case CMD_CLOSE_XRI_CX:
  6541. break;
  6542. default:
  6543. goto iocb_busy;
  6544. }
  6545. /*
  6546. * For FCP commands, we must be in a state where we can process link
  6547. * attention events.
  6548. */
  6549. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6550. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6551. goto iocb_busy;
  6552. }
  6553. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6554. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6555. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6556. if (iocb)
  6557. lpfc_sli_update_ring(phba, pring);
  6558. else
  6559. lpfc_sli_update_full_ring(phba, pring);
  6560. if (!piocb)
  6561. return IOCB_SUCCESS;
  6562. goto out_busy;
  6563. iocb_busy:
  6564. pring->stats.iocb_cmd_delay++;
  6565. out_busy:
  6566. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6567. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6568. return IOCB_SUCCESS;
  6569. }
  6570. return IOCB_BUSY;
  6571. }
  6572. /**
  6573. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6574. * @phba: Pointer to HBA context object.
  6575. * @piocb: Pointer to command iocb.
  6576. * @sglq: Pointer to the scatter gather queue object.
  6577. *
  6578. * This routine converts the bpl or bde that is in the IOCB
  6579. * to a sgl list for the sli4 hardware. The physical address
  6580. * of the bpl/bde is converted back to a virtual address.
  6581. * If the IOCB contains a BPL then the list of BDE's is
  6582. * converted to sli4_sge's. If the IOCB contains a single
  6583. * BDE then it is converted to a single sli_sge.
  6584. * The IOCB is still in cpu endianess so the contents of
  6585. * the bpl can be used without byte swapping.
  6586. *
  6587. * Returns valid XRI = Success, NO_XRI = Failure.
  6588. **/
  6589. static uint16_t
  6590. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6591. struct lpfc_sglq *sglq)
  6592. {
  6593. uint16_t xritag = NO_XRI;
  6594. struct ulp_bde64 *bpl = NULL;
  6595. struct ulp_bde64 bde;
  6596. struct sli4_sge *sgl = NULL;
  6597. IOCB_t *icmd;
  6598. int numBdes = 0;
  6599. int i = 0;
  6600. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6601. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6602. if (!piocbq || !sglq)
  6603. return xritag;
  6604. sgl = (struct sli4_sge *)sglq->sgl;
  6605. icmd = &piocbq->iocb;
  6606. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6607. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6608. sizeof(struct ulp_bde64);
  6609. /* The addrHigh and addrLow fields within the IOCB
  6610. * have not been byteswapped yet so there is no
  6611. * need to swap them back.
  6612. */
  6613. bpl = (struct ulp_bde64 *)
  6614. ((struct lpfc_dmabuf *)piocbq->context3)->virt;
  6615. if (!bpl)
  6616. return xritag;
  6617. for (i = 0; i < numBdes; i++) {
  6618. /* Should already be byte swapped. */
  6619. sgl->addr_hi = bpl->addrHigh;
  6620. sgl->addr_lo = bpl->addrLow;
  6621. sgl->word2 = le32_to_cpu(sgl->word2);
  6622. if ((i+1) == numBdes)
  6623. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6624. else
  6625. bf_set(lpfc_sli4_sge_last, sgl, 0);
  6626. /* swap the size field back to the cpu so we
  6627. * can assign it to the sgl.
  6628. */
  6629. bde.tus.w = le32_to_cpu(bpl->tus.w);
  6630. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  6631. /* The offsets in the sgl need to be accumulated
  6632. * separately for the request and reply lists.
  6633. * The request is always first, the reply follows.
  6634. */
  6635. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  6636. /* add up the reply sg entries */
  6637. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  6638. inbound++;
  6639. /* first inbound? reset the offset */
  6640. if (inbound == 1)
  6641. offset = 0;
  6642. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  6643. offset += bde.tus.f.bdeSize;
  6644. }
  6645. sgl->word2 = cpu_to_le32(sgl->word2);
  6646. bpl++;
  6647. sgl++;
  6648. }
  6649. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  6650. /* The addrHigh and addrLow fields of the BDE have not
  6651. * been byteswapped yet so they need to be swapped
  6652. * before putting them in the sgl.
  6653. */
  6654. sgl->addr_hi =
  6655. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  6656. sgl->addr_lo =
  6657. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  6658. sgl->word2 = le32_to_cpu(sgl->word2);
  6659. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6660. sgl->word2 = cpu_to_le32(sgl->word2);
  6661. sgl->sge_len =
  6662. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  6663. }
  6664. return sglq->sli4_xritag;
  6665. }
  6666. /**
  6667. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  6668. * @phba: Pointer to HBA context object.
  6669. *
  6670. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  6671. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  6672. * held.
  6673. *
  6674. * Return: index into SLI4 fast-path FCP queue index.
  6675. **/
  6676. static uint32_t
  6677. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  6678. {
  6679. ++phba->fcp_qidx;
  6680. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  6681. phba->fcp_qidx = 0;
  6682. return phba->fcp_qidx;
  6683. }
  6684. /**
  6685. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  6686. * @phba: Pointer to HBA context object.
  6687. * @piocb: Pointer to command iocb.
  6688. * @wqe: Pointer to the work queue entry.
  6689. *
  6690. * This routine converts the iocb command to its Work Queue Entry
  6691. * equivalent. The wqe pointer should not have any fields set when
  6692. * this routine is called because it will memcpy over them.
  6693. * This routine does not set the CQ_ID or the WQEC bits in the
  6694. * wqe.
  6695. *
  6696. * Returns: 0 = Success, IOCB_ERROR = Failure.
  6697. **/
  6698. static int
  6699. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  6700. union lpfc_wqe *wqe)
  6701. {
  6702. uint32_t xmit_len = 0, total_len = 0;
  6703. uint8_t ct = 0;
  6704. uint32_t fip;
  6705. uint32_t abort_tag;
  6706. uint8_t command_type = ELS_COMMAND_NON_FIP;
  6707. uint8_t cmnd;
  6708. uint16_t xritag;
  6709. uint16_t abrt_iotag;
  6710. struct lpfc_iocbq *abrtiocbq;
  6711. struct ulp_bde64 *bpl = NULL;
  6712. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  6713. int numBdes, i;
  6714. struct ulp_bde64 bde;
  6715. struct lpfc_nodelist *ndlp;
  6716. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  6717. /* The fcp commands will set command type */
  6718. if (iocbq->iocb_flag & LPFC_IO_FCP)
  6719. command_type = FCP_COMMAND;
  6720. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  6721. command_type = ELS_COMMAND_FIP;
  6722. else
  6723. command_type = ELS_COMMAND_NON_FIP;
  6724. /* Some of the fields are in the right position already */
  6725. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  6726. abort_tag = (uint32_t) iocbq->iotag;
  6727. xritag = iocbq->sli4_xritag;
  6728. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  6729. /* words0-2 bpl convert bde */
  6730. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6731. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  6732. sizeof(struct ulp_bde64);
  6733. bpl = (struct ulp_bde64 *)
  6734. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  6735. if (!bpl)
  6736. return IOCB_ERROR;
  6737. /* Should already be byte swapped. */
  6738. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  6739. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  6740. /* swap the size field back to the cpu so we
  6741. * can assign it to the sgl.
  6742. */
  6743. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  6744. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  6745. total_len = 0;
  6746. for (i = 0; i < numBdes; i++) {
  6747. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  6748. total_len += bde.tus.f.bdeSize;
  6749. }
  6750. } else
  6751. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  6752. iocbq->iocb.ulpIoTag = iocbq->iotag;
  6753. cmnd = iocbq->iocb.ulpCommand;
  6754. switch (iocbq->iocb.ulpCommand) {
  6755. case CMD_ELS_REQUEST64_CR:
  6756. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  6757. if (!iocbq->iocb.ulpLe) {
  6758. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6759. "2007 Only Limited Edition cmd Format"
  6760. " supported 0x%x\n",
  6761. iocbq->iocb.ulpCommand);
  6762. return IOCB_ERROR;
  6763. }
  6764. wqe->els_req.payload_len = xmit_len;
  6765. /* Els_reguest64 has a TMO */
  6766. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  6767. iocbq->iocb.ulpTimeout);
  6768. /* Need a VF for word 4 set the vf bit*/
  6769. bf_set(els_req64_vf, &wqe->els_req, 0);
  6770. /* And a VFID for word 12 */
  6771. bf_set(els_req64_vfid, &wqe->els_req, 0);
  6772. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  6773. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  6774. iocbq->iocb.ulpContext);
  6775. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  6776. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  6777. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  6778. if (command_type == ELS_COMMAND_FIP) {
  6779. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  6780. >> LPFC_FIP_ELS_ID_SHIFT);
  6781. }
  6782. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  6783. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  6784. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  6785. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  6786. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  6787. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  6788. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  6789. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  6790. break;
  6791. case CMD_XMIT_SEQUENCE64_CX:
  6792. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  6793. iocbq->iocb.un.ulpWord[3]);
  6794. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  6795. iocbq->iocb.unsli3.rcvsli3.ox_id);
  6796. /* The entire sequence is transmitted for this IOCB */
  6797. xmit_len = total_len;
  6798. cmnd = CMD_XMIT_SEQUENCE64_CR;
  6799. case CMD_XMIT_SEQUENCE64_CR:
  6800. /* word3 iocb=io_tag32 wqe=reserved */
  6801. wqe->xmit_sequence.rsvd3 = 0;
  6802. /* word4 relative_offset memcpy */
  6803. /* word5 r_ctl/df_ctl memcpy */
  6804. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  6805. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  6806. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  6807. LPFC_WQE_IOD_WRITE);
  6808. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  6809. LPFC_WQE_LENLOC_WORD12);
  6810. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  6811. wqe->xmit_sequence.xmit_len = xmit_len;
  6812. command_type = OTHER_COMMAND;
  6813. break;
  6814. case CMD_XMIT_BCAST64_CN:
  6815. /* word3 iocb=iotag32 wqe=seq_payload_len */
  6816. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  6817. /* word4 iocb=rsvd wqe=rsvd */
  6818. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  6819. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  6820. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  6821. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  6822. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  6823. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  6824. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  6825. LPFC_WQE_LENLOC_WORD3);
  6826. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  6827. break;
  6828. case CMD_FCP_IWRITE64_CR:
  6829. command_type = FCP_COMMAND_DATA_OUT;
  6830. /* word3 iocb=iotag wqe=payload_offset_len */
  6831. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  6832. wqe->fcp_iwrite.payload_offset_len =
  6833. xmit_len + sizeof(struct fcp_rsp);
  6834. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  6835. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  6836. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  6837. iocbq->iocb.ulpFCP2Rcvy);
  6838. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  6839. /* Always open the exchange */
  6840. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  6841. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  6842. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  6843. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  6844. LPFC_WQE_LENLOC_WORD4);
  6845. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  6846. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  6847. break;
  6848. case CMD_FCP_IREAD64_CR:
  6849. /* word3 iocb=iotag wqe=payload_offset_len */
  6850. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  6851. wqe->fcp_iread.payload_offset_len =
  6852. xmit_len + sizeof(struct fcp_rsp);
  6853. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  6854. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  6855. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  6856. iocbq->iocb.ulpFCP2Rcvy);
  6857. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  6858. /* Always open the exchange */
  6859. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  6860. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  6861. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  6862. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  6863. LPFC_WQE_LENLOC_WORD4);
  6864. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  6865. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  6866. break;
  6867. case CMD_FCP_ICMND64_CR:
  6868. /* word3 iocb=IO_TAG wqe=reserved */
  6869. wqe->fcp_icmd.rsrvd3 = 0;
  6870. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  6871. /* Always open the exchange */
  6872. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  6873. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  6874. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  6875. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  6876. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  6877. LPFC_WQE_LENLOC_NONE);
  6878. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  6879. break;
  6880. case CMD_GEN_REQUEST64_CR:
  6881. /* For this command calculate the xmit length of the
  6882. * request bde.
  6883. */
  6884. xmit_len = 0;
  6885. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  6886. sizeof(struct ulp_bde64);
  6887. for (i = 0; i < numBdes; i++) {
  6888. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  6889. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  6890. break;
  6891. xmit_len += bde.tus.f.bdeSize;
  6892. }
  6893. /* word3 iocb=IO_TAG wqe=request_payload_len */
  6894. wqe->gen_req.request_payload_len = xmit_len;
  6895. /* word4 iocb=parameter wqe=relative_offset memcpy */
  6896. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  6897. /* word6 context tag copied in memcpy */
  6898. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  6899. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  6900. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6901. "2015 Invalid CT %x command 0x%x\n",
  6902. ct, iocbq->iocb.ulpCommand);
  6903. return IOCB_ERROR;
  6904. }
  6905. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  6906. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  6907. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  6908. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  6909. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  6910. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  6911. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  6912. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  6913. command_type = OTHER_COMMAND;
  6914. break;
  6915. case CMD_XMIT_ELS_RSP64_CX:
  6916. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  6917. /* words0-2 BDE memcpy */
  6918. /* word3 iocb=iotag32 wqe=response_payload_len */
  6919. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  6920. /* word4 iocb=did wge=rsvd. */
  6921. wqe->xmit_els_rsp.rsvd4 = 0;
  6922. /* word5 iocb=rsvd wge=did */
  6923. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  6924. iocbq->iocb.un.elsreq64.remoteID);
  6925. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  6926. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  6927. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  6928. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  6929. iocbq->iocb.unsli3.rcvsli3.ox_id);
  6930. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  6931. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  6932. phba->vpi_ids[iocbq->vport->vpi]);
  6933. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  6934. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  6935. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  6936. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  6937. LPFC_WQE_LENLOC_WORD3);
  6938. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  6939. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  6940. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  6941. command_type = OTHER_COMMAND;
  6942. break;
  6943. case CMD_CLOSE_XRI_CN:
  6944. case CMD_ABORT_XRI_CN:
  6945. case CMD_ABORT_XRI_CX:
  6946. /* words 0-2 memcpy should be 0 rserved */
  6947. /* port will send abts */
  6948. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  6949. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  6950. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  6951. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  6952. } else
  6953. fip = 0;
  6954. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  6955. /*
  6956. * The link is down, or the command was ELS_FIP
  6957. * so the fw does not need to send abts
  6958. * on the wire.
  6959. */
  6960. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  6961. else
  6962. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  6963. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  6964. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  6965. wqe->abort_cmd.rsrvd5 = 0;
  6966. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  6967. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  6968. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  6969. /*
  6970. * The abort handler will send us CMD_ABORT_XRI_CN or
  6971. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  6972. */
  6973. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  6974. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  6975. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  6976. LPFC_WQE_LENLOC_NONE);
  6977. cmnd = CMD_ABORT_XRI_CX;
  6978. command_type = OTHER_COMMAND;
  6979. xritag = 0;
  6980. break;
  6981. case CMD_XMIT_BLS_RSP64_CX:
  6982. /* As BLS ABTS RSP WQE is very different from other WQEs,
  6983. * we re-construct this WQE here based on information in
  6984. * iocbq from scratch.
  6985. */
  6986. memset(wqe, 0, sizeof(union lpfc_wqe));
  6987. /* OX_ID is invariable to who sent ABTS to CT exchange */
  6988. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  6989. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  6990. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  6991. LPFC_ABTS_UNSOL_INT) {
  6992. /* ABTS sent by initiator to CT exchange, the
  6993. * RX_ID field will be filled with the newly
  6994. * allocated responder XRI.
  6995. */
  6996. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  6997. iocbq->sli4_xritag);
  6998. } else {
  6999. /* ABTS sent by responder to CT exchange, the
  7000. * RX_ID field will be filled with the responder
  7001. * RX_ID from ABTS.
  7002. */
  7003. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7004. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7005. }
  7006. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7007. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7008. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7009. iocbq->iocb.ulpContext);
  7010. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7011. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7012. LPFC_WQE_LENLOC_NONE);
  7013. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7014. command_type = OTHER_COMMAND;
  7015. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7016. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7017. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7018. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7019. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7020. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7021. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7022. }
  7023. break;
  7024. case CMD_XRI_ABORTED_CX:
  7025. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7026. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7027. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7028. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7029. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7030. default:
  7031. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7032. "2014 Invalid command 0x%x\n",
  7033. iocbq->iocb.ulpCommand);
  7034. return IOCB_ERROR;
  7035. break;
  7036. }
  7037. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7038. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7039. wqe->generic.wqe_com.abort_tag = abort_tag;
  7040. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7041. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7042. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7043. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7044. return 0;
  7045. }
  7046. /**
  7047. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7048. * @phba: Pointer to HBA context object.
  7049. * @ring_number: SLI ring number to issue iocb on.
  7050. * @piocb: Pointer to command iocb.
  7051. * @flag: Flag indicating if this command can be put into txq.
  7052. *
  7053. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7054. * an iocb command to an HBA with SLI-4 interface spec.
  7055. *
  7056. * This function is called with hbalock held. The function will return success
  7057. * after it successfully submit the iocb to firmware or after adding to the
  7058. * txq.
  7059. **/
  7060. static int
  7061. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7062. struct lpfc_iocbq *piocb, uint32_t flag)
  7063. {
  7064. struct lpfc_sglq *sglq;
  7065. union lpfc_wqe wqe;
  7066. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7067. if (piocb->sli4_xritag == NO_XRI) {
  7068. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7069. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
  7070. piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7071. sglq = NULL;
  7072. else {
  7073. if (pring->txq_cnt) {
  7074. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7075. __lpfc_sli_ringtx_put(phba,
  7076. pring, piocb);
  7077. return IOCB_SUCCESS;
  7078. } else {
  7079. return IOCB_BUSY;
  7080. }
  7081. } else {
  7082. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7083. if (!sglq) {
  7084. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7085. __lpfc_sli_ringtx_put(phba,
  7086. pring,
  7087. piocb);
  7088. return IOCB_SUCCESS;
  7089. } else
  7090. return IOCB_BUSY;
  7091. }
  7092. }
  7093. }
  7094. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7095. /* These IO's already have an XRI and a mapped sgl. */
  7096. sglq = NULL;
  7097. } else {
  7098. /*
  7099. * This is a continuation of a commandi,(CX) so this
  7100. * sglq is on the active list
  7101. */
  7102. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7103. if (!sglq)
  7104. return IOCB_ERROR;
  7105. }
  7106. if (sglq) {
  7107. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7108. piocb->sli4_xritag = sglq->sli4_xritag;
  7109. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7110. return IOCB_ERROR;
  7111. }
  7112. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7113. return IOCB_ERROR;
  7114. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7115. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7116. /*
  7117. * For FCP command IOCB, get a new WQ index to distribute
  7118. * WQE across the WQsr. On the other hand, for abort IOCB,
  7119. * it carries the same WQ index to the original command
  7120. * IOCB.
  7121. */
  7122. if (piocb->iocb_flag & LPFC_IO_FCP)
  7123. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7124. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7125. &wqe))
  7126. return IOCB_ERROR;
  7127. } else {
  7128. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7129. return IOCB_ERROR;
  7130. }
  7131. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7132. return 0;
  7133. }
  7134. /**
  7135. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7136. *
  7137. * This routine wraps the actual lockless version for issusing IOCB function
  7138. * pointer from the lpfc_hba struct.
  7139. *
  7140. * Return codes:
  7141. * IOCB_ERROR - Error
  7142. * IOCB_SUCCESS - Success
  7143. * IOCB_BUSY - Busy
  7144. **/
  7145. int
  7146. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7147. struct lpfc_iocbq *piocb, uint32_t flag)
  7148. {
  7149. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7150. }
  7151. /**
  7152. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7153. * @phba: The hba struct for which this call is being executed.
  7154. * @dev_grp: The HBA PCI-Device group number.
  7155. *
  7156. * This routine sets up the SLI interface API function jump table in @phba
  7157. * struct.
  7158. * Returns: 0 - success, -ENODEV - failure.
  7159. **/
  7160. int
  7161. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7162. {
  7163. switch (dev_grp) {
  7164. case LPFC_PCI_DEV_LP:
  7165. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7166. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7167. break;
  7168. case LPFC_PCI_DEV_OC:
  7169. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7170. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7171. break;
  7172. default:
  7173. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7174. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7175. dev_grp);
  7176. return -ENODEV;
  7177. break;
  7178. }
  7179. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7180. return 0;
  7181. }
  7182. /**
  7183. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7184. * @phba: Pointer to HBA context object.
  7185. * @pring: Pointer to driver SLI ring object.
  7186. * @piocb: Pointer to command iocb.
  7187. * @flag: Flag indicating if this command can be put into txq.
  7188. *
  7189. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7190. * function. This function gets the hbalock and calls
  7191. * __lpfc_sli_issue_iocb function and will return the error returned
  7192. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7193. * functions which do not hold hbalock.
  7194. **/
  7195. int
  7196. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7197. struct lpfc_iocbq *piocb, uint32_t flag)
  7198. {
  7199. unsigned long iflags;
  7200. int rc;
  7201. spin_lock_irqsave(&phba->hbalock, iflags);
  7202. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7203. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7204. return rc;
  7205. }
  7206. /**
  7207. * lpfc_extra_ring_setup - Extra ring setup function
  7208. * @phba: Pointer to HBA context object.
  7209. *
  7210. * This function is called while driver attaches with the
  7211. * HBA to setup the extra ring. The extra ring is used
  7212. * only when driver needs to support target mode functionality
  7213. * or IP over FC functionalities.
  7214. *
  7215. * This function is called with no lock held.
  7216. **/
  7217. static int
  7218. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7219. {
  7220. struct lpfc_sli *psli;
  7221. struct lpfc_sli_ring *pring;
  7222. psli = &phba->sli;
  7223. /* Adjust cmd/rsp ring iocb entries more evenly */
  7224. /* Take some away from the FCP ring */
  7225. pring = &psli->ring[psli->fcp_ring];
  7226. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7227. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7228. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7229. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7230. /* and give them to the extra ring */
  7231. pring = &psli->ring[psli->extra_ring];
  7232. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7233. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7234. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7235. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7236. /* Setup default profile for this ring */
  7237. pring->iotag_max = 4096;
  7238. pring->num_mask = 1;
  7239. pring->prt[0].profile = 0; /* Mask 0 */
  7240. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7241. pring->prt[0].type = phba->cfg_multi_ring_type;
  7242. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7243. return 0;
  7244. }
  7245. /**
  7246. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7247. * @phba: Pointer to HBA context object.
  7248. * @pring: Pointer to driver SLI ring object.
  7249. * @iocbq: Pointer to iocb object.
  7250. *
  7251. * This function is called by the slow ring event handler
  7252. * function when there is an ASYNC event iocb in the ring.
  7253. * This function is called with no lock held.
  7254. * Currently this function handles only temperature related
  7255. * ASYNC events. The function decodes the temperature sensor
  7256. * event message and posts events for the management applications.
  7257. **/
  7258. static void
  7259. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7260. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7261. {
  7262. IOCB_t *icmd;
  7263. uint16_t evt_code;
  7264. uint16_t temp;
  7265. struct temp_event temp_event_data;
  7266. struct Scsi_Host *shost;
  7267. uint32_t *iocb_w;
  7268. icmd = &iocbq->iocb;
  7269. evt_code = icmd->un.asyncstat.evt_code;
  7270. temp = icmd->ulpContext;
  7271. if ((evt_code != ASYNC_TEMP_WARN) &&
  7272. (evt_code != ASYNC_TEMP_SAFE)) {
  7273. iocb_w = (uint32_t *) icmd;
  7274. lpfc_printf_log(phba,
  7275. KERN_ERR,
  7276. LOG_SLI,
  7277. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7278. " evt_code 0x%x\n"
  7279. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7280. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7281. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7282. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7283. pring->ringno,
  7284. icmd->un.asyncstat.evt_code,
  7285. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7286. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7287. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7288. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7289. return;
  7290. }
  7291. temp_event_data.data = (uint32_t)temp;
  7292. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7293. if (evt_code == ASYNC_TEMP_WARN) {
  7294. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7295. lpfc_printf_log(phba,
  7296. KERN_ERR,
  7297. LOG_TEMP,
  7298. "0347 Adapter is very hot, please take "
  7299. "corrective action. temperature : %d Celsius\n",
  7300. temp);
  7301. }
  7302. if (evt_code == ASYNC_TEMP_SAFE) {
  7303. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7304. lpfc_printf_log(phba,
  7305. KERN_ERR,
  7306. LOG_TEMP,
  7307. "0340 Adapter temperature is OK now. "
  7308. "temperature : %d Celsius\n",
  7309. temp);
  7310. }
  7311. /* Send temperature change event to applications */
  7312. shost = lpfc_shost_from_vport(phba->pport);
  7313. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7314. sizeof(temp_event_data), (char *) &temp_event_data,
  7315. LPFC_NL_VENDOR_ID);
  7316. }
  7317. /**
  7318. * lpfc_sli_setup - SLI ring setup function
  7319. * @phba: Pointer to HBA context object.
  7320. *
  7321. * lpfc_sli_setup sets up rings of the SLI interface with
  7322. * number of iocbs per ring and iotags. This function is
  7323. * called while driver attach to the HBA and before the
  7324. * interrupts are enabled. So there is no need for locking.
  7325. *
  7326. * This function always returns 0.
  7327. **/
  7328. int
  7329. lpfc_sli_setup(struct lpfc_hba *phba)
  7330. {
  7331. int i, totiocbsize = 0;
  7332. struct lpfc_sli *psli = &phba->sli;
  7333. struct lpfc_sli_ring *pring;
  7334. psli->num_rings = MAX_CONFIGURED_RINGS;
  7335. psli->sli_flag = 0;
  7336. psli->fcp_ring = LPFC_FCP_RING;
  7337. psli->next_ring = LPFC_FCP_NEXT_RING;
  7338. psli->extra_ring = LPFC_EXTRA_RING;
  7339. psli->iocbq_lookup = NULL;
  7340. psli->iocbq_lookup_len = 0;
  7341. psli->last_iotag = 0;
  7342. for (i = 0; i < psli->num_rings; i++) {
  7343. pring = &psli->ring[i];
  7344. switch (i) {
  7345. case LPFC_FCP_RING: /* ring 0 - FCP */
  7346. /* numCiocb and numRiocb are used in config_port */
  7347. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7348. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7349. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7350. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7351. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7352. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7353. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7354. SLI3_IOCB_CMD_SIZE :
  7355. SLI2_IOCB_CMD_SIZE;
  7356. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7357. SLI3_IOCB_RSP_SIZE :
  7358. SLI2_IOCB_RSP_SIZE;
  7359. pring->iotag_ctr = 0;
  7360. pring->iotag_max =
  7361. (phba->cfg_hba_queue_depth * 2);
  7362. pring->fast_iotag = pring->iotag_max;
  7363. pring->num_mask = 0;
  7364. break;
  7365. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7366. /* numCiocb and numRiocb are used in config_port */
  7367. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7368. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7369. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7370. SLI3_IOCB_CMD_SIZE :
  7371. SLI2_IOCB_CMD_SIZE;
  7372. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7373. SLI3_IOCB_RSP_SIZE :
  7374. SLI2_IOCB_RSP_SIZE;
  7375. pring->iotag_max = phba->cfg_hba_queue_depth;
  7376. pring->num_mask = 0;
  7377. break;
  7378. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7379. /* numCiocb and numRiocb are used in config_port */
  7380. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7381. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7382. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7383. SLI3_IOCB_CMD_SIZE :
  7384. SLI2_IOCB_CMD_SIZE;
  7385. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7386. SLI3_IOCB_RSP_SIZE :
  7387. SLI2_IOCB_RSP_SIZE;
  7388. pring->fast_iotag = 0;
  7389. pring->iotag_ctr = 0;
  7390. pring->iotag_max = 4096;
  7391. pring->lpfc_sli_rcv_async_status =
  7392. lpfc_sli_async_event_handler;
  7393. pring->num_mask = LPFC_MAX_RING_MASK;
  7394. pring->prt[0].profile = 0; /* Mask 0 */
  7395. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7396. pring->prt[0].type = FC_TYPE_ELS;
  7397. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7398. lpfc_els_unsol_event;
  7399. pring->prt[1].profile = 0; /* Mask 1 */
  7400. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7401. pring->prt[1].type = FC_TYPE_ELS;
  7402. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7403. lpfc_els_unsol_event;
  7404. pring->prt[2].profile = 0; /* Mask 2 */
  7405. /* NameServer Inquiry */
  7406. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7407. /* NameServer */
  7408. pring->prt[2].type = FC_TYPE_CT;
  7409. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7410. lpfc_ct_unsol_event;
  7411. pring->prt[3].profile = 0; /* Mask 3 */
  7412. /* NameServer response */
  7413. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7414. /* NameServer */
  7415. pring->prt[3].type = FC_TYPE_CT;
  7416. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7417. lpfc_ct_unsol_event;
  7418. /* abort unsolicited sequence */
  7419. pring->prt[4].profile = 0; /* Mask 4 */
  7420. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7421. pring->prt[4].type = FC_TYPE_BLS;
  7422. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7423. lpfc_sli4_ct_abort_unsol_event;
  7424. break;
  7425. }
  7426. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7427. (pring->numRiocb * pring->sizeRiocb);
  7428. }
  7429. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7430. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7431. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7432. "SLI2 SLIM Data: x%x x%lx\n",
  7433. phba->brd_no, totiocbsize,
  7434. (unsigned long) MAX_SLIM_IOCB_SIZE);
  7435. }
  7436. if (phba->cfg_multi_ring_support == 2)
  7437. lpfc_extra_ring_setup(phba);
  7438. return 0;
  7439. }
  7440. /**
  7441. * lpfc_sli_queue_setup - Queue initialization function
  7442. * @phba: Pointer to HBA context object.
  7443. *
  7444. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  7445. * ring. This function also initializes ring indices of each ring.
  7446. * This function is called during the initialization of the SLI
  7447. * interface of an HBA.
  7448. * This function is called with no lock held and always returns
  7449. * 1.
  7450. **/
  7451. int
  7452. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  7453. {
  7454. struct lpfc_sli *psli;
  7455. struct lpfc_sli_ring *pring;
  7456. int i;
  7457. psli = &phba->sli;
  7458. spin_lock_irq(&phba->hbalock);
  7459. INIT_LIST_HEAD(&psli->mboxq);
  7460. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  7461. /* Initialize list headers for txq and txcmplq as double linked lists */
  7462. for (i = 0; i < psli->num_rings; i++) {
  7463. pring = &psli->ring[i];
  7464. pring->ringno = i;
  7465. pring->next_cmdidx = 0;
  7466. pring->local_getidx = 0;
  7467. pring->cmdidx = 0;
  7468. INIT_LIST_HEAD(&pring->txq);
  7469. INIT_LIST_HEAD(&pring->txcmplq);
  7470. INIT_LIST_HEAD(&pring->iocb_continueq);
  7471. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  7472. INIT_LIST_HEAD(&pring->postbufq);
  7473. }
  7474. spin_unlock_irq(&phba->hbalock);
  7475. return 1;
  7476. }
  7477. /**
  7478. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  7479. * @phba: Pointer to HBA context object.
  7480. *
  7481. * This routine flushes the mailbox command subsystem. It will unconditionally
  7482. * flush all the mailbox commands in the three possible stages in the mailbox
  7483. * command sub-system: pending mailbox command queue; the outstanding mailbox
  7484. * command; and completed mailbox command queue. It is caller's responsibility
  7485. * to make sure that the driver is in the proper state to flush the mailbox
  7486. * command sub-system. Namely, the posting of mailbox commands into the
  7487. * pending mailbox command queue from the various clients must be stopped;
  7488. * either the HBA is in a state that it will never works on the outstanding
  7489. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  7490. * mailbox command has been completed.
  7491. **/
  7492. static void
  7493. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  7494. {
  7495. LIST_HEAD(completions);
  7496. struct lpfc_sli *psli = &phba->sli;
  7497. LPFC_MBOXQ_t *pmb;
  7498. unsigned long iflag;
  7499. /* Flush all the mailbox commands in the mbox system */
  7500. spin_lock_irqsave(&phba->hbalock, iflag);
  7501. /* The pending mailbox command queue */
  7502. list_splice_init(&phba->sli.mboxq, &completions);
  7503. /* The outstanding active mailbox command */
  7504. if (psli->mbox_active) {
  7505. list_add_tail(&psli->mbox_active->list, &completions);
  7506. psli->mbox_active = NULL;
  7507. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7508. }
  7509. /* The completed mailbox command queue */
  7510. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  7511. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7512. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  7513. while (!list_empty(&completions)) {
  7514. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  7515. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7516. if (pmb->mbox_cmpl)
  7517. pmb->mbox_cmpl(phba, pmb);
  7518. }
  7519. }
  7520. /**
  7521. * lpfc_sli_host_down - Vport cleanup function
  7522. * @vport: Pointer to virtual port object.
  7523. *
  7524. * lpfc_sli_host_down is called to clean up the resources
  7525. * associated with a vport before destroying virtual
  7526. * port data structures.
  7527. * This function does following operations:
  7528. * - Free discovery resources associated with this virtual
  7529. * port.
  7530. * - Free iocbs associated with this virtual port in
  7531. * the txq.
  7532. * - Send abort for all iocb commands associated with this
  7533. * vport in txcmplq.
  7534. *
  7535. * This function is called with no lock held and always returns 1.
  7536. **/
  7537. int
  7538. lpfc_sli_host_down(struct lpfc_vport *vport)
  7539. {
  7540. LIST_HEAD(completions);
  7541. struct lpfc_hba *phba = vport->phba;
  7542. struct lpfc_sli *psli = &phba->sli;
  7543. struct lpfc_sli_ring *pring;
  7544. struct lpfc_iocbq *iocb, *next_iocb;
  7545. int i;
  7546. unsigned long flags = 0;
  7547. uint16_t prev_pring_flag;
  7548. lpfc_cleanup_discovery_resources(vport);
  7549. spin_lock_irqsave(&phba->hbalock, flags);
  7550. for (i = 0; i < psli->num_rings; i++) {
  7551. pring = &psli->ring[i];
  7552. prev_pring_flag = pring->flag;
  7553. /* Only slow rings */
  7554. if (pring->ringno == LPFC_ELS_RING) {
  7555. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  7556. /* Set the lpfc data pending flag */
  7557. set_bit(LPFC_DATA_READY, &phba->data_flags);
  7558. }
  7559. /*
  7560. * Error everything on the txq since these iocbs have not been
  7561. * given to the FW yet.
  7562. */
  7563. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  7564. if (iocb->vport != vport)
  7565. continue;
  7566. list_move_tail(&iocb->list, &completions);
  7567. pring->txq_cnt--;
  7568. }
  7569. /* Next issue ABTS for everything on the txcmplq */
  7570. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  7571. list) {
  7572. if (iocb->vport != vport)
  7573. continue;
  7574. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  7575. }
  7576. pring->flag = prev_pring_flag;
  7577. }
  7578. spin_unlock_irqrestore(&phba->hbalock, flags);
  7579. /* Cancel all the IOCBs from the completions list */
  7580. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  7581. IOERR_SLI_DOWN);
  7582. return 1;
  7583. }
  7584. /**
  7585. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  7586. * @phba: Pointer to HBA context object.
  7587. *
  7588. * This function cleans up all iocb, buffers, mailbox commands
  7589. * while shutting down the HBA. This function is called with no
  7590. * lock held and always returns 1.
  7591. * This function does the following to cleanup driver resources:
  7592. * - Free discovery resources for each virtual port
  7593. * - Cleanup any pending fabric iocbs
  7594. * - Iterate through the iocb txq and free each entry
  7595. * in the list.
  7596. * - Free up any buffer posted to the HBA
  7597. * - Free mailbox commands in the mailbox queue.
  7598. **/
  7599. int
  7600. lpfc_sli_hba_down(struct lpfc_hba *phba)
  7601. {
  7602. LIST_HEAD(completions);
  7603. struct lpfc_sli *psli = &phba->sli;
  7604. struct lpfc_sli_ring *pring;
  7605. struct lpfc_dmabuf *buf_ptr;
  7606. unsigned long flags = 0;
  7607. int i;
  7608. /* Shutdown the mailbox command sub-system */
  7609. lpfc_sli_mbox_sys_shutdown(phba);
  7610. lpfc_hba_down_prep(phba);
  7611. lpfc_fabric_abort_hba(phba);
  7612. spin_lock_irqsave(&phba->hbalock, flags);
  7613. for (i = 0; i < psli->num_rings; i++) {
  7614. pring = &psli->ring[i];
  7615. /* Only slow rings */
  7616. if (pring->ringno == LPFC_ELS_RING) {
  7617. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  7618. /* Set the lpfc data pending flag */
  7619. set_bit(LPFC_DATA_READY, &phba->data_flags);
  7620. }
  7621. /*
  7622. * Error everything on the txq since these iocbs have not been
  7623. * given to the FW yet.
  7624. */
  7625. list_splice_init(&pring->txq, &completions);
  7626. pring->txq_cnt = 0;
  7627. }
  7628. spin_unlock_irqrestore(&phba->hbalock, flags);
  7629. /* Cancel all the IOCBs from the completions list */
  7630. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  7631. IOERR_SLI_DOWN);
  7632. spin_lock_irqsave(&phba->hbalock, flags);
  7633. list_splice_init(&phba->elsbuf, &completions);
  7634. phba->elsbuf_cnt = 0;
  7635. phba->elsbuf_prev_cnt = 0;
  7636. spin_unlock_irqrestore(&phba->hbalock, flags);
  7637. while (!list_empty(&completions)) {
  7638. list_remove_head(&completions, buf_ptr,
  7639. struct lpfc_dmabuf, list);
  7640. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  7641. kfree(buf_ptr);
  7642. }
  7643. /* Return any active mbox cmds */
  7644. del_timer_sync(&psli->mbox_tmo);
  7645. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  7646. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  7647. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  7648. return 1;
  7649. }
  7650. /**
  7651. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  7652. * @srcp: Source memory pointer.
  7653. * @destp: Destination memory pointer.
  7654. * @cnt: Number of words required to be copied.
  7655. *
  7656. * This function is used for copying data between driver memory
  7657. * and the SLI memory. This function also changes the endianness
  7658. * of each word if native endianness is different from SLI
  7659. * endianness. This function can be called with or without
  7660. * lock.
  7661. **/
  7662. void
  7663. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  7664. {
  7665. uint32_t *src = srcp;
  7666. uint32_t *dest = destp;
  7667. uint32_t ldata;
  7668. int i;
  7669. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  7670. ldata = *src;
  7671. ldata = le32_to_cpu(ldata);
  7672. *dest = ldata;
  7673. src++;
  7674. dest++;
  7675. }
  7676. }
  7677. /**
  7678. * lpfc_sli_bemem_bcopy - SLI memory copy function
  7679. * @srcp: Source memory pointer.
  7680. * @destp: Destination memory pointer.
  7681. * @cnt: Number of words required to be copied.
  7682. *
  7683. * This function is used for copying data between a data structure
  7684. * with big endian representation to local endianness.
  7685. * This function can be called with or without lock.
  7686. **/
  7687. void
  7688. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  7689. {
  7690. uint32_t *src = srcp;
  7691. uint32_t *dest = destp;
  7692. uint32_t ldata;
  7693. int i;
  7694. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  7695. ldata = *src;
  7696. ldata = be32_to_cpu(ldata);
  7697. *dest = ldata;
  7698. src++;
  7699. dest++;
  7700. }
  7701. }
  7702. /**
  7703. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  7704. * @phba: Pointer to HBA context object.
  7705. * @pring: Pointer to driver SLI ring object.
  7706. * @mp: Pointer to driver buffer object.
  7707. *
  7708. * This function is called with no lock held.
  7709. * It always return zero after adding the buffer to the postbufq
  7710. * buffer list.
  7711. **/
  7712. int
  7713. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7714. struct lpfc_dmabuf *mp)
  7715. {
  7716. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  7717. later */
  7718. spin_lock_irq(&phba->hbalock);
  7719. list_add_tail(&mp->list, &pring->postbufq);
  7720. pring->postbufq_cnt++;
  7721. spin_unlock_irq(&phba->hbalock);
  7722. return 0;
  7723. }
  7724. /**
  7725. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  7726. * @phba: Pointer to HBA context object.
  7727. *
  7728. * When HBQ is enabled, buffers are searched based on tags. This function
  7729. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  7730. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  7731. * does not conflict with tags of buffer posted for unsolicited events.
  7732. * The function returns the allocated tag. The function is called with
  7733. * no locks held.
  7734. **/
  7735. uint32_t
  7736. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  7737. {
  7738. spin_lock_irq(&phba->hbalock);
  7739. phba->buffer_tag_count++;
  7740. /*
  7741. * Always set the QUE_BUFTAG_BIT to distiguish between
  7742. * a tag assigned by HBQ.
  7743. */
  7744. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  7745. spin_unlock_irq(&phba->hbalock);
  7746. return phba->buffer_tag_count;
  7747. }
  7748. /**
  7749. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  7750. * @phba: Pointer to HBA context object.
  7751. * @pring: Pointer to driver SLI ring object.
  7752. * @tag: Buffer tag.
  7753. *
  7754. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  7755. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  7756. * iocb is posted to the response ring with the tag of the buffer.
  7757. * This function searches the pring->postbufq list using the tag
  7758. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  7759. * iocb. If the buffer is found then lpfc_dmabuf object of the
  7760. * buffer is returned to the caller else NULL is returned.
  7761. * This function is called with no lock held.
  7762. **/
  7763. struct lpfc_dmabuf *
  7764. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7765. uint32_t tag)
  7766. {
  7767. struct lpfc_dmabuf *mp, *next_mp;
  7768. struct list_head *slp = &pring->postbufq;
  7769. /* Search postbufq, from the beginning, looking for a match on tag */
  7770. spin_lock_irq(&phba->hbalock);
  7771. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  7772. if (mp->buffer_tag == tag) {
  7773. list_del_init(&mp->list);
  7774. pring->postbufq_cnt--;
  7775. spin_unlock_irq(&phba->hbalock);
  7776. return mp;
  7777. }
  7778. }
  7779. spin_unlock_irq(&phba->hbalock);
  7780. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7781. "0402 Cannot find virtual addr for buffer tag on "
  7782. "ring %d Data x%lx x%p x%p x%x\n",
  7783. pring->ringno, (unsigned long) tag,
  7784. slp->next, slp->prev, pring->postbufq_cnt);
  7785. return NULL;
  7786. }
  7787. /**
  7788. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  7789. * @phba: Pointer to HBA context object.
  7790. * @pring: Pointer to driver SLI ring object.
  7791. * @phys: DMA address of the buffer.
  7792. *
  7793. * This function searches the buffer list using the dma_address
  7794. * of unsolicited event to find the driver's lpfc_dmabuf object
  7795. * corresponding to the dma_address. The function returns the
  7796. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  7797. * This function is called by the ct and els unsolicited event
  7798. * handlers to get the buffer associated with the unsolicited
  7799. * event.
  7800. *
  7801. * This function is called with no lock held.
  7802. **/
  7803. struct lpfc_dmabuf *
  7804. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7805. dma_addr_t phys)
  7806. {
  7807. struct lpfc_dmabuf *mp, *next_mp;
  7808. struct list_head *slp = &pring->postbufq;
  7809. /* Search postbufq, from the beginning, looking for a match on phys */
  7810. spin_lock_irq(&phba->hbalock);
  7811. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  7812. if (mp->phys == phys) {
  7813. list_del_init(&mp->list);
  7814. pring->postbufq_cnt--;
  7815. spin_unlock_irq(&phba->hbalock);
  7816. return mp;
  7817. }
  7818. }
  7819. spin_unlock_irq(&phba->hbalock);
  7820. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7821. "0410 Cannot find virtual addr for mapped buf on "
  7822. "ring %d Data x%llx x%p x%p x%x\n",
  7823. pring->ringno, (unsigned long long)phys,
  7824. slp->next, slp->prev, pring->postbufq_cnt);
  7825. return NULL;
  7826. }
  7827. /**
  7828. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  7829. * @phba: Pointer to HBA context object.
  7830. * @cmdiocb: Pointer to driver command iocb object.
  7831. * @rspiocb: Pointer to driver response iocb object.
  7832. *
  7833. * This function is the completion handler for the abort iocbs for
  7834. * ELS commands. This function is called from the ELS ring event
  7835. * handler with no lock held. This function frees memory resources
  7836. * associated with the abort iocb.
  7837. **/
  7838. static void
  7839. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  7840. struct lpfc_iocbq *rspiocb)
  7841. {
  7842. IOCB_t *irsp = &rspiocb->iocb;
  7843. uint16_t abort_iotag, abort_context;
  7844. struct lpfc_iocbq *abort_iocb;
  7845. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  7846. abort_iocb = NULL;
  7847. if (irsp->ulpStatus) {
  7848. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  7849. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  7850. spin_lock_irq(&phba->hbalock);
  7851. if (phba->sli_rev < LPFC_SLI_REV4) {
  7852. if (abort_iotag != 0 &&
  7853. abort_iotag <= phba->sli.last_iotag)
  7854. abort_iocb =
  7855. phba->sli.iocbq_lookup[abort_iotag];
  7856. } else
  7857. /* For sli4 the abort_tag is the XRI,
  7858. * so the abort routine puts the iotag of the iocb
  7859. * being aborted in the context field of the abort
  7860. * IOCB.
  7861. */
  7862. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  7863. /*
  7864. * If the iocb is not found in Firmware queue the iocb
  7865. * might have completed already. Do not free it again.
  7866. */
  7867. if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  7868. if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
  7869. spin_unlock_irq(&phba->hbalock);
  7870. lpfc_sli_release_iocbq(phba, cmdiocb);
  7871. return;
  7872. }
  7873. /* For SLI4 the ulpContext field for abort IOCB
  7874. * holds the iotag of the IOCB being aborted so
  7875. * the local abort_context needs to be reset to
  7876. * match the aborted IOCBs ulpContext.
  7877. */
  7878. if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
  7879. abort_context = abort_iocb->iocb.ulpContext;
  7880. }
  7881. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  7882. "0327 Cannot abort els iocb %p "
  7883. "with tag %x context %x, abort status %x, "
  7884. "abort code %x\n",
  7885. abort_iocb, abort_iotag, abort_context,
  7886. irsp->ulpStatus, irsp->un.ulpWord[4]);
  7887. /*
  7888. * make sure we have the right iocbq before taking it
  7889. * off the txcmplq and try to call completion routine.
  7890. */
  7891. if (!abort_iocb ||
  7892. abort_iocb->iocb.ulpContext != abort_context ||
  7893. (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
  7894. spin_unlock_irq(&phba->hbalock);
  7895. else if (phba->sli_rev < LPFC_SLI_REV4) {
  7896. /*
  7897. * leave the SLI4 aborted command on the txcmplq
  7898. * list and the command complete WCQE's XB bit
  7899. * will tell whether the SGL (XRI) can be released
  7900. * immediately or to the aborted SGL list for the
  7901. * following abort XRI from the HBA.
  7902. */
  7903. list_del_init(&abort_iocb->list);
  7904. if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
  7905. abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  7906. pring->txcmplq_cnt--;
  7907. }
  7908. /* Firmware could still be in progress of DMAing
  7909. * payload, so don't free data buffer till after
  7910. * a hbeat.
  7911. */
  7912. abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
  7913. abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  7914. spin_unlock_irq(&phba->hbalock);
  7915. abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  7916. abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
  7917. (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
  7918. } else
  7919. spin_unlock_irq(&phba->hbalock);
  7920. }
  7921. lpfc_sli_release_iocbq(phba, cmdiocb);
  7922. return;
  7923. }
  7924. /**
  7925. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  7926. * @phba: Pointer to HBA context object.
  7927. * @cmdiocb: Pointer to driver command iocb object.
  7928. * @rspiocb: Pointer to driver response iocb object.
  7929. *
  7930. * The function is called from SLI ring event handler with no
  7931. * lock held. This function is the completion handler for ELS commands
  7932. * which are aborted. The function frees memory resources used for
  7933. * the aborted ELS commands.
  7934. **/
  7935. static void
  7936. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  7937. struct lpfc_iocbq *rspiocb)
  7938. {
  7939. IOCB_t *irsp = &rspiocb->iocb;
  7940. /* ELS cmd tag <ulpIoTag> completes */
  7941. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  7942. "0139 Ignoring ELS cmd tag x%x completion Data: "
  7943. "x%x x%x x%x\n",
  7944. irsp->ulpIoTag, irsp->ulpStatus,
  7945. irsp->un.ulpWord[4], irsp->ulpTimeout);
  7946. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  7947. lpfc_ct_free_iocb(phba, cmdiocb);
  7948. else
  7949. lpfc_els_free_iocb(phba, cmdiocb);
  7950. return;
  7951. }
  7952. /**
  7953. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  7954. * @phba: Pointer to HBA context object.
  7955. * @pring: Pointer to driver SLI ring object.
  7956. * @cmdiocb: Pointer to driver command iocb object.
  7957. *
  7958. * This function issues an abort iocb for the provided command iocb down to
  7959. * the port. Other than the case the outstanding command iocb is an abort
  7960. * request, this function issues abort out unconditionally. This function is
  7961. * called with hbalock held. The function returns 0 when it fails due to
  7962. * memory allocation failure or when the command iocb is an abort request.
  7963. **/
  7964. static int
  7965. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7966. struct lpfc_iocbq *cmdiocb)
  7967. {
  7968. struct lpfc_vport *vport = cmdiocb->vport;
  7969. struct lpfc_iocbq *abtsiocbp;
  7970. IOCB_t *icmd = NULL;
  7971. IOCB_t *iabt = NULL;
  7972. int retval;
  7973. /*
  7974. * There are certain command types we don't want to abort. And we
  7975. * don't want to abort commands that are already in the process of
  7976. * being aborted.
  7977. */
  7978. icmd = &cmdiocb->iocb;
  7979. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  7980. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  7981. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  7982. return 0;
  7983. /* issue ABTS for this IOCB based on iotag */
  7984. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  7985. if (abtsiocbp == NULL)
  7986. return 0;
  7987. /* This signals the response to set the correct status
  7988. * before calling the completion handler
  7989. */
  7990. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  7991. iabt = &abtsiocbp->iocb;
  7992. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  7993. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  7994. if (phba->sli_rev == LPFC_SLI_REV4) {
  7995. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  7996. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  7997. }
  7998. else
  7999. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8000. iabt->ulpLe = 1;
  8001. iabt->ulpClass = icmd->ulpClass;
  8002. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8003. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8004. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8005. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8006. if (phba->link_state >= LPFC_LINK_UP)
  8007. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8008. else
  8009. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8010. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8011. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8012. "0339 Abort xri x%x, original iotag x%x, "
  8013. "abort cmd iotag x%x\n",
  8014. iabt->un.acxri.abortIoTag,
  8015. iabt->un.acxri.abortContextTag,
  8016. abtsiocbp->iotag);
  8017. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8018. if (retval)
  8019. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8020. /*
  8021. * Caller to this routine should check for IOCB_ERROR
  8022. * and handle it properly. This routine no longer removes
  8023. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8024. */
  8025. return retval;
  8026. }
  8027. /**
  8028. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8029. * @phba: Pointer to HBA context object.
  8030. * @pring: Pointer to driver SLI ring object.
  8031. * @cmdiocb: Pointer to driver command iocb object.
  8032. *
  8033. * This function issues an abort iocb for the provided command iocb. In case
  8034. * of unloading, the abort iocb will not be issued to commands on the ELS
  8035. * ring. Instead, the callback function shall be changed to those commands
  8036. * so that nothing happens when them finishes. This function is called with
  8037. * hbalock held. The function returns 0 when the command iocb is an abort
  8038. * request.
  8039. **/
  8040. int
  8041. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8042. struct lpfc_iocbq *cmdiocb)
  8043. {
  8044. struct lpfc_vport *vport = cmdiocb->vport;
  8045. int retval = IOCB_ERROR;
  8046. IOCB_t *icmd = NULL;
  8047. /*
  8048. * There are certain command types we don't want to abort. And we
  8049. * don't want to abort commands that are already in the process of
  8050. * being aborted.
  8051. */
  8052. icmd = &cmdiocb->iocb;
  8053. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8054. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8055. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8056. return 0;
  8057. /*
  8058. * If we're unloading, don't abort iocb on the ELS ring, but change
  8059. * the callback so that nothing happens when it finishes.
  8060. */
  8061. if ((vport->load_flag & FC_UNLOADING) &&
  8062. (pring->ringno == LPFC_ELS_RING)) {
  8063. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8064. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8065. else
  8066. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8067. goto abort_iotag_exit;
  8068. }
  8069. /* Now, we try to issue the abort to the cmdiocb out */
  8070. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8071. abort_iotag_exit:
  8072. /*
  8073. * Caller to this routine should check for IOCB_ERROR
  8074. * and handle it properly. This routine no longer removes
  8075. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8076. */
  8077. return retval;
  8078. }
  8079. /**
  8080. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8081. * @phba: Pointer to HBA context object.
  8082. * @pring: Pointer to driver SLI ring object.
  8083. *
  8084. * This function aborts all iocbs in the given ring and frees all the iocb
  8085. * objects in txq. This function issues abort iocbs unconditionally for all
  8086. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8087. * to complete before the return of this function. The caller is not required
  8088. * to hold any locks.
  8089. **/
  8090. static void
  8091. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8092. {
  8093. LIST_HEAD(completions);
  8094. struct lpfc_iocbq *iocb, *next_iocb;
  8095. if (pring->ringno == LPFC_ELS_RING)
  8096. lpfc_fabric_abort_hba(phba);
  8097. spin_lock_irq(&phba->hbalock);
  8098. /* Take off all the iocbs on txq for cancelling */
  8099. list_splice_init(&pring->txq, &completions);
  8100. pring->txq_cnt = 0;
  8101. /* Next issue ABTS for everything on the txcmplq */
  8102. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8103. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8104. spin_unlock_irq(&phba->hbalock);
  8105. /* Cancel all the IOCBs from the completions list */
  8106. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8107. IOERR_SLI_ABORTED);
  8108. }
  8109. /**
  8110. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8111. * @phba: pointer to lpfc HBA data structure.
  8112. *
  8113. * This routine will abort all pending and outstanding iocbs to an HBA.
  8114. **/
  8115. void
  8116. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8117. {
  8118. struct lpfc_sli *psli = &phba->sli;
  8119. struct lpfc_sli_ring *pring;
  8120. int i;
  8121. for (i = 0; i < psli->num_rings; i++) {
  8122. pring = &psli->ring[i];
  8123. lpfc_sli_iocb_ring_abort(phba, pring);
  8124. }
  8125. }
  8126. /**
  8127. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8128. * @iocbq: Pointer to driver iocb object.
  8129. * @vport: Pointer to driver virtual port object.
  8130. * @tgt_id: SCSI ID of the target.
  8131. * @lun_id: LUN ID of the scsi device.
  8132. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8133. *
  8134. * This function acts as an iocb filter for functions which abort or count
  8135. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8136. * 0 if the filtering criteria is met for the given iocb and will return
  8137. * 1 if the filtering criteria is not met.
  8138. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8139. * given iocb is for the SCSI device specified by vport, tgt_id and
  8140. * lun_id parameter.
  8141. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8142. * given iocb is for the SCSI target specified by vport and tgt_id
  8143. * parameters.
  8144. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8145. * given iocb is for the SCSI host associated with the given vport.
  8146. * This function is called with no locks held.
  8147. **/
  8148. static int
  8149. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8150. uint16_t tgt_id, uint64_t lun_id,
  8151. lpfc_ctx_cmd ctx_cmd)
  8152. {
  8153. struct lpfc_scsi_buf *lpfc_cmd;
  8154. int rc = 1;
  8155. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8156. return rc;
  8157. if (iocbq->vport != vport)
  8158. return rc;
  8159. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8160. if (lpfc_cmd->pCmd == NULL)
  8161. return rc;
  8162. switch (ctx_cmd) {
  8163. case LPFC_CTX_LUN:
  8164. if ((lpfc_cmd->rdata->pnode) &&
  8165. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8166. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8167. rc = 0;
  8168. break;
  8169. case LPFC_CTX_TGT:
  8170. if ((lpfc_cmd->rdata->pnode) &&
  8171. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8172. rc = 0;
  8173. break;
  8174. case LPFC_CTX_HOST:
  8175. rc = 0;
  8176. break;
  8177. default:
  8178. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8179. __func__, ctx_cmd);
  8180. break;
  8181. }
  8182. return rc;
  8183. }
  8184. /**
  8185. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8186. * @vport: Pointer to virtual port.
  8187. * @tgt_id: SCSI ID of the target.
  8188. * @lun_id: LUN ID of the scsi device.
  8189. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8190. *
  8191. * This function returns number of FCP commands pending for the vport.
  8192. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8193. * commands pending on the vport associated with SCSI device specified
  8194. * by tgt_id and lun_id parameters.
  8195. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8196. * commands pending on the vport associated with SCSI target specified
  8197. * by tgt_id parameter.
  8198. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8199. * commands pending on the vport.
  8200. * This function returns the number of iocbs which satisfy the filter.
  8201. * This function is called without any lock held.
  8202. **/
  8203. int
  8204. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8205. lpfc_ctx_cmd ctx_cmd)
  8206. {
  8207. struct lpfc_hba *phba = vport->phba;
  8208. struct lpfc_iocbq *iocbq;
  8209. int sum, i;
  8210. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8211. iocbq = phba->sli.iocbq_lookup[i];
  8212. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8213. ctx_cmd) == 0)
  8214. sum++;
  8215. }
  8216. return sum;
  8217. }
  8218. /**
  8219. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8220. * @phba: Pointer to HBA context object
  8221. * @cmdiocb: Pointer to command iocb object.
  8222. * @rspiocb: Pointer to response iocb object.
  8223. *
  8224. * This function is called when an aborted FCP iocb completes. This
  8225. * function is called by the ring event handler with no lock held.
  8226. * This function frees the iocb.
  8227. **/
  8228. void
  8229. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8230. struct lpfc_iocbq *rspiocb)
  8231. {
  8232. lpfc_sli_release_iocbq(phba, cmdiocb);
  8233. return;
  8234. }
  8235. /**
  8236. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8237. * @vport: Pointer to virtual port.
  8238. * @pring: Pointer to driver SLI ring object.
  8239. * @tgt_id: SCSI ID of the target.
  8240. * @lun_id: LUN ID of the scsi device.
  8241. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8242. *
  8243. * This function sends an abort command for every SCSI command
  8244. * associated with the given virtual port pending on the ring
  8245. * filtered by lpfc_sli_validate_fcp_iocb function.
  8246. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8247. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8248. * parameters
  8249. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8250. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8251. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8252. * FCP iocbs associated with virtual port.
  8253. * This function returns number of iocbs it failed to abort.
  8254. * This function is called with no locks held.
  8255. **/
  8256. int
  8257. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8258. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8259. {
  8260. struct lpfc_hba *phba = vport->phba;
  8261. struct lpfc_iocbq *iocbq;
  8262. struct lpfc_iocbq *abtsiocb;
  8263. IOCB_t *cmd = NULL;
  8264. int errcnt = 0, ret_val = 0;
  8265. int i;
  8266. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8267. iocbq = phba->sli.iocbq_lookup[i];
  8268. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8269. abort_cmd) != 0)
  8270. continue;
  8271. /* issue ABTS for this IOCB based on iotag */
  8272. abtsiocb = lpfc_sli_get_iocbq(phba);
  8273. if (abtsiocb == NULL) {
  8274. errcnt++;
  8275. continue;
  8276. }
  8277. cmd = &iocbq->iocb;
  8278. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8279. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8280. if (phba->sli_rev == LPFC_SLI_REV4)
  8281. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8282. else
  8283. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8284. abtsiocb->iocb.ulpLe = 1;
  8285. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8286. abtsiocb->vport = phba->pport;
  8287. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8288. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8289. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8290. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8291. if (lpfc_is_link_up(phba))
  8292. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8293. else
  8294. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8295. /* Setup callback routine and issue the command. */
  8296. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8297. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8298. abtsiocb, 0);
  8299. if (ret_val == IOCB_ERROR) {
  8300. lpfc_sli_release_iocbq(phba, abtsiocb);
  8301. errcnt++;
  8302. continue;
  8303. }
  8304. }
  8305. return errcnt;
  8306. }
  8307. /**
  8308. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8309. * @phba: Pointer to HBA context object.
  8310. * @cmdiocbq: Pointer to command iocb.
  8311. * @rspiocbq: Pointer to response iocb.
  8312. *
  8313. * This function is the completion handler for iocbs issued using
  8314. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8315. * ring event handler function without any lock held. This function
  8316. * can be called from both worker thread context and interrupt
  8317. * context. This function also can be called from other thread which
  8318. * cleans up the SLI layer objects.
  8319. * This function copy the contents of the response iocb to the
  8320. * response iocb memory object provided by the caller of
  8321. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8322. * sleeps for the iocb completion.
  8323. **/
  8324. static void
  8325. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8326. struct lpfc_iocbq *cmdiocbq,
  8327. struct lpfc_iocbq *rspiocbq)
  8328. {
  8329. wait_queue_head_t *pdone_q;
  8330. unsigned long iflags;
  8331. struct lpfc_scsi_buf *lpfc_cmd;
  8332. spin_lock_irqsave(&phba->hbalock, iflags);
  8333. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8334. if (cmdiocbq->context2 && rspiocbq)
  8335. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8336. &rspiocbq->iocb, sizeof(IOCB_t));
  8337. /* Set the exchange busy flag for task management commands */
  8338. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8339. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8340. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8341. cur_iocbq);
  8342. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8343. }
  8344. pdone_q = cmdiocbq->context_un.wait_queue;
  8345. if (pdone_q)
  8346. wake_up(pdone_q);
  8347. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8348. return;
  8349. }
  8350. /**
  8351. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8352. * @phba: Pointer to HBA context object..
  8353. * @piocbq: Pointer to command iocb.
  8354. * @flag: Flag to test.
  8355. *
  8356. * This routine grabs the hbalock and then test the iocb_flag to
  8357. * see if the passed in flag is set.
  8358. * Returns:
  8359. * 1 if flag is set.
  8360. * 0 if flag is not set.
  8361. **/
  8362. static int
  8363. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8364. struct lpfc_iocbq *piocbq, uint32_t flag)
  8365. {
  8366. unsigned long iflags;
  8367. int ret;
  8368. spin_lock_irqsave(&phba->hbalock, iflags);
  8369. ret = piocbq->iocb_flag & flag;
  8370. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8371. return ret;
  8372. }
  8373. /**
  8374. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8375. * @phba: Pointer to HBA context object..
  8376. * @pring: Pointer to sli ring.
  8377. * @piocb: Pointer to command iocb.
  8378. * @prspiocbq: Pointer to response iocb.
  8379. * @timeout: Timeout in number of seconds.
  8380. *
  8381. * This function issues the iocb to firmware and waits for the
  8382. * iocb to complete. If the iocb command is not
  8383. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8384. * Caller should not free the iocb resources if this function
  8385. * returns IOCB_TIMEDOUT.
  8386. * The function waits for the iocb completion using an
  8387. * non-interruptible wait.
  8388. * This function will sleep while waiting for iocb completion.
  8389. * So, this function should not be called from any context which
  8390. * does not allow sleeping. Due to the same reason, this function
  8391. * cannot be called with interrupt disabled.
  8392. * This function assumes that the iocb completions occur while
  8393. * this function sleep. So, this function cannot be called from
  8394. * the thread which process iocb completion for this ring.
  8395. * This function clears the iocb_flag of the iocb object before
  8396. * issuing the iocb and the iocb completion handler sets this
  8397. * flag and wakes this thread when the iocb completes.
  8398. * The contents of the response iocb will be copied to prspiocbq
  8399. * by the completion handler when the command completes.
  8400. * This function returns IOCB_SUCCESS when success.
  8401. * This function is called with no lock held.
  8402. **/
  8403. int
  8404. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8405. uint32_t ring_number,
  8406. struct lpfc_iocbq *piocb,
  8407. struct lpfc_iocbq *prspiocbq,
  8408. uint32_t timeout)
  8409. {
  8410. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8411. long timeleft, timeout_req = 0;
  8412. int retval = IOCB_SUCCESS;
  8413. uint32_t creg_val;
  8414. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8415. /*
  8416. * If the caller has provided a response iocbq buffer, then context2
  8417. * is NULL or its an error.
  8418. */
  8419. if (prspiocbq) {
  8420. if (piocb->context2)
  8421. return IOCB_ERROR;
  8422. piocb->context2 = prspiocbq;
  8423. }
  8424. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8425. piocb->context_un.wait_queue = &done_q;
  8426. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8427. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8428. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8429. return IOCB_ERROR;
  8430. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8431. writel(creg_val, phba->HCregaddr);
  8432. readl(phba->HCregaddr); /* flush */
  8433. }
  8434. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8435. SLI_IOCB_RET_IOCB);
  8436. if (retval == IOCB_SUCCESS) {
  8437. timeout_req = timeout * HZ;
  8438. timeleft = wait_event_timeout(done_q,
  8439. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8440. timeout_req);
  8441. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8442. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8443. "0331 IOCB wake signaled\n");
  8444. } else if (timeleft == 0) {
  8445. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8446. "0338 IOCB wait timeout error - no "
  8447. "wake response Data x%x\n", timeout);
  8448. retval = IOCB_TIMEDOUT;
  8449. } else {
  8450. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8451. "0330 IOCB wake NOT set, "
  8452. "Data x%x x%lx\n",
  8453. timeout, (timeleft / jiffies));
  8454. retval = IOCB_TIMEDOUT;
  8455. }
  8456. } else if (retval == IOCB_BUSY) {
  8457. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8458. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8459. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8460. return retval;
  8461. } else {
  8462. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8463. "0332 IOCB wait issue failed, Data x%x\n",
  8464. retval);
  8465. retval = IOCB_ERROR;
  8466. }
  8467. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8468. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8469. return IOCB_ERROR;
  8470. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8471. writel(creg_val, phba->HCregaddr);
  8472. readl(phba->HCregaddr); /* flush */
  8473. }
  8474. if (prspiocbq)
  8475. piocb->context2 = NULL;
  8476. piocb->context_un.wait_queue = NULL;
  8477. piocb->iocb_cmpl = NULL;
  8478. return retval;
  8479. }
  8480. /**
  8481. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  8482. * @phba: Pointer to HBA context object.
  8483. * @pmboxq: Pointer to driver mailbox object.
  8484. * @timeout: Timeout in number of seconds.
  8485. *
  8486. * This function issues the mailbox to firmware and waits for the
  8487. * mailbox command to complete. If the mailbox command is not
  8488. * completed within timeout seconds, it returns MBX_TIMEOUT.
  8489. * The function waits for the mailbox completion using an
  8490. * interruptible wait. If the thread is woken up due to a
  8491. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  8492. * should not free the mailbox resources, if this function returns
  8493. * MBX_TIMEOUT.
  8494. * This function will sleep while waiting for mailbox completion.
  8495. * So, this function should not be called from any context which
  8496. * does not allow sleeping. Due to the same reason, this function
  8497. * cannot be called with interrupt disabled.
  8498. * This function assumes that the mailbox completion occurs while
  8499. * this function sleep. So, this function cannot be called from
  8500. * the worker thread which processes mailbox completion.
  8501. * This function is called in the context of HBA management
  8502. * applications.
  8503. * This function returns MBX_SUCCESS when successful.
  8504. * This function is called with no lock held.
  8505. **/
  8506. int
  8507. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  8508. uint32_t timeout)
  8509. {
  8510. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8511. int retval;
  8512. unsigned long flag;
  8513. /* The caller must leave context1 empty. */
  8514. if (pmboxq->context1)
  8515. return MBX_NOT_FINISHED;
  8516. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  8517. /* setup wake call as IOCB callback */
  8518. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  8519. /* setup context field to pass wait_queue pointer to wake function */
  8520. pmboxq->context1 = &done_q;
  8521. /* now issue the command */
  8522. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  8523. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  8524. wait_event_interruptible_timeout(done_q,
  8525. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  8526. timeout * HZ);
  8527. spin_lock_irqsave(&phba->hbalock, flag);
  8528. pmboxq->context1 = NULL;
  8529. /*
  8530. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  8531. * else do not free the resources.
  8532. */
  8533. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  8534. retval = MBX_SUCCESS;
  8535. lpfc_sli4_swap_str(phba, pmboxq);
  8536. } else {
  8537. retval = MBX_TIMEOUT;
  8538. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  8539. }
  8540. spin_unlock_irqrestore(&phba->hbalock, flag);
  8541. }
  8542. return retval;
  8543. }
  8544. /**
  8545. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  8546. * @phba: Pointer to HBA context.
  8547. *
  8548. * This function is called to shutdown the driver's mailbox sub-system.
  8549. * It first marks the mailbox sub-system is in a block state to prevent
  8550. * the asynchronous mailbox command from issued off the pending mailbox
  8551. * command queue. If the mailbox command sub-system shutdown is due to
  8552. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  8553. * the mailbox sub-system flush routine to forcefully bring down the
  8554. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  8555. * as with offline or HBA function reset), this routine will wait for the
  8556. * outstanding mailbox command to complete before invoking the mailbox
  8557. * sub-system flush routine to gracefully bring down mailbox sub-system.
  8558. **/
  8559. void
  8560. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  8561. {
  8562. struct lpfc_sli *psli = &phba->sli;
  8563. uint8_t actcmd = MBX_HEARTBEAT;
  8564. unsigned long timeout;
  8565. spin_lock_irq(&phba->hbalock);
  8566. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  8567. spin_unlock_irq(&phba->hbalock);
  8568. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  8569. spin_lock_irq(&phba->hbalock);
  8570. if (phba->sli.mbox_active)
  8571. actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
  8572. spin_unlock_irq(&phba->hbalock);
  8573. /* Determine how long we might wait for the active mailbox
  8574. * command to be gracefully completed by firmware.
  8575. */
  8576. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
  8577. 1000) + jiffies;
  8578. while (phba->sli.mbox_active) {
  8579. /* Check active mailbox complete status every 2ms */
  8580. msleep(2);
  8581. if (time_after(jiffies, timeout))
  8582. /* Timeout, let the mailbox flush routine to
  8583. * forcefully release active mailbox command
  8584. */
  8585. break;
  8586. }
  8587. }
  8588. lpfc_sli_mbox_sys_flush(phba);
  8589. }
  8590. /**
  8591. * lpfc_sli_eratt_read - read sli-3 error attention events
  8592. * @phba: Pointer to HBA context.
  8593. *
  8594. * This function is called to read the SLI3 device error attention registers
  8595. * for possible error attention events. The caller must hold the hostlock
  8596. * with spin_lock_irq().
  8597. *
  8598. * This function returns 1 when there is Error Attention in the Host Attention
  8599. * Register and returns 0 otherwise.
  8600. **/
  8601. static int
  8602. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  8603. {
  8604. uint32_t ha_copy;
  8605. /* Read chip Host Attention (HA) register */
  8606. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  8607. goto unplug_err;
  8608. if (ha_copy & HA_ERATT) {
  8609. /* Read host status register to retrieve error event */
  8610. if (lpfc_sli_read_hs(phba))
  8611. goto unplug_err;
  8612. /* Check if there is a deferred error condition is active */
  8613. if ((HS_FFER1 & phba->work_hs) &&
  8614. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  8615. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  8616. phba->hba_flag |= DEFER_ERATT;
  8617. /* Clear all interrupt enable conditions */
  8618. writel(0, phba->HCregaddr);
  8619. readl(phba->HCregaddr);
  8620. }
  8621. /* Set the driver HA work bitmap */
  8622. phba->work_ha |= HA_ERATT;
  8623. /* Indicate polling handles this ERATT */
  8624. phba->hba_flag |= HBA_ERATT_HANDLED;
  8625. return 1;
  8626. }
  8627. return 0;
  8628. unplug_err:
  8629. /* Set the driver HS work bitmap */
  8630. phba->work_hs |= UNPLUG_ERR;
  8631. /* Set the driver HA work bitmap */
  8632. phba->work_ha |= HA_ERATT;
  8633. /* Indicate polling handles this ERATT */
  8634. phba->hba_flag |= HBA_ERATT_HANDLED;
  8635. return 1;
  8636. }
  8637. /**
  8638. * lpfc_sli4_eratt_read - read sli-4 error attention events
  8639. * @phba: Pointer to HBA context.
  8640. *
  8641. * This function is called to read the SLI4 device error attention registers
  8642. * for possible error attention events. The caller must hold the hostlock
  8643. * with spin_lock_irq().
  8644. *
  8645. * This function returns 1 when there is Error Attention in the Host Attention
  8646. * Register and returns 0 otherwise.
  8647. **/
  8648. static int
  8649. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  8650. {
  8651. uint32_t uerr_sta_hi, uerr_sta_lo;
  8652. uint32_t if_type, portsmphr;
  8653. struct lpfc_register portstat_reg;
  8654. /*
  8655. * For now, use the SLI4 device internal unrecoverable error
  8656. * registers for error attention. This can be changed later.
  8657. */
  8658. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  8659. switch (if_type) {
  8660. case LPFC_SLI_INTF_IF_TYPE_0:
  8661. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  8662. &uerr_sta_lo) ||
  8663. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  8664. &uerr_sta_hi)) {
  8665. phba->work_hs |= UNPLUG_ERR;
  8666. phba->work_ha |= HA_ERATT;
  8667. phba->hba_flag |= HBA_ERATT_HANDLED;
  8668. return 1;
  8669. }
  8670. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  8671. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  8672. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8673. "1423 HBA Unrecoverable error: "
  8674. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  8675. "ue_mask_lo_reg=0x%x, "
  8676. "ue_mask_hi_reg=0x%x\n",
  8677. uerr_sta_lo, uerr_sta_hi,
  8678. phba->sli4_hba.ue_mask_lo,
  8679. phba->sli4_hba.ue_mask_hi);
  8680. phba->work_status[0] = uerr_sta_lo;
  8681. phba->work_status[1] = uerr_sta_hi;
  8682. phba->work_ha |= HA_ERATT;
  8683. phba->hba_flag |= HBA_ERATT_HANDLED;
  8684. return 1;
  8685. }
  8686. break;
  8687. case LPFC_SLI_INTF_IF_TYPE_2:
  8688. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  8689. &portstat_reg.word0) ||
  8690. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  8691. &portsmphr)){
  8692. phba->work_hs |= UNPLUG_ERR;
  8693. phba->work_ha |= HA_ERATT;
  8694. phba->hba_flag |= HBA_ERATT_HANDLED;
  8695. return 1;
  8696. }
  8697. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  8698. phba->work_status[0] =
  8699. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  8700. phba->work_status[1] =
  8701. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  8702. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8703. "2885 Port Error Detected: "
  8704. "port status reg 0x%x, "
  8705. "port smphr reg 0x%x, "
  8706. "error 1=0x%x, error 2=0x%x\n",
  8707. portstat_reg.word0,
  8708. portsmphr,
  8709. phba->work_status[0],
  8710. phba->work_status[1]);
  8711. phba->work_ha |= HA_ERATT;
  8712. phba->hba_flag |= HBA_ERATT_HANDLED;
  8713. return 1;
  8714. }
  8715. break;
  8716. case LPFC_SLI_INTF_IF_TYPE_1:
  8717. default:
  8718. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8719. "2886 HBA Error Attention on unsupported "
  8720. "if type %d.", if_type);
  8721. return 1;
  8722. }
  8723. return 0;
  8724. }
  8725. /**
  8726. * lpfc_sli_check_eratt - check error attention events
  8727. * @phba: Pointer to HBA context.
  8728. *
  8729. * This function is called from timer soft interrupt context to check HBA's
  8730. * error attention register bit for error attention events.
  8731. *
  8732. * This function returns 1 when there is Error Attention in the Host Attention
  8733. * Register and returns 0 otherwise.
  8734. **/
  8735. int
  8736. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  8737. {
  8738. uint32_t ha_copy;
  8739. /* If somebody is waiting to handle an eratt, don't process it
  8740. * here. The brdkill function will do this.
  8741. */
  8742. if (phba->link_flag & LS_IGNORE_ERATT)
  8743. return 0;
  8744. /* Check if interrupt handler handles this ERATT */
  8745. spin_lock_irq(&phba->hbalock);
  8746. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  8747. /* Interrupt handler has handled ERATT */
  8748. spin_unlock_irq(&phba->hbalock);
  8749. return 0;
  8750. }
  8751. /*
  8752. * If there is deferred error attention, do not check for error
  8753. * attention
  8754. */
  8755. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  8756. spin_unlock_irq(&phba->hbalock);
  8757. return 0;
  8758. }
  8759. /* If PCI channel is offline, don't process it */
  8760. if (unlikely(pci_channel_offline(phba->pcidev))) {
  8761. spin_unlock_irq(&phba->hbalock);
  8762. return 0;
  8763. }
  8764. switch (phba->sli_rev) {
  8765. case LPFC_SLI_REV2:
  8766. case LPFC_SLI_REV3:
  8767. /* Read chip Host Attention (HA) register */
  8768. ha_copy = lpfc_sli_eratt_read(phba);
  8769. break;
  8770. case LPFC_SLI_REV4:
  8771. /* Read device Uncoverable Error (UERR) registers */
  8772. ha_copy = lpfc_sli4_eratt_read(phba);
  8773. break;
  8774. default:
  8775. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8776. "0299 Invalid SLI revision (%d)\n",
  8777. phba->sli_rev);
  8778. ha_copy = 0;
  8779. break;
  8780. }
  8781. spin_unlock_irq(&phba->hbalock);
  8782. return ha_copy;
  8783. }
  8784. /**
  8785. * lpfc_intr_state_check - Check device state for interrupt handling
  8786. * @phba: Pointer to HBA context.
  8787. *
  8788. * This inline routine checks whether a device or its PCI slot is in a state
  8789. * that the interrupt should be handled.
  8790. *
  8791. * This function returns 0 if the device or the PCI slot is in a state that
  8792. * interrupt should be handled, otherwise -EIO.
  8793. */
  8794. static inline int
  8795. lpfc_intr_state_check(struct lpfc_hba *phba)
  8796. {
  8797. /* If the pci channel is offline, ignore all the interrupts */
  8798. if (unlikely(pci_channel_offline(phba->pcidev)))
  8799. return -EIO;
  8800. /* Update device level interrupt statistics */
  8801. phba->sli.slistat.sli_intr++;
  8802. /* Ignore all interrupts during initialization. */
  8803. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  8804. return -EIO;
  8805. return 0;
  8806. }
  8807. /**
  8808. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  8809. * @irq: Interrupt number.
  8810. * @dev_id: The device context pointer.
  8811. *
  8812. * This function is directly called from the PCI layer as an interrupt
  8813. * service routine when device with SLI-3 interface spec is enabled with
  8814. * MSI-X multi-message interrupt mode and there are slow-path events in
  8815. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  8816. * interrupt mode, this function is called as part of the device-level
  8817. * interrupt handler. When the PCI slot is in error recovery or the HBA
  8818. * is undergoing initialization, the interrupt handler will not process
  8819. * the interrupt. The link attention and ELS ring attention events are
  8820. * handled by the worker thread. The interrupt handler signals the worker
  8821. * thread and returns for these events. This function is called without
  8822. * any lock held. It gets the hbalock to access and update SLI data
  8823. * structures.
  8824. *
  8825. * This function returns IRQ_HANDLED when interrupt is handled else it
  8826. * returns IRQ_NONE.
  8827. **/
  8828. irqreturn_t
  8829. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  8830. {
  8831. struct lpfc_hba *phba;
  8832. uint32_t ha_copy, hc_copy;
  8833. uint32_t work_ha_copy;
  8834. unsigned long status;
  8835. unsigned long iflag;
  8836. uint32_t control;
  8837. MAILBOX_t *mbox, *pmbox;
  8838. struct lpfc_vport *vport;
  8839. struct lpfc_nodelist *ndlp;
  8840. struct lpfc_dmabuf *mp;
  8841. LPFC_MBOXQ_t *pmb;
  8842. int rc;
  8843. /*
  8844. * Get the driver's phba structure from the dev_id and
  8845. * assume the HBA is not interrupting.
  8846. */
  8847. phba = (struct lpfc_hba *)dev_id;
  8848. if (unlikely(!phba))
  8849. return IRQ_NONE;
  8850. /*
  8851. * Stuff needs to be attented to when this function is invoked as an
  8852. * individual interrupt handler in MSI-X multi-message interrupt mode
  8853. */
  8854. if (phba->intr_type == MSIX) {
  8855. /* Check device state for handling interrupt */
  8856. if (lpfc_intr_state_check(phba))
  8857. return IRQ_NONE;
  8858. /* Need to read HA REG for slow-path events */
  8859. spin_lock_irqsave(&phba->hbalock, iflag);
  8860. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  8861. goto unplug_error;
  8862. /* If somebody is waiting to handle an eratt don't process it
  8863. * here. The brdkill function will do this.
  8864. */
  8865. if (phba->link_flag & LS_IGNORE_ERATT)
  8866. ha_copy &= ~HA_ERATT;
  8867. /* Check the need for handling ERATT in interrupt handler */
  8868. if (ha_copy & HA_ERATT) {
  8869. if (phba->hba_flag & HBA_ERATT_HANDLED)
  8870. /* ERATT polling has handled ERATT */
  8871. ha_copy &= ~HA_ERATT;
  8872. else
  8873. /* Indicate interrupt handler handles ERATT */
  8874. phba->hba_flag |= HBA_ERATT_HANDLED;
  8875. }
  8876. /*
  8877. * If there is deferred error attention, do not check for any
  8878. * interrupt.
  8879. */
  8880. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  8881. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8882. return IRQ_NONE;
  8883. }
  8884. /* Clear up only attention source related to slow-path */
  8885. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  8886. goto unplug_error;
  8887. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  8888. HC_LAINT_ENA | HC_ERINT_ENA),
  8889. phba->HCregaddr);
  8890. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  8891. phba->HAregaddr);
  8892. writel(hc_copy, phba->HCregaddr);
  8893. readl(phba->HAregaddr); /* flush */
  8894. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8895. } else
  8896. ha_copy = phba->ha_copy;
  8897. work_ha_copy = ha_copy & phba->work_ha_mask;
  8898. if (work_ha_copy) {
  8899. if (work_ha_copy & HA_LATT) {
  8900. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  8901. /*
  8902. * Turn off Link Attention interrupts
  8903. * until CLEAR_LA done
  8904. */
  8905. spin_lock_irqsave(&phba->hbalock, iflag);
  8906. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  8907. if (lpfc_readl(phba->HCregaddr, &control))
  8908. goto unplug_error;
  8909. control &= ~HC_LAINT_ENA;
  8910. writel(control, phba->HCregaddr);
  8911. readl(phba->HCregaddr); /* flush */
  8912. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8913. }
  8914. else
  8915. work_ha_copy &= ~HA_LATT;
  8916. }
  8917. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  8918. /*
  8919. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  8920. * the only slow ring.
  8921. */
  8922. status = (work_ha_copy &
  8923. (HA_RXMASK << (4*LPFC_ELS_RING)));
  8924. status >>= (4*LPFC_ELS_RING);
  8925. if (status & HA_RXMASK) {
  8926. spin_lock_irqsave(&phba->hbalock, iflag);
  8927. if (lpfc_readl(phba->HCregaddr, &control))
  8928. goto unplug_error;
  8929. lpfc_debugfs_slow_ring_trc(phba,
  8930. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  8931. control, status,
  8932. (uint32_t)phba->sli.slistat.sli_intr);
  8933. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  8934. lpfc_debugfs_slow_ring_trc(phba,
  8935. "ISR Disable ring:"
  8936. "pwork:x%x hawork:x%x wait:x%x",
  8937. phba->work_ha, work_ha_copy,
  8938. (uint32_t)((unsigned long)
  8939. &phba->work_waitq));
  8940. control &=
  8941. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  8942. writel(control, phba->HCregaddr);
  8943. readl(phba->HCregaddr); /* flush */
  8944. }
  8945. else {
  8946. lpfc_debugfs_slow_ring_trc(phba,
  8947. "ISR slow ring: pwork:"
  8948. "x%x hawork:x%x wait:x%x",
  8949. phba->work_ha, work_ha_copy,
  8950. (uint32_t)((unsigned long)
  8951. &phba->work_waitq));
  8952. }
  8953. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8954. }
  8955. }
  8956. spin_lock_irqsave(&phba->hbalock, iflag);
  8957. if (work_ha_copy & HA_ERATT) {
  8958. if (lpfc_sli_read_hs(phba))
  8959. goto unplug_error;
  8960. /*
  8961. * Check if there is a deferred error condition
  8962. * is active
  8963. */
  8964. if ((HS_FFER1 & phba->work_hs) &&
  8965. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  8966. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  8967. phba->work_hs)) {
  8968. phba->hba_flag |= DEFER_ERATT;
  8969. /* Clear all interrupt enable conditions */
  8970. writel(0, phba->HCregaddr);
  8971. readl(phba->HCregaddr);
  8972. }
  8973. }
  8974. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  8975. pmb = phba->sli.mbox_active;
  8976. pmbox = &pmb->u.mb;
  8977. mbox = phba->mbox;
  8978. vport = pmb->vport;
  8979. /* First check out the status word */
  8980. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  8981. if (pmbox->mbxOwner != OWN_HOST) {
  8982. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8983. /*
  8984. * Stray Mailbox Interrupt, mbxCommand <cmd>
  8985. * mbxStatus <status>
  8986. */
  8987. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  8988. LOG_SLI,
  8989. "(%d):0304 Stray Mailbox "
  8990. "Interrupt mbxCommand x%x "
  8991. "mbxStatus x%x\n",
  8992. (vport ? vport->vpi : 0),
  8993. pmbox->mbxCommand,
  8994. pmbox->mbxStatus);
  8995. /* clear mailbox attention bit */
  8996. work_ha_copy &= ~HA_MBATT;
  8997. } else {
  8998. phba->sli.mbox_active = NULL;
  8999. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9000. phba->last_completion_time = jiffies;
  9001. del_timer(&phba->sli.mbox_tmo);
  9002. if (pmb->mbox_cmpl) {
  9003. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9004. MAILBOX_CMD_SIZE);
  9005. if (pmb->out_ext_byte_len &&
  9006. pmb->context2)
  9007. lpfc_sli_pcimem_bcopy(
  9008. phba->mbox_ext,
  9009. pmb->context2,
  9010. pmb->out_ext_byte_len);
  9011. }
  9012. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9013. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9014. lpfc_debugfs_disc_trc(vport,
  9015. LPFC_DISC_TRC_MBOX_VPORT,
  9016. "MBOX dflt rpi: : "
  9017. "status:x%x rpi:x%x",
  9018. (uint32_t)pmbox->mbxStatus,
  9019. pmbox->un.varWords[0], 0);
  9020. if (!pmbox->mbxStatus) {
  9021. mp = (struct lpfc_dmabuf *)
  9022. (pmb->context1);
  9023. ndlp = (struct lpfc_nodelist *)
  9024. pmb->context2;
  9025. /* Reg_LOGIN of dflt RPI was
  9026. * successful. new lets get
  9027. * rid of the RPI using the
  9028. * same mbox buffer.
  9029. */
  9030. lpfc_unreg_login(phba,
  9031. vport->vpi,
  9032. pmbox->un.varWords[0],
  9033. pmb);
  9034. pmb->mbox_cmpl =
  9035. lpfc_mbx_cmpl_dflt_rpi;
  9036. pmb->context1 = mp;
  9037. pmb->context2 = ndlp;
  9038. pmb->vport = vport;
  9039. rc = lpfc_sli_issue_mbox(phba,
  9040. pmb,
  9041. MBX_NOWAIT);
  9042. if (rc != MBX_BUSY)
  9043. lpfc_printf_log(phba,
  9044. KERN_ERR,
  9045. LOG_MBOX | LOG_SLI,
  9046. "0350 rc should have"
  9047. "been MBX_BUSY\n");
  9048. if (rc != MBX_NOT_FINISHED)
  9049. goto send_current_mbox;
  9050. }
  9051. }
  9052. spin_lock_irqsave(
  9053. &phba->pport->work_port_lock,
  9054. iflag);
  9055. phba->pport->work_port_events &=
  9056. ~WORKER_MBOX_TMO;
  9057. spin_unlock_irqrestore(
  9058. &phba->pport->work_port_lock,
  9059. iflag);
  9060. lpfc_mbox_cmpl_put(phba, pmb);
  9061. }
  9062. } else
  9063. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9064. if ((work_ha_copy & HA_MBATT) &&
  9065. (phba->sli.mbox_active == NULL)) {
  9066. send_current_mbox:
  9067. /* Process next mailbox command if there is one */
  9068. do {
  9069. rc = lpfc_sli_issue_mbox(phba, NULL,
  9070. MBX_NOWAIT);
  9071. } while (rc == MBX_NOT_FINISHED);
  9072. if (rc != MBX_SUCCESS)
  9073. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9074. LOG_SLI, "0349 rc should be "
  9075. "MBX_SUCCESS\n");
  9076. }
  9077. spin_lock_irqsave(&phba->hbalock, iflag);
  9078. phba->work_ha |= work_ha_copy;
  9079. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9080. lpfc_worker_wake_up(phba);
  9081. }
  9082. return IRQ_HANDLED;
  9083. unplug_error:
  9084. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9085. return IRQ_HANDLED;
  9086. } /* lpfc_sli_sp_intr_handler */
  9087. /**
  9088. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9089. * @irq: Interrupt number.
  9090. * @dev_id: The device context pointer.
  9091. *
  9092. * This function is directly called from the PCI layer as an interrupt
  9093. * service routine when device with SLI-3 interface spec is enabled with
  9094. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9095. * ring event in the HBA. However, when the device is enabled with either
  9096. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9097. * device-level interrupt handler. When the PCI slot is in error recovery
  9098. * or the HBA is undergoing initialization, the interrupt handler will not
  9099. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9100. * the intrrupt context. This function is called without any lock held.
  9101. * It gets the hbalock to access and update SLI data structures.
  9102. *
  9103. * This function returns IRQ_HANDLED when interrupt is handled else it
  9104. * returns IRQ_NONE.
  9105. **/
  9106. irqreturn_t
  9107. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9108. {
  9109. struct lpfc_hba *phba;
  9110. uint32_t ha_copy;
  9111. unsigned long status;
  9112. unsigned long iflag;
  9113. /* Get the driver's phba structure from the dev_id and
  9114. * assume the HBA is not interrupting.
  9115. */
  9116. phba = (struct lpfc_hba *) dev_id;
  9117. if (unlikely(!phba))
  9118. return IRQ_NONE;
  9119. /*
  9120. * Stuff needs to be attented to when this function is invoked as an
  9121. * individual interrupt handler in MSI-X multi-message interrupt mode
  9122. */
  9123. if (phba->intr_type == MSIX) {
  9124. /* Check device state for handling interrupt */
  9125. if (lpfc_intr_state_check(phba))
  9126. return IRQ_NONE;
  9127. /* Need to read HA REG for FCP ring and other ring events */
  9128. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9129. return IRQ_HANDLED;
  9130. /* Clear up only attention source related to fast-path */
  9131. spin_lock_irqsave(&phba->hbalock, iflag);
  9132. /*
  9133. * If there is deferred error attention, do not check for
  9134. * any interrupt.
  9135. */
  9136. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9137. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9138. return IRQ_NONE;
  9139. }
  9140. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9141. phba->HAregaddr);
  9142. readl(phba->HAregaddr); /* flush */
  9143. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9144. } else
  9145. ha_copy = phba->ha_copy;
  9146. /*
  9147. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9148. */
  9149. ha_copy &= ~(phba->work_ha_mask);
  9150. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9151. status >>= (4*LPFC_FCP_RING);
  9152. if (status & HA_RXMASK)
  9153. lpfc_sli_handle_fast_ring_event(phba,
  9154. &phba->sli.ring[LPFC_FCP_RING],
  9155. status);
  9156. if (phba->cfg_multi_ring_support == 2) {
  9157. /*
  9158. * Process all events on extra ring. Take the optimized path
  9159. * for extra ring IO.
  9160. */
  9161. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9162. status >>= (4*LPFC_EXTRA_RING);
  9163. if (status & HA_RXMASK) {
  9164. lpfc_sli_handle_fast_ring_event(phba,
  9165. &phba->sli.ring[LPFC_EXTRA_RING],
  9166. status);
  9167. }
  9168. }
  9169. return IRQ_HANDLED;
  9170. } /* lpfc_sli_fp_intr_handler */
  9171. /**
  9172. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9173. * @irq: Interrupt number.
  9174. * @dev_id: The device context pointer.
  9175. *
  9176. * This function is the HBA device-level interrupt handler to device with
  9177. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9178. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9179. * requires driver attention. This function invokes the slow-path interrupt
  9180. * attention handling function and fast-path interrupt attention handling
  9181. * function in turn to process the relevant HBA attention events. This
  9182. * function is called without any lock held. It gets the hbalock to access
  9183. * and update SLI data structures.
  9184. *
  9185. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9186. * returns IRQ_NONE.
  9187. **/
  9188. irqreturn_t
  9189. lpfc_sli_intr_handler(int irq, void *dev_id)
  9190. {
  9191. struct lpfc_hba *phba;
  9192. irqreturn_t sp_irq_rc, fp_irq_rc;
  9193. unsigned long status1, status2;
  9194. uint32_t hc_copy;
  9195. /*
  9196. * Get the driver's phba structure from the dev_id and
  9197. * assume the HBA is not interrupting.
  9198. */
  9199. phba = (struct lpfc_hba *) dev_id;
  9200. if (unlikely(!phba))
  9201. return IRQ_NONE;
  9202. /* Check device state for handling interrupt */
  9203. if (lpfc_intr_state_check(phba))
  9204. return IRQ_NONE;
  9205. spin_lock(&phba->hbalock);
  9206. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9207. spin_unlock(&phba->hbalock);
  9208. return IRQ_HANDLED;
  9209. }
  9210. if (unlikely(!phba->ha_copy)) {
  9211. spin_unlock(&phba->hbalock);
  9212. return IRQ_NONE;
  9213. } else if (phba->ha_copy & HA_ERATT) {
  9214. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9215. /* ERATT polling has handled ERATT */
  9216. phba->ha_copy &= ~HA_ERATT;
  9217. else
  9218. /* Indicate interrupt handler handles ERATT */
  9219. phba->hba_flag |= HBA_ERATT_HANDLED;
  9220. }
  9221. /*
  9222. * If there is deferred error attention, do not check for any interrupt.
  9223. */
  9224. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9225. spin_unlock(&phba->hbalock);
  9226. return IRQ_NONE;
  9227. }
  9228. /* Clear attention sources except link and error attentions */
  9229. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9230. spin_unlock(&phba->hbalock);
  9231. return IRQ_HANDLED;
  9232. }
  9233. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9234. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9235. phba->HCregaddr);
  9236. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9237. writel(hc_copy, phba->HCregaddr);
  9238. readl(phba->HAregaddr); /* flush */
  9239. spin_unlock(&phba->hbalock);
  9240. /*
  9241. * Invokes slow-path host attention interrupt handling as appropriate.
  9242. */
  9243. /* status of events with mailbox and link attention */
  9244. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9245. /* status of events with ELS ring */
  9246. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9247. status2 >>= (4*LPFC_ELS_RING);
  9248. if (status1 || (status2 & HA_RXMASK))
  9249. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9250. else
  9251. sp_irq_rc = IRQ_NONE;
  9252. /*
  9253. * Invoke fast-path host attention interrupt handling as appropriate.
  9254. */
  9255. /* status of events with FCP ring */
  9256. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9257. status1 >>= (4*LPFC_FCP_RING);
  9258. /* status of events with extra ring */
  9259. if (phba->cfg_multi_ring_support == 2) {
  9260. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9261. status2 >>= (4*LPFC_EXTRA_RING);
  9262. } else
  9263. status2 = 0;
  9264. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9265. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9266. else
  9267. fp_irq_rc = IRQ_NONE;
  9268. /* Return device-level interrupt handling status */
  9269. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9270. } /* lpfc_sli_intr_handler */
  9271. /**
  9272. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9273. * @phba: pointer to lpfc hba data structure.
  9274. *
  9275. * This routine is invoked by the worker thread to process all the pending
  9276. * SLI4 FCP abort XRI events.
  9277. **/
  9278. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9279. {
  9280. struct lpfc_cq_event *cq_event;
  9281. /* First, declare the fcp xri abort event has been handled */
  9282. spin_lock_irq(&phba->hbalock);
  9283. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9284. spin_unlock_irq(&phba->hbalock);
  9285. /* Now, handle all the fcp xri abort events */
  9286. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9287. /* Get the first event from the head of the event queue */
  9288. spin_lock_irq(&phba->hbalock);
  9289. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9290. cq_event, struct lpfc_cq_event, list);
  9291. spin_unlock_irq(&phba->hbalock);
  9292. /* Notify aborted XRI for FCP work queue */
  9293. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9294. /* Free the event processed back to the free pool */
  9295. lpfc_sli4_cq_event_release(phba, cq_event);
  9296. }
  9297. }
  9298. /**
  9299. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9300. * @phba: pointer to lpfc hba data structure.
  9301. *
  9302. * This routine is invoked by the worker thread to process all the pending
  9303. * SLI4 els abort xri events.
  9304. **/
  9305. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9306. {
  9307. struct lpfc_cq_event *cq_event;
  9308. /* First, declare the els xri abort event has been handled */
  9309. spin_lock_irq(&phba->hbalock);
  9310. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9311. spin_unlock_irq(&phba->hbalock);
  9312. /* Now, handle all the els xri abort events */
  9313. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9314. /* Get the first event from the head of the event queue */
  9315. spin_lock_irq(&phba->hbalock);
  9316. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9317. cq_event, struct lpfc_cq_event, list);
  9318. spin_unlock_irq(&phba->hbalock);
  9319. /* Notify aborted XRI for ELS work queue */
  9320. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9321. /* Free the event processed back to the free pool */
  9322. lpfc_sli4_cq_event_release(phba, cq_event);
  9323. }
  9324. }
  9325. /**
  9326. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9327. * @phba: pointer to lpfc hba data structure
  9328. * @pIocbIn: pointer to the rspiocbq
  9329. * @pIocbOut: pointer to the cmdiocbq
  9330. * @wcqe: pointer to the complete wcqe
  9331. *
  9332. * This routine transfers the fields of a command iocbq to a response iocbq
  9333. * by copying all the IOCB fields from command iocbq and transferring the
  9334. * completion status information from the complete wcqe.
  9335. **/
  9336. static void
  9337. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9338. struct lpfc_iocbq *pIocbIn,
  9339. struct lpfc_iocbq *pIocbOut,
  9340. struct lpfc_wcqe_complete *wcqe)
  9341. {
  9342. unsigned long iflags;
  9343. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9344. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9345. sizeof(struct lpfc_iocbq) - offset);
  9346. /* Map WCQE parameters into irspiocb parameters */
  9347. pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
  9348. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9349. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9350. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9351. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9352. wcqe->total_data_placed;
  9353. else
  9354. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9355. else {
  9356. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9357. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9358. }
  9359. /* Pick up HBA exchange busy condition */
  9360. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9361. spin_lock_irqsave(&phba->hbalock, iflags);
  9362. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9363. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9364. }
  9365. }
  9366. /**
  9367. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9368. * @phba: Pointer to HBA context object.
  9369. * @wcqe: Pointer to work-queue completion queue entry.
  9370. *
  9371. * This routine handles an ELS work-queue completion event and construct
  9372. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9373. * discovery engine to handle.
  9374. *
  9375. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9376. **/
  9377. static struct lpfc_iocbq *
  9378. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9379. struct lpfc_iocbq *irspiocbq)
  9380. {
  9381. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9382. struct lpfc_iocbq *cmdiocbq;
  9383. struct lpfc_wcqe_complete *wcqe;
  9384. unsigned long iflags;
  9385. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9386. spin_lock_irqsave(&phba->hbalock, iflags);
  9387. pring->stats.iocb_event++;
  9388. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9389. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9390. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9391. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9392. if (unlikely(!cmdiocbq)) {
  9393. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9394. "0386 ELS complete with no corresponding "
  9395. "cmdiocb: iotag (%d)\n",
  9396. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9397. lpfc_sli_release_iocbq(phba, irspiocbq);
  9398. return NULL;
  9399. }
  9400. /* Fake the irspiocbq and copy necessary response information */
  9401. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9402. return irspiocbq;
  9403. }
  9404. /**
  9405. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9406. * @phba: Pointer to HBA context object.
  9407. * @cqe: Pointer to mailbox completion queue entry.
  9408. *
  9409. * This routine process a mailbox completion queue entry with asynchrous
  9410. * event.
  9411. *
  9412. * Return: true if work posted to worker thread, otherwise false.
  9413. **/
  9414. static bool
  9415. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9416. {
  9417. struct lpfc_cq_event *cq_event;
  9418. unsigned long iflags;
  9419. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9420. "0392 Async Event: word0:x%x, word1:x%x, "
  9421. "word2:x%x, word3:x%x\n", mcqe->word0,
  9422. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9423. /* Allocate a new internal CQ_EVENT entry */
  9424. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9425. if (!cq_event) {
  9426. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9427. "0394 Failed to allocate CQ_EVENT entry\n");
  9428. return false;
  9429. }
  9430. /* Move the CQE into an asynchronous event entry */
  9431. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9432. spin_lock_irqsave(&phba->hbalock, iflags);
  9433. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9434. /* Set the async event flag */
  9435. phba->hba_flag |= ASYNC_EVENT;
  9436. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9437. return true;
  9438. }
  9439. /**
  9440. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  9441. * @phba: Pointer to HBA context object.
  9442. * @cqe: Pointer to mailbox completion queue entry.
  9443. *
  9444. * This routine process a mailbox completion queue entry with mailbox
  9445. * completion event.
  9446. *
  9447. * Return: true if work posted to worker thread, otherwise false.
  9448. **/
  9449. static bool
  9450. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9451. {
  9452. uint32_t mcqe_status;
  9453. MAILBOX_t *mbox, *pmbox;
  9454. struct lpfc_mqe *mqe;
  9455. struct lpfc_vport *vport;
  9456. struct lpfc_nodelist *ndlp;
  9457. struct lpfc_dmabuf *mp;
  9458. unsigned long iflags;
  9459. LPFC_MBOXQ_t *pmb;
  9460. bool workposted = false;
  9461. int rc;
  9462. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  9463. if (!bf_get(lpfc_trailer_completed, mcqe))
  9464. goto out_no_mqe_complete;
  9465. /* Get the reference to the active mbox command */
  9466. spin_lock_irqsave(&phba->hbalock, iflags);
  9467. pmb = phba->sli.mbox_active;
  9468. if (unlikely(!pmb)) {
  9469. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9470. "1832 No pending MBOX command to handle\n");
  9471. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9472. goto out_no_mqe_complete;
  9473. }
  9474. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9475. mqe = &pmb->u.mqe;
  9476. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  9477. mbox = phba->mbox;
  9478. vport = pmb->vport;
  9479. /* Reset heartbeat timer */
  9480. phba->last_completion_time = jiffies;
  9481. del_timer(&phba->sli.mbox_tmo);
  9482. /* Move mbox data to caller's mailbox region, do endian swapping */
  9483. if (pmb->mbox_cmpl && mbox)
  9484. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  9485. /* Set the mailbox status with SLI4 range 0x4000 */
  9486. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  9487. if (mcqe_status != MB_CQE_STATUS_SUCCESS)
  9488. bf_set(lpfc_mqe_status, mqe,
  9489. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  9490. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9491. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9492. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  9493. "MBOX dflt rpi: status:x%x rpi:x%x",
  9494. mcqe_status,
  9495. pmbox->un.varWords[0], 0);
  9496. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  9497. mp = (struct lpfc_dmabuf *)(pmb->context1);
  9498. ndlp = (struct lpfc_nodelist *)pmb->context2;
  9499. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  9500. * RID of the PPI using the same mbox buffer.
  9501. */
  9502. lpfc_unreg_login(phba, vport->vpi,
  9503. pmbox->un.varWords[0], pmb);
  9504. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  9505. pmb->context1 = mp;
  9506. pmb->context2 = ndlp;
  9507. pmb->vport = vport;
  9508. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  9509. if (rc != MBX_BUSY)
  9510. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9511. LOG_SLI, "0385 rc should "
  9512. "have been MBX_BUSY\n");
  9513. if (rc != MBX_NOT_FINISHED)
  9514. goto send_current_mbox;
  9515. }
  9516. }
  9517. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  9518. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  9519. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  9520. /* There is mailbox completion work to do */
  9521. spin_lock_irqsave(&phba->hbalock, iflags);
  9522. __lpfc_mbox_cmpl_put(phba, pmb);
  9523. phba->work_ha |= HA_MBATT;
  9524. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9525. workposted = true;
  9526. send_current_mbox:
  9527. spin_lock_irqsave(&phba->hbalock, iflags);
  9528. /* Release the mailbox command posting token */
  9529. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  9530. /* Setting active mailbox pointer need to be in sync to flag clear */
  9531. phba->sli.mbox_active = NULL;
  9532. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9533. /* Wake up worker thread to post the next pending mailbox command */
  9534. lpfc_worker_wake_up(phba);
  9535. out_no_mqe_complete:
  9536. if (bf_get(lpfc_trailer_consumed, mcqe))
  9537. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  9538. return workposted;
  9539. }
  9540. /**
  9541. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  9542. * @phba: Pointer to HBA context object.
  9543. * @cqe: Pointer to mailbox completion queue entry.
  9544. *
  9545. * This routine process a mailbox completion queue entry, it invokes the
  9546. * proper mailbox complete handling or asynchrous event handling routine
  9547. * according to the MCQE's async bit.
  9548. *
  9549. * Return: true if work posted to worker thread, otherwise false.
  9550. **/
  9551. static bool
  9552. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  9553. {
  9554. struct lpfc_mcqe mcqe;
  9555. bool workposted;
  9556. /* Copy the mailbox MCQE and convert endian order as needed */
  9557. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  9558. /* Invoke the proper event handling routine */
  9559. if (!bf_get(lpfc_trailer_async, &mcqe))
  9560. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  9561. else
  9562. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  9563. return workposted;
  9564. }
  9565. /**
  9566. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  9567. * @phba: Pointer to HBA context object.
  9568. * @wcqe: Pointer to work-queue completion queue entry.
  9569. *
  9570. * This routine handles an ELS work-queue completion event.
  9571. *
  9572. * Return: true if work posted to worker thread, otherwise false.
  9573. **/
  9574. static bool
  9575. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  9576. struct lpfc_wcqe_complete *wcqe)
  9577. {
  9578. struct lpfc_iocbq *irspiocbq;
  9579. unsigned long iflags;
  9580. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  9581. /* Get an irspiocbq for later ELS response processing use */
  9582. irspiocbq = lpfc_sli_get_iocbq(phba);
  9583. if (!irspiocbq) {
  9584. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9585. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  9586. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  9587. pring->txq_cnt, phba->iocb_cnt,
  9588. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  9589. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  9590. return false;
  9591. }
  9592. /* Save off the slow-path queue event for work thread to process */
  9593. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  9594. spin_lock_irqsave(&phba->hbalock, iflags);
  9595. list_add_tail(&irspiocbq->cq_event.list,
  9596. &phba->sli4_hba.sp_queue_event);
  9597. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  9598. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9599. return true;
  9600. }
  9601. /**
  9602. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  9603. * @phba: Pointer to HBA context object.
  9604. * @wcqe: Pointer to work-queue completion queue entry.
  9605. *
  9606. * This routine handles slow-path WQ entry comsumed event by invoking the
  9607. * proper WQ release routine to the slow-path WQ.
  9608. **/
  9609. static void
  9610. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  9611. struct lpfc_wcqe_release *wcqe)
  9612. {
  9613. /* Check for the slow-path ELS work queue */
  9614. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  9615. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  9616. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  9617. else
  9618. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9619. "2579 Slow-path wqe consume event carries "
  9620. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  9621. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  9622. phba->sli4_hba.els_wq->queue_id);
  9623. }
  9624. /**
  9625. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  9626. * @phba: Pointer to HBA context object.
  9627. * @cq: Pointer to a WQ completion queue.
  9628. * @wcqe: Pointer to work-queue completion queue entry.
  9629. *
  9630. * This routine handles an XRI abort event.
  9631. *
  9632. * Return: true if work posted to worker thread, otherwise false.
  9633. **/
  9634. static bool
  9635. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  9636. struct lpfc_queue *cq,
  9637. struct sli4_wcqe_xri_aborted *wcqe)
  9638. {
  9639. bool workposted = false;
  9640. struct lpfc_cq_event *cq_event;
  9641. unsigned long iflags;
  9642. /* Allocate a new internal CQ_EVENT entry */
  9643. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9644. if (!cq_event) {
  9645. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9646. "0602 Failed to allocate CQ_EVENT entry\n");
  9647. return false;
  9648. }
  9649. /* Move the CQE into the proper xri abort event list */
  9650. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  9651. switch (cq->subtype) {
  9652. case LPFC_FCP:
  9653. spin_lock_irqsave(&phba->hbalock, iflags);
  9654. list_add_tail(&cq_event->list,
  9655. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  9656. /* Set the fcp xri abort event flag */
  9657. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  9658. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9659. workposted = true;
  9660. break;
  9661. case LPFC_ELS:
  9662. spin_lock_irqsave(&phba->hbalock, iflags);
  9663. list_add_tail(&cq_event->list,
  9664. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  9665. /* Set the els xri abort event flag */
  9666. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  9667. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9668. workposted = true;
  9669. break;
  9670. default:
  9671. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9672. "0603 Invalid work queue CQE subtype (x%x)\n",
  9673. cq->subtype);
  9674. workposted = false;
  9675. break;
  9676. }
  9677. return workposted;
  9678. }
  9679. /**
  9680. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  9681. * @phba: Pointer to HBA context object.
  9682. * @rcqe: Pointer to receive-queue completion queue entry.
  9683. *
  9684. * This routine process a receive-queue completion queue entry.
  9685. *
  9686. * Return: true if work posted to worker thread, otherwise false.
  9687. **/
  9688. static bool
  9689. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  9690. {
  9691. bool workposted = false;
  9692. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  9693. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  9694. struct hbq_dmabuf *dma_buf;
  9695. uint32_t status, rq_id;
  9696. unsigned long iflags;
  9697. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  9698. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  9699. else
  9700. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  9701. if (rq_id != hrq->queue_id)
  9702. goto out;
  9703. status = bf_get(lpfc_rcqe_status, rcqe);
  9704. switch (status) {
  9705. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  9706. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9707. "2537 Receive Frame Truncated!!\n");
  9708. case FC_STATUS_RQ_SUCCESS:
  9709. lpfc_sli4_rq_release(hrq, drq);
  9710. spin_lock_irqsave(&phba->hbalock, iflags);
  9711. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  9712. if (!dma_buf) {
  9713. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9714. goto out;
  9715. }
  9716. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  9717. /* save off the frame for the word thread to process */
  9718. list_add_tail(&dma_buf->cq_event.list,
  9719. &phba->sli4_hba.sp_queue_event);
  9720. /* Frame received */
  9721. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  9722. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9723. workposted = true;
  9724. break;
  9725. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  9726. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  9727. /* Post more buffers if possible */
  9728. spin_lock_irqsave(&phba->hbalock, iflags);
  9729. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  9730. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9731. workposted = true;
  9732. break;
  9733. }
  9734. out:
  9735. return workposted;
  9736. }
  9737. /**
  9738. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  9739. * @phba: Pointer to HBA context object.
  9740. * @cq: Pointer to the completion queue.
  9741. * @wcqe: Pointer to a completion queue entry.
  9742. *
  9743. * This routine process a slow-path work-queue or receive queue completion queue
  9744. * entry.
  9745. *
  9746. * Return: true if work posted to worker thread, otherwise false.
  9747. **/
  9748. static bool
  9749. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  9750. struct lpfc_cqe *cqe)
  9751. {
  9752. struct lpfc_cqe cqevt;
  9753. bool workposted = false;
  9754. /* Copy the work queue CQE and convert endian order if needed */
  9755. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  9756. /* Check and process for different type of WCQE and dispatch */
  9757. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  9758. case CQE_CODE_COMPL_WQE:
  9759. /* Process the WQ/RQ complete event */
  9760. phba->last_completion_time = jiffies;
  9761. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  9762. (struct lpfc_wcqe_complete *)&cqevt);
  9763. break;
  9764. case CQE_CODE_RELEASE_WQE:
  9765. /* Process the WQ release event */
  9766. lpfc_sli4_sp_handle_rel_wcqe(phba,
  9767. (struct lpfc_wcqe_release *)&cqevt);
  9768. break;
  9769. case CQE_CODE_XRI_ABORTED:
  9770. /* Process the WQ XRI abort event */
  9771. phba->last_completion_time = jiffies;
  9772. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  9773. (struct sli4_wcqe_xri_aborted *)&cqevt);
  9774. break;
  9775. case CQE_CODE_RECEIVE:
  9776. case CQE_CODE_RECEIVE_V1:
  9777. /* Process the RQ event */
  9778. phba->last_completion_time = jiffies;
  9779. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  9780. (struct lpfc_rcqe *)&cqevt);
  9781. break;
  9782. default:
  9783. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9784. "0388 Not a valid WCQE code: x%x\n",
  9785. bf_get(lpfc_cqe_code, &cqevt));
  9786. break;
  9787. }
  9788. return workposted;
  9789. }
  9790. /**
  9791. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  9792. * @phba: Pointer to HBA context object.
  9793. * @eqe: Pointer to fast-path event queue entry.
  9794. *
  9795. * This routine process a event queue entry from the slow-path event queue.
  9796. * It will check the MajorCode and MinorCode to determine this is for a
  9797. * completion event on a completion queue, if not, an error shall be logged
  9798. * and just return. Otherwise, it will get to the corresponding completion
  9799. * queue and process all the entries on that completion queue, rearm the
  9800. * completion queue, and then return.
  9801. *
  9802. **/
  9803. static void
  9804. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  9805. {
  9806. struct lpfc_queue *cq = NULL, *childq, *speq;
  9807. struct lpfc_cqe *cqe;
  9808. bool workposted = false;
  9809. int ecount = 0;
  9810. uint16_t cqid;
  9811. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  9812. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9813. "0359 Not a valid slow-path completion "
  9814. "event: majorcode=x%x, minorcode=x%x\n",
  9815. bf_get_le32(lpfc_eqe_major_code, eqe),
  9816. bf_get_le32(lpfc_eqe_minor_code, eqe));
  9817. return;
  9818. }
  9819. /* Get the reference to the corresponding CQ */
  9820. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  9821. /* Search for completion queue pointer matching this cqid */
  9822. speq = phba->sli4_hba.sp_eq;
  9823. list_for_each_entry(childq, &speq->child_list, list) {
  9824. if (childq->queue_id == cqid) {
  9825. cq = childq;
  9826. break;
  9827. }
  9828. }
  9829. if (unlikely(!cq)) {
  9830. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  9831. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9832. "0365 Slow-path CQ identifier "
  9833. "(%d) does not exist\n", cqid);
  9834. return;
  9835. }
  9836. /* Process all the entries to the CQ */
  9837. switch (cq->type) {
  9838. case LPFC_MCQ:
  9839. while ((cqe = lpfc_sli4_cq_get(cq))) {
  9840. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  9841. if (!(++ecount % LPFC_GET_QE_REL_INT))
  9842. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  9843. }
  9844. break;
  9845. case LPFC_WCQ:
  9846. while ((cqe = lpfc_sli4_cq_get(cq))) {
  9847. if (cq->subtype == LPFC_FCP)
  9848. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  9849. cqe);
  9850. else
  9851. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  9852. cqe);
  9853. if (!(++ecount % LPFC_GET_QE_REL_INT))
  9854. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  9855. }
  9856. break;
  9857. default:
  9858. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9859. "0370 Invalid completion queue type (%d)\n",
  9860. cq->type);
  9861. return;
  9862. }
  9863. /* Catch the no cq entry condition, log an error */
  9864. if (unlikely(ecount == 0))
  9865. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9866. "0371 No entry from the CQ: identifier "
  9867. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  9868. /* In any case, flash and re-arm the RCQ */
  9869. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  9870. /* wake up worker thread if there are works to be done */
  9871. if (workposted)
  9872. lpfc_worker_wake_up(phba);
  9873. }
  9874. /**
  9875. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  9876. * @eqe: Pointer to fast-path completion queue entry.
  9877. *
  9878. * This routine process a fast-path work queue completion entry from fast-path
  9879. * event queue for FCP command response completion.
  9880. **/
  9881. static void
  9882. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  9883. struct lpfc_wcqe_complete *wcqe)
  9884. {
  9885. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  9886. struct lpfc_iocbq *cmdiocbq;
  9887. struct lpfc_iocbq irspiocbq;
  9888. unsigned long iflags;
  9889. spin_lock_irqsave(&phba->hbalock, iflags);
  9890. pring->stats.iocb_event++;
  9891. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9892. /* Check for response status */
  9893. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  9894. /* If resource errors reported from HBA, reduce queue
  9895. * depth of the SCSI device.
  9896. */
  9897. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  9898. IOSTAT_LOCAL_REJECT) &&
  9899. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  9900. phba->lpfc_rampdown_queue_depth(phba);
  9901. }
  9902. /* Log the error status */
  9903. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9904. "0373 FCP complete error: status=x%x, "
  9905. "hw_status=x%x, total_data_specified=%d, "
  9906. "parameter=x%x, word3=x%x\n",
  9907. bf_get(lpfc_wcqe_c_status, wcqe),
  9908. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  9909. wcqe->total_data_placed, wcqe->parameter,
  9910. wcqe->word3);
  9911. }
  9912. /* Look up the FCP command IOCB and create pseudo response IOCB */
  9913. spin_lock_irqsave(&phba->hbalock, iflags);
  9914. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9915. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9916. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9917. if (unlikely(!cmdiocbq)) {
  9918. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9919. "0374 FCP complete with no corresponding "
  9920. "cmdiocb: iotag (%d)\n",
  9921. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9922. return;
  9923. }
  9924. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  9925. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9926. "0375 FCP cmdiocb not callback function "
  9927. "iotag: (%d)\n",
  9928. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9929. return;
  9930. }
  9931. /* Fake the irspiocb and copy necessary response information */
  9932. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  9933. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  9934. spin_lock_irqsave(&phba->hbalock, iflags);
  9935. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  9936. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9937. }
  9938. /* Pass the cmd_iocb and the rsp state to the upper layer */
  9939. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  9940. }
  9941. /**
  9942. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  9943. * @phba: Pointer to HBA context object.
  9944. * @cq: Pointer to completion queue.
  9945. * @wcqe: Pointer to work-queue completion queue entry.
  9946. *
  9947. * This routine handles an fast-path WQ entry comsumed event by invoking the
  9948. * proper WQ release routine to the slow-path WQ.
  9949. **/
  9950. static void
  9951. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  9952. struct lpfc_wcqe_release *wcqe)
  9953. {
  9954. struct lpfc_queue *childwq;
  9955. bool wqid_matched = false;
  9956. uint16_t fcp_wqid;
  9957. /* Check for fast-path FCP work queue release */
  9958. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  9959. list_for_each_entry(childwq, &cq->child_list, list) {
  9960. if (childwq->queue_id == fcp_wqid) {
  9961. lpfc_sli4_wq_release(childwq,
  9962. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  9963. wqid_matched = true;
  9964. break;
  9965. }
  9966. }
  9967. /* Report warning log message if no match found */
  9968. if (wqid_matched != true)
  9969. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9970. "2580 Fast-path wqe consume event carries "
  9971. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  9972. }
  9973. /**
  9974. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  9975. * @cq: Pointer to the completion queue.
  9976. * @eqe: Pointer to fast-path completion queue entry.
  9977. *
  9978. * This routine process a fast-path work queue completion entry from fast-path
  9979. * event queue for FCP command response completion.
  9980. **/
  9981. static int
  9982. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  9983. struct lpfc_cqe *cqe)
  9984. {
  9985. struct lpfc_wcqe_release wcqe;
  9986. bool workposted = false;
  9987. /* Copy the work queue CQE and convert endian order if needed */
  9988. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  9989. /* Check and process for different type of WCQE and dispatch */
  9990. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  9991. case CQE_CODE_COMPL_WQE:
  9992. /* Process the WQ complete event */
  9993. phba->last_completion_time = jiffies;
  9994. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  9995. (struct lpfc_wcqe_complete *)&wcqe);
  9996. break;
  9997. case CQE_CODE_RELEASE_WQE:
  9998. /* Process the WQ release event */
  9999. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10000. (struct lpfc_wcqe_release *)&wcqe);
  10001. break;
  10002. case CQE_CODE_XRI_ABORTED:
  10003. /* Process the WQ XRI abort event */
  10004. phba->last_completion_time = jiffies;
  10005. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10006. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10007. break;
  10008. default:
  10009. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10010. "0144 Not a valid WCQE code: x%x\n",
  10011. bf_get(lpfc_wcqe_c_code, &wcqe));
  10012. break;
  10013. }
  10014. return workposted;
  10015. }
  10016. /**
  10017. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10018. * @phba: Pointer to HBA context object.
  10019. * @eqe: Pointer to fast-path event queue entry.
  10020. *
  10021. * This routine process a event queue entry from the fast-path event queue.
  10022. * It will check the MajorCode and MinorCode to determine this is for a
  10023. * completion event on a completion queue, if not, an error shall be logged
  10024. * and just return. Otherwise, it will get to the corresponding completion
  10025. * queue and process all the entries on the completion queue, rearm the
  10026. * completion queue, and then return.
  10027. **/
  10028. static void
  10029. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10030. uint32_t fcp_cqidx)
  10031. {
  10032. struct lpfc_queue *cq;
  10033. struct lpfc_cqe *cqe;
  10034. bool workposted = false;
  10035. uint16_t cqid;
  10036. int ecount = 0;
  10037. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10038. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10039. "0366 Not a valid fast-path completion "
  10040. "event: majorcode=x%x, minorcode=x%x\n",
  10041. bf_get_le32(lpfc_eqe_major_code, eqe),
  10042. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10043. return;
  10044. }
  10045. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10046. if (unlikely(!cq)) {
  10047. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10048. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10049. "0367 Fast-path completion queue "
  10050. "does not exist\n");
  10051. return;
  10052. }
  10053. /* Get the reference to the corresponding CQ */
  10054. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10055. if (unlikely(cqid != cq->queue_id)) {
  10056. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10057. "0368 Miss-matched fast-path completion "
  10058. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10059. cqid, cq->queue_id);
  10060. return;
  10061. }
  10062. /* Process all the entries to the CQ */
  10063. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10064. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10065. if (!(++ecount % LPFC_GET_QE_REL_INT))
  10066. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10067. }
  10068. /* Catch the no cq entry condition */
  10069. if (unlikely(ecount == 0))
  10070. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10071. "0369 No entry from fast-path completion "
  10072. "queue fcpcqid=%d\n", cq->queue_id);
  10073. /* In any case, flash and re-arm the CQ */
  10074. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10075. /* wake up worker thread if there are works to be done */
  10076. if (workposted)
  10077. lpfc_worker_wake_up(phba);
  10078. }
  10079. static void
  10080. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10081. {
  10082. struct lpfc_eqe *eqe;
  10083. /* walk all the EQ entries and drop on the floor */
  10084. while ((eqe = lpfc_sli4_eq_get(eq)))
  10085. ;
  10086. /* Clear and re-arm the EQ */
  10087. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10088. }
  10089. /**
  10090. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10091. * @irq: Interrupt number.
  10092. * @dev_id: The device context pointer.
  10093. *
  10094. * This function is directly called from the PCI layer as an interrupt
  10095. * service routine when device with SLI-4 interface spec is enabled with
  10096. * MSI-X multi-message interrupt mode and there are slow-path events in
  10097. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10098. * interrupt mode, this function is called as part of the device-level
  10099. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10100. * undergoing initialization, the interrupt handler will not process the
  10101. * interrupt. The link attention and ELS ring attention events are handled
  10102. * by the worker thread. The interrupt handler signals the worker thread
  10103. * and returns for these events. This function is called without any lock
  10104. * held. It gets the hbalock to access and update SLI data structures.
  10105. *
  10106. * This function returns IRQ_HANDLED when interrupt is handled else it
  10107. * returns IRQ_NONE.
  10108. **/
  10109. irqreturn_t
  10110. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10111. {
  10112. struct lpfc_hba *phba;
  10113. struct lpfc_queue *speq;
  10114. struct lpfc_eqe *eqe;
  10115. unsigned long iflag;
  10116. int ecount = 0;
  10117. /*
  10118. * Get the driver's phba structure from the dev_id
  10119. */
  10120. phba = (struct lpfc_hba *)dev_id;
  10121. if (unlikely(!phba))
  10122. return IRQ_NONE;
  10123. /* Get to the EQ struct associated with this vector */
  10124. speq = phba->sli4_hba.sp_eq;
  10125. /* Check device state for handling interrupt */
  10126. if (unlikely(lpfc_intr_state_check(phba))) {
  10127. /* Check again for link_state with lock held */
  10128. spin_lock_irqsave(&phba->hbalock, iflag);
  10129. if (phba->link_state < LPFC_LINK_DOWN)
  10130. /* Flush, clear interrupt, and rearm the EQ */
  10131. lpfc_sli4_eq_flush(phba, speq);
  10132. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10133. return IRQ_NONE;
  10134. }
  10135. /*
  10136. * Process all the event on FCP slow-path EQ
  10137. */
  10138. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10139. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10140. if (!(++ecount % LPFC_GET_QE_REL_INT))
  10141. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10142. }
  10143. /* Always clear and re-arm the slow-path EQ */
  10144. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10145. /* Catch the no cq entry condition */
  10146. if (unlikely(ecount == 0)) {
  10147. if (phba->intr_type == MSIX)
  10148. /* MSI-X treated interrupt served as no EQ share INT */
  10149. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10150. "0357 MSI-X interrupt with no EQE\n");
  10151. else
  10152. /* Non MSI-X treated on interrupt as EQ share INT */
  10153. return IRQ_NONE;
  10154. }
  10155. return IRQ_HANDLED;
  10156. } /* lpfc_sli4_sp_intr_handler */
  10157. /**
  10158. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10159. * @irq: Interrupt number.
  10160. * @dev_id: The device context pointer.
  10161. *
  10162. * This function is directly called from the PCI layer as an interrupt
  10163. * service routine when device with SLI-4 interface spec is enabled with
  10164. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10165. * ring event in the HBA. However, when the device is enabled with either
  10166. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10167. * device-level interrupt handler. When the PCI slot is in error recovery
  10168. * or the HBA is undergoing initialization, the interrupt handler will not
  10169. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10170. * the intrrupt context. This function is called without any lock held.
  10171. * It gets the hbalock to access and update SLI data structures. Note that,
  10172. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10173. * equal to that of FCP CQ index.
  10174. *
  10175. * This function returns IRQ_HANDLED when interrupt is handled else it
  10176. * returns IRQ_NONE.
  10177. **/
  10178. irqreturn_t
  10179. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10180. {
  10181. struct lpfc_hba *phba;
  10182. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10183. struct lpfc_queue *fpeq;
  10184. struct lpfc_eqe *eqe;
  10185. unsigned long iflag;
  10186. int ecount = 0;
  10187. uint32_t fcp_eqidx;
  10188. /* Get the driver's phba structure from the dev_id */
  10189. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10190. phba = fcp_eq_hdl->phba;
  10191. fcp_eqidx = fcp_eq_hdl->idx;
  10192. if (unlikely(!phba))
  10193. return IRQ_NONE;
  10194. /* Get to the EQ struct associated with this vector */
  10195. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10196. /* Check device state for handling interrupt */
  10197. if (unlikely(lpfc_intr_state_check(phba))) {
  10198. /* Check again for link_state with lock held */
  10199. spin_lock_irqsave(&phba->hbalock, iflag);
  10200. if (phba->link_state < LPFC_LINK_DOWN)
  10201. /* Flush, clear interrupt, and rearm the EQ */
  10202. lpfc_sli4_eq_flush(phba, fpeq);
  10203. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10204. return IRQ_NONE;
  10205. }
  10206. /*
  10207. * Process all the event on FCP fast-path EQ
  10208. */
  10209. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10210. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10211. if (!(++ecount % LPFC_GET_QE_REL_INT))
  10212. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10213. }
  10214. /* Always clear and re-arm the fast-path EQ */
  10215. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10216. if (unlikely(ecount == 0)) {
  10217. if (phba->intr_type == MSIX)
  10218. /* MSI-X treated interrupt served as no EQ share INT */
  10219. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10220. "0358 MSI-X interrupt with no EQE\n");
  10221. else
  10222. /* Non MSI-X treated on interrupt as EQ share INT */
  10223. return IRQ_NONE;
  10224. }
  10225. return IRQ_HANDLED;
  10226. } /* lpfc_sli4_fp_intr_handler */
  10227. /**
  10228. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10229. * @irq: Interrupt number.
  10230. * @dev_id: The device context pointer.
  10231. *
  10232. * This function is the device-level interrupt handler to device with SLI-4
  10233. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10234. * interrupt mode is enabled and there is an event in the HBA which requires
  10235. * driver attention. This function invokes the slow-path interrupt attention
  10236. * handling function and fast-path interrupt attention handling function in
  10237. * turn to process the relevant HBA attention events. This function is called
  10238. * without any lock held. It gets the hbalock to access and update SLI data
  10239. * structures.
  10240. *
  10241. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10242. * returns IRQ_NONE.
  10243. **/
  10244. irqreturn_t
  10245. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10246. {
  10247. struct lpfc_hba *phba;
  10248. irqreturn_t sp_irq_rc, fp_irq_rc;
  10249. bool fp_handled = false;
  10250. uint32_t fcp_eqidx;
  10251. /* Get the driver's phba structure from the dev_id */
  10252. phba = (struct lpfc_hba *)dev_id;
  10253. if (unlikely(!phba))
  10254. return IRQ_NONE;
  10255. /*
  10256. * Invokes slow-path host attention interrupt handling as appropriate.
  10257. */
  10258. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10259. /*
  10260. * Invoke fast-path host attention interrupt handling as appropriate.
  10261. */
  10262. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10263. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10264. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10265. if (fp_irq_rc == IRQ_HANDLED)
  10266. fp_handled |= true;
  10267. }
  10268. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10269. } /* lpfc_sli4_intr_handler */
  10270. /**
  10271. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10272. * @queue: The queue structure to free.
  10273. *
  10274. * This function frees a queue structure and the DMAable memory used for
  10275. * the host resident queue. This function must be called after destroying the
  10276. * queue on the HBA.
  10277. **/
  10278. void
  10279. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10280. {
  10281. struct lpfc_dmabuf *dmabuf;
  10282. if (!queue)
  10283. return;
  10284. while (!list_empty(&queue->page_list)) {
  10285. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10286. list);
  10287. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10288. dmabuf->virt, dmabuf->phys);
  10289. kfree(dmabuf);
  10290. }
  10291. kfree(queue);
  10292. return;
  10293. }
  10294. /**
  10295. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10296. * @phba: The HBA that this queue is being created on.
  10297. * @entry_size: The size of each queue entry for this queue.
  10298. * @entry count: The number of entries that this queue will handle.
  10299. *
  10300. * This function allocates a queue structure and the DMAable memory used for
  10301. * the host resident queue. This function must be called before creating the
  10302. * queue on the HBA.
  10303. **/
  10304. struct lpfc_queue *
  10305. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10306. uint32_t entry_count)
  10307. {
  10308. struct lpfc_queue *queue;
  10309. struct lpfc_dmabuf *dmabuf;
  10310. int x, total_qe_count;
  10311. void *dma_pointer;
  10312. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10313. if (!phba->sli4_hba.pc_sli4_params.supported)
  10314. hw_page_size = SLI4_PAGE_SIZE;
  10315. queue = kzalloc(sizeof(struct lpfc_queue) +
  10316. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10317. if (!queue)
  10318. return NULL;
  10319. queue->page_count = (ALIGN(entry_size * entry_count,
  10320. hw_page_size))/hw_page_size;
  10321. INIT_LIST_HEAD(&queue->list);
  10322. INIT_LIST_HEAD(&queue->page_list);
  10323. INIT_LIST_HEAD(&queue->child_list);
  10324. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10325. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10326. if (!dmabuf)
  10327. goto out_fail;
  10328. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10329. hw_page_size, &dmabuf->phys,
  10330. GFP_KERNEL);
  10331. if (!dmabuf->virt) {
  10332. kfree(dmabuf);
  10333. goto out_fail;
  10334. }
  10335. memset(dmabuf->virt, 0, hw_page_size);
  10336. dmabuf->buffer_tag = x;
  10337. list_add_tail(&dmabuf->list, &queue->page_list);
  10338. /* initialize queue's entry array */
  10339. dma_pointer = dmabuf->virt;
  10340. for (; total_qe_count < entry_count &&
  10341. dma_pointer < (hw_page_size + dmabuf->virt);
  10342. total_qe_count++, dma_pointer += entry_size) {
  10343. queue->qe[total_qe_count].address = dma_pointer;
  10344. }
  10345. }
  10346. queue->entry_size = entry_size;
  10347. queue->entry_count = entry_count;
  10348. queue->phba = phba;
  10349. return queue;
  10350. out_fail:
  10351. lpfc_sli4_queue_free(queue);
  10352. return NULL;
  10353. }
  10354. /**
  10355. * lpfc_eq_create - Create an Event Queue on the HBA
  10356. * @phba: HBA structure that indicates port to create a queue on.
  10357. * @eq: The queue structure to use to create the event queue.
  10358. * @imax: The maximum interrupt per second limit.
  10359. *
  10360. * This function creates an event queue, as detailed in @eq, on a port,
  10361. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10362. *
  10363. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10364. * is used to get the entry count and entry size that are necessary to
  10365. * determine the number of pages to allocate and use for this queue. This
  10366. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10367. * event queue. This function is asynchronous and will wait for the mailbox
  10368. * command to finish before continuing.
  10369. *
  10370. * On success this function will return a zero. If unable to allocate enough
  10371. * memory this function will return -ENOMEM. If the queue create mailbox command
  10372. * fails this function will return -ENXIO.
  10373. **/
  10374. uint32_t
  10375. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10376. {
  10377. struct lpfc_mbx_eq_create *eq_create;
  10378. LPFC_MBOXQ_t *mbox;
  10379. int rc, length, status = 0;
  10380. struct lpfc_dmabuf *dmabuf;
  10381. uint32_t shdr_status, shdr_add_status;
  10382. union lpfc_sli4_cfg_shdr *shdr;
  10383. uint16_t dmult;
  10384. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10385. if (!phba->sli4_hba.pc_sli4_params.supported)
  10386. hw_page_size = SLI4_PAGE_SIZE;
  10387. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10388. if (!mbox)
  10389. return -ENOMEM;
  10390. length = (sizeof(struct lpfc_mbx_eq_create) -
  10391. sizeof(struct lpfc_sli4_cfg_mhdr));
  10392. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10393. LPFC_MBOX_OPCODE_EQ_CREATE,
  10394. length, LPFC_SLI4_MBX_EMBED);
  10395. eq_create = &mbox->u.mqe.un.eq_create;
  10396. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10397. eq->page_count);
  10398. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10399. LPFC_EQE_SIZE);
  10400. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10401. /* Calculate delay multiper from maximum interrupt per second */
  10402. dmult = LPFC_DMULT_CONST/imax - 1;
  10403. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  10404. dmult);
  10405. switch (eq->entry_count) {
  10406. default:
  10407. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10408. "0360 Unsupported EQ count. (%d)\n",
  10409. eq->entry_count);
  10410. if (eq->entry_count < 256)
  10411. return -EINVAL;
  10412. /* otherwise default to smallest count (drop through) */
  10413. case 256:
  10414. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10415. LPFC_EQ_CNT_256);
  10416. break;
  10417. case 512:
  10418. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10419. LPFC_EQ_CNT_512);
  10420. break;
  10421. case 1024:
  10422. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10423. LPFC_EQ_CNT_1024);
  10424. break;
  10425. case 2048:
  10426. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10427. LPFC_EQ_CNT_2048);
  10428. break;
  10429. case 4096:
  10430. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10431. LPFC_EQ_CNT_4096);
  10432. break;
  10433. }
  10434. list_for_each_entry(dmabuf, &eq->page_list, list) {
  10435. memset(dmabuf->virt, 0, hw_page_size);
  10436. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10437. putPaddrLow(dmabuf->phys);
  10438. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10439. putPaddrHigh(dmabuf->phys);
  10440. }
  10441. mbox->vport = phba->pport;
  10442. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  10443. mbox->context1 = NULL;
  10444. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10445. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  10446. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10447. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10448. if (shdr_status || shdr_add_status || rc) {
  10449. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10450. "2500 EQ_CREATE mailbox failed with "
  10451. "status x%x add_status x%x, mbx status x%x\n",
  10452. shdr_status, shdr_add_status, rc);
  10453. status = -ENXIO;
  10454. }
  10455. eq->type = LPFC_EQ;
  10456. eq->subtype = LPFC_NONE;
  10457. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  10458. if (eq->queue_id == 0xFFFF)
  10459. status = -ENXIO;
  10460. eq->host_index = 0;
  10461. eq->hba_index = 0;
  10462. mempool_free(mbox, phba->mbox_mem_pool);
  10463. return status;
  10464. }
  10465. /**
  10466. * lpfc_cq_create - Create a Completion Queue on the HBA
  10467. * @phba: HBA structure that indicates port to create a queue on.
  10468. * @cq: The queue structure to use to create the completion queue.
  10469. * @eq: The event queue to bind this completion queue to.
  10470. *
  10471. * This function creates a completion queue, as detailed in @wq, on a port,
  10472. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  10473. *
  10474. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  10475. * is used to get the entry count and entry size that are necessary to
  10476. * determine the number of pages to allocate and use for this queue. The @eq
  10477. * is used to indicate which event queue to bind this completion queue to. This
  10478. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  10479. * completion queue. This function is asynchronous and will wait for the mailbox
  10480. * command to finish before continuing.
  10481. *
  10482. * On success this function will return a zero. If unable to allocate enough
  10483. * memory this function will return -ENOMEM. If the queue create mailbox command
  10484. * fails this function will return -ENXIO.
  10485. **/
  10486. uint32_t
  10487. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10488. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  10489. {
  10490. struct lpfc_mbx_cq_create *cq_create;
  10491. struct lpfc_dmabuf *dmabuf;
  10492. LPFC_MBOXQ_t *mbox;
  10493. int rc, length, status = 0;
  10494. uint32_t shdr_status, shdr_add_status;
  10495. union lpfc_sli4_cfg_shdr *shdr;
  10496. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10497. if (!phba->sli4_hba.pc_sli4_params.supported)
  10498. hw_page_size = SLI4_PAGE_SIZE;
  10499. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10500. if (!mbox)
  10501. return -ENOMEM;
  10502. length = (sizeof(struct lpfc_mbx_cq_create) -
  10503. sizeof(struct lpfc_sli4_cfg_mhdr));
  10504. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10505. LPFC_MBOX_OPCODE_CQ_CREATE,
  10506. length, LPFC_SLI4_MBX_EMBED);
  10507. cq_create = &mbox->u.mqe.un.cq_create;
  10508. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  10509. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  10510. cq->page_count);
  10511. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  10512. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  10513. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10514. phba->sli4_hba.pc_sli4_params.cqv);
  10515. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  10516. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  10517. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  10518. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  10519. eq->queue_id);
  10520. } else {
  10521. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  10522. eq->queue_id);
  10523. }
  10524. switch (cq->entry_count) {
  10525. default:
  10526. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10527. "0361 Unsupported CQ count. (%d)\n",
  10528. cq->entry_count);
  10529. if (cq->entry_count < 256)
  10530. return -EINVAL;
  10531. /* otherwise default to smallest count (drop through) */
  10532. case 256:
  10533. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10534. LPFC_CQ_CNT_256);
  10535. break;
  10536. case 512:
  10537. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10538. LPFC_CQ_CNT_512);
  10539. break;
  10540. case 1024:
  10541. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10542. LPFC_CQ_CNT_1024);
  10543. break;
  10544. }
  10545. list_for_each_entry(dmabuf, &cq->page_list, list) {
  10546. memset(dmabuf->virt, 0, hw_page_size);
  10547. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10548. putPaddrLow(dmabuf->phys);
  10549. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10550. putPaddrHigh(dmabuf->phys);
  10551. }
  10552. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10553. /* The IOCTL status is embedded in the mailbox subheader. */
  10554. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10555. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10556. if (shdr_status || shdr_add_status || rc) {
  10557. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10558. "2501 CQ_CREATE mailbox failed with "
  10559. "status x%x add_status x%x, mbx status x%x\n",
  10560. shdr_status, shdr_add_status, rc);
  10561. status = -ENXIO;
  10562. goto out;
  10563. }
  10564. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  10565. if (cq->queue_id == 0xFFFF) {
  10566. status = -ENXIO;
  10567. goto out;
  10568. }
  10569. /* link the cq onto the parent eq child list */
  10570. list_add_tail(&cq->list, &eq->child_list);
  10571. /* Set up completion queue's type and subtype */
  10572. cq->type = type;
  10573. cq->subtype = subtype;
  10574. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  10575. cq->assoc_qid = eq->queue_id;
  10576. cq->host_index = 0;
  10577. cq->hba_index = 0;
  10578. out:
  10579. mempool_free(mbox, phba->mbox_mem_pool);
  10580. return status;
  10581. }
  10582. /**
  10583. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  10584. * @phba: HBA structure that indicates port to create a queue on.
  10585. * @mq: The queue structure to use to create the mailbox queue.
  10586. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  10587. * @cq: The completion queue to associate with this cq.
  10588. *
  10589. * This function provides failback (fb) functionality when the
  10590. * mq_create_ext fails on older FW generations. It's purpose is identical
  10591. * to mq_create_ext otherwise.
  10592. *
  10593. * This routine cannot fail as all attributes were previously accessed and
  10594. * initialized in mq_create_ext.
  10595. **/
  10596. static void
  10597. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  10598. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  10599. {
  10600. struct lpfc_mbx_mq_create *mq_create;
  10601. struct lpfc_dmabuf *dmabuf;
  10602. int length;
  10603. length = (sizeof(struct lpfc_mbx_mq_create) -
  10604. sizeof(struct lpfc_sli4_cfg_mhdr));
  10605. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10606. LPFC_MBOX_OPCODE_MQ_CREATE,
  10607. length, LPFC_SLI4_MBX_EMBED);
  10608. mq_create = &mbox->u.mqe.un.mq_create;
  10609. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  10610. mq->page_count);
  10611. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  10612. cq->queue_id);
  10613. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  10614. switch (mq->entry_count) {
  10615. case 16:
  10616. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  10617. LPFC_MQ_RING_SIZE_16);
  10618. break;
  10619. case 32:
  10620. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  10621. LPFC_MQ_RING_SIZE_32);
  10622. break;
  10623. case 64:
  10624. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  10625. LPFC_MQ_RING_SIZE_64);
  10626. break;
  10627. case 128:
  10628. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  10629. LPFC_MQ_RING_SIZE_128);
  10630. break;
  10631. }
  10632. list_for_each_entry(dmabuf, &mq->page_list, list) {
  10633. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10634. putPaddrLow(dmabuf->phys);
  10635. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10636. putPaddrHigh(dmabuf->phys);
  10637. }
  10638. }
  10639. /**
  10640. * lpfc_mq_create - Create a mailbox Queue on the HBA
  10641. * @phba: HBA structure that indicates port to create a queue on.
  10642. * @mq: The queue structure to use to create the mailbox queue.
  10643. * @cq: The completion queue to associate with this cq.
  10644. * @subtype: The queue's subtype.
  10645. *
  10646. * This function creates a mailbox queue, as detailed in @mq, on a port,
  10647. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  10648. *
  10649. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  10650. * is used to get the entry count and entry size that are necessary to
  10651. * determine the number of pages to allocate and use for this queue. This
  10652. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  10653. * mailbox queue. This function is asynchronous and will wait for the mailbox
  10654. * command to finish before continuing.
  10655. *
  10656. * On success this function will return a zero. If unable to allocate enough
  10657. * memory this function will return -ENOMEM. If the queue create mailbox command
  10658. * fails this function will return -ENXIO.
  10659. **/
  10660. int32_t
  10661. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  10662. struct lpfc_queue *cq, uint32_t subtype)
  10663. {
  10664. struct lpfc_mbx_mq_create *mq_create;
  10665. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  10666. struct lpfc_dmabuf *dmabuf;
  10667. LPFC_MBOXQ_t *mbox;
  10668. int rc, length, status = 0;
  10669. uint32_t shdr_status, shdr_add_status;
  10670. union lpfc_sli4_cfg_shdr *shdr;
  10671. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10672. if (!phba->sli4_hba.pc_sli4_params.supported)
  10673. hw_page_size = SLI4_PAGE_SIZE;
  10674. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10675. if (!mbox)
  10676. return -ENOMEM;
  10677. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  10678. sizeof(struct lpfc_sli4_cfg_mhdr));
  10679. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10680. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  10681. length, LPFC_SLI4_MBX_EMBED);
  10682. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  10683. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  10684. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  10685. &mq_create_ext->u.request, mq->page_count);
  10686. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  10687. &mq_create_ext->u.request, 1);
  10688. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  10689. &mq_create_ext->u.request, 1);
  10690. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  10691. &mq_create_ext->u.request, 1);
  10692. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  10693. &mq_create_ext->u.request, 1);
  10694. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  10695. &mq_create_ext->u.request, 1);
  10696. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  10697. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10698. phba->sli4_hba.pc_sli4_params.mqv);
  10699. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  10700. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  10701. cq->queue_id);
  10702. else
  10703. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  10704. cq->queue_id);
  10705. switch (mq->entry_count) {
  10706. default:
  10707. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10708. "0362 Unsupported MQ count. (%d)\n",
  10709. mq->entry_count);
  10710. if (mq->entry_count < 16)
  10711. return -EINVAL;
  10712. /* otherwise default to smallest count (drop through) */
  10713. case 16:
  10714. bf_set(lpfc_mq_context_ring_size,
  10715. &mq_create_ext->u.request.context,
  10716. LPFC_MQ_RING_SIZE_16);
  10717. break;
  10718. case 32:
  10719. bf_set(lpfc_mq_context_ring_size,
  10720. &mq_create_ext->u.request.context,
  10721. LPFC_MQ_RING_SIZE_32);
  10722. break;
  10723. case 64:
  10724. bf_set(lpfc_mq_context_ring_size,
  10725. &mq_create_ext->u.request.context,
  10726. LPFC_MQ_RING_SIZE_64);
  10727. break;
  10728. case 128:
  10729. bf_set(lpfc_mq_context_ring_size,
  10730. &mq_create_ext->u.request.context,
  10731. LPFC_MQ_RING_SIZE_128);
  10732. break;
  10733. }
  10734. list_for_each_entry(dmabuf, &mq->page_list, list) {
  10735. memset(dmabuf->virt, 0, hw_page_size);
  10736. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  10737. putPaddrLow(dmabuf->phys);
  10738. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  10739. putPaddrHigh(dmabuf->phys);
  10740. }
  10741. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10742. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  10743. &mq_create_ext->u.response);
  10744. if (rc != MBX_SUCCESS) {
  10745. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10746. "2795 MQ_CREATE_EXT failed with "
  10747. "status x%x. Failback to MQ_CREATE.\n",
  10748. rc);
  10749. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  10750. mq_create = &mbox->u.mqe.un.mq_create;
  10751. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10752. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  10753. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  10754. &mq_create->u.response);
  10755. }
  10756. /* The IOCTL status is embedded in the mailbox subheader. */
  10757. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10758. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10759. if (shdr_status || shdr_add_status || rc) {
  10760. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10761. "2502 MQ_CREATE mailbox failed with "
  10762. "status x%x add_status x%x, mbx status x%x\n",
  10763. shdr_status, shdr_add_status, rc);
  10764. status = -ENXIO;
  10765. goto out;
  10766. }
  10767. if (mq->queue_id == 0xFFFF) {
  10768. status = -ENXIO;
  10769. goto out;
  10770. }
  10771. mq->type = LPFC_MQ;
  10772. mq->assoc_qid = cq->queue_id;
  10773. mq->subtype = subtype;
  10774. mq->host_index = 0;
  10775. mq->hba_index = 0;
  10776. /* link the mq onto the parent cq child list */
  10777. list_add_tail(&mq->list, &cq->child_list);
  10778. out:
  10779. mempool_free(mbox, phba->mbox_mem_pool);
  10780. return status;
  10781. }
  10782. /**
  10783. * lpfc_wq_create - Create a Work Queue on the HBA
  10784. * @phba: HBA structure that indicates port to create a queue on.
  10785. * @wq: The queue structure to use to create the work queue.
  10786. * @cq: The completion queue to bind this work queue to.
  10787. * @subtype: The subtype of the work queue indicating its functionality.
  10788. *
  10789. * This function creates a work queue, as detailed in @wq, on a port, described
  10790. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  10791. *
  10792. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  10793. * is used to get the entry count and entry size that are necessary to
  10794. * determine the number of pages to allocate and use for this queue. The @cq
  10795. * is used to indicate which completion queue to bind this work queue to. This
  10796. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  10797. * work queue. This function is asynchronous and will wait for the mailbox
  10798. * command to finish before continuing.
  10799. *
  10800. * On success this function will return a zero. If unable to allocate enough
  10801. * memory this function will return -ENOMEM. If the queue create mailbox command
  10802. * fails this function will return -ENXIO.
  10803. **/
  10804. uint32_t
  10805. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  10806. struct lpfc_queue *cq, uint32_t subtype)
  10807. {
  10808. struct lpfc_mbx_wq_create *wq_create;
  10809. struct lpfc_dmabuf *dmabuf;
  10810. LPFC_MBOXQ_t *mbox;
  10811. int rc, length, status = 0;
  10812. uint32_t shdr_status, shdr_add_status;
  10813. union lpfc_sli4_cfg_shdr *shdr;
  10814. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10815. struct dma_address *page;
  10816. if (!phba->sli4_hba.pc_sli4_params.supported)
  10817. hw_page_size = SLI4_PAGE_SIZE;
  10818. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10819. if (!mbox)
  10820. return -ENOMEM;
  10821. length = (sizeof(struct lpfc_mbx_wq_create) -
  10822. sizeof(struct lpfc_sli4_cfg_mhdr));
  10823. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  10824. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  10825. length, LPFC_SLI4_MBX_EMBED);
  10826. wq_create = &mbox->u.mqe.un.wq_create;
  10827. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  10828. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  10829. wq->page_count);
  10830. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  10831. cq->queue_id);
  10832. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10833. phba->sli4_hba.pc_sli4_params.wqv);
  10834. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  10835. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  10836. wq->entry_count);
  10837. switch (wq->entry_size) {
  10838. default:
  10839. case 64:
  10840. bf_set(lpfc_mbx_wq_create_wqe_size,
  10841. &wq_create->u.request_1,
  10842. LPFC_WQ_WQE_SIZE_64);
  10843. break;
  10844. case 128:
  10845. bf_set(lpfc_mbx_wq_create_wqe_size,
  10846. &wq_create->u.request_1,
  10847. LPFC_WQ_WQE_SIZE_128);
  10848. break;
  10849. }
  10850. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  10851. (PAGE_SIZE/SLI4_PAGE_SIZE));
  10852. page = wq_create->u.request_1.page;
  10853. } else {
  10854. page = wq_create->u.request.page;
  10855. }
  10856. list_for_each_entry(dmabuf, &wq->page_list, list) {
  10857. memset(dmabuf->virt, 0, hw_page_size);
  10858. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  10859. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  10860. }
  10861. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10862. /* The IOCTL status is embedded in the mailbox subheader. */
  10863. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10864. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10865. if (shdr_status || shdr_add_status || rc) {
  10866. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10867. "2503 WQ_CREATE mailbox failed with "
  10868. "status x%x add_status x%x, mbx status x%x\n",
  10869. shdr_status, shdr_add_status, rc);
  10870. status = -ENXIO;
  10871. goto out;
  10872. }
  10873. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  10874. if (wq->queue_id == 0xFFFF) {
  10875. status = -ENXIO;
  10876. goto out;
  10877. }
  10878. wq->type = LPFC_WQ;
  10879. wq->assoc_qid = cq->queue_id;
  10880. wq->subtype = subtype;
  10881. wq->host_index = 0;
  10882. wq->hba_index = 0;
  10883. /* link the wq onto the parent cq child list */
  10884. list_add_tail(&wq->list, &cq->child_list);
  10885. out:
  10886. mempool_free(mbox, phba->mbox_mem_pool);
  10887. return status;
  10888. }
  10889. /**
  10890. * lpfc_rq_create - Create a Receive Queue on the HBA
  10891. * @phba: HBA structure that indicates port to create a queue on.
  10892. * @hrq: The queue structure to use to create the header receive queue.
  10893. * @drq: The queue structure to use to create the data receive queue.
  10894. * @cq: The completion queue to bind this work queue to.
  10895. *
  10896. * This function creates a receive buffer queue pair , as detailed in @hrq and
  10897. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  10898. * to the HBA.
  10899. *
  10900. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  10901. * struct is used to get the entry count that is necessary to determine the
  10902. * number of pages to use for this queue. The @cq is used to indicate which
  10903. * completion queue to bind received buffers that are posted to these queues to.
  10904. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  10905. * receive queue pair. This function is asynchronous and will wait for the
  10906. * mailbox command to finish before continuing.
  10907. *
  10908. * On success this function will return a zero. If unable to allocate enough
  10909. * memory this function will return -ENOMEM. If the queue create mailbox command
  10910. * fails this function will return -ENXIO.
  10911. **/
  10912. uint32_t
  10913. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  10914. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  10915. {
  10916. struct lpfc_mbx_rq_create *rq_create;
  10917. struct lpfc_dmabuf *dmabuf;
  10918. LPFC_MBOXQ_t *mbox;
  10919. int rc, length, status = 0;
  10920. uint32_t shdr_status, shdr_add_status;
  10921. union lpfc_sli4_cfg_shdr *shdr;
  10922. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10923. if (!phba->sli4_hba.pc_sli4_params.supported)
  10924. hw_page_size = SLI4_PAGE_SIZE;
  10925. if (hrq->entry_count != drq->entry_count)
  10926. return -EINVAL;
  10927. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10928. if (!mbox)
  10929. return -ENOMEM;
  10930. length = (sizeof(struct lpfc_mbx_rq_create) -
  10931. sizeof(struct lpfc_sli4_cfg_mhdr));
  10932. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  10933. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  10934. length, LPFC_SLI4_MBX_EMBED);
  10935. rq_create = &mbox->u.mqe.un.rq_create;
  10936. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  10937. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10938. phba->sli4_hba.pc_sli4_params.rqv);
  10939. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  10940. bf_set(lpfc_rq_context_rqe_count_1,
  10941. &rq_create->u.request.context,
  10942. hrq->entry_count);
  10943. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  10944. bf_set(lpfc_rq_context_rqe_size,
  10945. &rq_create->u.request.context,
  10946. LPFC_RQE_SIZE_8);
  10947. bf_set(lpfc_rq_context_page_size,
  10948. &rq_create->u.request.context,
  10949. (PAGE_SIZE/SLI4_PAGE_SIZE));
  10950. } else {
  10951. switch (hrq->entry_count) {
  10952. default:
  10953. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10954. "2535 Unsupported RQ count. (%d)\n",
  10955. hrq->entry_count);
  10956. if (hrq->entry_count < 512)
  10957. return -EINVAL;
  10958. /* otherwise default to smallest count (drop through) */
  10959. case 512:
  10960. bf_set(lpfc_rq_context_rqe_count,
  10961. &rq_create->u.request.context,
  10962. LPFC_RQ_RING_SIZE_512);
  10963. break;
  10964. case 1024:
  10965. bf_set(lpfc_rq_context_rqe_count,
  10966. &rq_create->u.request.context,
  10967. LPFC_RQ_RING_SIZE_1024);
  10968. break;
  10969. case 2048:
  10970. bf_set(lpfc_rq_context_rqe_count,
  10971. &rq_create->u.request.context,
  10972. LPFC_RQ_RING_SIZE_2048);
  10973. break;
  10974. case 4096:
  10975. bf_set(lpfc_rq_context_rqe_count,
  10976. &rq_create->u.request.context,
  10977. LPFC_RQ_RING_SIZE_4096);
  10978. break;
  10979. }
  10980. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  10981. LPFC_HDR_BUF_SIZE);
  10982. }
  10983. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  10984. cq->queue_id);
  10985. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  10986. hrq->page_count);
  10987. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  10988. memset(dmabuf->virt, 0, hw_page_size);
  10989. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10990. putPaddrLow(dmabuf->phys);
  10991. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10992. putPaddrHigh(dmabuf->phys);
  10993. }
  10994. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10995. /* The IOCTL status is embedded in the mailbox subheader. */
  10996. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10997. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10998. if (shdr_status || shdr_add_status || rc) {
  10999. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11000. "2504 RQ_CREATE mailbox failed with "
  11001. "status x%x add_status x%x, mbx status x%x\n",
  11002. shdr_status, shdr_add_status, rc);
  11003. status = -ENXIO;
  11004. goto out;
  11005. }
  11006. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11007. if (hrq->queue_id == 0xFFFF) {
  11008. status = -ENXIO;
  11009. goto out;
  11010. }
  11011. hrq->type = LPFC_HRQ;
  11012. hrq->assoc_qid = cq->queue_id;
  11013. hrq->subtype = subtype;
  11014. hrq->host_index = 0;
  11015. hrq->hba_index = 0;
  11016. /* now create the data queue */
  11017. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11018. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11019. length, LPFC_SLI4_MBX_EMBED);
  11020. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11021. phba->sli4_hba.pc_sli4_params.rqv);
  11022. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11023. bf_set(lpfc_rq_context_rqe_count_1,
  11024. &rq_create->u.request.context, hrq->entry_count);
  11025. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11026. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11027. LPFC_RQE_SIZE_8);
  11028. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11029. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11030. } else {
  11031. switch (drq->entry_count) {
  11032. default:
  11033. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11034. "2536 Unsupported RQ count. (%d)\n",
  11035. drq->entry_count);
  11036. if (drq->entry_count < 512)
  11037. return -EINVAL;
  11038. /* otherwise default to smallest count (drop through) */
  11039. case 512:
  11040. bf_set(lpfc_rq_context_rqe_count,
  11041. &rq_create->u.request.context,
  11042. LPFC_RQ_RING_SIZE_512);
  11043. break;
  11044. case 1024:
  11045. bf_set(lpfc_rq_context_rqe_count,
  11046. &rq_create->u.request.context,
  11047. LPFC_RQ_RING_SIZE_1024);
  11048. break;
  11049. case 2048:
  11050. bf_set(lpfc_rq_context_rqe_count,
  11051. &rq_create->u.request.context,
  11052. LPFC_RQ_RING_SIZE_2048);
  11053. break;
  11054. case 4096:
  11055. bf_set(lpfc_rq_context_rqe_count,
  11056. &rq_create->u.request.context,
  11057. LPFC_RQ_RING_SIZE_4096);
  11058. break;
  11059. }
  11060. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11061. LPFC_DATA_BUF_SIZE);
  11062. }
  11063. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11064. cq->queue_id);
  11065. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11066. drq->page_count);
  11067. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11068. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11069. putPaddrLow(dmabuf->phys);
  11070. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11071. putPaddrHigh(dmabuf->phys);
  11072. }
  11073. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11074. /* The IOCTL status is embedded in the mailbox subheader. */
  11075. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11076. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11077. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11078. if (shdr_status || shdr_add_status || rc) {
  11079. status = -ENXIO;
  11080. goto out;
  11081. }
  11082. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11083. if (drq->queue_id == 0xFFFF) {
  11084. status = -ENXIO;
  11085. goto out;
  11086. }
  11087. drq->type = LPFC_DRQ;
  11088. drq->assoc_qid = cq->queue_id;
  11089. drq->subtype = subtype;
  11090. drq->host_index = 0;
  11091. drq->hba_index = 0;
  11092. /* link the header and data RQs onto the parent cq child list */
  11093. list_add_tail(&hrq->list, &cq->child_list);
  11094. list_add_tail(&drq->list, &cq->child_list);
  11095. out:
  11096. mempool_free(mbox, phba->mbox_mem_pool);
  11097. return status;
  11098. }
  11099. /**
  11100. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11101. * @eq: The queue structure associated with the queue to destroy.
  11102. *
  11103. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11104. * command, specific to the type of queue, to the HBA.
  11105. *
  11106. * The @eq struct is used to get the queue ID of the queue to destroy.
  11107. *
  11108. * On success this function will return a zero. If the queue destroy mailbox
  11109. * command fails this function will return -ENXIO.
  11110. **/
  11111. uint32_t
  11112. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11113. {
  11114. LPFC_MBOXQ_t *mbox;
  11115. int rc, length, status = 0;
  11116. uint32_t shdr_status, shdr_add_status;
  11117. union lpfc_sli4_cfg_shdr *shdr;
  11118. if (!eq)
  11119. return -ENODEV;
  11120. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11121. if (!mbox)
  11122. return -ENOMEM;
  11123. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11124. sizeof(struct lpfc_sli4_cfg_mhdr));
  11125. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11126. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11127. length, LPFC_SLI4_MBX_EMBED);
  11128. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11129. eq->queue_id);
  11130. mbox->vport = eq->phba->pport;
  11131. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11132. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11133. /* The IOCTL status is embedded in the mailbox subheader. */
  11134. shdr = (union lpfc_sli4_cfg_shdr *)
  11135. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11136. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11137. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11138. if (shdr_status || shdr_add_status || rc) {
  11139. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11140. "2505 EQ_DESTROY mailbox failed with "
  11141. "status x%x add_status x%x, mbx status x%x\n",
  11142. shdr_status, shdr_add_status, rc);
  11143. status = -ENXIO;
  11144. }
  11145. /* Remove eq from any list */
  11146. list_del_init(&eq->list);
  11147. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11148. return status;
  11149. }
  11150. /**
  11151. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11152. * @cq: The queue structure associated with the queue to destroy.
  11153. *
  11154. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11155. * command, specific to the type of queue, to the HBA.
  11156. *
  11157. * The @cq struct is used to get the queue ID of the queue to destroy.
  11158. *
  11159. * On success this function will return a zero. If the queue destroy mailbox
  11160. * command fails this function will return -ENXIO.
  11161. **/
  11162. uint32_t
  11163. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11164. {
  11165. LPFC_MBOXQ_t *mbox;
  11166. int rc, length, status = 0;
  11167. uint32_t shdr_status, shdr_add_status;
  11168. union lpfc_sli4_cfg_shdr *shdr;
  11169. if (!cq)
  11170. return -ENODEV;
  11171. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11172. if (!mbox)
  11173. return -ENOMEM;
  11174. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11175. sizeof(struct lpfc_sli4_cfg_mhdr));
  11176. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11177. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11178. length, LPFC_SLI4_MBX_EMBED);
  11179. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11180. cq->queue_id);
  11181. mbox->vport = cq->phba->pport;
  11182. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11183. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11184. /* The IOCTL status is embedded in the mailbox subheader. */
  11185. shdr = (union lpfc_sli4_cfg_shdr *)
  11186. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11187. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11188. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11189. if (shdr_status || shdr_add_status || rc) {
  11190. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11191. "2506 CQ_DESTROY mailbox failed with "
  11192. "status x%x add_status x%x, mbx status x%x\n",
  11193. shdr_status, shdr_add_status, rc);
  11194. status = -ENXIO;
  11195. }
  11196. /* Remove cq from any list */
  11197. list_del_init(&cq->list);
  11198. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11199. return status;
  11200. }
  11201. /**
  11202. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11203. * @qm: The queue structure associated with the queue to destroy.
  11204. *
  11205. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11206. * command, specific to the type of queue, to the HBA.
  11207. *
  11208. * The @mq struct is used to get the queue ID of the queue to destroy.
  11209. *
  11210. * On success this function will return a zero. If the queue destroy mailbox
  11211. * command fails this function will return -ENXIO.
  11212. **/
  11213. uint32_t
  11214. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11215. {
  11216. LPFC_MBOXQ_t *mbox;
  11217. int rc, length, status = 0;
  11218. uint32_t shdr_status, shdr_add_status;
  11219. union lpfc_sli4_cfg_shdr *shdr;
  11220. if (!mq)
  11221. return -ENODEV;
  11222. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11223. if (!mbox)
  11224. return -ENOMEM;
  11225. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11226. sizeof(struct lpfc_sli4_cfg_mhdr));
  11227. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11228. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11229. length, LPFC_SLI4_MBX_EMBED);
  11230. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11231. mq->queue_id);
  11232. mbox->vport = mq->phba->pport;
  11233. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11234. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11235. /* The IOCTL status is embedded in the mailbox subheader. */
  11236. shdr = (union lpfc_sli4_cfg_shdr *)
  11237. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11238. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11239. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11240. if (shdr_status || shdr_add_status || rc) {
  11241. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11242. "2507 MQ_DESTROY mailbox failed with "
  11243. "status x%x add_status x%x, mbx status x%x\n",
  11244. shdr_status, shdr_add_status, rc);
  11245. status = -ENXIO;
  11246. }
  11247. /* Remove mq from any list */
  11248. list_del_init(&mq->list);
  11249. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11250. return status;
  11251. }
  11252. /**
  11253. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11254. * @wq: The queue structure associated with the queue to destroy.
  11255. *
  11256. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11257. * command, specific to the type of queue, to the HBA.
  11258. *
  11259. * The @wq struct is used to get the queue ID of the queue to destroy.
  11260. *
  11261. * On success this function will return a zero. If the queue destroy mailbox
  11262. * command fails this function will return -ENXIO.
  11263. **/
  11264. uint32_t
  11265. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11266. {
  11267. LPFC_MBOXQ_t *mbox;
  11268. int rc, length, status = 0;
  11269. uint32_t shdr_status, shdr_add_status;
  11270. union lpfc_sli4_cfg_shdr *shdr;
  11271. if (!wq)
  11272. return -ENODEV;
  11273. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11274. if (!mbox)
  11275. return -ENOMEM;
  11276. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11277. sizeof(struct lpfc_sli4_cfg_mhdr));
  11278. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11279. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11280. length, LPFC_SLI4_MBX_EMBED);
  11281. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11282. wq->queue_id);
  11283. mbox->vport = wq->phba->pport;
  11284. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11285. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11286. shdr = (union lpfc_sli4_cfg_shdr *)
  11287. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  11288. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11289. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11290. if (shdr_status || shdr_add_status || rc) {
  11291. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11292. "2508 WQ_DESTROY mailbox failed with "
  11293. "status x%x add_status x%x, mbx status x%x\n",
  11294. shdr_status, shdr_add_status, rc);
  11295. status = -ENXIO;
  11296. }
  11297. /* Remove wq from any list */
  11298. list_del_init(&wq->list);
  11299. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11300. return status;
  11301. }
  11302. /**
  11303. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11304. * @rq: The queue structure associated with the queue to destroy.
  11305. *
  11306. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11307. * command, specific to the type of queue, to the HBA.
  11308. *
  11309. * The @rq struct is used to get the queue ID of the queue to destroy.
  11310. *
  11311. * On success this function will return a zero. If the queue destroy mailbox
  11312. * command fails this function will return -ENXIO.
  11313. **/
  11314. uint32_t
  11315. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11316. struct lpfc_queue *drq)
  11317. {
  11318. LPFC_MBOXQ_t *mbox;
  11319. int rc, length, status = 0;
  11320. uint32_t shdr_status, shdr_add_status;
  11321. union lpfc_sli4_cfg_shdr *shdr;
  11322. if (!hrq || !drq)
  11323. return -ENODEV;
  11324. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11325. if (!mbox)
  11326. return -ENOMEM;
  11327. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11328. sizeof(struct lpfc_sli4_cfg_mhdr));
  11329. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11330. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11331. length, LPFC_SLI4_MBX_EMBED);
  11332. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11333. hrq->queue_id);
  11334. mbox->vport = hrq->phba->pport;
  11335. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11336. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11337. /* The IOCTL status is embedded in the mailbox subheader. */
  11338. shdr = (union lpfc_sli4_cfg_shdr *)
  11339. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11340. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11341. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11342. if (shdr_status || shdr_add_status || rc) {
  11343. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11344. "2509 RQ_DESTROY mailbox failed with "
  11345. "status x%x add_status x%x, mbx status x%x\n",
  11346. shdr_status, shdr_add_status, rc);
  11347. if (rc != MBX_TIMEOUT)
  11348. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11349. return -ENXIO;
  11350. }
  11351. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11352. drq->queue_id);
  11353. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11354. shdr = (union lpfc_sli4_cfg_shdr *)
  11355. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11356. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11357. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11358. if (shdr_status || shdr_add_status || rc) {
  11359. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11360. "2510 RQ_DESTROY mailbox failed with "
  11361. "status x%x add_status x%x, mbx status x%x\n",
  11362. shdr_status, shdr_add_status, rc);
  11363. status = -ENXIO;
  11364. }
  11365. list_del_init(&hrq->list);
  11366. list_del_init(&drq->list);
  11367. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11368. return status;
  11369. }
  11370. /**
  11371. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  11372. * @phba: The virtual port for which this call being executed.
  11373. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  11374. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  11375. * @xritag: the xritag that ties this io to the SGL pages.
  11376. *
  11377. * This routine will post the sgl pages for the IO that has the xritag
  11378. * that is in the iocbq structure. The xritag is assigned during iocbq
  11379. * creation and persists for as long as the driver is loaded.
  11380. * if the caller has fewer than 256 scatter gather segments to map then
  11381. * pdma_phys_addr1 should be 0.
  11382. * If the caller needs to map more than 256 scatter gather segment then
  11383. * pdma_phys_addr1 should be a valid physical address.
  11384. * physical address for SGLs must be 64 byte aligned.
  11385. * If you are going to map 2 SGL's then the first one must have 256 entries
  11386. * the second sgl can have between 1 and 256 entries.
  11387. *
  11388. * Return codes:
  11389. * 0 - Success
  11390. * -ENXIO, -ENOMEM - Failure
  11391. **/
  11392. int
  11393. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  11394. dma_addr_t pdma_phys_addr0,
  11395. dma_addr_t pdma_phys_addr1,
  11396. uint16_t xritag)
  11397. {
  11398. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  11399. LPFC_MBOXQ_t *mbox;
  11400. int rc;
  11401. uint32_t shdr_status, shdr_add_status;
  11402. uint32_t mbox_tmo;
  11403. union lpfc_sli4_cfg_shdr *shdr;
  11404. if (xritag == NO_XRI) {
  11405. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11406. "0364 Invalid param:\n");
  11407. return -EINVAL;
  11408. }
  11409. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11410. if (!mbox)
  11411. return -ENOMEM;
  11412. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11413. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11414. sizeof(struct lpfc_mbx_post_sgl_pages) -
  11415. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  11416. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  11417. &mbox->u.mqe.un.post_sgl_pages;
  11418. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  11419. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  11420. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  11421. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  11422. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  11423. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  11424. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  11425. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  11426. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  11427. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  11428. if (!phba->sli4_hba.intr_enable)
  11429. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11430. else {
  11431. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  11432. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11433. }
  11434. /* The IOCTL status is embedded in the mailbox subheader. */
  11435. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  11436. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11437. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11438. if (rc != MBX_TIMEOUT)
  11439. mempool_free(mbox, phba->mbox_mem_pool);
  11440. if (shdr_status || shdr_add_status || rc) {
  11441. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11442. "2511 POST_SGL mailbox failed with "
  11443. "status x%x add_status x%x, mbx status x%x\n",
  11444. shdr_status, shdr_add_status, rc);
  11445. rc = -ENXIO;
  11446. }
  11447. return 0;
  11448. }
  11449. /**
  11450. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  11451. * @phba: pointer to lpfc hba data structure.
  11452. *
  11453. * This routine is invoked to post rpi header templates to the
  11454. * HBA consistent with the SLI-4 interface spec. This routine
  11455. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  11456. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  11457. *
  11458. * Returns
  11459. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  11460. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  11461. **/
  11462. uint16_t
  11463. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  11464. {
  11465. unsigned long xri;
  11466. /*
  11467. * Fetch the next logical xri. Because this index is logical,
  11468. * the driver starts at 0 each time.
  11469. */
  11470. spin_lock_irq(&phba->hbalock);
  11471. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  11472. phba->sli4_hba.max_cfg_param.max_xri, 0);
  11473. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  11474. spin_unlock_irq(&phba->hbalock);
  11475. return NO_XRI;
  11476. } else {
  11477. set_bit(xri, phba->sli4_hba.xri_bmask);
  11478. phba->sli4_hba.max_cfg_param.xri_used++;
  11479. phba->sli4_hba.xri_count++;
  11480. }
  11481. spin_unlock_irq(&phba->hbalock);
  11482. return xri;
  11483. }
  11484. /**
  11485. * lpfc_sli4_free_xri - Release an xri for reuse.
  11486. * @phba: pointer to lpfc hba data structure.
  11487. *
  11488. * This routine is invoked to release an xri to the pool of
  11489. * available rpis maintained by the driver.
  11490. **/
  11491. void
  11492. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11493. {
  11494. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  11495. phba->sli4_hba.xri_count--;
  11496. phba->sli4_hba.max_cfg_param.xri_used--;
  11497. }
  11498. }
  11499. /**
  11500. * lpfc_sli4_free_xri - Release an xri for reuse.
  11501. * @phba: pointer to lpfc hba data structure.
  11502. *
  11503. * This routine is invoked to release an xri to the pool of
  11504. * available rpis maintained by the driver.
  11505. **/
  11506. void
  11507. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11508. {
  11509. spin_lock_irq(&phba->hbalock);
  11510. __lpfc_sli4_free_xri(phba, xri);
  11511. spin_unlock_irq(&phba->hbalock);
  11512. }
  11513. /**
  11514. * lpfc_sli4_next_xritag - Get an xritag for the io
  11515. * @phba: Pointer to HBA context object.
  11516. *
  11517. * This function gets an xritag for the iocb. If there is no unused xritag
  11518. * it will return 0xffff.
  11519. * The function returns the allocated xritag if successful, else returns zero.
  11520. * Zero is not a valid xritag.
  11521. * The caller is not required to hold any lock.
  11522. **/
  11523. uint16_t
  11524. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  11525. {
  11526. uint16_t xri_index;
  11527. xri_index = lpfc_sli4_alloc_xri(phba);
  11528. if (xri_index != NO_XRI)
  11529. return xri_index;
  11530. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11531. "2004 Failed to allocate XRI.last XRITAG is %d"
  11532. " Max XRI is %d, Used XRI is %d\n",
  11533. xri_index,
  11534. phba->sli4_hba.max_cfg_param.max_xri,
  11535. phba->sli4_hba.max_cfg_param.xri_used);
  11536. return NO_XRI;
  11537. }
  11538. /**
  11539. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  11540. * @phba: pointer to lpfc hba data structure.
  11541. *
  11542. * This routine is invoked to post a block of driver's sgl pages to the
  11543. * HBA using non-embedded mailbox command. No Lock is held. This routine
  11544. * is only called when the driver is loading and after all IO has been
  11545. * stopped.
  11546. **/
  11547. int
  11548. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
  11549. {
  11550. struct lpfc_sglq *sglq_entry;
  11551. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  11552. struct sgl_page_pairs *sgl_pg_pairs;
  11553. void *viraddr;
  11554. LPFC_MBOXQ_t *mbox;
  11555. uint32_t reqlen, alloclen, pg_pairs;
  11556. uint32_t mbox_tmo;
  11557. uint16_t xritag_start = 0, lxri = 0;
  11558. int els_xri_cnt, rc = 0;
  11559. uint32_t shdr_status, shdr_add_status;
  11560. union lpfc_sli4_cfg_shdr *shdr;
  11561. /* The number of sgls to be posted */
  11562. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  11563. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  11564. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  11565. if (reqlen > SLI4_PAGE_SIZE) {
  11566. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11567. "2559 Block sgl registration required DMA "
  11568. "size (%d) great than a page\n", reqlen);
  11569. return -ENOMEM;
  11570. }
  11571. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11572. if (!mbox)
  11573. return -ENOMEM;
  11574. /* Allocate DMA memory and set up the non-embedded mailbox command */
  11575. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11576. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  11577. LPFC_SLI4_MBX_NEMBED);
  11578. if (alloclen < reqlen) {
  11579. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11580. "0285 Allocated DMA memory size (%d) is "
  11581. "less than the requested DMA memory "
  11582. "size (%d)\n", alloclen, reqlen);
  11583. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11584. return -ENOMEM;
  11585. }
  11586. /* Set up the SGL pages in the non-embedded DMA pages */
  11587. viraddr = mbox->sge_array->addr[0];
  11588. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  11589. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  11590. for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
  11591. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
  11592. /*
  11593. * Assign the sglq a physical xri only if the driver has not
  11594. * initialized those resources. A port reset only needs
  11595. * the sglq's posted.
  11596. */
  11597. if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  11598. LPFC_XRI_RSRC_RDY) {
  11599. lxri = lpfc_sli4_next_xritag(phba);
  11600. if (lxri == NO_XRI) {
  11601. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11602. return -ENOMEM;
  11603. }
  11604. sglq_entry->sli4_lxritag = lxri;
  11605. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  11606. }
  11607. /* Set up the sge entry */
  11608. sgl_pg_pairs->sgl_pg0_addr_lo =
  11609. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  11610. sgl_pg_pairs->sgl_pg0_addr_hi =
  11611. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  11612. sgl_pg_pairs->sgl_pg1_addr_lo =
  11613. cpu_to_le32(putPaddrLow(0));
  11614. sgl_pg_pairs->sgl_pg1_addr_hi =
  11615. cpu_to_le32(putPaddrHigh(0));
  11616. /* Keep the first xritag on the list */
  11617. if (pg_pairs == 0)
  11618. xritag_start = sglq_entry->sli4_xritag;
  11619. sgl_pg_pairs++;
  11620. }
  11621. /* Complete initialization and perform endian conversion. */
  11622. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  11623. bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
  11624. sgl->word0 = cpu_to_le32(sgl->word0);
  11625. if (!phba->sli4_hba.intr_enable)
  11626. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11627. else {
  11628. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  11629. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11630. }
  11631. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  11632. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11633. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11634. if (rc != MBX_TIMEOUT)
  11635. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11636. if (shdr_status || shdr_add_status || rc) {
  11637. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11638. "2513 POST_SGL_BLOCK mailbox command failed "
  11639. "status x%x add_status x%x mbx status x%x\n",
  11640. shdr_status, shdr_add_status, rc);
  11641. rc = -ENXIO;
  11642. }
  11643. if (rc == 0)
  11644. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  11645. LPFC_XRI_RSRC_RDY);
  11646. return rc;
  11647. }
  11648. /**
  11649. * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
  11650. * @phba: pointer to lpfc hba data structure.
  11651. *
  11652. * This routine is invoked to post a block of driver's sgl pages to the
  11653. * HBA using non-embedded mailbox command. No Lock is held. This routine
  11654. * is only called when the driver is loading and after all IO has been
  11655. * stopped.
  11656. **/
  11657. int
  11658. lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
  11659. {
  11660. struct lpfc_sglq *sglq_entry;
  11661. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  11662. struct sgl_page_pairs *sgl_pg_pairs;
  11663. void *viraddr;
  11664. LPFC_MBOXQ_t *mbox;
  11665. uint32_t reqlen, alloclen, index;
  11666. uint32_t mbox_tmo;
  11667. uint16_t rsrc_start, rsrc_size, els_xri_cnt;
  11668. uint16_t xritag_start = 0, lxri = 0;
  11669. struct lpfc_rsrc_blks *rsrc_blk;
  11670. int cnt, ttl_cnt, rc = 0;
  11671. int loop_cnt;
  11672. uint32_t shdr_status, shdr_add_status;
  11673. union lpfc_sli4_cfg_shdr *shdr;
  11674. /* The number of sgls to be posted */
  11675. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  11676. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  11677. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  11678. if (reqlen > SLI4_PAGE_SIZE) {
  11679. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11680. "2989 Block sgl registration required DMA "
  11681. "size (%d) great than a page\n", reqlen);
  11682. return -ENOMEM;
  11683. }
  11684. cnt = 0;
  11685. ttl_cnt = 0;
  11686. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  11687. list) {
  11688. rsrc_start = rsrc_blk->rsrc_start;
  11689. rsrc_size = rsrc_blk->rsrc_size;
  11690. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11691. "3014 Working ELS Extent start %d, cnt %d\n",
  11692. rsrc_start, rsrc_size);
  11693. loop_cnt = min(els_xri_cnt, rsrc_size);
  11694. if (ttl_cnt + loop_cnt >= els_xri_cnt) {
  11695. loop_cnt = els_xri_cnt - ttl_cnt;
  11696. ttl_cnt = els_xri_cnt;
  11697. }
  11698. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11699. if (!mbox)
  11700. return -ENOMEM;
  11701. /*
  11702. * Allocate DMA memory and set up the non-embedded mailbox
  11703. * command.
  11704. */
  11705. alloclen = lpfc_sli4_config(phba, mbox,
  11706. LPFC_MBOX_SUBSYSTEM_FCOE,
  11707. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11708. reqlen, LPFC_SLI4_MBX_NEMBED);
  11709. if (alloclen < reqlen) {
  11710. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11711. "2987 Allocated DMA memory size (%d) "
  11712. "is less than the requested DMA memory "
  11713. "size (%d)\n", alloclen, reqlen);
  11714. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11715. return -ENOMEM;
  11716. }
  11717. /* Set up the SGL pages in the non-embedded DMA pages */
  11718. viraddr = mbox->sge_array->addr[0];
  11719. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  11720. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  11721. /*
  11722. * The starting resource may not begin at zero. Control
  11723. * the loop variants via the block resource parameters,
  11724. * but handle the sge pointers with a zero-based index
  11725. * that doesn't get reset per loop pass.
  11726. */
  11727. for (index = rsrc_start;
  11728. index < rsrc_start + loop_cnt;
  11729. index++) {
  11730. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
  11731. /*
  11732. * Assign the sglq a physical xri only if the driver
  11733. * has not initialized those resources. A port reset
  11734. * only needs the sglq's posted.
  11735. */
  11736. if (bf_get(lpfc_xri_rsrc_rdy,
  11737. &phba->sli4_hba.sli4_flags) !=
  11738. LPFC_XRI_RSRC_RDY) {
  11739. lxri = lpfc_sli4_next_xritag(phba);
  11740. if (lxri == NO_XRI) {
  11741. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11742. rc = -ENOMEM;
  11743. goto err_exit;
  11744. }
  11745. sglq_entry->sli4_lxritag = lxri;
  11746. sglq_entry->sli4_xritag =
  11747. phba->sli4_hba.xri_ids[lxri];
  11748. }
  11749. /* Set up the sge entry */
  11750. sgl_pg_pairs->sgl_pg0_addr_lo =
  11751. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  11752. sgl_pg_pairs->sgl_pg0_addr_hi =
  11753. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  11754. sgl_pg_pairs->sgl_pg1_addr_lo =
  11755. cpu_to_le32(putPaddrLow(0));
  11756. sgl_pg_pairs->sgl_pg1_addr_hi =
  11757. cpu_to_le32(putPaddrHigh(0));
  11758. /* Track the starting physical XRI for the mailbox. */
  11759. if (index == rsrc_start)
  11760. xritag_start = sglq_entry->sli4_xritag;
  11761. sgl_pg_pairs++;
  11762. cnt++;
  11763. }
  11764. /* Complete initialization and perform endian conversion. */
  11765. rsrc_blk->rsrc_used += loop_cnt;
  11766. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  11767. bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
  11768. sgl->word0 = cpu_to_le32(sgl->word0);
  11769. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11770. "3015 Post ELS Extent SGL, start %d, "
  11771. "cnt %d, used %d\n",
  11772. xritag_start, loop_cnt, rsrc_blk->rsrc_used);
  11773. if (!phba->sli4_hba.intr_enable)
  11774. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11775. else {
  11776. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  11777. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11778. }
  11779. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  11780. shdr_status = bf_get(lpfc_mbox_hdr_status,
  11781. &shdr->response);
  11782. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  11783. &shdr->response);
  11784. if (rc != MBX_TIMEOUT)
  11785. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11786. if (shdr_status || shdr_add_status || rc) {
  11787. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11788. "2988 POST_SGL_BLOCK mailbox "
  11789. "command failed status x%x "
  11790. "add_status x%x mbx status x%x\n",
  11791. shdr_status, shdr_add_status, rc);
  11792. rc = -ENXIO;
  11793. goto err_exit;
  11794. }
  11795. if (ttl_cnt >= els_xri_cnt)
  11796. break;
  11797. }
  11798. err_exit:
  11799. if (rc == 0)
  11800. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  11801. LPFC_XRI_RSRC_RDY);
  11802. return rc;
  11803. }
  11804. /**
  11805. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  11806. * @phba: pointer to lpfc hba data structure.
  11807. * @sblist: pointer to scsi buffer list.
  11808. * @count: number of scsi buffers on the list.
  11809. *
  11810. * This routine is invoked to post a block of @count scsi sgl pages from a
  11811. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  11812. * No Lock is held.
  11813. *
  11814. **/
  11815. int
  11816. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
  11817. int cnt)
  11818. {
  11819. struct lpfc_scsi_buf *psb;
  11820. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  11821. struct sgl_page_pairs *sgl_pg_pairs;
  11822. void *viraddr;
  11823. LPFC_MBOXQ_t *mbox;
  11824. uint32_t reqlen, alloclen, pg_pairs;
  11825. uint32_t mbox_tmo;
  11826. uint16_t xritag_start = 0;
  11827. int rc = 0;
  11828. uint32_t shdr_status, shdr_add_status;
  11829. dma_addr_t pdma_phys_bpl1;
  11830. union lpfc_sli4_cfg_shdr *shdr;
  11831. /* Calculate the requested length of the dma memory */
  11832. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  11833. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  11834. if (reqlen > SLI4_PAGE_SIZE) {
  11835. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11836. "0217 Block sgl registration required DMA "
  11837. "size (%d) great than a page\n", reqlen);
  11838. return -ENOMEM;
  11839. }
  11840. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11841. if (!mbox) {
  11842. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11843. "0283 Failed to allocate mbox cmd memory\n");
  11844. return -ENOMEM;
  11845. }
  11846. /* Allocate DMA memory and set up the non-embedded mailbox command */
  11847. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11848. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  11849. LPFC_SLI4_MBX_NEMBED);
  11850. if (alloclen < reqlen) {
  11851. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11852. "2561 Allocated DMA memory size (%d) is "
  11853. "less than the requested DMA memory "
  11854. "size (%d)\n", alloclen, reqlen);
  11855. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11856. return -ENOMEM;
  11857. }
  11858. /* Get the first SGE entry from the non-embedded DMA memory */
  11859. viraddr = mbox->sge_array->addr[0];
  11860. /* Set up the SGL pages in the non-embedded DMA pages */
  11861. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  11862. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  11863. pg_pairs = 0;
  11864. list_for_each_entry(psb, sblist, list) {
  11865. /* Set up the sge entry */
  11866. sgl_pg_pairs->sgl_pg0_addr_lo =
  11867. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  11868. sgl_pg_pairs->sgl_pg0_addr_hi =
  11869. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  11870. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  11871. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  11872. else
  11873. pdma_phys_bpl1 = 0;
  11874. sgl_pg_pairs->sgl_pg1_addr_lo =
  11875. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  11876. sgl_pg_pairs->sgl_pg1_addr_hi =
  11877. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  11878. /* Keep the first xritag on the list */
  11879. if (pg_pairs == 0)
  11880. xritag_start = psb->cur_iocbq.sli4_xritag;
  11881. sgl_pg_pairs++;
  11882. pg_pairs++;
  11883. }
  11884. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  11885. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  11886. /* Perform endian conversion if necessary */
  11887. sgl->word0 = cpu_to_le32(sgl->word0);
  11888. if (!phba->sli4_hba.intr_enable)
  11889. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11890. else {
  11891. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  11892. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11893. }
  11894. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  11895. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11896. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11897. if (rc != MBX_TIMEOUT)
  11898. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11899. if (shdr_status || shdr_add_status || rc) {
  11900. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11901. "2564 POST_SGL_BLOCK mailbox command failed "
  11902. "status x%x add_status x%x mbx status x%x\n",
  11903. shdr_status, shdr_add_status, rc);
  11904. rc = -ENXIO;
  11905. }
  11906. return rc;
  11907. }
  11908. /**
  11909. * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
  11910. * @phba: pointer to lpfc hba data structure.
  11911. * @sblist: pointer to scsi buffer list.
  11912. * @count: number of scsi buffers on the list.
  11913. *
  11914. * This routine is invoked to post a block of @count scsi sgl pages from a
  11915. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  11916. * No Lock is held.
  11917. *
  11918. **/
  11919. int
  11920. lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
  11921. int cnt)
  11922. {
  11923. struct lpfc_scsi_buf *psb = NULL;
  11924. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  11925. struct sgl_page_pairs *sgl_pg_pairs;
  11926. void *viraddr;
  11927. LPFC_MBOXQ_t *mbox;
  11928. uint32_t reqlen, alloclen, pg_pairs;
  11929. uint32_t mbox_tmo;
  11930. uint16_t xri_start = 0, scsi_xri_start;
  11931. uint16_t rsrc_range;
  11932. int rc = 0, avail_cnt;
  11933. uint32_t shdr_status, shdr_add_status;
  11934. dma_addr_t pdma_phys_bpl1;
  11935. union lpfc_sli4_cfg_shdr *shdr;
  11936. struct lpfc_rsrc_blks *rsrc_blk;
  11937. uint32_t xri_cnt = 0;
  11938. /* Calculate the total requested length of the dma memory */
  11939. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  11940. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  11941. if (reqlen > SLI4_PAGE_SIZE) {
  11942. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11943. "2932 Block sgl registration required DMA "
  11944. "size (%d) great than a page\n", reqlen);
  11945. return -ENOMEM;
  11946. }
  11947. /*
  11948. * The use of extents requires the driver to post the sgl headers
  11949. * in multiple postings to meet the contiguous resource assignment.
  11950. */
  11951. psb = list_prepare_entry(psb, sblist, list);
  11952. scsi_xri_start = phba->sli4_hba.scsi_xri_start;
  11953. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  11954. list) {
  11955. rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
  11956. if (rsrc_range < scsi_xri_start)
  11957. continue;
  11958. else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
  11959. continue;
  11960. else
  11961. avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
  11962. reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
  11963. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  11964. /*
  11965. * Allocate DMA memory and set up the non-embedded mailbox
  11966. * command. The mbox is used to post an SGL page per loop
  11967. * but the DMA memory has a use-once semantic so the mailbox
  11968. * is used and freed per loop pass.
  11969. */
  11970. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11971. if (!mbox) {
  11972. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11973. "2933 Failed to allocate mbox cmd "
  11974. "memory\n");
  11975. return -ENOMEM;
  11976. }
  11977. alloclen = lpfc_sli4_config(phba, mbox,
  11978. LPFC_MBOX_SUBSYSTEM_FCOE,
  11979. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11980. reqlen,
  11981. LPFC_SLI4_MBX_NEMBED);
  11982. if (alloclen < reqlen) {
  11983. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11984. "2934 Allocated DMA memory size (%d) "
  11985. "is less than the requested DMA memory "
  11986. "size (%d)\n", alloclen, reqlen);
  11987. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11988. return -ENOMEM;
  11989. }
  11990. /* Get the first SGE entry from the non-embedded DMA memory */
  11991. viraddr = mbox->sge_array->addr[0];
  11992. /* Set up the SGL pages in the non-embedded DMA pages */
  11993. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  11994. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  11995. /* pg_pairs tracks posted SGEs per loop iteration. */
  11996. pg_pairs = 0;
  11997. list_for_each_entry_continue(psb, sblist, list) {
  11998. /* Set up the sge entry */
  11999. sgl_pg_pairs->sgl_pg0_addr_lo =
  12000. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12001. sgl_pg_pairs->sgl_pg0_addr_hi =
  12002. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12003. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12004. pdma_phys_bpl1 = psb->dma_phys_bpl +
  12005. SGL_PAGE_SIZE;
  12006. else
  12007. pdma_phys_bpl1 = 0;
  12008. sgl_pg_pairs->sgl_pg1_addr_lo =
  12009. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12010. sgl_pg_pairs->sgl_pg1_addr_hi =
  12011. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12012. /* Keep the first xri for this extent. */
  12013. if (pg_pairs == 0)
  12014. xri_start = psb->cur_iocbq.sli4_xritag;
  12015. sgl_pg_pairs++;
  12016. pg_pairs++;
  12017. xri_cnt++;
  12018. /*
  12019. * Track two exit conditions - the loop has constructed
  12020. * all of the caller's SGE pairs or all available
  12021. * resource IDs in this extent are consumed.
  12022. */
  12023. if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
  12024. break;
  12025. }
  12026. rsrc_blk->rsrc_used += pg_pairs;
  12027. bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
  12028. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12029. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12030. "3016 Post SCSI Extent SGL, start %d, cnt %d "
  12031. "blk use %d\n",
  12032. xri_start, pg_pairs, rsrc_blk->rsrc_used);
  12033. /* Perform endian conversion if necessary */
  12034. sgl->word0 = cpu_to_le32(sgl->word0);
  12035. if (!phba->sli4_hba.intr_enable)
  12036. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12037. else {
  12038. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  12039. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12040. }
  12041. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12042. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12043. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12044. &shdr->response);
  12045. if (rc != MBX_TIMEOUT)
  12046. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12047. if (shdr_status || shdr_add_status || rc) {
  12048. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12049. "2935 POST_SGL_BLOCK mailbox command "
  12050. "failed status x%x add_status x%x "
  12051. "mbx status x%x\n",
  12052. shdr_status, shdr_add_status, rc);
  12053. return -ENXIO;
  12054. }
  12055. /* Post only what is requested. */
  12056. if (xri_cnt >= cnt)
  12057. break;
  12058. }
  12059. return rc;
  12060. }
  12061. /**
  12062. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12063. * @phba: pointer to lpfc_hba struct that the frame was received on
  12064. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12065. *
  12066. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12067. * valid type of frame that the LPFC driver will handle. This function will
  12068. * return a zero if the frame is a valid frame or a non zero value when the
  12069. * frame does not pass the check.
  12070. **/
  12071. static int
  12072. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12073. {
  12074. /* make rctl_names static to save stack space */
  12075. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12076. char *type_names[] = FC_TYPE_NAMES_INIT;
  12077. struct fc_vft_header *fc_vft_hdr;
  12078. uint32_t *header = (uint32_t *) fc_hdr;
  12079. switch (fc_hdr->fh_r_ctl) {
  12080. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12081. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12082. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12083. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12084. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12085. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12086. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12087. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12088. case FC_RCTL_ELS_REQ: /* extended link services request */
  12089. case FC_RCTL_ELS_REP: /* extended link services reply */
  12090. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12091. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12092. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12093. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12094. case FC_RCTL_BA_RMC: /* remove connection */
  12095. case FC_RCTL_BA_ACC: /* basic accept */
  12096. case FC_RCTL_BA_RJT: /* basic reject */
  12097. case FC_RCTL_BA_PRMT:
  12098. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12099. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12100. case FC_RCTL_P_RJT: /* port reject */
  12101. case FC_RCTL_F_RJT: /* fabric reject */
  12102. case FC_RCTL_P_BSY: /* port busy */
  12103. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12104. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12105. case FC_RCTL_LCR: /* link credit reset */
  12106. case FC_RCTL_END: /* end */
  12107. break;
  12108. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12109. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12110. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12111. return lpfc_fc_frame_check(phba, fc_hdr);
  12112. default:
  12113. goto drop;
  12114. }
  12115. switch (fc_hdr->fh_type) {
  12116. case FC_TYPE_BLS:
  12117. case FC_TYPE_ELS:
  12118. case FC_TYPE_FCP:
  12119. case FC_TYPE_CT:
  12120. break;
  12121. case FC_TYPE_IP:
  12122. case FC_TYPE_ILS:
  12123. default:
  12124. goto drop;
  12125. }
  12126. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12127. "2538 Received frame rctl:%s type:%s "
  12128. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12129. rctl_names[fc_hdr->fh_r_ctl],
  12130. type_names[fc_hdr->fh_type],
  12131. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12132. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12133. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12134. return 0;
  12135. drop:
  12136. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12137. "2539 Dropped frame rctl:%s type:%s\n",
  12138. rctl_names[fc_hdr->fh_r_ctl],
  12139. type_names[fc_hdr->fh_type]);
  12140. return 1;
  12141. }
  12142. /**
  12143. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12144. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12145. *
  12146. * This function processes the FC header to retrieve the VFI from the VF
  12147. * header, if one exists. This function will return the VFI if one exists
  12148. * or 0 if no VSAN Header exists.
  12149. **/
  12150. static uint32_t
  12151. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12152. {
  12153. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12154. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12155. return 0;
  12156. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12157. }
  12158. /**
  12159. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12160. * @phba: Pointer to the HBA structure to search for the vport on
  12161. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12162. * @fcfi: The FC Fabric ID that the frame came from
  12163. *
  12164. * This function searches the @phba for a vport that matches the content of the
  12165. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12166. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12167. * returns the matching vport pointer or NULL if unable to match frame to a
  12168. * vport.
  12169. **/
  12170. static struct lpfc_vport *
  12171. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12172. uint16_t fcfi)
  12173. {
  12174. struct lpfc_vport **vports;
  12175. struct lpfc_vport *vport = NULL;
  12176. int i;
  12177. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12178. fc_hdr->fh_d_id[1] << 8 |
  12179. fc_hdr->fh_d_id[2]);
  12180. vports = lpfc_create_vport_work_array(phba);
  12181. if (vports != NULL)
  12182. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12183. if (phba->fcf.fcfi == fcfi &&
  12184. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12185. vports[i]->fc_myDID == did) {
  12186. vport = vports[i];
  12187. break;
  12188. }
  12189. }
  12190. lpfc_destroy_vport_work_array(phba, vports);
  12191. return vport;
  12192. }
  12193. /**
  12194. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12195. * @vport: The vport to work on.
  12196. *
  12197. * This function updates the receive sequence time stamp for this vport. The
  12198. * receive sequence time stamp indicates the time that the last frame of the
  12199. * the sequence that has been idle for the longest amount of time was received.
  12200. * the driver uses this time stamp to indicate if any received sequences have
  12201. * timed out.
  12202. **/
  12203. void
  12204. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12205. {
  12206. struct lpfc_dmabuf *h_buf;
  12207. struct hbq_dmabuf *dmabuf = NULL;
  12208. /* get the oldest sequence on the rcv list */
  12209. h_buf = list_get_first(&vport->rcv_buffer_list,
  12210. struct lpfc_dmabuf, list);
  12211. if (!h_buf)
  12212. return;
  12213. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12214. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12215. }
  12216. /**
  12217. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12218. * @vport: The vport that the received sequences were sent to.
  12219. *
  12220. * This function cleans up all outstanding received sequences. This is called
  12221. * by the driver when a link event or user action invalidates all the received
  12222. * sequences.
  12223. **/
  12224. void
  12225. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12226. {
  12227. struct lpfc_dmabuf *h_buf, *hnext;
  12228. struct lpfc_dmabuf *d_buf, *dnext;
  12229. struct hbq_dmabuf *dmabuf = NULL;
  12230. /* start with the oldest sequence on the rcv list */
  12231. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12232. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12233. list_del_init(&dmabuf->hbuf.list);
  12234. list_for_each_entry_safe(d_buf, dnext,
  12235. &dmabuf->dbuf.list, list) {
  12236. list_del_init(&d_buf->list);
  12237. lpfc_in_buf_free(vport->phba, d_buf);
  12238. }
  12239. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12240. }
  12241. }
  12242. /**
  12243. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12244. * @vport: The vport that the received sequences were sent to.
  12245. *
  12246. * This function determines whether any received sequences have timed out by
  12247. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12248. * indicates that there is at least one timed out sequence this routine will
  12249. * go through the received sequences one at a time from most inactive to most
  12250. * active to determine which ones need to be cleaned up. Once it has determined
  12251. * that a sequence needs to be cleaned up it will simply free up the resources
  12252. * without sending an abort.
  12253. **/
  12254. void
  12255. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12256. {
  12257. struct lpfc_dmabuf *h_buf, *hnext;
  12258. struct lpfc_dmabuf *d_buf, *dnext;
  12259. struct hbq_dmabuf *dmabuf = NULL;
  12260. unsigned long timeout;
  12261. int abort_count = 0;
  12262. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12263. vport->rcv_buffer_time_stamp);
  12264. if (list_empty(&vport->rcv_buffer_list) ||
  12265. time_before(jiffies, timeout))
  12266. return;
  12267. /* start with the oldest sequence on the rcv list */
  12268. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12269. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12270. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12271. dmabuf->time_stamp);
  12272. if (time_before(jiffies, timeout))
  12273. break;
  12274. abort_count++;
  12275. list_del_init(&dmabuf->hbuf.list);
  12276. list_for_each_entry_safe(d_buf, dnext,
  12277. &dmabuf->dbuf.list, list) {
  12278. list_del_init(&d_buf->list);
  12279. lpfc_in_buf_free(vport->phba, d_buf);
  12280. }
  12281. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12282. }
  12283. if (abort_count)
  12284. lpfc_update_rcv_time_stamp(vport);
  12285. }
  12286. /**
  12287. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12288. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12289. *
  12290. * This function searches through the existing incomplete sequences that have
  12291. * been sent to this @vport. If the frame matches one of the incomplete
  12292. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12293. * make up that sequence. If no sequence is found that matches this frame then
  12294. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12295. * This function returns a pointer to the first dmabuf in the sequence list that
  12296. * the frame was linked to.
  12297. **/
  12298. static struct hbq_dmabuf *
  12299. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12300. {
  12301. struct fc_frame_header *new_hdr;
  12302. struct fc_frame_header *temp_hdr;
  12303. struct lpfc_dmabuf *d_buf;
  12304. struct lpfc_dmabuf *h_buf;
  12305. struct hbq_dmabuf *seq_dmabuf = NULL;
  12306. struct hbq_dmabuf *temp_dmabuf = NULL;
  12307. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12308. dmabuf->time_stamp = jiffies;
  12309. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12310. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12311. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12312. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12313. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12314. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12315. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12316. continue;
  12317. /* found a pending sequence that matches this frame */
  12318. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12319. break;
  12320. }
  12321. if (!seq_dmabuf) {
  12322. /*
  12323. * This indicates first frame received for this sequence.
  12324. * Queue the buffer on the vport's rcv_buffer_list.
  12325. */
  12326. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12327. lpfc_update_rcv_time_stamp(vport);
  12328. return dmabuf;
  12329. }
  12330. temp_hdr = seq_dmabuf->hbuf.virt;
  12331. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12332. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12333. list_del_init(&seq_dmabuf->hbuf.list);
  12334. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12335. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12336. lpfc_update_rcv_time_stamp(vport);
  12337. return dmabuf;
  12338. }
  12339. /* move this sequence to the tail to indicate a young sequence */
  12340. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12341. seq_dmabuf->time_stamp = jiffies;
  12342. lpfc_update_rcv_time_stamp(vport);
  12343. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12344. temp_hdr = dmabuf->hbuf.virt;
  12345. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12346. return seq_dmabuf;
  12347. }
  12348. /* find the correct place in the sequence to insert this frame */
  12349. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12350. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12351. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12352. /*
  12353. * If the frame's sequence count is greater than the frame on
  12354. * the list then insert the frame right after this frame
  12355. */
  12356. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12357. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12358. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12359. return seq_dmabuf;
  12360. }
  12361. }
  12362. return NULL;
  12363. }
  12364. /**
  12365. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12366. * @vport: pointer to a vitural port
  12367. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12368. *
  12369. * This function tries to abort from the partially assembed sequence, described
  12370. * by the information from basic abbort @dmabuf. It checks to see whether such
  12371. * partially assembled sequence held by the driver. If so, it shall free up all
  12372. * the frames from the partially assembled sequence.
  12373. *
  12374. * Return
  12375. * true -- if there is matching partially assembled sequence present and all
  12376. * the frames freed with the sequence;
  12377. * false -- if there is no matching partially assembled sequence present so
  12378. * nothing got aborted in the lower layer driver
  12379. **/
  12380. static bool
  12381. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12382. struct hbq_dmabuf *dmabuf)
  12383. {
  12384. struct fc_frame_header *new_hdr;
  12385. struct fc_frame_header *temp_hdr;
  12386. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12387. struct hbq_dmabuf *seq_dmabuf = NULL;
  12388. /* Use the hdr_buf to find the sequence that matches this frame */
  12389. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12390. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12391. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12392. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12393. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12394. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12395. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12396. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12397. continue;
  12398. /* found a pending sequence that matches this frame */
  12399. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12400. break;
  12401. }
  12402. /* Free up all the frames from the partially assembled sequence */
  12403. if (seq_dmabuf) {
  12404. list_for_each_entry_safe(d_buf, n_buf,
  12405. &seq_dmabuf->dbuf.list, list) {
  12406. list_del_init(&d_buf->list);
  12407. lpfc_in_buf_free(vport->phba, d_buf);
  12408. }
  12409. return true;
  12410. }
  12411. return false;
  12412. }
  12413. /**
  12414. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12415. * @phba: Pointer to HBA context object.
  12416. * @cmd_iocbq: pointer to the command iocbq structure.
  12417. * @rsp_iocbq: pointer to the response iocbq structure.
  12418. *
  12419. * This function handles the sequence abort response iocb command complete
  12420. * event. It properly releases the memory allocated to the sequence abort
  12421. * accept iocb.
  12422. **/
  12423. static void
  12424. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12425. struct lpfc_iocbq *cmd_iocbq,
  12426. struct lpfc_iocbq *rsp_iocbq)
  12427. {
  12428. if (cmd_iocbq)
  12429. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12430. }
  12431. /**
  12432. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12433. * @phba: Pointer to HBA context object.
  12434. * @xri: xri id in transaction.
  12435. *
  12436. * This function validates the xri maps to the known range of XRIs allocated an
  12437. * used by the driver.
  12438. **/
  12439. uint16_t
  12440. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12441. uint16_t xri)
  12442. {
  12443. int i;
  12444. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12445. if (xri == phba->sli4_hba.xri_ids[i])
  12446. return i;
  12447. }
  12448. return NO_XRI;
  12449. }
  12450. /**
  12451. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12452. * @phba: Pointer to HBA context object.
  12453. * @fc_hdr: pointer to a FC frame header.
  12454. *
  12455. * This function sends a basic response to a previous unsol sequence abort
  12456. * event after aborting the sequence handling.
  12457. **/
  12458. static void
  12459. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12460. struct fc_frame_header *fc_hdr)
  12461. {
  12462. struct lpfc_iocbq *ctiocb = NULL;
  12463. struct lpfc_nodelist *ndlp;
  12464. uint16_t oxid, rxid;
  12465. uint32_t sid, fctl;
  12466. IOCB_t *icmd;
  12467. int rc;
  12468. if (!lpfc_is_link_up(phba))
  12469. return;
  12470. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12471. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12472. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12473. ndlp = lpfc_findnode_did(phba->pport, sid);
  12474. if (!ndlp) {
  12475. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12476. "1268 Find ndlp returned NULL for oxid:x%x "
  12477. "SID:x%x\n", oxid, sid);
  12478. return;
  12479. }
  12480. if (lpfc_sli4_xri_inrange(phba, rxid))
  12481. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12482. /* Allocate buffer for rsp iocb */
  12483. ctiocb = lpfc_sli_get_iocbq(phba);
  12484. if (!ctiocb)
  12485. return;
  12486. /* Extract the F_CTL field from FC_HDR */
  12487. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12488. icmd = &ctiocb->iocb;
  12489. icmd->un.xseq64.bdl.bdeSize = 0;
  12490. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12491. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12492. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12493. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12494. /* Fill in the rest of iocb fields */
  12495. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12496. icmd->ulpBdeCount = 0;
  12497. icmd->ulpLe = 1;
  12498. icmd->ulpClass = CLASS3;
  12499. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12500. ctiocb->context1 = ndlp;
  12501. ctiocb->iocb_cmpl = NULL;
  12502. ctiocb->vport = phba->pport;
  12503. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12504. ctiocb->sli4_lxritag = NO_XRI;
  12505. ctiocb->sli4_xritag = NO_XRI;
  12506. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12507. * send a BLS_RJT. The driver no longer has that exchange.
  12508. * Override the IOCB for a BA_RJT.
  12509. */
  12510. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  12511. phba->sli4_hba.max_cfg_param.xri_base) ||
  12512. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12513. phba->sli4_hba.max_cfg_param.xri_base)) {
  12514. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12515. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12516. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12517. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12518. }
  12519. if (fctl & FC_FC_EX_CTX) {
  12520. /* ABTS sent by responder to CT exchange, construction
  12521. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12522. * field and RX_ID from ABTS for RX_ID field.
  12523. */
  12524. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12525. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12526. } else {
  12527. /* ABTS sent by initiator to CT exchange, construction
  12528. * of BA_ACC will need to allocate a new XRI as for the
  12529. * XRI_TAG and RX_ID fields.
  12530. */
  12531. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12532. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
  12533. }
  12534. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12535. /* Xmit CT abts response on exchange <xid> */
  12536. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12537. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12538. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12539. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12540. if (rc == IOCB_ERROR) {
  12541. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12542. "2925 Failed to issue CT ABTS RSP x%x on "
  12543. "xri x%x, Data x%x\n",
  12544. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12545. phba->link_state);
  12546. lpfc_sli_release_iocbq(phba, ctiocb);
  12547. }
  12548. }
  12549. /**
  12550. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12551. * @vport: Pointer to the vport on which this sequence was received
  12552. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12553. *
  12554. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  12555. * receive sequence is only partially assembed by the driver, it shall abort
  12556. * the partially assembled frames for the sequence. Otherwise, if the
  12557. * unsolicited receive sequence has been completely assembled and passed to
  12558. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  12559. * unsolicited sequence has been aborted. After that, it will issue a basic
  12560. * accept to accept the abort.
  12561. **/
  12562. void
  12563. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  12564. struct hbq_dmabuf *dmabuf)
  12565. {
  12566. struct lpfc_hba *phba = vport->phba;
  12567. struct fc_frame_header fc_hdr;
  12568. uint32_t fctl;
  12569. bool abts_par;
  12570. /* Make a copy of fc_hdr before the dmabuf being released */
  12571. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  12572. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  12573. if (fctl & FC_FC_EX_CTX) {
  12574. /*
  12575. * ABTS sent by responder to exchange, just free the buffer
  12576. */
  12577. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12578. } else {
  12579. /*
  12580. * ABTS sent by initiator to exchange, need to do cleanup
  12581. */
  12582. /* Try to abort partially assembled seq */
  12583. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  12584. /* Send abort to ULP if partially seq abort failed */
  12585. if (abts_par == false)
  12586. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  12587. else
  12588. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12589. }
  12590. /* Send basic accept (BA_ACC) to the abort requester */
  12591. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  12592. }
  12593. /**
  12594. * lpfc_seq_complete - Indicates if a sequence is complete
  12595. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12596. *
  12597. * This function checks the sequence, starting with the frame described by
  12598. * @dmabuf, to see if all the frames associated with this sequence are present.
  12599. * the frames associated with this sequence are linked to the @dmabuf using the
  12600. * dbuf list. This function looks for two major things. 1) That the first frame
  12601. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  12602. * set. 3) That there are no holes in the sequence count. The function will
  12603. * return 1 when the sequence is complete, otherwise it will return 0.
  12604. **/
  12605. static int
  12606. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  12607. {
  12608. struct fc_frame_header *hdr;
  12609. struct lpfc_dmabuf *d_buf;
  12610. struct hbq_dmabuf *seq_dmabuf;
  12611. uint32_t fctl;
  12612. int seq_count = 0;
  12613. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12614. /* make sure first fame of sequence has a sequence count of zero */
  12615. if (hdr->fh_seq_cnt != seq_count)
  12616. return 0;
  12617. fctl = (hdr->fh_f_ctl[0] << 16 |
  12618. hdr->fh_f_ctl[1] << 8 |
  12619. hdr->fh_f_ctl[2]);
  12620. /* If last frame of sequence we can return success. */
  12621. if (fctl & FC_FC_END_SEQ)
  12622. return 1;
  12623. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  12624. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12625. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12626. /* If there is a hole in the sequence count then fail. */
  12627. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  12628. return 0;
  12629. fctl = (hdr->fh_f_ctl[0] << 16 |
  12630. hdr->fh_f_ctl[1] << 8 |
  12631. hdr->fh_f_ctl[2]);
  12632. /* If last frame of sequence we can return success. */
  12633. if (fctl & FC_FC_END_SEQ)
  12634. return 1;
  12635. }
  12636. return 0;
  12637. }
  12638. /**
  12639. * lpfc_prep_seq - Prep sequence for ULP processing
  12640. * @vport: Pointer to the vport on which this sequence was received
  12641. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12642. *
  12643. * This function takes a sequence, described by a list of frames, and creates
  12644. * a list of iocbq structures to describe the sequence. This iocbq list will be
  12645. * used to issue to the generic unsolicited sequence handler. This routine
  12646. * returns a pointer to the first iocbq in the list. If the function is unable
  12647. * to allocate an iocbq then it throw out the received frames that were not
  12648. * able to be described and return a pointer to the first iocbq. If unable to
  12649. * allocate any iocbqs (including the first) this function will return NULL.
  12650. **/
  12651. static struct lpfc_iocbq *
  12652. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  12653. {
  12654. struct hbq_dmabuf *hbq_buf;
  12655. struct lpfc_dmabuf *d_buf, *n_buf;
  12656. struct lpfc_iocbq *first_iocbq, *iocbq;
  12657. struct fc_frame_header *fc_hdr;
  12658. uint32_t sid;
  12659. uint32_t len, tot_len;
  12660. struct ulp_bde64 *pbde;
  12661. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12662. /* remove from receive buffer list */
  12663. list_del_init(&seq_dmabuf->hbuf.list);
  12664. lpfc_update_rcv_time_stamp(vport);
  12665. /* get the Remote Port's SID */
  12666. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12667. tot_len = 0;
  12668. /* Get an iocbq struct to fill in. */
  12669. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  12670. if (first_iocbq) {
  12671. /* Initialize the first IOCB. */
  12672. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  12673. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  12674. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  12675. first_iocbq->iocb.ulpContext = NO_XRI;
  12676. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  12677. be16_to_cpu(fc_hdr->fh_ox_id);
  12678. /* iocbq is prepped for internal consumption. Physical vpi. */
  12679. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  12680. vport->phba->vpi_ids[vport->vpi];
  12681. /* put the first buffer into the first IOCBq */
  12682. first_iocbq->context2 = &seq_dmabuf->dbuf;
  12683. first_iocbq->context3 = NULL;
  12684. first_iocbq->iocb.ulpBdeCount = 1;
  12685. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  12686. LPFC_DATA_BUF_SIZE;
  12687. first_iocbq->iocb.un.rcvels.remoteID = sid;
  12688. tot_len = bf_get(lpfc_rcqe_length,
  12689. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  12690. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  12691. }
  12692. iocbq = first_iocbq;
  12693. /*
  12694. * Each IOCBq can have two Buffers assigned, so go through the list
  12695. * of buffers for this sequence and save two buffers in each IOCBq
  12696. */
  12697. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  12698. if (!iocbq) {
  12699. lpfc_in_buf_free(vport->phba, d_buf);
  12700. continue;
  12701. }
  12702. if (!iocbq->context3) {
  12703. iocbq->context3 = d_buf;
  12704. iocbq->iocb.ulpBdeCount++;
  12705. pbde = (struct ulp_bde64 *)
  12706. &iocbq->iocb.unsli3.sli3Words[4];
  12707. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  12708. /* We need to get the size out of the right CQE */
  12709. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12710. len = bf_get(lpfc_rcqe_length,
  12711. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  12712. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  12713. tot_len += len;
  12714. } else {
  12715. iocbq = lpfc_sli_get_iocbq(vport->phba);
  12716. if (!iocbq) {
  12717. if (first_iocbq) {
  12718. first_iocbq->iocb.ulpStatus =
  12719. IOSTAT_FCP_RSP_ERROR;
  12720. first_iocbq->iocb.un.ulpWord[4] =
  12721. IOERR_NO_RESOURCES;
  12722. }
  12723. lpfc_in_buf_free(vport->phba, d_buf);
  12724. continue;
  12725. }
  12726. iocbq->context2 = d_buf;
  12727. iocbq->context3 = NULL;
  12728. iocbq->iocb.ulpBdeCount = 1;
  12729. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  12730. LPFC_DATA_BUF_SIZE;
  12731. /* We need to get the size out of the right CQE */
  12732. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12733. len = bf_get(lpfc_rcqe_length,
  12734. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  12735. tot_len += len;
  12736. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  12737. iocbq->iocb.un.rcvels.remoteID = sid;
  12738. list_add_tail(&iocbq->list, &first_iocbq->list);
  12739. }
  12740. }
  12741. return first_iocbq;
  12742. }
  12743. static void
  12744. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  12745. struct hbq_dmabuf *seq_dmabuf)
  12746. {
  12747. struct fc_frame_header *fc_hdr;
  12748. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  12749. struct lpfc_hba *phba = vport->phba;
  12750. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12751. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  12752. if (!iocbq) {
  12753. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12754. "2707 Ring %d handler: Failed to allocate "
  12755. "iocb Rctl x%x Type x%x received\n",
  12756. LPFC_ELS_RING,
  12757. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  12758. return;
  12759. }
  12760. if (!lpfc_complete_unsol_iocb(phba,
  12761. &phba->sli.ring[LPFC_ELS_RING],
  12762. iocbq, fc_hdr->fh_r_ctl,
  12763. fc_hdr->fh_type))
  12764. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12765. "2540 Ring %d handler: unexpected Rctl "
  12766. "x%x Type x%x received\n",
  12767. LPFC_ELS_RING,
  12768. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  12769. /* Free iocb created in lpfc_prep_seq */
  12770. list_for_each_entry_safe(curr_iocb, next_iocb,
  12771. &iocbq->list, list) {
  12772. list_del_init(&curr_iocb->list);
  12773. lpfc_sli_release_iocbq(phba, curr_iocb);
  12774. }
  12775. lpfc_sli_release_iocbq(phba, iocbq);
  12776. }
  12777. /**
  12778. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  12779. * @phba: Pointer to HBA context object.
  12780. *
  12781. * This function is called with no lock held. This function processes all
  12782. * the received buffers and gives it to upper layers when a received buffer
  12783. * indicates that it is the final frame in the sequence. The interrupt
  12784. * service routine processes received buffers at interrupt contexts and adds
  12785. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  12786. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  12787. * appropriate receive function when the final frame in a sequence is received.
  12788. **/
  12789. void
  12790. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  12791. struct hbq_dmabuf *dmabuf)
  12792. {
  12793. struct hbq_dmabuf *seq_dmabuf;
  12794. struct fc_frame_header *fc_hdr;
  12795. struct lpfc_vport *vport;
  12796. uint32_t fcfi;
  12797. /* Process each received buffer */
  12798. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12799. /* check to see if this a valid type of frame */
  12800. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  12801. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12802. return;
  12803. }
  12804. if ((bf_get(lpfc_cqe_code,
  12805. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  12806. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  12807. &dmabuf->cq_event.cqe.rcqe_cmpl);
  12808. else
  12809. fcfi = bf_get(lpfc_rcqe_fcf_id,
  12810. &dmabuf->cq_event.cqe.rcqe_cmpl);
  12811. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  12812. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  12813. /* throw out the frame */
  12814. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12815. return;
  12816. }
  12817. /* Handle the basic abort sequence (BA_ABTS) event */
  12818. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  12819. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  12820. return;
  12821. }
  12822. /* Link this frame */
  12823. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  12824. if (!seq_dmabuf) {
  12825. /* unable to add frame to vport - throw it out */
  12826. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12827. return;
  12828. }
  12829. /* If not last frame in sequence continue processing frames. */
  12830. if (!lpfc_seq_complete(seq_dmabuf))
  12831. return;
  12832. /* Send the complete sequence to the upper layer protocol */
  12833. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  12834. }
  12835. /**
  12836. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  12837. * @phba: pointer to lpfc hba data structure.
  12838. *
  12839. * This routine is invoked to post rpi header templates to the
  12840. * HBA consistent with the SLI-4 interface spec. This routine
  12841. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12842. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12843. *
  12844. * This routine does not require any locks. It's usage is expected
  12845. * to be driver load or reset recovery when the driver is
  12846. * sequential.
  12847. *
  12848. * Return codes
  12849. * 0 - successful
  12850. * -EIO - The mailbox failed to complete successfully.
  12851. * When this error occurs, the driver is not guaranteed
  12852. * to have any rpi regions posted to the device and
  12853. * must either attempt to repost the regions or take a
  12854. * fatal error.
  12855. **/
  12856. int
  12857. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  12858. {
  12859. struct lpfc_rpi_hdr *rpi_page;
  12860. uint32_t rc = 0;
  12861. uint16_t lrpi = 0;
  12862. /* SLI4 ports that support extents do not require RPI headers. */
  12863. if (!phba->sli4_hba.rpi_hdrs_in_use)
  12864. goto exit;
  12865. if (phba->sli4_hba.extents_in_use)
  12866. return -EIO;
  12867. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  12868. /*
  12869. * Assign the rpi headers a physical rpi only if the driver
  12870. * has not initialized those resources. A port reset only
  12871. * needs the headers posted.
  12872. */
  12873. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  12874. LPFC_RPI_RSRC_RDY)
  12875. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  12876. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  12877. if (rc != MBX_SUCCESS) {
  12878. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12879. "2008 Error %d posting all rpi "
  12880. "headers\n", rc);
  12881. rc = -EIO;
  12882. break;
  12883. }
  12884. }
  12885. exit:
  12886. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12887. LPFC_RPI_RSRC_RDY);
  12888. return rc;
  12889. }
  12890. /**
  12891. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  12892. * @phba: pointer to lpfc hba data structure.
  12893. * @rpi_page: pointer to the rpi memory region.
  12894. *
  12895. * This routine is invoked to post a single rpi header to the
  12896. * HBA consistent with the SLI-4 interface spec. This memory region
  12897. * maps up to 64 rpi context regions.
  12898. *
  12899. * Return codes
  12900. * 0 - successful
  12901. * -ENOMEM - No available memory
  12902. * -EIO - The mailbox failed to complete successfully.
  12903. **/
  12904. int
  12905. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  12906. {
  12907. LPFC_MBOXQ_t *mboxq;
  12908. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  12909. uint32_t rc = 0;
  12910. uint32_t shdr_status, shdr_add_status;
  12911. union lpfc_sli4_cfg_shdr *shdr;
  12912. /* SLI4 ports that support extents do not require RPI headers. */
  12913. if (!phba->sli4_hba.rpi_hdrs_in_use)
  12914. return rc;
  12915. if (phba->sli4_hba.extents_in_use)
  12916. return -EIO;
  12917. /* The port is notified of the header region via a mailbox command. */
  12918. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12919. if (!mboxq) {
  12920. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12921. "2001 Unable to allocate memory for issuing "
  12922. "SLI_CONFIG_SPECIAL mailbox command\n");
  12923. return -ENOMEM;
  12924. }
  12925. /* Post all rpi memory regions to the port. */
  12926. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  12927. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  12928. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  12929. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  12930. sizeof(struct lpfc_sli4_cfg_mhdr),
  12931. LPFC_SLI4_MBX_EMBED);
  12932. /* Post the physical rpi to the port for this rpi header. */
  12933. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  12934. rpi_page->start_rpi);
  12935. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  12936. hdr_tmpl, rpi_page->page_count);
  12937. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  12938. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  12939. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  12940. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  12941. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12942. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12943. if (rc != MBX_TIMEOUT)
  12944. mempool_free(mboxq, phba->mbox_mem_pool);
  12945. if (shdr_status || shdr_add_status || rc) {
  12946. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12947. "2514 POST_RPI_HDR mailbox failed with "
  12948. "status x%x add_status x%x, mbx status x%x\n",
  12949. shdr_status, shdr_add_status, rc);
  12950. rc = -ENXIO;
  12951. }
  12952. return rc;
  12953. }
  12954. /**
  12955. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  12956. * @phba: pointer to lpfc hba data structure.
  12957. *
  12958. * This routine is invoked to post rpi header templates to the
  12959. * HBA consistent with the SLI-4 interface spec. This routine
  12960. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12961. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12962. *
  12963. * Returns
  12964. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12965. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12966. **/
  12967. int
  12968. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  12969. {
  12970. unsigned long rpi;
  12971. uint16_t max_rpi, rpi_limit;
  12972. uint16_t rpi_remaining, lrpi = 0;
  12973. struct lpfc_rpi_hdr *rpi_hdr;
  12974. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  12975. rpi_limit = phba->sli4_hba.next_rpi;
  12976. /*
  12977. * Fetch the next logical rpi. Because this index is logical,
  12978. * the driver starts at 0 each time.
  12979. */
  12980. spin_lock_irq(&phba->hbalock);
  12981. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  12982. if (rpi >= rpi_limit)
  12983. rpi = LPFC_RPI_ALLOC_ERROR;
  12984. else {
  12985. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  12986. phba->sli4_hba.max_cfg_param.rpi_used++;
  12987. phba->sli4_hba.rpi_count++;
  12988. }
  12989. /*
  12990. * Don't try to allocate more rpi header regions if the device limit
  12991. * has been exhausted.
  12992. */
  12993. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  12994. (phba->sli4_hba.rpi_count >= max_rpi)) {
  12995. spin_unlock_irq(&phba->hbalock);
  12996. return rpi;
  12997. }
  12998. /*
  12999. * RPI header postings are not required for SLI4 ports capable of
  13000. * extents.
  13001. */
  13002. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13003. spin_unlock_irq(&phba->hbalock);
  13004. return rpi;
  13005. }
  13006. /*
  13007. * If the driver is running low on rpi resources, allocate another
  13008. * page now. Note that the next_rpi value is used because
  13009. * it represents how many are actually in use whereas max_rpi notes
  13010. * how many are supported max by the device.
  13011. */
  13012. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13013. spin_unlock_irq(&phba->hbalock);
  13014. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13015. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13016. if (!rpi_hdr) {
  13017. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13018. "2002 Error Could not grow rpi "
  13019. "count\n");
  13020. } else {
  13021. lrpi = rpi_hdr->start_rpi;
  13022. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13023. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13024. }
  13025. }
  13026. return rpi;
  13027. }
  13028. /**
  13029. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13030. * @phba: pointer to lpfc hba data structure.
  13031. *
  13032. * This routine is invoked to release an rpi to the pool of
  13033. * available rpis maintained by the driver.
  13034. **/
  13035. void
  13036. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13037. {
  13038. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13039. phba->sli4_hba.rpi_count--;
  13040. phba->sli4_hba.max_cfg_param.rpi_used--;
  13041. }
  13042. }
  13043. /**
  13044. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13045. * @phba: pointer to lpfc hba data structure.
  13046. *
  13047. * This routine is invoked to release an rpi to the pool of
  13048. * available rpis maintained by the driver.
  13049. **/
  13050. void
  13051. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13052. {
  13053. spin_lock_irq(&phba->hbalock);
  13054. __lpfc_sli4_free_rpi(phba, rpi);
  13055. spin_unlock_irq(&phba->hbalock);
  13056. }
  13057. /**
  13058. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13059. * @phba: pointer to lpfc hba data structure.
  13060. *
  13061. * This routine is invoked to remove the memory region that
  13062. * provided rpi via a bitmask.
  13063. **/
  13064. void
  13065. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13066. {
  13067. kfree(phba->sli4_hba.rpi_bmask);
  13068. kfree(phba->sli4_hba.rpi_ids);
  13069. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13070. }
  13071. /**
  13072. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13073. * @phba: pointer to lpfc hba data structure.
  13074. *
  13075. * This routine is invoked to remove the memory region that
  13076. * provided rpi via a bitmask.
  13077. **/
  13078. int
  13079. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
  13080. {
  13081. LPFC_MBOXQ_t *mboxq;
  13082. struct lpfc_hba *phba = ndlp->phba;
  13083. int rc;
  13084. /* The port is notified of the header region via a mailbox command. */
  13085. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13086. if (!mboxq)
  13087. return -ENOMEM;
  13088. /* Post all rpi memory regions to the port. */
  13089. lpfc_resume_rpi(mboxq, ndlp);
  13090. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13091. if (rc == MBX_NOT_FINISHED) {
  13092. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13093. "2010 Resume RPI Mailbox failed "
  13094. "status %d, mbxStatus x%x\n", rc,
  13095. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13096. mempool_free(mboxq, phba->mbox_mem_pool);
  13097. return -EIO;
  13098. }
  13099. return 0;
  13100. }
  13101. /**
  13102. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13103. * @vport: Pointer to the vport for which the vpi is being initialized
  13104. *
  13105. * This routine is invoked to activate a vpi with the port.
  13106. *
  13107. * Returns:
  13108. * 0 success
  13109. * -Evalue otherwise
  13110. **/
  13111. int
  13112. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13113. {
  13114. LPFC_MBOXQ_t *mboxq;
  13115. int rc = 0;
  13116. int retval = MBX_SUCCESS;
  13117. uint32_t mbox_tmo;
  13118. struct lpfc_hba *phba = vport->phba;
  13119. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13120. if (!mboxq)
  13121. return -ENOMEM;
  13122. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13123. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
  13124. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13125. if (rc != MBX_SUCCESS) {
  13126. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13127. "2022 INIT VPI Mailbox failed "
  13128. "status %d, mbxStatus x%x\n", rc,
  13129. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13130. retval = -EIO;
  13131. }
  13132. if (rc != MBX_TIMEOUT)
  13133. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13134. return retval;
  13135. }
  13136. /**
  13137. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13138. * @phba: pointer to lpfc hba data structure.
  13139. * @mboxq: Pointer to mailbox object.
  13140. *
  13141. * This routine is invoked to manually add a single FCF record. The caller
  13142. * must pass a completely initialized FCF_Record. This routine takes
  13143. * care of the nonembedded mailbox operations.
  13144. **/
  13145. static void
  13146. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13147. {
  13148. void *virt_addr;
  13149. union lpfc_sli4_cfg_shdr *shdr;
  13150. uint32_t shdr_status, shdr_add_status;
  13151. virt_addr = mboxq->sge_array->addr[0];
  13152. /* The IOCTL status is embedded in the mailbox subheader. */
  13153. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13154. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13155. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13156. if ((shdr_status || shdr_add_status) &&
  13157. (shdr_status != STATUS_FCF_IN_USE))
  13158. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13159. "2558 ADD_FCF_RECORD mailbox failed with "
  13160. "status x%x add_status x%x\n",
  13161. shdr_status, shdr_add_status);
  13162. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13163. }
  13164. /**
  13165. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13166. * @phba: pointer to lpfc hba data structure.
  13167. * @fcf_record: pointer to the initialized fcf record to add.
  13168. *
  13169. * This routine is invoked to manually add a single FCF record. The caller
  13170. * must pass a completely initialized FCF_Record. This routine takes
  13171. * care of the nonembedded mailbox operations.
  13172. **/
  13173. int
  13174. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13175. {
  13176. int rc = 0;
  13177. LPFC_MBOXQ_t *mboxq;
  13178. uint8_t *bytep;
  13179. void *virt_addr;
  13180. dma_addr_t phys_addr;
  13181. struct lpfc_mbx_sge sge;
  13182. uint32_t alloc_len, req_len;
  13183. uint32_t fcfindex;
  13184. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13185. if (!mboxq) {
  13186. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13187. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13188. return -ENOMEM;
  13189. }
  13190. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13191. sizeof(uint32_t);
  13192. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13193. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13194. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13195. req_len, LPFC_SLI4_MBX_NEMBED);
  13196. if (alloc_len < req_len) {
  13197. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13198. "2523 Allocated DMA memory size (x%x) is "
  13199. "less than the requested DMA memory "
  13200. "size (x%x)\n", alloc_len, req_len);
  13201. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13202. return -ENOMEM;
  13203. }
  13204. /*
  13205. * Get the first SGE entry from the non-embedded DMA memory. This
  13206. * routine only uses a single SGE.
  13207. */
  13208. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13209. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13210. virt_addr = mboxq->sge_array->addr[0];
  13211. /*
  13212. * Configure the FCF record for FCFI 0. This is the driver's
  13213. * hardcoded default and gets used in nonFIP mode.
  13214. */
  13215. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13216. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13217. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13218. /*
  13219. * Copy the fcf_index and the FCF Record Data. The data starts after
  13220. * the FCoE header plus word10. The data copy needs to be endian
  13221. * correct.
  13222. */
  13223. bytep += sizeof(uint32_t);
  13224. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13225. mboxq->vport = phba->pport;
  13226. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13227. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13228. if (rc == MBX_NOT_FINISHED) {
  13229. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13230. "2515 ADD_FCF_RECORD mailbox failed with "
  13231. "status 0x%x\n", rc);
  13232. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13233. rc = -EIO;
  13234. } else
  13235. rc = 0;
  13236. return rc;
  13237. }
  13238. /**
  13239. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13240. * @phba: pointer to lpfc hba data structure.
  13241. * @fcf_record: pointer to the fcf record to write the default data.
  13242. * @fcf_index: FCF table entry index.
  13243. *
  13244. * This routine is invoked to build the driver's default FCF record. The
  13245. * values used are hardcoded. This routine handles memory initialization.
  13246. *
  13247. **/
  13248. void
  13249. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13250. struct fcf_record *fcf_record,
  13251. uint16_t fcf_index)
  13252. {
  13253. memset(fcf_record, 0, sizeof(struct fcf_record));
  13254. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13255. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13256. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13257. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13258. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13259. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13260. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13261. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13262. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13263. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13264. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13265. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13266. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13267. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13268. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13269. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13270. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13271. /* Set the VLAN bit map */
  13272. if (phba->valid_vlan) {
  13273. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13274. = 1 << (phba->vlan_id % 8);
  13275. }
  13276. }
  13277. /**
  13278. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13279. * @phba: pointer to lpfc hba data structure.
  13280. * @fcf_index: FCF table entry offset.
  13281. *
  13282. * This routine is invoked to scan the entire FCF table by reading FCF
  13283. * record and processing it one at a time starting from the @fcf_index
  13284. * for initial FCF discovery or fast FCF failover rediscovery.
  13285. *
  13286. * Return 0 if the mailbox command is submitted successfully, none 0
  13287. * otherwise.
  13288. **/
  13289. int
  13290. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13291. {
  13292. int rc = 0, error;
  13293. LPFC_MBOXQ_t *mboxq;
  13294. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13295. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13296. if (!mboxq) {
  13297. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13298. "2000 Failed to allocate mbox for "
  13299. "READ_FCF cmd\n");
  13300. error = -ENOMEM;
  13301. goto fail_fcf_scan;
  13302. }
  13303. /* Construct the read FCF record mailbox command */
  13304. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13305. if (rc) {
  13306. error = -EINVAL;
  13307. goto fail_fcf_scan;
  13308. }
  13309. /* Issue the mailbox command asynchronously */
  13310. mboxq->vport = phba->pport;
  13311. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13312. spin_lock_irq(&phba->hbalock);
  13313. phba->hba_flag |= FCF_TS_INPROG;
  13314. spin_unlock_irq(&phba->hbalock);
  13315. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13316. if (rc == MBX_NOT_FINISHED)
  13317. error = -EIO;
  13318. else {
  13319. /* Reset eligible FCF count for new scan */
  13320. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13321. phba->fcf.eligible_fcf_cnt = 0;
  13322. error = 0;
  13323. }
  13324. fail_fcf_scan:
  13325. if (error) {
  13326. if (mboxq)
  13327. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13328. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13329. spin_lock_irq(&phba->hbalock);
  13330. phba->hba_flag &= ~FCF_TS_INPROG;
  13331. spin_unlock_irq(&phba->hbalock);
  13332. }
  13333. return error;
  13334. }
  13335. /**
  13336. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13337. * @phba: pointer to lpfc hba data structure.
  13338. * @fcf_index: FCF table entry offset.
  13339. *
  13340. * This routine is invoked to read an FCF record indicated by @fcf_index
  13341. * and to use it for FLOGI roundrobin FCF failover.
  13342. *
  13343. * Return 0 if the mailbox command is submitted successfully, none 0
  13344. * otherwise.
  13345. **/
  13346. int
  13347. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13348. {
  13349. int rc = 0, error;
  13350. LPFC_MBOXQ_t *mboxq;
  13351. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13352. if (!mboxq) {
  13353. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13354. "2763 Failed to allocate mbox for "
  13355. "READ_FCF cmd\n");
  13356. error = -ENOMEM;
  13357. goto fail_fcf_read;
  13358. }
  13359. /* Construct the read FCF record mailbox command */
  13360. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13361. if (rc) {
  13362. error = -EINVAL;
  13363. goto fail_fcf_read;
  13364. }
  13365. /* Issue the mailbox command asynchronously */
  13366. mboxq->vport = phba->pport;
  13367. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13368. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13369. if (rc == MBX_NOT_FINISHED)
  13370. error = -EIO;
  13371. else
  13372. error = 0;
  13373. fail_fcf_read:
  13374. if (error && mboxq)
  13375. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13376. return error;
  13377. }
  13378. /**
  13379. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13380. * @phba: pointer to lpfc hba data structure.
  13381. * @fcf_index: FCF table entry offset.
  13382. *
  13383. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13384. * determine whether it's eligible for FLOGI roundrobin failover list.
  13385. *
  13386. * Return 0 if the mailbox command is submitted successfully, none 0
  13387. * otherwise.
  13388. **/
  13389. int
  13390. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13391. {
  13392. int rc = 0, error;
  13393. LPFC_MBOXQ_t *mboxq;
  13394. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13395. if (!mboxq) {
  13396. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13397. "2758 Failed to allocate mbox for "
  13398. "READ_FCF cmd\n");
  13399. error = -ENOMEM;
  13400. goto fail_fcf_read;
  13401. }
  13402. /* Construct the read FCF record mailbox command */
  13403. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13404. if (rc) {
  13405. error = -EINVAL;
  13406. goto fail_fcf_read;
  13407. }
  13408. /* Issue the mailbox command asynchronously */
  13409. mboxq->vport = phba->pport;
  13410. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13411. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13412. if (rc == MBX_NOT_FINISHED)
  13413. error = -EIO;
  13414. else
  13415. error = 0;
  13416. fail_fcf_read:
  13417. if (error && mboxq)
  13418. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13419. return error;
  13420. }
  13421. /**
  13422. * lpfc_check_next_fcf_pri
  13423. * phba pointer to the lpfc_hba struct for this port.
  13424. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13425. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13426. * rr_bmask based on their priority level. Starting from the highest priority
  13427. * to the lowest. The most likely FCF candidate will be in the highest
  13428. * priority group. When this routine is called it searches the fcf_pri list for
  13429. * next lowest priority group and repopulates the rr_bmask with only those
  13430. * fcf_indexes.
  13431. * returns:
  13432. * 1=success 0=failure
  13433. **/
  13434. int
  13435. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13436. {
  13437. uint16_t next_fcf_pri;
  13438. uint16_t last_index;
  13439. struct lpfc_fcf_pri *fcf_pri;
  13440. int rc;
  13441. int ret = 0;
  13442. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13443. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13444. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13445. "3060 Last IDX %d\n", last_index);
  13446. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13447. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13448. "3061 Last IDX %d\n", last_index);
  13449. return 0; /* Empty rr list */
  13450. }
  13451. next_fcf_pri = 0;
  13452. /*
  13453. * Clear the rr_bmask and set all of the bits that are at this
  13454. * priority.
  13455. */
  13456. memset(phba->fcf.fcf_rr_bmask, 0,
  13457. sizeof(*phba->fcf.fcf_rr_bmask));
  13458. spin_lock_irq(&phba->hbalock);
  13459. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13460. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13461. continue;
  13462. /*
  13463. * the 1st priority that has not FLOGI failed
  13464. * will be the highest.
  13465. */
  13466. if (!next_fcf_pri)
  13467. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13468. spin_unlock_irq(&phba->hbalock);
  13469. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13470. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13471. fcf_pri->fcf_rec.fcf_index);
  13472. if (rc)
  13473. return 0;
  13474. }
  13475. spin_lock_irq(&phba->hbalock);
  13476. }
  13477. /*
  13478. * if next_fcf_pri was not set above and the list is not empty then
  13479. * we have failed flogis on all of them. So reset flogi failed
  13480. * and start at the begining.
  13481. */
  13482. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13483. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13484. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13485. /*
  13486. * the 1st priority that has not FLOGI failed
  13487. * will be the highest.
  13488. */
  13489. if (!next_fcf_pri)
  13490. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13491. spin_unlock_irq(&phba->hbalock);
  13492. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13493. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13494. fcf_pri->fcf_rec.fcf_index);
  13495. if (rc)
  13496. return 0;
  13497. }
  13498. spin_lock_irq(&phba->hbalock);
  13499. }
  13500. } else
  13501. ret = 1;
  13502. spin_unlock_irq(&phba->hbalock);
  13503. return ret;
  13504. }
  13505. /**
  13506. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13507. * @phba: pointer to lpfc hba data structure.
  13508. *
  13509. * This routine is to get the next eligible FCF record index in a round
  13510. * robin fashion. If the next eligible FCF record index equals to the
  13511. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13512. * shall be returned, otherwise, the next eligible FCF record's index
  13513. * shall be returned.
  13514. **/
  13515. uint16_t
  13516. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13517. {
  13518. uint16_t next_fcf_index;
  13519. /* Search start from next bit of currently registered FCF index */
  13520. next_priority:
  13521. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13522. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13523. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13524. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13525. next_fcf_index);
  13526. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  13527. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13528. /*
  13529. * If we have wrapped then we need to clear the bits that
  13530. * have been tested so that we can detect when we should
  13531. * change the priority level.
  13532. */
  13533. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13534. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  13535. }
  13536. /* Check roundrobin failover list empty condition */
  13537. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  13538. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  13539. /*
  13540. * If next fcf index is not found check if there are lower
  13541. * Priority level fcf's in the fcf_priority list.
  13542. * Set up the rr_bmask with all of the avaiable fcf bits
  13543. * at that level and continue the selection process.
  13544. */
  13545. if (lpfc_check_next_fcf_pri_level(phba))
  13546. goto next_priority;
  13547. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13548. "2844 No roundrobin failover FCF available\n");
  13549. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  13550. return LPFC_FCOE_FCF_NEXT_NONE;
  13551. else {
  13552. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13553. "3063 Only FCF available idx %d, flag %x\n",
  13554. next_fcf_index,
  13555. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  13556. return next_fcf_index;
  13557. }
  13558. }
  13559. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  13560. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  13561. LPFC_FCF_FLOGI_FAILED)
  13562. goto next_priority;
  13563. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13564. "2845 Get next roundrobin failover FCF (x%x)\n",
  13565. next_fcf_index);
  13566. return next_fcf_index;
  13567. }
  13568. /**
  13569. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  13570. * @phba: pointer to lpfc hba data structure.
  13571. *
  13572. * This routine sets the FCF record index in to the eligible bmask for
  13573. * roundrobin failover search. It checks to make sure that the index
  13574. * does not go beyond the range of the driver allocated bmask dimension
  13575. * before setting the bit.
  13576. *
  13577. * Returns 0 if the index bit successfully set, otherwise, it returns
  13578. * -EINVAL.
  13579. **/
  13580. int
  13581. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  13582. {
  13583. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13584. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13585. "2610 FCF (x%x) reached driver's book "
  13586. "keeping dimension:x%x\n",
  13587. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13588. return -EINVAL;
  13589. }
  13590. /* Set the eligible FCF record index bmask */
  13591. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13592. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13593. "2790 Set FCF (x%x) to roundrobin FCF failover "
  13594. "bmask\n", fcf_index);
  13595. return 0;
  13596. }
  13597. /**
  13598. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  13599. * @phba: pointer to lpfc hba data structure.
  13600. *
  13601. * This routine clears the FCF record index from the eligible bmask for
  13602. * roundrobin failover search. It checks to make sure that the index
  13603. * does not go beyond the range of the driver allocated bmask dimension
  13604. * before clearing the bit.
  13605. **/
  13606. void
  13607. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  13608. {
  13609. struct lpfc_fcf_pri *fcf_pri;
  13610. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13611. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13612. "2762 FCF (x%x) reached driver's book "
  13613. "keeping dimension:x%x\n",
  13614. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13615. return;
  13616. }
  13617. /* Clear the eligible FCF record index bmask */
  13618. spin_lock_irq(&phba->hbalock);
  13619. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13620. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  13621. list_del_init(&fcf_pri->list);
  13622. break;
  13623. }
  13624. }
  13625. spin_unlock_irq(&phba->hbalock);
  13626. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13627. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13628. "2791 Clear FCF (x%x) from roundrobin failover "
  13629. "bmask\n", fcf_index);
  13630. }
  13631. /**
  13632. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  13633. * @phba: pointer to lpfc hba data structure.
  13634. *
  13635. * This routine is the completion routine for the rediscover FCF table mailbox
  13636. * command. If the mailbox command returned failure, it will try to stop the
  13637. * FCF rediscover wait timer.
  13638. **/
  13639. void
  13640. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  13641. {
  13642. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  13643. uint32_t shdr_status, shdr_add_status;
  13644. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  13645. shdr_status = bf_get(lpfc_mbox_hdr_status,
  13646. &redisc_fcf->header.cfg_shdr.response);
  13647. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  13648. &redisc_fcf->header.cfg_shdr.response);
  13649. if (shdr_status || shdr_add_status) {
  13650. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13651. "2746 Requesting for FCF rediscovery failed "
  13652. "status x%x add_status x%x\n",
  13653. shdr_status, shdr_add_status);
  13654. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  13655. spin_lock_irq(&phba->hbalock);
  13656. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  13657. spin_unlock_irq(&phba->hbalock);
  13658. /*
  13659. * CVL event triggered FCF rediscover request failed,
  13660. * last resort to re-try current registered FCF entry.
  13661. */
  13662. lpfc_retry_pport_discovery(phba);
  13663. } else {
  13664. spin_lock_irq(&phba->hbalock);
  13665. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  13666. spin_unlock_irq(&phba->hbalock);
  13667. /*
  13668. * DEAD FCF event triggered FCF rediscover request
  13669. * failed, last resort to fail over as a link down
  13670. * to FCF registration.
  13671. */
  13672. lpfc_sli4_fcf_dead_failthrough(phba);
  13673. }
  13674. } else {
  13675. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13676. "2775 Start FCF rediscover quiescent timer\n");
  13677. /*
  13678. * Start FCF rediscovery wait timer for pending FCF
  13679. * before rescan FCF record table.
  13680. */
  13681. lpfc_fcf_redisc_wait_start_timer(phba);
  13682. }
  13683. mempool_free(mbox, phba->mbox_mem_pool);
  13684. }
  13685. /**
  13686. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  13687. * @phba: pointer to lpfc hba data structure.
  13688. *
  13689. * This routine is invoked to request for rediscovery of the entire FCF table
  13690. * by the port.
  13691. **/
  13692. int
  13693. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  13694. {
  13695. LPFC_MBOXQ_t *mbox;
  13696. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  13697. int rc, length;
  13698. /* Cancel retry delay timers to all vports before FCF rediscover */
  13699. lpfc_cancel_all_vport_retry_delay_timer(phba);
  13700. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13701. if (!mbox) {
  13702. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13703. "2745 Failed to allocate mbox for "
  13704. "requesting FCF rediscover.\n");
  13705. return -ENOMEM;
  13706. }
  13707. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  13708. sizeof(struct lpfc_sli4_cfg_mhdr));
  13709. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13710. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  13711. length, LPFC_SLI4_MBX_EMBED);
  13712. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  13713. /* Set count to 0 for invalidating the entire FCF database */
  13714. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  13715. /* Issue the mailbox command asynchronously */
  13716. mbox->vport = phba->pport;
  13717. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  13718. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  13719. if (rc == MBX_NOT_FINISHED) {
  13720. mempool_free(mbox, phba->mbox_mem_pool);
  13721. return -EIO;
  13722. }
  13723. return 0;
  13724. }
  13725. /**
  13726. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  13727. * @phba: pointer to lpfc hba data structure.
  13728. *
  13729. * This function is the failover routine as a last resort to the FCF DEAD
  13730. * event when driver failed to perform fast FCF failover.
  13731. **/
  13732. void
  13733. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  13734. {
  13735. uint32_t link_state;
  13736. /*
  13737. * Last resort as FCF DEAD event failover will treat this as
  13738. * a link down, but save the link state because we don't want
  13739. * it to be changed to Link Down unless it is already down.
  13740. */
  13741. link_state = phba->link_state;
  13742. lpfc_linkdown(phba);
  13743. phba->link_state = link_state;
  13744. /* Unregister FCF if no devices connected to it */
  13745. lpfc_unregister_unused_fcf(phba);
  13746. }
  13747. /**
  13748. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  13749. * @phba: pointer to lpfc hba data structure.
  13750. *
  13751. * This function read region 23 and parse TLV for port status to
  13752. * decide if the user disaled the port. If the TLV indicates the
  13753. * port is disabled, the hba_flag is set accordingly.
  13754. **/
  13755. void
  13756. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  13757. {
  13758. LPFC_MBOXQ_t *pmb = NULL;
  13759. MAILBOX_t *mb;
  13760. uint8_t *rgn23_data = NULL;
  13761. uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
  13762. int rc;
  13763. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13764. if (!pmb) {
  13765. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13766. "2600 lpfc_sli_read_serdes_param failed to"
  13767. " allocate mailbox memory\n");
  13768. goto out;
  13769. }
  13770. mb = &pmb->u.mb;
  13771. /* Get adapter Region 23 data */
  13772. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  13773. if (!rgn23_data)
  13774. goto out;
  13775. do {
  13776. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  13777. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  13778. if (rc != MBX_SUCCESS) {
  13779. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  13780. "2601 lpfc_sli_read_link_ste failed to"
  13781. " read config region 23 rc 0x%x Status 0x%x\n",
  13782. rc, mb->mbxStatus);
  13783. mb->un.varDmp.word_cnt = 0;
  13784. }
  13785. /*
  13786. * dump mem may return a zero when finished or we got a
  13787. * mailbox error, either way we are done.
  13788. */
  13789. if (mb->un.varDmp.word_cnt == 0)
  13790. break;
  13791. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  13792. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  13793. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  13794. rgn23_data + offset,
  13795. mb->un.varDmp.word_cnt);
  13796. offset += mb->un.varDmp.word_cnt;
  13797. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  13798. data_size = offset;
  13799. offset = 0;
  13800. if (!data_size)
  13801. goto out;
  13802. /* Check the region signature first */
  13803. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  13804. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13805. "2619 Config region 23 has bad signature\n");
  13806. goto out;
  13807. }
  13808. offset += 4;
  13809. /* Check the data structure version */
  13810. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  13811. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13812. "2620 Config region 23 has bad version\n");
  13813. goto out;
  13814. }
  13815. offset += 4;
  13816. /* Parse TLV entries in the region */
  13817. while (offset < data_size) {
  13818. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  13819. break;
  13820. /*
  13821. * If the TLV is not driver specific TLV or driver id is
  13822. * not linux driver id, skip the record.
  13823. */
  13824. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  13825. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  13826. (rgn23_data[offset + 3] != 0)) {
  13827. offset += rgn23_data[offset + 1] * 4 + 4;
  13828. continue;
  13829. }
  13830. /* Driver found a driver specific TLV in the config region */
  13831. sub_tlv_len = rgn23_data[offset + 1] * 4;
  13832. offset += 4;
  13833. tlv_offset = 0;
  13834. /*
  13835. * Search for configured port state sub-TLV.
  13836. */
  13837. while ((offset < data_size) &&
  13838. (tlv_offset < sub_tlv_len)) {
  13839. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  13840. offset += 4;
  13841. tlv_offset += 4;
  13842. break;
  13843. }
  13844. if (rgn23_data[offset] != PORT_STE_TYPE) {
  13845. offset += rgn23_data[offset + 1] * 4 + 4;
  13846. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  13847. continue;
  13848. }
  13849. /* This HBA contains PORT_STE configured */
  13850. if (!rgn23_data[offset + 2])
  13851. phba->hba_flag |= LINK_DISABLED;
  13852. goto out;
  13853. }
  13854. }
  13855. out:
  13856. if (pmb)
  13857. mempool_free(pmb, phba->mbox_mem_pool);
  13858. kfree(rgn23_data);
  13859. return;
  13860. }
  13861. /**
  13862. * lpfc_wr_object - write an object to the firmware
  13863. * @phba: HBA structure that indicates port to create a queue on.
  13864. * @dmabuf_list: list of dmabufs to write to the port.
  13865. * @size: the total byte value of the objects to write to the port.
  13866. * @offset: the current offset to be used to start the transfer.
  13867. *
  13868. * This routine will create a wr_object mailbox command to send to the port.
  13869. * the mailbox command will be constructed using the dma buffers described in
  13870. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  13871. * BDEs that the imbedded mailbox can support. The @offset variable will be
  13872. * used to indicate the starting offset of the transfer and will also return
  13873. * the offset after the write object mailbox has completed. @size is used to
  13874. * determine the end of the object and whether the eof bit should be set.
  13875. *
  13876. * Return 0 is successful and offset will contain the the new offset to use
  13877. * for the next write.
  13878. * Return negative value for error cases.
  13879. **/
  13880. int
  13881. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  13882. uint32_t size, uint32_t *offset)
  13883. {
  13884. struct lpfc_mbx_wr_object *wr_object;
  13885. LPFC_MBOXQ_t *mbox;
  13886. int rc = 0, i = 0;
  13887. uint32_t shdr_status, shdr_add_status;
  13888. uint32_t mbox_tmo;
  13889. union lpfc_sli4_cfg_shdr *shdr;
  13890. struct lpfc_dmabuf *dmabuf;
  13891. uint32_t written = 0;
  13892. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13893. if (!mbox)
  13894. return -ENOMEM;
  13895. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  13896. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  13897. sizeof(struct lpfc_mbx_wr_object) -
  13898. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  13899. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  13900. wr_object->u.request.write_offset = *offset;
  13901. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  13902. wr_object->u.request.object_name[0] =
  13903. cpu_to_le32(wr_object->u.request.object_name[0]);
  13904. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  13905. list_for_each_entry(dmabuf, dmabuf_list, list) {
  13906. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  13907. break;
  13908. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  13909. wr_object->u.request.bde[i].addrHigh =
  13910. putPaddrHigh(dmabuf->phys);
  13911. if (written + SLI4_PAGE_SIZE >= size) {
  13912. wr_object->u.request.bde[i].tus.f.bdeSize =
  13913. (size - written);
  13914. written += (size - written);
  13915. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  13916. } else {
  13917. wr_object->u.request.bde[i].tus.f.bdeSize =
  13918. SLI4_PAGE_SIZE;
  13919. written += SLI4_PAGE_SIZE;
  13920. }
  13921. i++;
  13922. }
  13923. wr_object->u.request.bde_count = i;
  13924. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  13925. if (!phba->sli4_hba.intr_enable)
  13926. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13927. else {
  13928. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  13929. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13930. }
  13931. /* The IOCTL status is embedded in the mailbox subheader. */
  13932. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  13933. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13934. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13935. if (rc != MBX_TIMEOUT)
  13936. mempool_free(mbox, phba->mbox_mem_pool);
  13937. if (shdr_status || shdr_add_status || rc) {
  13938. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13939. "3025 Write Object mailbox failed with "
  13940. "status x%x add_status x%x, mbx status x%x\n",
  13941. shdr_status, shdr_add_status, rc);
  13942. rc = -ENXIO;
  13943. } else
  13944. *offset += wr_object->u.response.actual_write_length;
  13945. return rc;
  13946. }
  13947. /**
  13948. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  13949. * @vport: pointer to vport data structure.
  13950. *
  13951. * This function iterate through the mailboxq and clean up all REG_LOGIN
  13952. * and REG_VPI mailbox commands associated with the vport. This function
  13953. * is called when driver want to restart discovery of the vport due to
  13954. * a Clear Virtual Link event.
  13955. **/
  13956. void
  13957. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  13958. {
  13959. struct lpfc_hba *phba = vport->phba;
  13960. LPFC_MBOXQ_t *mb, *nextmb;
  13961. struct lpfc_dmabuf *mp;
  13962. struct lpfc_nodelist *ndlp;
  13963. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  13964. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  13965. LIST_HEAD(mbox_cmd_list);
  13966. uint8_t restart_loop;
  13967. /* Clean up internally queued mailbox commands with the vport */
  13968. spin_lock_irq(&phba->hbalock);
  13969. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  13970. if (mb->vport != vport)
  13971. continue;
  13972. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  13973. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  13974. continue;
  13975. list_del(&mb->list);
  13976. list_add_tail(&mb->list, &mbox_cmd_list);
  13977. }
  13978. /* Clean up active mailbox command with the vport */
  13979. mb = phba->sli.mbox_active;
  13980. if (mb && (mb->vport == vport)) {
  13981. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  13982. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  13983. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13984. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  13985. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  13986. /* Put reference count for delayed processing */
  13987. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  13988. /* Unregister the RPI when mailbox complete */
  13989. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  13990. }
  13991. }
  13992. /* Cleanup any mailbox completions which are not yet processed */
  13993. do {
  13994. restart_loop = 0;
  13995. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  13996. /*
  13997. * If this mailox is already processed or it is
  13998. * for another vport ignore it.
  13999. */
  14000. if ((mb->vport != vport) ||
  14001. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14002. continue;
  14003. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14004. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14005. continue;
  14006. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14007. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14008. ndlp = (struct lpfc_nodelist *)mb->context2;
  14009. /* Unregister the RPI when mailbox complete */
  14010. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14011. restart_loop = 1;
  14012. spin_unlock_irq(&phba->hbalock);
  14013. spin_lock(shost->host_lock);
  14014. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14015. spin_unlock(shost->host_lock);
  14016. spin_lock_irq(&phba->hbalock);
  14017. break;
  14018. }
  14019. }
  14020. } while (restart_loop);
  14021. spin_unlock_irq(&phba->hbalock);
  14022. /* Release the cleaned-up mailbox commands */
  14023. while (!list_empty(&mbox_cmd_list)) {
  14024. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14025. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14026. mp = (struct lpfc_dmabuf *) (mb->context1);
  14027. if (mp) {
  14028. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14029. kfree(mp);
  14030. }
  14031. ndlp = (struct lpfc_nodelist *) mb->context2;
  14032. mb->context2 = NULL;
  14033. if (ndlp) {
  14034. spin_lock(shost->host_lock);
  14035. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14036. spin_unlock(shost->host_lock);
  14037. lpfc_nlp_put(ndlp);
  14038. }
  14039. }
  14040. mempool_free(mb, phba->mbox_mem_pool);
  14041. }
  14042. /* Release the ndlp with the cleaned-up active mailbox command */
  14043. if (act_mbx_ndlp) {
  14044. spin_lock(shost->host_lock);
  14045. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14046. spin_unlock(shost->host_lock);
  14047. lpfc_nlp_put(act_mbx_ndlp);
  14048. }
  14049. }
  14050. /**
  14051. * lpfc_drain_txq - Drain the txq
  14052. * @phba: Pointer to HBA context object.
  14053. *
  14054. * This function attempt to submit IOCBs on the txq
  14055. * to the adapter. For SLI4 adapters, the txq contains
  14056. * ELS IOCBs that have been deferred because the there
  14057. * are no SGLs. This congestion can occur with large
  14058. * vport counts during node discovery.
  14059. **/
  14060. uint32_t
  14061. lpfc_drain_txq(struct lpfc_hba *phba)
  14062. {
  14063. LIST_HEAD(completions);
  14064. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14065. struct lpfc_iocbq *piocbq = 0;
  14066. unsigned long iflags = 0;
  14067. char *fail_msg = NULL;
  14068. struct lpfc_sglq *sglq;
  14069. union lpfc_wqe wqe;
  14070. spin_lock_irqsave(&phba->hbalock, iflags);
  14071. if (pring->txq_cnt > pring->txq_max)
  14072. pring->txq_max = pring->txq_cnt;
  14073. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14074. while (pring->txq_cnt) {
  14075. spin_lock_irqsave(&phba->hbalock, iflags);
  14076. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14077. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14078. if (!sglq) {
  14079. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14080. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14081. break;
  14082. } else {
  14083. if (!piocbq) {
  14084. /* The txq_cnt out of sync. This should
  14085. * never happen
  14086. */
  14087. sglq = __lpfc_clear_active_sglq(phba,
  14088. sglq->sli4_lxritag);
  14089. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14090. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14091. "2823 txq empty and txq_cnt is %d\n ",
  14092. pring->txq_cnt);
  14093. break;
  14094. }
  14095. }
  14096. /* The xri and iocb resources secured,
  14097. * attempt to issue request
  14098. */
  14099. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14100. piocbq->sli4_xritag = sglq->sli4_xritag;
  14101. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14102. fail_msg = "to convert bpl to sgl";
  14103. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14104. fail_msg = "to convert iocb to wqe";
  14105. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14106. fail_msg = " - Wq is full";
  14107. else
  14108. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14109. if (fail_msg) {
  14110. /* Failed means we can't issue and need to cancel */
  14111. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14112. "2822 IOCB failed %s iotag 0x%x "
  14113. "xri 0x%x\n",
  14114. fail_msg,
  14115. piocbq->iotag, piocbq->sli4_xritag);
  14116. list_add_tail(&piocbq->list, &completions);
  14117. }
  14118. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14119. }
  14120. /* Cancel all the IOCBs that cannot be issued */
  14121. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14122. IOERR_SLI_ABORTED);
  14123. return pring->txq_cnt;
  14124. }