lpfc_sli.c 357 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2009 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 <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_transport_fc.h>
  30. #include <scsi/fc/fc_fs.h>
  31. #include "lpfc_hw4.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_sli4.h"
  35. #include "lpfc_nl.h"
  36. #include "lpfc_disc.h"
  37. #include "lpfc_scsi.h"
  38. #include "lpfc.h"
  39. #include "lpfc_crtn.h"
  40. #include "lpfc_logmsg.h"
  41. #include "lpfc_compat.h"
  42. #include "lpfc_debugfs.h"
  43. #include "lpfc_vport.h"
  44. /* There are only four IOCB completion types. */
  45. typedef enum _lpfc_iocb_type {
  46. LPFC_UNKNOWN_IOCB,
  47. LPFC_UNSOL_IOCB,
  48. LPFC_SOL_IOCB,
  49. LPFC_ABORT_IOCB
  50. } lpfc_iocb_type;
  51. /* Provide function prototypes local to this module. */
  52. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  53. uint32_t);
  54. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint8_t *, uint32_t *);
  56. static IOCB_t *
  57. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  58. {
  59. return &iocbq->iocb;
  60. }
  61. /**
  62. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  63. * @q: The Work Queue to operate on.
  64. * @wqe: The work Queue Entry to put on the Work queue.
  65. *
  66. * This routine will copy the contents of @wqe to the next available entry on
  67. * the @q. This function will then ring the Work Queue Doorbell to signal the
  68. * HBA to start processing the Work Queue Entry. This function returns 0 if
  69. * successful. If no entries are available on @q then this function will return
  70. * -ENOMEM.
  71. * The caller is expected to hold the hbalock when calling this routine.
  72. **/
  73. static uint32_t
  74. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  75. {
  76. union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
  77. struct lpfc_register doorbell;
  78. uint32_t host_index;
  79. /* If the host has not yet processed the next entry then we are done */
  80. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  81. return -ENOMEM;
  82. /* set consumption flag every once in a while */
  83. if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
  84. bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
  85. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  86. /* Update the host index before invoking device */
  87. host_index = q->host_index;
  88. q->host_index = ((q->host_index + 1) % q->entry_count);
  89. /* Ring Doorbell */
  90. doorbell.word0 = 0;
  91. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  92. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  93. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  94. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  95. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  96. return 0;
  97. }
  98. /**
  99. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  100. * @q: The Work Queue to operate on.
  101. * @index: The index to advance the hba index to.
  102. *
  103. * This routine will update the HBA index of a queue to reflect consumption of
  104. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  105. * an entry the host calls this function to update the queue's internal
  106. * pointers. This routine returns the number of entries that were consumed by
  107. * the HBA.
  108. **/
  109. static uint32_t
  110. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  111. {
  112. uint32_t released = 0;
  113. if (q->hba_index == index)
  114. return 0;
  115. do {
  116. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  117. released++;
  118. } while (q->hba_index != index);
  119. return released;
  120. }
  121. /**
  122. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  123. * @q: The Mailbox Queue to operate on.
  124. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  125. *
  126. * This routine will copy the contents of @mqe to the next available entry on
  127. * the @q. This function will then ring the Work Queue Doorbell to signal the
  128. * HBA to start processing the Work Queue Entry. This function returns 0 if
  129. * successful. If no entries are available on @q then this function will return
  130. * -ENOMEM.
  131. * The caller is expected to hold the hbalock when calling this routine.
  132. **/
  133. static uint32_t
  134. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  135. {
  136. struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
  137. struct lpfc_register doorbell;
  138. uint32_t host_index;
  139. /* If the host has not yet processed the next entry then we are done */
  140. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  141. return -ENOMEM;
  142. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  143. /* Save off the mailbox pointer for completion */
  144. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  145. /* Update the host index before invoking device */
  146. host_index = q->host_index;
  147. q->host_index = ((q->host_index + 1) % q->entry_count);
  148. /* Ring Doorbell */
  149. doorbell.word0 = 0;
  150. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  151. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  152. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  153. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  154. return 0;
  155. }
  156. /**
  157. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  158. * @q: The Mailbox Queue to operate on.
  159. *
  160. * This routine will update the HBA index of a queue to reflect consumption of
  161. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  162. * an entry the host calls this function to update the queue's internal
  163. * pointers. This routine returns the number of entries that were consumed by
  164. * the HBA.
  165. **/
  166. static uint32_t
  167. lpfc_sli4_mq_release(struct lpfc_queue *q)
  168. {
  169. /* Clear the mailbox pointer for completion */
  170. q->phba->mbox = NULL;
  171. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  172. return 1;
  173. }
  174. /**
  175. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  176. * @q: The Event Queue to get the first valid EQE from
  177. *
  178. * This routine will get the first valid Event Queue Entry from @q, update
  179. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  180. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  181. * processed, but not popped back to the HBA then this routine will return NULL.
  182. **/
  183. static struct lpfc_eqe *
  184. lpfc_sli4_eq_get(struct lpfc_queue *q)
  185. {
  186. struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
  187. /* If the next EQE is not valid then we are done */
  188. if (!bf_get(lpfc_eqe_valid, eqe))
  189. return NULL;
  190. /* If the host has not yet processed the next entry then we are done */
  191. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  192. return NULL;
  193. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  194. return eqe;
  195. }
  196. /**
  197. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  198. * @q: The Event Queue that the host has completed processing for.
  199. * @arm: Indicates whether the host wants to arms this CQ.
  200. *
  201. * This routine will mark all Event Queue Entries on @q, from the last
  202. * known completed entry to the last entry that was processed, as completed
  203. * by clearing the valid bit for each completion queue entry. Then it will
  204. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  205. * The internal host index in the @q will be updated by this routine to indicate
  206. * that the host has finished processing the entries. The @arm parameter
  207. * indicates that the queue should be rearmed when ringing the doorbell.
  208. *
  209. * This function will return the number of EQEs that were popped.
  210. **/
  211. uint32_t
  212. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  213. {
  214. uint32_t released = 0;
  215. struct lpfc_eqe *temp_eqe;
  216. struct lpfc_register doorbell;
  217. /* while there are valid entries */
  218. while (q->hba_index != q->host_index) {
  219. temp_eqe = q->qe[q->host_index].eqe;
  220. bf_set(lpfc_eqe_valid, temp_eqe, 0);
  221. released++;
  222. q->host_index = ((q->host_index + 1) % q->entry_count);
  223. }
  224. if (unlikely(released == 0 && !arm))
  225. return 0;
  226. /* ring doorbell for number popped */
  227. doorbell.word0 = 0;
  228. if (arm) {
  229. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  230. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  231. }
  232. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  233. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  234. bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
  235. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  236. return released;
  237. }
  238. /**
  239. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  240. * @q: The Completion Queue to get the first valid CQE from
  241. *
  242. * This routine will get the first valid Completion Queue Entry from @q, update
  243. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  244. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  245. * processed, but not popped back to the HBA then this routine will return NULL.
  246. **/
  247. static struct lpfc_cqe *
  248. lpfc_sli4_cq_get(struct lpfc_queue *q)
  249. {
  250. struct lpfc_cqe *cqe;
  251. /* If the next CQE is not valid then we are done */
  252. if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  253. return NULL;
  254. /* If the host has not yet processed the next entry then we are done */
  255. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  256. return NULL;
  257. cqe = q->qe[q->hba_index].cqe;
  258. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  259. return cqe;
  260. }
  261. /**
  262. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  263. * @q: The Completion Queue that the host has completed processing for.
  264. * @arm: Indicates whether the host wants to arms this CQ.
  265. *
  266. * This routine will mark all Completion queue entries on @q, from the last
  267. * known completed entry to the last entry that was processed, as completed
  268. * by clearing the valid bit for each completion queue entry. Then it will
  269. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  270. * The internal host index in the @q will be updated by this routine to indicate
  271. * that the host has finished processing the entries. The @arm parameter
  272. * indicates that the queue should be rearmed when ringing the doorbell.
  273. *
  274. * This function will return the number of CQEs that were released.
  275. **/
  276. uint32_t
  277. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  278. {
  279. uint32_t released = 0;
  280. struct lpfc_cqe *temp_qe;
  281. struct lpfc_register doorbell;
  282. /* while there are valid entries */
  283. while (q->hba_index != q->host_index) {
  284. temp_qe = q->qe[q->host_index].cqe;
  285. bf_set(lpfc_cqe_valid, temp_qe, 0);
  286. released++;
  287. q->host_index = ((q->host_index + 1) % q->entry_count);
  288. }
  289. if (unlikely(released == 0 && !arm))
  290. return 0;
  291. /* ring doorbell for number popped */
  292. doorbell.word0 = 0;
  293. if (arm)
  294. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  295. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  296. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  297. bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
  298. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  299. return released;
  300. }
  301. /**
  302. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  303. * @q: The Header Receive Queue to operate on.
  304. * @wqe: The Receive Queue Entry to put on the Receive queue.
  305. *
  306. * This routine will copy the contents of @wqe to the next available entry on
  307. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  308. * HBA to start processing the Receive Queue Entry. This function returns the
  309. * index that the rqe was copied to if successful. If no entries are available
  310. * on @q then this function will return -ENOMEM.
  311. * The caller is expected to hold the hbalock when calling this routine.
  312. **/
  313. static int
  314. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  315. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  316. {
  317. struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
  318. struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
  319. struct lpfc_register doorbell;
  320. int put_index = hq->host_index;
  321. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  322. return -EINVAL;
  323. if (hq->host_index != dq->host_index)
  324. return -EINVAL;
  325. /* If the host has not yet processed the next entry then we are done */
  326. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  327. return -EBUSY;
  328. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  329. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  330. /* Update the host index to point to the next slot */
  331. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  332. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  333. /* Ring The Header Receive Queue Doorbell */
  334. if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
  335. doorbell.word0 = 0;
  336. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  337. LPFC_RQ_POST_BATCH);
  338. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  339. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  340. }
  341. return put_index;
  342. }
  343. /**
  344. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  345. * @q: The Header Receive Queue to operate on.
  346. *
  347. * This routine will update the HBA index of a queue to reflect consumption of
  348. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  349. * consumed an entry the host calls this function to update the queue's
  350. * internal pointers. This routine returns the number of entries that were
  351. * consumed by the HBA.
  352. **/
  353. static uint32_t
  354. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  355. {
  356. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  357. return 0;
  358. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  359. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  360. return 1;
  361. }
  362. /**
  363. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  364. * @phba: Pointer to HBA context object.
  365. * @pring: Pointer to driver SLI ring object.
  366. *
  367. * This function returns pointer to next command iocb entry
  368. * in the command ring. The caller must hold hbalock to prevent
  369. * other threads consume the next command iocb.
  370. * SLI-2/SLI-3 provide different sized iocbs.
  371. **/
  372. static inline IOCB_t *
  373. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  374. {
  375. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  376. pring->cmdidx * phba->iocb_cmd_size);
  377. }
  378. /**
  379. * lpfc_resp_iocb - Get next response iocb entry in the ring
  380. * @phba: Pointer to HBA context object.
  381. * @pring: Pointer to driver SLI ring object.
  382. *
  383. * This function returns pointer to next response iocb entry
  384. * in the response ring. The caller must hold hbalock to make sure
  385. * that no other thread consume the next response iocb.
  386. * SLI-2/SLI-3 provide different sized iocbs.
  387. **/
  388. static inline IOCB_t *
  389. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  390. {
  391. return (IOCB_t *) (((char *) pring->rspringaddr) +
  392. pring->rspidx * phba->iocb_rsp_size);
  393. }
  394. /**
  395. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  396. * @phba: Pointer to HBA context object.
  397. *
  398. * This function is called with hbalock held. This function
  399. * allocates a new driver iocb object from the iocb pool. If the
  400. * allocation is successful, it returns pointer to the newly
  401. * allocated iocb object else it returns NULL.
  402. **/
  403. static struct lpfc_iocbq *
  404. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  405. {
  406. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  407. struct lpfc_iocbq * iocbq = NULL;
  408. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  409. return iocbq;
  410. }
  411. /**
  412. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  413. * @phba: Pointer to HBA context object.
  414. * @xritag: XRI value.
  415. *
  416. * This function clears the sglq pointer from the array of acive
  417. * sglq's. The xritag that is passed in is used to index into the
  418. * array. Before the xritag can be used it needs to be adjusted
  419. * by subtracting the xribase.
  420. *
  421. * Returns sglq ponter = success, NULL = Failure.
  422. **/
  423. static struct lpfc_sglq *
  424. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  425. {
  426. uint16_t adj_xri;
  427. struct lpfc_sglq *sglq;
  428. adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
  429. if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
  430. return NULL;
  431. sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
  432. phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
  433. return sglq;
  434. }
  435. /**
  436. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  437. * @phba: Pointer to HBA context object.
  438. * @xritag: XRI value.
  439. *
  440. * This function returns the sglq pointer from the array of acive
  441. * sglq's. The xritag that is passed in is used to index into the
  442. * array. Before the xritag can be used it needs to be adjusted
  443. * by subtracting the xribase.
  444. *
  445. * Returns sglq ponter = success, NULL = Failure.
  446. **/
  447. static struct lpfc_sglq *
  448. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  449. {
  450. uint16_t adj_xri;
  451. struct lpfc_sglq *sglq;
  452. adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
  453. if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
  454. return NULL;
  455. sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
  456. return sglq;
  457. }
  458. /**
  459. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  460. * @phba: Pointer to HBA context object.
  461. *
  462. * This function is called with hbalock held. This function
  463. * Gets a new driver sglq object from the sglq list. If the
  464. * list is not empty then it is successful, it returns pointer to the newly
  465. * allocated sglq object else it returns NULL.
  466. **/
  467. static struct lpfc_sglq *
  468. __lpfc_sli_get_sglq(struct lpfc_hba *phba)
  469. {
  470. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  471. struct lpfc_sglq *sglq = NULL;
  472. uint16_t adj_xri;
  473. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  474. adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
  475. phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
  476. return sglq;
  477. }
  478. /**
  479. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  480. * @phba: Pointer to HBA context object.
  481. *
  482. * This function is called with no lock held. This function
  483. * allocates a new driver iocb object from the iocb pool. If the
  484. * allocation is successful, it returns pointer to the newly
  485. * allocated iocb object else it returns NULL.
  486. **/
  487. struct lpfc_iocbq *
  488. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  489. {
  490. struct lpfc_iocbq * iocbq = NULL;
  491. unsigned long iflags;
  492. spin_lock_irqsave(&phba->hbalock, iflags);
  493. iocbq = __lpfc_sli_get_iocbq(phba);
  494. spin_unlock_irqrestore(&phba->hbalock, iflags);
  495. return iocbq;
  496. }
  497. /**
  498. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  499. * @phba: Pointer to HBA context object.
  500. * @iocbq: Pointer to driver iocb object.
  501. *
  502. * This function is called with hbalock held to release driver
  503. * iocb object to the iocb pool. The iotag in the iocb object
  504. * does not change for each use of the iocb object. This function
  505. * clears all other fields of the iocb object when it is freed.
  506. * The sqlq structure that holds the xritag and phys and virtual
  507. * mappings for the scatter gather list is retrieved from the
  508. * active array of sglq. The get of the sglq pointer also clears
  509. * the entry in the array. If the status of the IO indiactes that
  510. * this IO was aborted then the sglq entry it put on the
  511. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  512. * IO has good status or fails for any other reason then the sglq
  513. * entry is added to the free list (lpfc_sgl_list).
  514. **/
  515. static void
  516. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  517. {
  518. struct lpfc_sglq *sglq;
  519. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  520. unsigned long iflag;
  521. if (iocbq->sli4_xritag == NO_XRI)
  522. sglq = NULL;
  523. else
  524. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
  525. if (sglq) {
  526. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
  527. || ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  528. && (iocbq->iocb.un.ulpWord[4]
  529. == IOERR_SLI_ABORTED))) {
  530. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  531. iflag);
  532. list_add(&sglq->list,
  533. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  534. spin_unlock_irqrestore(
  535. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  536. } else
  537. list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
  538. }
  539. /*
  540. * Clean all volatile data fields, preserve iotag and node struct.
  541. */
  542. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  543. iocbq->sli4_xritag = NO_XRI;
  544. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  545. }
  546. /**
  547. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  548. * @phba: Pointer to HBA context object.
  549. * @iocbq: Pointer to driver iocb object.
  550. *
  551. * This function is called with hbalock held to release driver
  552. * iocb object to the iocb pool. The iotag in the iocb object
  553. * does not change for each use of the iocb object. This function
  554. * clears all other fields of the iocb object when it is freed.
  555. **/
  556. static void
  557. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  558. {
  559. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  560. /*
  561. * Clean all volatile data fields, preserve iotag and node struct.
  562. */
  563. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  564. iocbq->sli4_xritag = NO_XRI;
  565. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  566. }
  567. /**
  568. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  569. * @phba: Pointer to HBA context object.
  570. * @iocbq: Pointer to driver iocb object.
  571. *
  572. * This function is called with hbalock held to release driver
  573. * iocb object to the iocb pool. The iotag in the iocb object
  574. * does not change for each use of the iocb object. This function
  575. * clears all other fields of the iocb object when it is freed.
  576. **/
  577. static void
  578. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  579. {
  580. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  581. }
  582. /**
  583. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  584. * @phba: Pointer to HBA context object.
  585. * @iocbq: Pointer to driver iocb object.
  586. *
  587. * This function is called with no lock held to release the iocb to
  588. * iocb pool.
  589. **/
  590. void
  591. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  592. {
  593. unsigned long iflags;
  594. /*
  595. * Clean all volatile data fields, preserve iotag and node struct.
  596. */
  597. spin_lock_irqsave(&phba->hbalock, iflags);
  598. __lpfc_sli_release_iocbq(phba, iocbq);
  599. spin_unlock_irqrestore(&phba->hbalock, iflags);
  600. }
  601. /**
  602. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  603. * @phba: Pointer to HBA context object.
  604. * @iocblist: List of IOCBs.
  605. * @ulpstatus: ULP status in IOCB command field.
  606. * @ulpWord4: ULP word-4 in IOCB command field.
  607. *
  608. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  609. * on the list by invoking the complete callback function associated with the
  610. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  611. * fields.
  612. **/
  613. void
  614. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  615. uint32_t ulpstatus, uint32_t ulpWord4)
  616. {
  617. struct lpfc_iocbq *piocb;
  618. while (!list_empty(iocblist)) {
  619. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  620. if (!piocb->iocb_cmpl)
  621. lpfc_sli_release_iocbq(phba, piocb);
  622. else {
  623. piocb->iocb.ulpStatus = ulpstatus;
  624. piocb->iocb.un.ulpWord[4] = ulpWord4;
  625. (piocb->iocb_cmpl) (phba, piocb, piocb);
  626. }
  627. }
  628. return;
  629. }
  630. /**
  631. * lpfc_sli_iocb_cmd_type - Get the iocb type
  632. * @iocb_cmnd: iocb command code.
  633. *
  634. * This function is called by ring event handler function to get the iocb type.
  635. * This function translates the iocb command to an iocb command type used to
  636. * decide the final disposition of each completed IOCB.
  637. * The function returns
  638. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  639. * LPFC_SOL_IOCB if it is a solicited iocb completion
  640. * LPFC_ABORT_IOCB if it is an abort iocb
  641. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  642. *
  643. * The caller is not required to hold any lock.
  644. **/
  645. static lpfc_iocb_type
  646. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  647. {
  648. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  649. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  650. return 0;
  651. switch (iocb_cmnd) {
  652. case CMD_XMIT_SEQUENCE_CR:
  653. case CMD_XMIT_SEQUENCE_CX:
  654. case CMD_XMIT_BCAST_CN:
  655. case CMD_XMIT_BCAST_CX:
  656. case CMD_ELS_REQUEST_CR:
  657. case CMD_ELS_REQUEST_CX:
  658. case CMD_CREATE_XRI_CR:
  659. case CMD_CREATE_XRI_CX:
  660. case CMD_GET_RPI_CN:
  661. case CMD_XMIT_ELS_RSP_CX:
  662. case CMD_GET_RPI_CR:
  663. case CMD_FCP_IWRITE_CR:
  664. case CMD_FCP_IWRITE_CX:
  665. case CMD_FCP_IREAD_CR:
  666. case CMD_FCP_IREAD_CX:
  667. case CMD_FCP_ICMND_CR:
  668. case CMD_FCP_ICMND_CX:
  669. case CMD_FCP_TSEND_CX:
  670. case CMD_FCP_TRSP_CX:
  671. case CMD_FCP_TRECEIVE_CX:
  672. case CMD_FCP_AUTO_TRSP_CX:
  673. case CMD_ADAPTER_MSG:
  674. case CMD_ADAPTER_DUMP:
  675. case CMD_XMIT_SEQUENCE64_CR:
  676. case CMD_XMIT_SEQUENCE64_CX:
  677. case CMD_XMIT_BCAST64_CN:
  678. case CMD_XMIT_BCAST64_CX:
  679. case CMD_ELS_REQUEST64_CR:
  680. case CMD_ELS_REQUEST64_CX:
  681. case CMD_FCP_IWRITE64_CR:
  682. case CMD_FCP_IWRITE64_CX:
  683. case CMD_FCP_IREAD64_CR:
  684. case CMD_FCP_IREAD64_CX:
  685. case CMD_FCP_ICMND64_CR:
  686. case CMD_FCP_ICMND64_CX:
  687. case CMD_FCP_TSEND64_CX:
  688. case CMD_FCP_TRSP64_CX:
  689. case CMD_FCP_TRECEIVE64_CX:
  690. case CMD_GEN_REQUEST64_CR:
  691. case CMD_GEN_REQUEST64_CX:
  692. case CMD_XMIT_ELS_RSP64_CX:
  693. case DSSCMD_IWRITE64_CR:
  694. case DSSCMD_IWRITE64_CX:
  695. case DSSCMD_IREAD64_CR:
  696. case DSSCMD_IREAD64_CX:
  697. case DSSCMD_INVALIDATE_DEK:
  698. case DSSCMD_SET_KEK:
  699. case DSSCMD_GET_KEK_ID:
  700. case DSSCMD_GEN_XFER:
  701. type = LPFC_SOL_IOCB;
  702. break;
  703. case CMD_ABORT_XRI_CN:
  704. case CMD_ABORT_XRI_CX:
  705. case CMD_CLOSE_XRI_CN:
  706. case CMD_CLOSE_XRI_CX:
  707. case CMD_XRI_ABORTED_CX:
  708. case CMD_ABORT_MXRI64_CN:
  709. type = LPFC_ABORT_IOCB;
  710. break;
  711. case CMD_RCV_SEQUENCE_CX:
  712. case CMD_RCV_ELS_REQ_CX:
  713. case CMD_RCV_SEQUENCE64_CX:
  714. case CMD_RCV_ELS_REQ64_CX:
  715. case CMD_ASYNC_STATUS:
  716. case CMD_IOCB_RCV_SEQ64_CX:
  717. case CMD_IOCB_RCV_ELS64_CX:
  718. case CMD_IOCB_RCV_CONT64_CX:
  719. case CMD_IOCB_RET_XRI64_CX:
  720. type = LPFC_UNSOL_IOCB;
  721. break;
  722. case CMD_IOCB_XMIT_MSEQ64_CR:
  723. case CMD_IOCB_XMIT_MSEQ64_CX:
  724. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  725. case CMD_IOCB_RCV_ELS_LIST64_CX:
  726. case CMD_IOCB_CLOSE_EXTENDED_CN:
  727. case CMD_IOCB_ABORT_EXTENDED_CN:
  728. case CMD_IOCB_RET_HBQE64_CN:
  729. case CMD_IOCB_FCP_IBIDIR64_CR:
  730. case CMD_IOCB_FCP_IBIDIR64_CX:
  731. case CMD_IOCB_FCP_ITASKMGT64_CX:
  732. case CMD_IOCB_LOGENTRY_CN:
  733. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  734. printk("%s - Unhandled SLI-3 Command x%x\n",
  735. __func__, iocb_cmnd);
  736. type = LPFC_UNKNOWN_IOCB;
  737. break;
  738. default:
  739. type = LPFC_UNKNOWN_IOCB;
  740. break;
  741. }
  742. return type;
  743. }
  744. /**
  745. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  746. * @phba: Pointer to HBA context object.
  747. *
  748. * This function is called from SLI initialization code
  749. * to configure every ring of the HBA's SLI interface. The
  750. * caller is not required to hold any lock. This function issues
  751. * a config_ring mailbox command for each ring.
  752. * This function returns zero if successful else returns a negative
  753. * error code.
  754. **/
  755. static int
  756. lpfc_sli_ring_map(struct lpfc_hba *phba)
  757. {
  758. struct lpfc_sli *psli = &phba->sli;
  759. LPFC_MBOXQ_t *pmb;
  760. MAILBOX_t *pmbox;
  761. int i, rc, ret = 0;
  762. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  763. if (!pmb)
  764. return -ENOMEM;
  765. pmbox = &pmb->u.mb;
  766. phba->link_state = LPFC_INIT_MBX_CMDS;
  767. for (i = 0; i < psli->num_rings; i++) {
  768. lpfc_config_ring(phba, i, pmb);
  769. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  770. if (rc != MBX_SUCCESS) {
  771. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  772. "0446 Adapter failed to init (%d), "
  773. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  774. "ring %d\n",
  775. rc, pmbox->mbxCommand,
  776. pmbox->mbxStatus, i);
  777. phba->link_state = LPFC_HBA_ERROR;
  778. ret = -ENXIO;
  779. break;
  780. }
  781. }
  782. mempool_free(pmb, phba->mbox_mem_pool);
  783. return ret;
  784. }
  785. /**
  786. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  787. * @phba: Pointer to HBA context object.
  788. * @pring: Pointer to driver SLI ring object.
  789. * @piocb: Pointer to the driver iocb object.
  790. *
  791. * This function is called with hbalock held. The function adds the
  792. * new iocb to txcmplq of the given ring. This function always returns
  793. * 0. If this function is called for ELS ring, this function checks if
  794. * there is a vport associated with the ELS command. This function also
  795. * starts els_tmofunc timer if this is an ELS command.
  796. **/
  797. static int
  798. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  799. struct lpfc_iocbq *piocb)
  800. {
  801. list_add_tail(&piocb->list, &pring->txcmplq);
  802. pring->txcmplq_cnt++;
  803. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  804. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  805. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  806. if (!piocb->vport)
  807. BUG();
  808. else
  809. mod_timer(&piocb->vport->els_tmofunc,
  810. jiffies + HZ * (phba->fc_ratov << 1));
  811. }
  812. return 0;
  813. }
  814. /**
  815. * lpfc_sli_ringtx_get - Get first element of the txq
  816. * @phba: Pointer to HBA context object.
  817. * @pring: Pointer to driver SLI ring object.
  818. *
  819. * This function is called with hbalock held to get next
  820. * iocb in txq of the given ring. If there is any iocb in
  821. * the txq, the function returns first iocb in the list after
  822. * removing the iocb from the list, else it returns NULL.
  823. **/
  824. static struct lpfc_iocbq *
  825. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  826. {
  827. struct lpfc_iocbq *cmd_iocb;
  828. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  829. if (cmd_iocb != NULL)
  830. pring->txq_cnt--;
  831. return cmd_iocb;
  832. }
  833. /**
  834. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  835. * @phba: Pointer to HBA context object.
  836. * @pring: Pointer to driver SLI ring object.
  837. *
  838. * This function is called with hbalock held and the caller must post the
  839. * iocb without releasing the lock. If the caller releases the lock,
  840. * iocb slot returned by the function is not guaranteed to be available.
  841. * The function returns pointer to the next available iocb slot if there
  842. * is available slot in the ring, else it returns NULL.
  843. * If the get index of the ring is ahead of the put index, the function
  844. * will post an error attention event to the worker thread to take the
  845. * HBA to offline state.
  846. **/
  847. static IOCB_t *
  848. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  849. {
  850. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  851. uint32_t max_cmd_idx = pring->numCiocb;
  852. if ((pring->next_cmdidx == pring->cmdidx) &&
  853. (++pring->next_cmdidx >= max_cmd_idx))
  854. pring->next_cmdidx = 0;
  855. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  856. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  857. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  858. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  859. "0315 Ring %d issue: portCmdGet %d "
  860. "is bigger than cmd ring %d\n",
  861. pring->ringno,
  862. pring->local_getidx, max_cmd_idx);
  863. phba->link_state = LPFC_HBA_ERROR;
  864. /*
  865. * All error attention handlers are posted to
  866. * worker thread
  867. */
  868. phba->work_ha |= HA_ERATT;
  869. phba->work_hs = HS_FFER3;
  870. lpfc_worker_wake_up(phba);
  871. return NULL;
  872. }
  873. if (pring->local_getidx == pring->next_cmdidx)
  874. return NULL;
  875. }
  876. return lpfc_cmd_iocb(phba, pring);
  877. }
  878. /**
  879. * lpfc_sli_next_iotag - Get an iotag for the iocb
  880. * @phba: Pointer to HBA context object.
  881. * @iocbq: Pointer to driver iocb object.
  882. *
  883. * This function gets an iotag for the iocb. If there is no unused iotag and
  884. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  885. * array and assigns a new iotag.
  886. * The function returns the allocated iotag if successful, else returns zero.
  887. * Zero is not a valid iotag.
  888. * The caller is not required to hold any lock.
  889. **/
  890. uint16_t
  891. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  892. {
  893. struct lpfc_iocbq **new_arr;
  894. struct lpfc_iocbq **old_arr;
  895. size_t new_len;
  896. struct lpfc_sli *psli = &phba->sli;
  897. uint16_t iotag;
  898. spin_lock_irq(&phba->hbalock);
  899. iotag = psli->last_iotag;
  900. if(++iotag < psli->iocbq_lookup_len) {
  901. psli->last_iotag = iotag;
  902. psli->iocbq_lookup[iotag] = iocbq;
  903. spin_unlock_irq(&phba->hbalock);
  904. iocbq->iotag = iotag;
  905. return iotag;
  906. } else if (psli->iocbq_lookup_len < (0xffff
  907. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  908. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  909. spin_unlock_irq(&phba->hbalock);
  910. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  911. GFP_KERNEL);
  912. if (new_arr) {
  913. spin_lock_irq(&phba->hbalock);
  914. old_arr = psli->iocbq_lookup;
  915. if (new_len <= psli->iocbq_lookup_len) {
  916. /* highly unprobable case */
  917. kfree(new_arr);
  918. iotag = psli->last_iotag;
  919. if(++iotag < psli->iocbq_lookup_len) {
  920. psli->last_iotag = iotag;
  921. psli->iocbq_lookup[iotag] = iocbq;
  922. spin_unlock_irq(&phba->hbalock);
  923. iocbq->iotag = iotag;
  924. return iotag;
  925. }
  926. spin_unlock_irq(&phba->hbalock);
  927. return 0;
  928. }
  929. if (psli->iocbq_lookup)
  930. memcpy(new_arr, old_arr,
  931. ((psli->last_iotag + 1) *
  932. sizeof (struct lpfc_iocbq *)));
  933. psli->iocbq_lookup = new_arr;
  934. psli->iocbq_lookup_len = new_len;
  935. psli->last_iotag = iotag;
  936. psli->iocbq_lookup[iotag] = iocbq;
  937. spin_unlock_irq(&phba->hbalock);
  938. iocbq->iotag = iotag;
  939. kfree(old_arr);
  940. return iotag;
  941. }
  942. } else
  943. spin_unlock_irq(&phba->hbalock);
  944. lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
  945. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  946. psli->last_iotag);
  947. return 0;
  948. }
  949. /**
  950. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  951. * @phba: Pointer to HBA context object.
  952. * @pring: Pointer to driver SLI ring object.
  953. * @iocb: Pointer to iocb slot in the ring.
  954. * @nextiocb: Pointer to driver iocb object which need to be
  955. * posted to firmware.
  956. *
  957. * This function is called with hbalock held to post a new iocb to
  958. * the firmware. This function copies the new iocb to ring iocb slot and
  959. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  960. * a completion call back for this iocb else the function will free the
  961. * iocb object.
  962. **/
  963. static void
  964. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  965. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  966. {
  967. /*
  968. * Set up an iotag
  969. */
  970. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  971. if (pring->ringno == LPFC_ELS_RING) {
  972. lpfc_debugfs_slow_ring_trc(phba,
  973. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  974. *(((uint32_t *) &nextiocb->iocb) + 4),
  975. *(((uint32_t *) &nextiocb->iocb) + 6),
  976. *(((uint32_t *) &nextiocb->iocb) + 7));
  977. }
  978. /*
  979. * Issue iocb command to adapter
  980. */
  981. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  982. wmb();
  983. pring->stats.iocb_cmd++;
  984. /*
  985. * If there is no completion routine to call, we can release the
  986. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  987. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  988. */
  989. if (nextiocb->iocb_cmpl)
  990. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  991. else
  992. __lpfc_sli_release_iocbq(phba, nextiocb);
  993. /*
  994. * Let the HBA know what IOCB slot will be the next one the
  995. * driver will put a command into.
  996. */
  997. pring->cmdidx = pring->next_cmdidx;
  998. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  999. }
  1000. /**
  1001. * lpfc_sli_update_full_ring - Update the chip attention register
  1002. * @phba: Pointer to HBA context object.
  1003. * @pring: Pointer to driver SLI ring object.
  1004. *
  1005. * The caller is not required to hold any lock for calling this function.
  1006. * This function updates the chip attention bits for the ring to inform firmware
  1007. * that there are pending work to be done for this ring and requests an
  1008. * interrupt when there is space available in the ring. This function is
  1009. * called when the driver is unable to post more iocbs to the ring due
  1010. * to unavailability of space in the ring.
  1011. **/
  1012. static void
  1013. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1014. {
  1015. int ringno = pring->ringno;
  1016. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1017. wmb();
  1018. /*
  1019. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1020. * The HBA will tell us when an IOCB entry is available.
  1021. */
  1022. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1023. readl(phba->CAregaddr); /* flush */
  1024. pring->stats.iocb_cmd_full++;
  1025. }
  1026. /**
  1027. * lpfc_sli_update_ring - Update chip attention register
  1028. * @phba: Pointer to HBA context object.
  1029. * @pring: Pointer to driver SLI ring object.
  1030. *
  1031. * This function updates the chip attention register bit for the
  1032. * given ring to inform HBA that there is more work to be done
  1033. * in this ring. The caller is not required to hold any lock.
  1034. **/
  1035. static void
  1036. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1037. {
  1038. int ringno = pring->ringno;
  1039. /*
  1040. * Tell the HBA that there is work to do in this ring.
  1041. */
  1042. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1043. wmb();
  1044. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1045. readl(phba->CAregaddr); /* flush */
  1046. }
  1047. }
  1048. /**
  1049. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1050. * @phba: Pointer to HBA context object.
  1051. * @pring: Pointer to driver SLI ring object.
  1052. *
  1053. * This function is called with hbalock held to post pending iocbs
  1054. * in the txq to the firmware. This function is called when driver
  1055. * detects space available in the ring.
  1056. **/
  1057. static void
  1058. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1059. {
  1060. IOCB_t *iocb;
  1061. struct lpfc_iocbq *nextiocb;
  1062. /*
  1063. * Check to see if:
  1064. * (a) there is anything on the txq to send
  1065. * (b) link is up
  1066. * (c) link attention events can be processed (fcp ring only)
  1067. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1068. */
  1069. if (pring->txq_cnt &&
  1070. lpfc_is_link_up(phba) &&
  1071. (pring->ringno != phba->sli.fcp_ring ||
  1072. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1073. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1074. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1075. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1076. if (iocb)
  1077. lpfc_sli_update_ring(phba, pring);
  1078. else
  1079. lpfc_sli_update_full_ring(phba, pring);
  1080. }
  1081. return;
  1082. }
  1083. /**
  1084. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1085. * @phba: Pointer to HBA context object.
  1086. * @hbqno: HBQ number.
  1087. *
  1088. * This function is called with hbalock held to get the next
  1089. * available slot for the given HBQ. If there is free slot
  1090. * available for the HBQ it will return pointer to the next available
  1091. * HBQ entry else it will return NULL.
  1092. **/
  1093. static struct lpfc_hbq_entry *
  1094. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1095. {
  1096. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1097. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1098. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1099. hbqp->next_hbqPutIdx = 0;
  1100. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1101. uint32_t raw_index = phba->hbq_get[hbqno];
  1102. uint32_t getidx = le32_to_cpu(raw_index);
  1103. hbqp->local_hbqGetIdx = getidx;
  1104. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1105. lpfc_printf_log(phba, KERN_ERR,
  1106. LOG_SLI | LOG_VPORT,
  1107. "1802 HBQ %d: local_hbqGetIdx "
  1108. "%u is > than hbqp->entry_count %u\n",
  1109. hbqno, hbqp->local_hbqGetIdx,
  1110. hbqp->entry_count);
  1111. phba->link_state = LPFC_HBA_ERROR;
  1112. return NULL;
  1113. }
  1114. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1115. return NULL;
  1116. }
  1117. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1118. hbqp->hbqPutIdx;
  1119. }
  1120. /**
  1121. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1122. * @phba: Pointer to HBA context object.
  1123. *
  1124. * This function is called with no lock held to free all the
  1125. * hbq buffers while uninitializing the SLI interface. It also
  1126. * frees the HBQ buffers returned by the firmware but not yet
  1127. * processed by the upper layers.
  1128. **/
  1129. void
  1130. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1131. {
  1132. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1133. struct hbq_dmabuf *hbq_buf;
  1134. unsigned long flags;
  1135. int i, hbq_count;
  1136. uint32_t hbqno;
  1137. hbq_count = lpfc_sli_hbq_count();
  1138. /* Return all memory used by all HBQs */
  1139. spin_lock_irqsave(&phba->hbalock, flags);
  1140. for (i = 0; i < hbq_count; ++i) {
  1141. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1142. &phba->hbqs[i].hbq_buffer_list, list) {
  1143. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1144. list_del(&hbq_buf->dbuf.list);
  1145. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1146. }
  1147. phba->hbqs[i].buffer_count = 0;
  1148. }
  1149. /* Return all HBQ buffer that are in-fly */
  1150. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1151. list) {
  1152. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1153. list_del(&hbq_buf->dbuf.list);
  1154. if (hbq_buf->tag == -1) {
  1155. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1156. (phba, hbq_buf);
  1157. } else {
  1158. hbqno = hbq_buf->tag >> 16;
  1159. if (hbqno >= LPFC_MAX_HBQS)
  1160. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1161. (phba, hbq_buf);
  1162. else
  1163. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1164. hbq_buf);
  1165. }
  1166. }
  1167. /* Mark the HBQs not in use */
  1168. phba->hbq_in_use = 0;
  1169. spin_unlock_irqrestore(&phba->hbalock, flags);
  1170. }
  1171. /**
  1172. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1173. * @phba: Pointer to HBA context object.
  1174. * @hbqno: HBQ number.
  1175. * @hbq_buf: Pointer to HBQ buffer.
  1176. *
  1177. * This function is called with the hbalock held to post a
  1178. * hbq buffer to the firmware. If the function finds an empty
  1179. * slot in the HBQ, it will post the buffer. The function will return
  1180. * pointer to the hbq entry if it successfully post the buffer
  1181. * else it will return NULL.
  1182. **/
  1183. static int
  1184. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1185. struct hbq_dmabuf *hbq_buf)
  1186. {
  1187. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1188. }
  1189. /**
  1190. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1191. * @phba: Pointer to HBA context object.
  1192. * @hbqno: HBQ number.
  1193. * @hbq_buf: Pointer to HBQ buffer.
  1194. *
  1195. * This function is called with the hbalock held to post a hbq buffer to the
  1196. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1197. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1198. * it successfully post the buffer else it will return an error.
  1199. **/
  1200. static int
  1201. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1202. struct hbq_dmabuf *hbq_buf)
  1203. {
  1204. struct lpfc_hbq_entry *hbqe;
  1205. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1206. /* Get next HBQ entry slot to use */
  1207. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1208. if (hbqe) {
  1209. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1210. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1211. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1212. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1213. hbqe->bde.tus.f.bdeFlags = 0;
  1214. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1215. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1216. /* Sync SLIM */
  1217. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1218. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1219. /* flush */
  1220. readl(phba->hbq_put + hbqno);
  1221. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1222. return 0;
  1223. } else
  1224. return -ENOMEM;
  1225. }
  1226. /**
  1227. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1228. * @phba: Pointer to HBA context object.
  1229. * @hbqno: HBQ number.
  1230. * @hbq_buf: Pointer to HBQ buffer.
  1231. *
  1232. * This function is called with the hbalock held to post an RQE to the SLI4
  1233. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1234. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1235. **/
  1236. static int
  1237. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1238. struct hbq_dmabuf *hbq_buf)
  1239. {
  1240. int rc;
  1241. struct lpfc_rqe hrqe;
  1242. struct lpfc_rqe drqe;
  1243. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1244. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1245. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1246. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1247. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1248. &hrqe, &drqe);
  1249. if (rc < 0)
  1250. return rc;
  1251. hbq_buf->tag = rc;
  1252. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1253. return 0;
  1254. }
  1255. /* HBQ for ELS and CT traffic. */
  1256. static struct lpfc_hbq_init lpfc_els_hbq = {
  1257. .rn = 1,
  1258. .entry_count = 200,
  1259. .mask_count = 0,
  1260. .profile = 0,
  1261. .ring_mask = (1 << LPFC_ELS_RING),
  1262. .buffer_count = 0,
  1263. .init_count = 40,
  1264. .add_count = 40,
  1265. };
  1266. /* HBQ for the extra ring if needed */
  1267. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1268. .rn = 1,
  1269. .entry_count = 200,
  1270. .mask_count = 0,
  1271. .profile = 0,
  1272. .ring_mask = (1 << LPFC_EXTRA_RING),
  1273. .buffer_count = 0,
  1274. .init_count = 0,
  1275. .add_count = 5,
  1276. };
  1277. /* Array of HBQs */
  1278. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1279. &lpfc_els_hbq,
  1280. &lpfc_extra_hbq,
  1281. };
  1282. /**
  1283. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1284. * @phba: Pointer to HBA context object.
  1285. * @hbqno: HBQ number.
  1286. * @count: Number of HBQ buffers to be posted.
  1287. *
  1288. * This function is called with no lock held to post more hbq buffers to the
  1289. * given HBQ. The function returns the number of HBQ buffers successfully
  1290. * posted.
  1291. **/
  1292. static int
  1293. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1294. {
  1295. uint32_t i, posted = 0;
  1296. unsigned long flags;
  1297. struct hbq_dmabuf *hbq_buffer;
  1298. LIST_HEAD(hbq_buf_list);
  1299. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1300. return 0;
  1301. if ((phba->hbqs[hbqno].buffer_count + count) >
  1302. lpfc_hbq_defs[hbqno]->entry_count)
  1303. count = lpfc_hbq_defs[hbqno]->entry_count -
  1304. phba->hbqs[hbqno].buffer_count;
  1305. if (!count)
  1306. return 0;
  1307. /* Allocate HBQ entries */
  1308. for (i = 0; i < count; i++) {
  1309. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1310. if (!hbq_buffer)
  1311. break;
  1312. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1313. }
  1314. /* Check whether HBQ is still in use */
  1315. spin_lock_irqsave(&phba->hbalock, flags);
  1316. if (!phba->hbq_in_use)
  1317. goto err;
  1318. while (!list_empty(&hbq_buf_list)) {
  1319. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1320. dbuf.list);
  1321. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1322. (hbqno << 16));
  1323. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1324. phba->hbqs[hbqno].buffer_count++;
  1325. posted++;
  1326. } else
  1327. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1328. }
  1329. spin_unlock_irqrestore(&phba->hbalock, flags);
  1330. return posted;
  1331. err:
  1332. spin_unlock_irqrestore(&phba->hbalock, flags);
  1333. while (!list_empty(&hbq_buf_list)) {
  1334. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1335. dbuf.list);
  1336. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1337. }
  1338. return 0;
  1339. }
  1340. /**
  1341. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1342. * @phba: Pointer to HBA context object.
  1343. * @qno: HBQ number.
  1344. *
  1345. * This function posts more buffers to the HBQ. This function
  1346. * is called with no lock held. The function returns the number of HBQ entries
  1347. * successfully allocated.
  1348. **/
  1349. int
  1350. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1351. {
  1352. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1353. lpfc_hbq_defs[qno]->add_count));
  1354. }
  1355. /**
  1356. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1357. * @phba: Pointer to HBA context object.
  1358. * @qno: HBQ queue number.
  1359. *
  1360. * This function is called from SLI initialization code path with
  1361. * no lock held to post initial HBQ buffers to firmware. The
  1362. * function returns the number of HBQ entries successfully allocated.
  1363. **/
  1364. static int
  1365. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1366. {
  1367. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1368. lpfc_hbq_defs[qno]->init_count));
  1369. }
  1370. /**
  1371. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1372. * @phba: Pointer to HBA context object.
  1373. * @hbqno: HBQ number.
  1374. *
  1375. * This function removes the first hbq buffer on an hbq list and returns a
  1376. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1377. **/
  1378. static struct hbq_dmabuf *
  1379. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1380. {
  1381. struct lpfc_dmabuf *d_buf;
  1382. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1383. if (!d_buf)
  1384. return NULL;
  1385. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1386. }
  1387. /**
  1388. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1389. * @phba: Pointer to HBA context object.
  1390. * @tag: Tag of the hbq buffer.
  1391. *
  1392. * This function is called with hbalock held. This function searches
  1393. * for the hbq buffer associated with the given tag in the hbq buffer
  1394. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1395. * it returns NULL.
  1396. **/
  1397. static struct hbq_dmabuf *
  1398. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1399. {
  1400. struct lpfc_dmabuf *d_buf;
  1401. struct hbq_dmabuf *hbq_buf;
  1402. uint32_t hbqno;
  1403. hbqno = tag >> 16;
  1404. if (hbqno >= LPFC_MAX_HBQS)
  1405. return NULL;
  1406. spin_lock_irq(&phba->hbalock);
  1407. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1408. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1409. if (hbq_buf->tag == tag) {
  1410. spin_unlock_irq(&phba->hbalock);
  1411. return hbq_buf;
  1412. }
  1413. }
  1414. spin_unlock_irq(&phba->hbalock);
  1415. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1416. "1803 Bad hbq tag. Data: x%x x%x\n",
  1417. tag, phba->hbqs[tag >> 16].buffer_count);
  1418. return NULL;
  1419. }
  1420. /**
  1421. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1422. * @phba: Pointer to HBA context object.
  1423. * @hbq_buffer: Pointer to HBQ buffer.
  1424. *
  1425. * This function is called with hbalock. This function gives back
  1426. * the hbq buffer to firmware. If the HBQ does not have space to
  1427. * post the buffer, it will free the buffer.
  1428. **/
  1429. void
  1430. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1431. {
  1432. uint32_t hbqno;
  1433. if (hbq_buffer) {
  1434. hbqno = hbq_buffer->tag >> 16;
  1435. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1436. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1437. }
  1438. }
  1439. /**
  1440. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1441. * @mbxCommand: mailbox command code.
  1442. *
  1443. * This function is called by the mailbox event handler function to verify
  1444. * that the completed mailbox command is a legitimate mailbox command. If the
  1445. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1446. * and the mailbox event handler will take the HBA offline.
  1447. **/
  1448. static int
  1449. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1450. {
  1451. uint8_t ret;
  1452. switch (mbxCommand) {
  1453. case MBX_LOAD_SM:
  1454. case MBX_READ_NV:
  1455. case MBX_WRITE_NV:
  1456. case MBX_WRITE_VPARMS:
  1457. case MBX_RUN_BIU_DIAG:
  1458. case MBX_INIT_LINK:
  1459. case MBX_DOWN_LINK:
  1460. case MBX_CONFIG_LINK:
  1461. case MBX_CONFIG_RING:
  1462. case MBX_RESET_RING:
  1463. case MBX_READ_CONFIG:
  1464. case MBX_READ_RCONFIG:
  1465. case MBX_READ_SPARM:
  1466. case MBX_READ_STATUS:
  1467. case MBX_READ_RPI:
  1468. case MBX_READ_XRI:
  1469. case MBX_READ_REV:
  1470. case MBX_READ_LNK_STAT:
  1471. case MBX_REG_LOGIN:
  1472. case MBX_UNREG_LOGIN:
  1473. case MBX_READ_LA:
  1474. case MBX_CLEAR_LA:
  1475. case MBX_DUMP_MEMORY:
  1476. case MBX_DUMP_CONTEXT:
  1477. case MBX_RUN_DIAGS:
  1478. case MBX_RESTART:
  1479. case MBX_UPDATE_CFG:
  1480. case MBX_DOWN_LOAD:
  1481. case MBX_DEL_LD_ENTRY:
  1482. case MBX_RUN_PROGRAM:
  1483. case MBX_SET_MASK:
  1484. case MBX_SET_VARIABLE:
  1485. case MBX_UNREG_D_ID:
  1486. case MBX_KILL_BOARD:
  1487. case MBX_CONFIG_FARP:
  1488. case MBX_BEACON:
  1489. case MBX_LOAD_AREA:
  1490. case MBX_RUN_BIU_DIAG64:
  1491. case MBX_CONFIG_PORT:
  1492. case MBX_READ_SPARM64:
  1493. case MBX_READ_RPI64:
  1494. case MBX_REG_LOGIN64:
  1495. case MBX_READ_LA64:
  1496. case MBX_WRITE_WWN:
  1497. case MBX_SET_DEBUG:
  1498. case MBX_LOAD_EXP_ROM:
  1499. case MBX_ASYNCEVT_ENABLE:
  1500. case MBX_REG_VPI:
  1501. case MBX_UNREG_VPI:
  1502. case MBX_HEARTBEAT:
  1503. case MBX_PORT_CAPABILITIES:
  1504. case MBX_PORT_IOV_CONTROL:
  1505. case MBX_SLI4_CONFIG:
  1506. case MBX_SLI4_REQ_FTRS:
  1507. case MBX_REG_FCFI:
  1508. case MBX_UNREG_FCFI:
  1509. case MBX_REG_VFI:
  1510. case MBX_UNREG_VFI:
  1511. case MBX_INIT_VPI:
  1512. case MBX_INIT_VFI:
  1513. case MBX_RESUME_RPI:
  1514. ret = mbxCommand;
  1515. break;
  1516. default:
  1517. ret = MBX_SHUTDOWN;
  1518. break;
  1519. }
  1520. return ret;
  1521. }
  1522. /**
  1523. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1524. * @phba: Pointer to HBA context object.
  1525. * @pmboxq: Pointer to mailbox command.
  1526. *
  1527. * This is completion handler function for mailbox commands issued from
  1528. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1529. * mailbox event handler function with no lock held. This function
  1530. * will wake up thread waiting on the wait queue pointed by context1
  1531. * of the mailbox.
  1532. **/
  1533. void
  1534. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1535. {
  1536. wait_queue_head_t *pdone_q;
  1537. unsigned long drvr_flag;
  1538. /*
  1539. * If pdone_q is empty, the driver thread gave up waiting and
  1540. * continued running.
  1541. */
  1542. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1543. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1544. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1545. if (pdone_q)
  1546. wake_up_interruptible(pdone_q);
  1547. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1548. return;
  1549. }
  1550. /**
  1551. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1552. * @phba: Pointer to HBA context object.
  1553. * @pmb: Pointer to mailbox object.
  1554. *
  1555. * This function is the default mailbox completion handler. It
  1556. * frees the memory resources associated with the completed mailbox
  1557. * command. If the completed command is a REG_LOGIN mailbox command,
  1558. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1559. **/
  1560. void
  1561. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1562. {
  1563. struct lpfc_dmabuf *mp;
  1564. uint16_t rpi, vpi;
  1565. int rc;
  1566. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1567. if (mp) {
  1568. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1569. kfree(mp);
  1570. }
  1571. if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
  1572. (phba->sli_rev == LPFC_SLI_REV4))
  1573. lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
  1574. /*
  1575. * If a REG_LOGIN succeeded after node is destroyed or node
  1576. * is in re-discovery driver need to cleanup the RPI.
  1577. */
  1578. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1579. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1580. !pmb->u.mb.mbxStatus) {
  1581. rpi = pmb->u.mb.un.varWords[0];
  1582. vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
  1583. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1584. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1585. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1586. if (rc != MBX_NOT_FINISHED)
  1587. return;
  1588. }
  1589. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1590. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1591. else
  1592. mempool_free(pmb, phba->mbox_mem_pool);
  1593. }
  1594. /**
  1595. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1596. * @phba: Pointer to HBA context object.
  1597. *
  1598. * This function is called with no lock held. This function processes all
  1599. * the completed mailbox commands and gives it to upper layers. The interrupt
  1600. * service routine processes mailbox completion interrupt and adds completed
  1601. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1602. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1603. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1604. * function returns the mailbox commands to the upper layer by calling the
  1605. * completion handler function of each mailbox.
  1606. **/
  1607. int
  1608. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1609. {
  1610. MAILBOX_t *pmbox;
  1611. LPFC_MBOXQ_t *pmb;
  1612. int rc;
  1613. LIST_HEAD(cmplq);
  1614. phba->sli.slistat.mbox_event++;
  1615. /* Get all completed mailboxe buffers into the cmplq */
  1616. spin_lock_irq(&phba->hbalock);
  1617. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  1618. spin_unlock_irq(&phba->hbalock);
  1619. /* Get a Mailbox buffer to setup mailbox commands for callback */
  1620. do {
  1621. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  1622. if (pmb == NULL)
  1623. break;
  1624. pmbox = &pmb->u.mb;
  1625. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  1626. if (pmb->vport) {
  1627. lpfc_debugfs_disc_trc(pmb->vport,
  1628. LPFC_DISC_TRC_MBOX_VPORT,
  1629. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  1630. (uint32_t)pmbox->mbxCommand,
  1631. pmbox->un.varWords[0],
  1632. pmbox->un.varWords[1]);
  1633. }
  1634. else {
  1635. lpfc_debugfs_disc_trc(phba->pport,
  1636. LPFC_DISC_TRC_MBOX,
  1637. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  1638. (uint32_t)pmbox->mbxCommand,
  1639. pmbox->un.varWords[0],
  1640. pmbox->un.varWords[1]);
  1641. }
  1642. }
  1643. /*
  1644. * It is a fatal error if unknown mbox command completion.
  1645. */
  1646. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  1647. MBX_SHUTDOWN) {
  1648. /* Unknow mailbox command compl */
  1649. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1650. "(%d):0323 Unknown Mailbox command "
  1651. "x%x (x%x) Cmpl\n",
  1652. pmb->vport ? pmb->vport->vpi : 0,
  1653. pmbox->mbxCommand,
  1654. lpfc_sli4_mbox_opcode_get(phba, pmb));
  1655. phba->link_state = LPFC_HBA_ERROR;
  1656. phba->work_hs = HS_FFER3;
  1657. lpfc_handle_eratt(phba);
  1658. continue;
  1659. }
  1660. if (pmbox->mbxStatus) {
  1661. phba->sli.slistat.mbox_stat_err++;
  1662. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  1663. /* Mbox cmd cmpl error - RETRYing */
  1664. lpfc_printf_log(phba, KERN_INFO,
  1665. LOG_MBOX | LOG_SLI,
  1666. "(%d):0305 Mbox cmd cmpl "
  1667. "error - RETRYing Data: x%x "
  1668. "(x%x) x%x x%x x%x\n",
  1669. pmb->vport ? pmb->vport->vpi :0,
  1670. pmbox->mbxCommand,
  1671. lpfc_sli4_mbox_opcode_get(phba,
  1672. pmb),
  1673. pmbox->mbxStatus,
  1674. pmbox->un.varWords[0],
  1675. pmb->vport->port_state);
  1676. pmbox->mbxStatus = 0;
  1677. pmbox->mbxOwner = OWN_HOST;
  1678. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1679. if (rc != MBX_NOT_FINISHED)
  1680. continue;
  1681. }
  1682. }
  1683. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  1684. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  1685. "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
  1686. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1687. pmb->vport ? pmb->vport->vpi : 0,
  1688. pmbox->mbxCommand,
  1689. lpfc_sli4_mbox_opcode_get(phba, pmb),
  1690. pmb->mbox_cmpl,
  1691. *((uint32_t *) pmbox),
  1692. pmbox->un.varWords[0],
  1693. pmbox->un.varWords[1],
  1694. pmbox->un.varWords[2],
  1695. pmbox->un.varWords[3],
  1696. pmbox->un.varWords[4],
  1697. pmbox->un.varWords[5],
  1698. pmbox->un.varWords[6],
  1699. pmbox->un.varWords[7]);
  1700. if (pmb->mbox_cmpl)
  1701. pmb->mbox_cmpl(phba,pmb);
  1702. } while (1);
  1703. return 0;
  1704. }
  1705. /**
  1706. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  1707. * @phba: Pointer to HBA context object.
  1708. * @pring: Pointer to driver SLI ring object.
  1709. * @tag: buffer tag.
  1710. *
  1711. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  1712. * is set in the tag the buffer is posted for a particular exchange,
  1713. * the function will return the buffer without replacing the buffer.
  1714. * If the buffer is for unsolicited ELS or CT traffic, this function
  1715. * returns the buffer and also posts another buffer to the firmware.
  1716. **/
  1717. static struct lpfc_dmabuf *
  1718. lpfc_sli_get_buff(struct lpfc_hba *phba,
  1719. struct lpfc_sli_ring *pring,
  1720. uint32_t tag)
  1721. {
  1722. struct hbq_dmabuf *hbq_entry;
  1723. if (tag & QUE_BUFTAG_BIT)
  1724. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  1725. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  1726. if (!hbq_entry)
  1727. return NULL;
  1728. return &hbq_entry->dbuf;
  1729. }
  1730. /**
  1731. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  1732. * @phba: Pointer to HBA context object.
  1733. * @pring: Pointer to driver SLI ring object.
  1734. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  1735. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  1736. * @fch_type: the type for the first frame of the sequence.
  1737. *
  1738. * This function is called with no lock held. This function uses the r_ctl and
  1739. * type of the received sequence to find the correct callback function to call
  1740. * to process the sequence.
  1741. **/
  1742. static int
  1743. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1744. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  1745. uint32_t fch_type)
  1746. {
  1747. int i;
  1748. /* unSolicited Responses */
  1749. if (pring->prt[0].profile) {
  1750. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  1751. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  1752. saveq);
  1753. return 1;
  1754. }
  1755. /* We must search, based on rctl / type
  1756. for the right routine */
  1757. for (i = 0; i < pring->num_mask; i++) {
  1758. if ((pring->prt[i].rctl == fch_r_ctl) &&
  1759. (pring->prt[i].type == fch_type)) {
  1760. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1761. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1762. (phba, pring, saveq);
  1763. return 1;
  1764. }
  1765. }
  1766. return 0;
  1767. }
  1768. /**
  1769. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  1770. * @phba: Pointer to HBA context object.
  1771. * @pring: Pointer to driver SLI ring object.
  1772. * @saveq: Pointer to the unsolicited iocb.
  1773. *
  1774. * This function is called with no lock held by the ring event handler
  1775. * when there is an unsolicited iocb posted to the response ring by the
  1776. * firmware. This function gets the buffer associated with the iocbs
  1777. * and calls the event handler for the ring. This function handles both
  1778. * qring buffers and hbq buffers.
  1779. * When the function returns 1 the caller can free the iocb object otherwise
  1780. * upper layer functions will free the iocb objects.
  1781. **/
  1782. static int
  1783. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1784. struct lpfc_iocbq *saveq)
  1785. {
  1786. IOCB_t * irsp;
  1787. WORD5 * w5p;
  1788. uint32_t Rctl, Type;
  1789. uint32_t match;
  1790. struct lpfc_iocbq *iocbq;
  1791. struct lpfc_dmabuf *dmzbuf;
  1792. match = 0;
  1793. irsp = &(saveq->iocb);
  1794. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  1795. if (pring->lpfc_sli_rcv_async_status)
  1796. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  1797. else
  1798. lpfc_printf_log(phba,
  1799. KERN_WARNING,
  1800. LOG_SLI,
  1801. "0316 Ring %d handler: unexpected "
  1802. "ASYNC_STATUS iocb received evt_code "
  1803. "0x%x\n",
  1804. pring->ringno,
  1805. irsp->un.asyncstat.evt_code);
  1806. return 1;
  1807. }
  1808. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  1809. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  1810. if (irsp->ulpBdeCount > 0) {
  1811. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1812. irsp->un.ulpWord[3]);
  1813. lpfc_in_buf_free(phba, dmzbuf);
  1814. }
  1815. if (irsp->ulpBdeCount > 1) {
  1816. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1817. irsp->unsli3.sli3Words[3]);
  1818. lpfc_in_buf_free(phba, dmzbuf);
  1819. }
  1820. if (irsp->ulpBdeCount > 2) {
  1821. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1822. irsp->unsli3.sli3Words[7]);
  1823. lpfc_in_buf_free(phba, dmzbuf);
  1824. }
  1825. return 1;
  1826. }
  1827. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  1828. if (irsp->ulpBdeCount != 0) {
  1829. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  1830. irsp->un.ulpWord[3]);
  1831. if (!saveq->context2)
  1832. lpfc_printf_log(phba,
  1833. KERN_ERR,
  1834. LOG_SLI,
  1835. "0341 Ring %d Cannot find buffer for "
  1836. "an unsolicited iocb. tag 0x%x\n",
  1837. pring->ringno,
  1838. irsp->un.ulpWord[3]);
  1839. }
  1840. if (irsp->ulpBdeCount == 2) {
  1841. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  1842. irsp->unsli3.sli3Words[7]);
  1843. if (!saveq->context3)
  1844. lpfc_printf_log(phba,
  1845. KERN_ERR,
  1846. LOG_SLI,
  1847. "0342 Ring %d Cannot find buffer for an"
  1848. " unsolicited iocb. tag 0x%x\n",
  1849. pring->ringno,
  1850. irsp->unsli3.sli3Words[7]);
  1851. }
  1852. list_for_each_entry(iocbq, &saveq->list, list) {
  1853. irsp = &(iocbq->iocb);
  1854. if (irsp->ulpBdeCount != 0) {
  1855. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  1856. irsp->un.ulpWord[3]);
  1857. if (!iocbq->context2)
  1858. lpfc_printf_log(phba,
  1859. KERN_ERR,
  1860. LOG_SLI,
  1861. "0343 Ring %d Cannot find "
  1862. "buffer for an unsolicited iocb"
  1863. ". tag 0x%x\n", pring->ringno,
  1864. irsp->un.ulpWord[3]);
  1865. }
  1866. if (irsp->ulpBdeCount == 2) {
  1867. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  1868. irsp->unsli3.sli3Words[7]);
  1869. if (!iocbq->context3)
  1870. lpfc_printf_log(phba,
  1871. KERN_ERR,
  1872. LOG_SLI,
  1873. "0344 Ring %d Cannot find "
  1874. "buffer for an unsolicited "
  1875. "iocb. tag 0x%x\n",
  1876. pring->ringno,
  1877. irsp->unsli3.sli3Words[7]);
  1878. }
  1879. }
  1880. }
  1881. if (irsp->ulpBdeCount != 0 &&
  1882. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  1883. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  1884. int found = 0;
  1885. /* search continue save q for same XRI */
  1886. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  1887. if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
  1888. list_add_tail(&saveq->list, &iocbq->list);
  1889. found = 1;
  1890. break;
  1891. }
  1892. }
  1893. if (!found)
  1894. list_add_tail(&saveq->clist,
  1895. &pring->iocb_continue_saveq);
  1896. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  1897. list_del_init(&iocbq->clist);
  1898. saveq = iocbq;
  1899. irsp = &(saveq->iocb);
  1900. } else
  1901. return 0;
  1902. }
  1903. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  1904. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  1905. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  1906. Rctl = FC_ELS_REQ;
  1907. Type = FC_ELS_DATA;
  1908. } else {
  1909. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  1910. Rctl = w5p->hcsw.Rctl;
  1911. Type = w5p->hcsw.Type;
  1912. /* Firmware Workaround */
  1913. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  1914. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  1915. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  1916. Rctl = FC_ELS_REQ;
  1917. Type = FC_ELS_DATA;
  1918. w5p->hcsw.Rctl = Rctl;
  1919. w5p->hcsw.Type = Type;
  1920. }
  1921. }
  1922. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  1923. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1924. "0313 Ring %d handler: unexpected Rctl x%x "
  1925. "Type x%x received\n",
  1926. pring->ringno, Rctl, Type);
  1927. return 1;
  1928. }
  1929. /**
  1930. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  1931. * @phba: Pointer to HBA context object.
  1932. * @pring: Pointer to driver SLI ring object.
  1933. * @prspiocb: Pointer to response iocb object.
  1934. *
  1935. * This function looks up the iocb_lookup table to get the command iocb
  1936. * corresponding to the given response iocb using the iotag of the
  1937. * response iocb. This function is called with the hbalock held.
  1938. * This function returns the command iocb object if it finds the command
  1939. * iocb else returns NULL.
  1940. **/
  1941. static struct lpfc_iocbq *
  1942. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  1943. struct lpfc_sli_ring *pring,
  1944. struct lpfc_iocbq *prspiocb)
  1945. {
  1946. struct lpfc_iocbq *cmd_iocb = NULL;
  1947. uint16_t iotag;
  1948. iotag = prspiocb->iocb.ulpIoTag;
  1949. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  1950. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  1951. list_del_init(&cmd_iocb->list);
  1952. pring->txcmplq_cnt--;
  1953. return cmd_iocb;
  1954. }
  1955. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1956. "0317 iotag x%x is out off "
  1957. "range: max iotag x%x wd0 x%x\n",
  1958. iotag, phba->sli.last_iotag,
  1959. *(((uint32_t *) &prspiocb->iocb) + 7));
  1960. return NULL;
  1961. }
  1962. /**
  1963. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  1964. * @phba: Pointer to HBA context object.
  1965. * @pring: Pointer to driver SLI ring object.
  1966. * @iotag: IOCB tag.
  1967. *
  1968. * This function looks up the iocb_lookup table to get the command iocb
  1969. * corresponding to the given iotag. This function is called with the
  1970. * hbalock held.
  1971. * This function returns the command iocb object if it finds the command
  1972. * iocb else returns NULL.
  1973. **/
  1974. static struct lpfc_iocbq *
  1975. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  1976. struct lpfc_sli_ring *pring, uint16_t iotag)
  1977. {
  1978. struct lpfc_iocbq *cmd_iocb;
  1979. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  1980. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  1981. list_del_init(&cmd_iocb->list);
  1982. pring->txcmplq_cnt--;
  1983. return cmd_iocb;
  1984. }
  1985. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1986. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  1987. iotag, phba->sli.last_iotag);
  1988. return NULL;
  1989. }
  1990. /**
  1991. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  1992. * @phba: Pointer to HBA context object.
  1993. * @pring: Pointer to driver SLI ring object.
  1994. * @saveq: Pointer to the response iocb to be processed.
  1995. *
  1996. * This function is called by the ring event handler for non-fcp
  1997. * rings when there is a new response iocb in the response ring.
  1998. * The caller is not required to hold any locks. This function
  1999. * gets the command iocb associated with the response iocb and
  2000. * calls the completion handler for the command iocb. If there
  2001. * is no completion handler, the function will free the resources
  2002. * associated with command iocb. If the response iocb is for
  2003. * an already aborted command iocb, the status of the completion
  2004. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2005. * This function always returns 1.
  2006. **/
  2007. static int
  2008. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2009. struct lpfc_iocbq *saveq)
  2010. {
  2011. struct lpfc_iocbq *cmdiocbp;
  2012. int rc = 1;
  2013. unsigned long iflag;
  2014. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2015. spin_lock_irqsave(&phba->hbalock, iflag);
  2016. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2017. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2018. if (cmdiocbp) {
  2019. if (cmdiocbp->iocb_cmpl) {
  2020. /*
  2021. * If an ELS command failed send an event to mgmt
  2022. * application.
  2023. */
  2024. if (saveq->iocb.ulpStatus &&
  2025. (pring->ringno == LPFC_ELS_RING) &&
  2026. (cmdiocbp->iocb.ulpCommand ==
  2027. CMD_ELS_REQUEST64_CR))
  2028. lpfc_send_els_failure_event(phba,
  2029. cmdiocbp, saveq);
  2030. /*
  2031. * Post all ELS completions to the worker thread.
  2032. * All other are passed to the completion callback.
  2033. */
  2034. if (pring->ringno == LPFC_ELS_RING) {
  2035. if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
  2036. cmdiocbp->iocb_flag &=
  2037. ~LPFC_DRIVER_ABORTED;
  2038. saveq->iocb.ulpStatus =
  2039. IOSTAT_LOCAL_REJECT;
  2040. saveq->iocb.un.ulpWord[4] =
  2041. IOERR_SLI_ABORTED;
  2042. /* Firmware could still be in progress
  2043. * of DMAing payload, so don't free data
  2044. * buffer till after a hbeat.
  2045. */
  2046. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2047. }
  2048. }
  2049. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2050. } else
  2051. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2052. } else {
  2053. /*
  2054. * Unknown initiating command based on the response iotag.
  2055. * This could be the case on the ELS ring because of
  2056. * lpfc_els_abort().
  2057. */
  2058. if (pring->ringno != LPFC_ELS_RING) {
  2059. /*
  2060. * Ring <ringno> handler: unexpected completion IoTag
  2061. * <IoTag>
  2062. */
  2063. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2064. "0322 Ring %d handler: "
  2065. "unexpected completion IoTag x%x "
  2066. "Data: x%x x%x x%x x%x\n",
  2067. pring->ringno,
  2068. saveq->iocb.ulpIoTag,
  2069. saveq->iocb.ulpStatus,
  2070. saveq->iocb.un.ulpWord[4],
  2071. saveq->iocb.ulpCommand,
  2072. saveq->iocb.ulpContext);
  2073. }
  2074. }
  2075. return rc;
  2076. }
  2077. /**
  2078. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2079. * @phba: Pointer to HBA context object.
  2080. * @pring: Pointer to driver SLI ring object.
  2081. *
  2082. * This function is called from the iocb ring event handlers when
  2083. * put pointer is ahead of the get pointer for a ring. This function signal
  2084. * an error attention condition to the worker thread and the worker
  2085. * thread will transition the HBA to offline state.
  2086. **/
  2087. static void
  2088. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2089. {
  2090. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2091. /*
  2092. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2093. * rsp ring <portRspMax>
  2094. */
  2095. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2096. "0312 Ring %d handler: portRspPut %d "
  2097. "is bigger than rsp ring %d\n",
  2098. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2099. pring->numRiocb);
  2100. phba->link_state = LPFC_HBA_ERROR;
  2101. /*
  2102. * All error attention handlers are posted to
  2103. * worker thread
  2104. */
  2105. phba->work_ha |= HA_ERATT;
  2106. phba->work_hs = HS_FFER3;
  2107. lpfc_worker_wake_up(phba);
  2108. return;
  2109. }
  2110. /**
  2111. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2112. * @ptr: Pointer to address of HBA context object.
  2113. *
  2114. * This function is invoked by the Error Attention polling timer when the
  2115. * timer times out. It will check the SLI Error Attention register for
  2116. * possible attention events. If so, it will post an Error Attention event
  2117. * and wake up worker thread to process it. Otherwise, it will set up the
  2118. * Error Attention polling timer for the next poll.
  2119. **/
  2120. void lpfc_poll_eratt(unsigned long ptr)
  2121. {
  2122. struct lpfc_hba *phba;
  2123. uint32_t eratt = 0;
  2124. phba = (struct lpfc_hba *)ptr;
  2125. /* Check chip HA register for error event */
  2126. eratt = lpfc_sli_check_eratt(phba);
  2127. if (eratt)
  2128. /* Tell the worker thread there is work to do */
  2129. lpfc_worker_wake_up(phba);
  2130. else
  2131. /* Restart the timer for next eratt poll */
  2132. mod_timer(&phba->eratt_poll, jiffies +
  2133. HZ * LPFC_ERATT_POLL_INTERVAL);
  2134. return;
  2135. }
  2136. /**
  2137. * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
  2138. * @phba: Pointer to HBA context object.
  2139. *
  2140. * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
  2141. * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
  2142. * is enabled.
  2143. *
  2144. * The caller does not hold any lock.
  2145. * The function processes each response iocb in the response ring until it
  2146. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  2147. * LE bit set. The function will call the completion handler of the command iocb
  2148. * if the response iocb indicates a completion for a command iocb or it is
  2149. * an abort completion.
  2150. **/
  2151. void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
  2152. {
  2153. struct lpfc_sli *psli = &phba->sli;
  2154. struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
  2155. IOCB_t *irsp = NULL;
  2156. IOCB_t *entry = NULL;
  2157. struct lpfc_iocbq *cmdiocbq = NULL;
  2158. struct lpfc_iocbq rspiocbq;
  2159. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2160. uint32_t status;
  2161. uint32_t portRspPut, portRspMax;
  2162. int type;
  2163. uint32_t rsp_cmpl = 0;
  2164. uint32_t ha_copy;
  2165. unsigned long iflags;
  2166. pring->stats.iocb_event++;
  2167. /*
  2168. * The next available response entry should never exceed the maximum
  2169. * entries. If it does, treat it as an adapter hardware error.
  2170. */
  2171. portRspMax = pring->numRiocb;
  2172. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2173. if (unlikely(portRspPut >= portRspMax)) {
  2174. lpfc_sli_rsp_pointers_error(phba, pring);
  2175. return;
  2176. }
  2177. rmb();
  2178. while (pring->rspidx != portRspPut) {
  2179. entry = lpfc_resp_iocb(phba, pring);
  2180. if (++pring->rspidx >= portRspMax)
  2181. pring->rspidx = 0;
  2182. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2183. (uint32_t *) &rspiocbq.iocb,
  2184. phba->iocb_rsp_size);
  2185. irsp = &rspiocbq.iocb;
  2186. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2187. pring->stats.iocb_rsp++;
  2188. rsp_cmpl++;
  2189. if (unlikely(irsp->ulpStatus)) {
  2190. /* Rsp ring <ringno> error: IOCB */
  2191. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2192. "0326 Rsp Ring %d error: IOCB Data: "
  2193. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2194. pring->ringno,
  2195. irsp->un.ulpWord[0],
  2196. irsp->un.ulpWord[1],
  2197. irsp->un.ulpWord[2],
  2198. irsp->un.ulpWord[3],
  2199. irsp->un.ulpWord[4],
  2200. irsp->un.ulpWord[5],
  2201. *(uint32_t *)&irsp->un1,
  2202. *((uint32_t *)&irsp->un1 + 1));
  2203. }
  2204. switch (type) {
  2205. case LPFC_ABORT_IOCB:
  2206. case LPFC_SOL_IOCB:
  2207. /*
  2208. * Idle exchange closed via ABTS from port. No iocb
  2209. * resources need to be recovered.
  2210. */
  2211. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2212. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2213. "0314 IOCB cmd 0x%x "
  2214. "processed. Skipping "
  2215. "completion",
  2216. irsp->ulpCommand);
  2217. break;
  2218. }
  2219. spin_lock_irqsave(&phba->hbalock, iflags);
  2220. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2221. &rspiocbq);
  2222. spin_unlock_irqrestore(&phba->hbalock, iflags);
  2223. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  2224. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2225. &rspiocbq);
  2226. }
  2227. break;
  2228. default:
  2229. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2230. char adaptermsg[LPFC_MAX_ADPTMSG];
  2231. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2232. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2233. MAX_MSG_DATA);
  2234. dev_warn(&((phba->pcidev)->dev),
  2235. "lpfc%d: %s\n",
  2236. phba->brd_no, adaptermsg);
  2237. } else {
  2238. /* Unknown IOCB command */
  2239. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2240. "0321 Unknown IOCB command "
  2241. "Data: x%x, x%x x%x x%x x%x\n",
  2242. type, irsp->ulpCommand,
  2243. irsp->ulpStatus,
  2244. irsp->ulpIoTag,
  2245. irsp->ulpContext);
  2246. }
  2247. break;
  2248. }
  2249. /*
  2250. * The response IOCB has been processed. Update the ring
  2251. * pointer in SLIM. If the port response put pointer has not
  2252. * been updated, sync the pgp->rspPutInx and fetch the new port
  2253. * response put pointer.
  2254. */
  2255. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2256. if (pring->rspidx == portRspPut)
  2257. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2258. }
  2259. ha_copy = readl(phba->HAregaddr);
  2260. ha_copy >>= (LPFC_FCP_RING * 4);
  2261. if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
  2262. spin_lock_irqsave(&phba->hbalock, iflags);
  2263. pring->stats.iocb_rsp_full++;
  2264. status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
  2265. writel(status, phba->CAregaddr);
  2266. readl(phba->CAregaddr);
  2267. spin_unlock_irqrestore(&phba->hbalock, iflags);
  2268. }
  2269. if ((ha_copy & HA_R0CE_RSP) &&
  2270. (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2271. spin_lock_irqsave(&phba->hbalock, iflags);
  2272. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2273. pring->stats.iocb_cmd_empty++;
  2274. /* Force update of the local copy of cmdGetInx */
  2275. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2276. lpfc_sli_resume_iocb(phba, pring);
  2277. if ((pring->lpfc_sli_cmd_available))
  2278. (pring->lpfc_sli_cmd_available) (phba, pring);
  2279. spin_unlock_irqrestore(&phba->hbalock, iflags);
  2280. }
  2281. return;
  2282. }
  2283. /**
  2284. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2285. * @phba: Pointer to HBA context object.
  2286. * @pring: Pointer to driver SLI ring object.
  2287. * @mask: Host attention register mask for this ring.
  2288. *
  2289. * This function is called from the interrupt context when there is a ring
  2290. * event for the fcp ring. The caller does not hold any lock.
  2291. * The function processes each response iocb in the response ring until it
  2292. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  2293. * LE bit set. The function will call the completion handler of the command iocb
  2294. * if the response iocb indicates a completion for a command iocb or it is
  2295. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2296. * function if this is an unsolicited iocb.
  2297. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2298. * to check it explicitly. This function always returns 1.
  2299. **/
  2300. static int
  2301. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2302. struct lpfc_sli_ring *pring, uint32_t mask)
  2303. {
  2304. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2305. IOCB_t *irsp = NULL;
  2306. IOCB_t *entry = NULL;
  2307. struct lpfc_iocbq *cmdiocbq = NULL;
  2308. struct lpfc_iocbq rspiocbq;
  2309. uint32_t status;
  2310. uint32_t portRspPut, portRspMax;
  2311. int rc = 1;
  2312. lpfc_iocb_type type;
  2313. unsigned long iflag;
  2314. uint32_t rsp_cmpl = 0;
  2315. spin_lock_irqsave(&phba->hbalock, iflag);
  2316. pring->stats.iocb_event++;
  2317. /*
  2318. * The next available response entry should never exceed the maximum
  2319. * entries. If it does, treat it as an adapter hardware error.
  2320. */
  2321. portRspMax = pring->numRiocb;
  2322. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2323. if (unlikely(portRspPut >= portRspMax)) {
  2324. lpfc_sli_rsp_pointers_error(phba, pring);
  2325. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2326. return 1;
  2327. }
  2328. rmb();
  2329. while (pring->rspidx != portRspPut) {
  2330. /*
  2331. * Fetch an entry off the ring and copy it into a local data
  2332. * structure. The copy involves a byte-swap since the
  2333. * network byte order and pci byte orders are different.
  2334. */
  2335. entry = lpfc_resp_iocb(phba, pring);
  2336. phba->last_completion_time = jiffies;
  2337. if (++pring->rspidx >= portRspMax)
  2338. pring->rspidx = 0;
  2339. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2340. (uint32_t *) &rspiocbq.iocb,
  2341. phba->iocb_rsp_size);
  2342. INIT_LIST_HEAD(&(rspiocbq.list));
  2343. irsp = &rspiocbq.iocb;
  2344. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2345. pring->stats.iocb_rsp++;
  2346. rsp_cmpl++;
  2347. if (unlikely(irsp->ulpStatus)) {
  2348. /*
  2349. * If resource errors reported from HBA, reduce
  2350. * queuedepths of the SCSI device.
  2351. */
  2352. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2353. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2354. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2355. phba->lpfc_rampdown_queue_depth(phba);
  2356. spin_lock_irqsave(&phba->hbalock, iflag);
  2357. }
  2358. /* Rsp ring <ringno> error: IOCB */
  2359. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2360. "0336 Rsp Ring %d error: IOCB Data: "
  2361. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2362. pring->ringno,
  2363. irsp->un.ulpWord[0],
  2364. irsp->un.ulpWord[1],
  2365. irsp->un.ulpWord[2],
  2366. irsp->un.ulpWord[3],
  2367. irsp->un.ulpWord[4],
  2368. irsp->un.ulpWord[5],
  2369. *(uint32_t *)&irsp->un1,
  2370. *((uint32_t *)&irsp->un1 + 1));
  2371. }
  2372. switch (type) {
  2373. case LPFC_ABORT_IOCB:
  2374. case LPFC_SOL_IOCB:
  2375. /*
  2376. * Idle exchange closed via ABTS from port. No iocb
  2377. * resources need to be recovered.
  2378. */
  2379. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2380. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2381. "0333 IOCB cmd 0x%x"
  2382. " processed. Skipping"
  2383. " completion\n",
  2384. irsp->ulpCommand);
  2385. break;
  2386. }
  2387. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2388. &rspiocbq);
  2389. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  2390. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2391. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2392. &rspiocbq);
  2393. } else {
  2394. spin_unlock_irqrestore(&phba->hbalock,
  2395. iflag);
  2396. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2397. &rspiocbq);
  2398. spin_lock_irqsave(&phba->hbalock,
  2399. iflag);
  2400. }
  2401. }
  2402. break;
  2403. case LPFC_UNSOL_IOCB:
  2404. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2405. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2406. spin_lock_irqsave(&phba->hbalock, iflag);
  2407. break;
  2408. default:
  2409. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2410. char adaptermsg[LPFC_MAX_ADPTMSG];
  2411. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2412. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2413. MAX_MSG_DATA);
  2414. dev_warn(&((phba->pcidev)->dev),
  2415. "lpfc%d: %s\n",
  2416. phba->brd_no, adaptermsg);
  2417. } else {
  2418. /* Unknown IOCB command */
  2419. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2420. "0334 Unknown IOCB command "
  2421. "Data: x%x, x%x x%x x%x x%x\n",
  2422. type, irsp->ulpCommand,
  2423. irsp->ulpStatus,
  2424. irsp->ulpIoTag,
  2425. irsp->ulpContext);
  2426. }
  2427. break;
  2428. }
  2429. /*
  2430. * The response IOCB has been processed. Update the ring
  2431. * pointer in SLIM. If the port response put pointer has not
  2432. * been updated, sync the pgp->rspPutInx and fetch the new port
  2433. * response put pointer.
  2434. */
  2435. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2436. if (pring->rspidx == portRspPut)
  2437. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2438. }
  2439. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2440. pring->stats.iocb_rsp_full++;
  2441. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2442. writel(status, phba->CAregaddr);
  2443. readl(phba->CAregaddr);
  2444. }
  2445. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2446. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2447. pring->stats.iocb_cmd_empty++;
  2448. /* Force update of the local copy of cmdGetInx */
  2449. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2450. lpfc_sli_resume_iocb(phba, pring);
  2451. if ((pring->lpfc_sli_cmd_available))
  2452. (pring->lpfc_sli_cmd_available) (phba, pring);
  2453. }
  2454. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2455. return rc;
  2456. }
  2457. /**
  2458. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2459. * @phba: Pointer to HBA context object.
  2460. * @pring: Pointer to driver SLI ring object.
  2461. * @rspiocbp: Pointer to driver response IOCB object.
  2462. *
  2463. * This function is called from the worker thread when there is a slow-path
  2464. * response IOCB to process. This function chains all the response iocbs until
  2465. * seeing the iocb with the LE bit set. The function will call
  2466. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2467. * completion of a command iocb. The function will call the
  2468. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2469. * The function frees the resources or calls the completion handler if this
  2470. * iocb is an abort completion. The function returns NULL when the response
  2471. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2472. * this function shall chain the iocb on to the iocb_continueq and return the
  2473. * response iocb passed in.
  2474. **/
  2475. static struct lpfc_iocbq *
  2476. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2477. struct lpfc_iocbq *rspiocbp)
  2478. {
  2479. struct lpfc_iocbq *saveq;
  2480. struct lpfc_iocbq *cmdiocbp;
  2481. struct lpfc_iocbq *next_iocb;
  2482. IOCB_t *irsp = NULL;
  2483. uint32_t free_saveq;
  2484. uint8_t iocb_cmd_type;
  2485. lpfc_iocb_type type;
  2486. unsigned long iflag;
  2487. int rc;
  2488. spin_lock_irqsave(&phba->hbalock, iflag);
  2489. /* First add the response iocb to the countinueq list */
  2490. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2491. pring->iocb_continueq_cnt++;
  2492. /* Now, determine whetehr the list is completed for processing */
  2493. irsp = &rspiocbp->iocb;
  2494. if (irsp->ulpLe) {
  2495. /*
  2496. * By default, the driver expects to free all resources
  2497. * associated with this iocb completion.
  2498. */
  2499. free_saveq = 1;
  2500. saveq = list_get_first(&pring->iocb_continueq,
  2501. struct lpfc_iocbq, list);
  2502. irsp = &(saveq->iocb);
  2503. list_del_init(&pring->iocb_continueq);
  2504. pring->iocb_continueq_cnt = 0;
  2505. pring->stats.iocb_rsp++;
  2506. /*
  2507. * If resource errors reported from HBA, reduce
  2508. * queuedepths of the SCSI device.
  2509. */
  2510. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2511. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2512. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2513. phba->lpfc_rampdown_queue_depth(phba);
  2514. spin_lock_irqsave(&phba->hbalock, iflag);
  2515. }
  2516. if (irsp->ulpStatus) {
  2517. /* Rsp ring <ringno> error: IOCB */
  2518. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2519. "0328 Rsp Ring %d error: "
  2520. "IOCB Data: "
  2521. "x%x x%x x%x x%x "
  2522. "x%x x%x x%x x%x "
  2523. "x%x x%x x%x x%x "
  2524. "x%x x%x x%x x%x\n",
  2525. pring->ringno,
  2526. irsp->un.ulpWord[0],
  2527. irsp->un.ulpWord[1],
  2528. irsp->un.ulpWord[2],
  2529. irsp->un.ulpWord[3],
  2530. irsp->un.ulpWord[4],
  2531. irsp->un.ulpWord[5],
  2532. *(((uint32_t *) irsp) + 6),
  2533. *(((uint32_t *) irsp) + 7),
  2534. *(((uint32_t *) irsp) + 8),
  2535. *(((uint32_t *) irsp) + 9),
  2536. *(((uint32_t *) irsp) + 10),
  2537. *(((uint32_t *) irsp) + 11),
  2538. *(((uint32_t *) irsp) + 12),
  2539. *(((uint32_t *) irsp) + 13),
  2540. *(((uint32_t *) irsp) + 14),
  2541. *(((uint32_t *) irsp) + 15));
  2542. }
  2543. /*
  2544. * Fetch the IOCB command type and call the correct completion
  2545. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2546. * get freed back to the lpfc_iocb_list by the discovery
  2547. * kernel thread.
  2548. */
  2549. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2550. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2551. switch (type) {
  2552. case LPFC_SOL_IOCB:
  2553. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2554. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2555. spin_lock_irqsave(&phba->hbalock, iflag);
  2556. break;
  2557. case LPFC_UNSOL_IOCB:
  2558. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2559. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2560. spin_lock_irqsave(&phba->hbalock, iflag);
  2561. if (!rc)
  2562. free_saveq = 0;
  2563. break;
  2564. case LPFC_ABORT_IOCB:
  2565. cmdiocbp = NULL;
  2566. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2567. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2568. saveq);
  2569. if (cmdiocbp) {
  2570. /* Call the specified completion routine */
  2571. if (cmdiocbp->iocb_cmpl) {
  2572. spin_unlock_irqrestore(&phba->hbalock,
  2573. iflag);
  2574. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2575. saveq);
  2576. spin_lock_irqsave(&phba->hbalock,
  2577. iflag);
  2578. } else
  2579. __lpfc_sli_release_iocbq(phba,
  2580. cmdiocbp);
  2581. }
  2582. break;
  2583. case LPFC_UNKNOWN_IOCB:
  2584. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2585. char adaptermsg[LPFC_MAX_ADPTMSG];
  2586. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2587. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2588. MAX_MSG_DATA);
  2589. dev_warn(&((phba->pcidev)->dev),
  2590. "lpfc%d: %s\n",
  2591. phba->brd_no, adaptermsg);
  2592. } else {
  2593. /* Unknown IOCB command */
  2594. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2595. "0335 Unknown IOCB "
  2596. "command Data: x%x "
  2597. "x%x x%x x%x\n",
  2598. irsp->ulpCommand,
  2599. irsp->ulpStatus,
  2600. irsp->ulpIoTag,
  2601. irsp->ulpContext);
  2602. }
  2603. break;
  2604. }
  2605. if (free_saveq) {
  2606. list_for_each_entry_safe(rspiocbp, next_iocb,
  2607. &saveq->list, list) {
  2608. list_del(&rspiocbp->list);
  2609. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2610. }
  2611. __lpfc_sli_release_iocbq(phba, saveq);
  2612. }
  2613. rspiocbp = NULL;
  2614. }
  2615. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2616. return rspiocbp;
  2617. }
  2618. /**
  2619. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2620. * @phba: Pointer to HBA context object.
  2621. * @pring: Pointer to driver SLI ring object.
  2622. * @mask: Host attention register mask for this ring.
  2623. *
  2624. * This routine wraps the actual slow_ring event process routine from the
  2625. * API jump table function pointer from the lpfc_hba struct.
  2626. **/
  2627. void
  2628. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2629. struct lpfc_sli_ring *pring, uint32_t mask)
  2630. {
  2631. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2632. }
  2633. /**
  2634. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2635. * @phba: Pointer to HBA context object.
  2636. * @pring: Pointer to driver SLI ring object.
  2637. * @mask: Host attention register mask for this ring.
  2638. *
  2639. * This function is called from the worker thread when there is a ring event
  2640. * for non-fcp rings. The caller does not hold any lock. The function will
  2641. * remove each response iocb in the response ring and calls the handle
  2642. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2643. **/
  2644. static void
  2645. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2646. struct lpfc_sli_ring *pring, uint32_t mask)
  2647. {
  2648. struct lpfc_pgp *pgp;
  2649. IOCB_t *entry;
  2650. IOCB_t *irsp = NULL;
  2651. struct lpfc_iocbq *rspiocbp = NULL;
  2652. uint32_t portRspPut, portRspMax;
  2653. unsigned long iflag;
  2654. uint32_t status;
  2655. pgp = &phba->port_gp[pring->ringno];
  2656. spin_lock_irqsave(&phba->hbalock, iflag);
  2657. pring->stats.iocb_event++;
  2658. /*
  2659. * The next available response entry should never exceed the maximum
  2660. * entries. If it does, treat it as an adapter hardware error.
  2661. */
  2662. portRspMax = pring->numRiocb;
  2663. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2664. if (portRspPut >= portRspMax) {
  2665. /*
  2666. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2667. * rsp ring <portRspMax>
  2668. */
  2669. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2670. "0303 Ring %d handler: portRspPut %d "
  2671. "is bigger than rsp ring %d\n",
  2672. pring->ringno, portRspPut, portRspMax);
  2673. phba->link_state = LPFC_HBA_ERROR;
  2674. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2675. phba->work_hs = HS_FFER3;
  2676. lpfc_handle_eratt(phba);
  2677. return;
  2678. }
  2679. rmb();
  2680. while (pring->rspidx != portRspPut) {
  2681. /*
  2682. * Build a completion list and call the appropriate handler.
  2683. * The process is to get the next available response iocb, get
  2684. * a free iocb from the list, copy the response data into the
  2685. * free iocb, insert to the continuation list, and update the
  2686. * next response index to slim. This process makes response
  2687. * iocb's in the ring available to DMA as fast as possible but
  2688. * pays a penalty for a copy operation. Since the iocb is
  2689. * only 32 bytes, this penalty is considered small relative to
  2690. * the PCI reads for register values and a slim write. When
  2691. * the ulpLe field is set, the entire Command has been
  2692. * received.
  2693. */
  2694. entry = lpfc_resp_iocb(phba, pring);
  2695. phba->last_completion_time = jiffies;
  2696. rspiocbp = __lpfc_sli_get_iocbq(phba);
  2697. if (rspiocbp == NULL) {
  2698. printk(KERN_ERR "%s: out of buffers! Failing "
  2699. "completion.\n", __func__);
  2700. break;
  2701. }
  2702. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  2703. phba->iocb_rsp_size);
  2704. irsp = &rspiocbp->iocb;
  2705. if (++pring->rspidx >= portRspMax)
  2706. pring->rspidx = 0;
  2707. if (pring->ringno == LPFC_ELS_RING) {
  2708. lpfc_debugfs_slow_ring_trc(phba,
  2709. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  2710. *(((uint32_t *) irsp) + 4),
  2711. *(((uint32_t *) irsp) + 6),
  2712. *(((uint32_t *) irsp) + 7));
  2713. }
  2714. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2715. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2716. /* Handle the response IOCB */
  2717. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  2718. spin_lock_irqsave(&phba->hbalock, iflag);
  2719. /*
  2720. * If the port response put pointer has not been updated, sync
  2721. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  2722. * response put pointer.
  2723. */
  2724. if (pring->rspidx == portRspPut) {
  2725. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2726. }
  2727. } /* while (pring->rspidx != portRspPut) */
  2728. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  2729. /* At least one response entry has been freed */
  2730. pring->stats.iocb_rsp_full++;
  2731. /* SET RxRE_RSP in Chip Att register */
  2732. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2733. writel(status, phba->CAregaddr);
  2734. readl(phba->CAregaddr); /* flush */
  2735. }
  2736. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2737. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2738. pring->stats.iocb_cmd_empty++;
  2739. /* Force update of the local copy of cmdGetInx */
  2740. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2741. lpfc_sli_resume_iocb(phba, pring);
  2742. if ((pring->lpfc_sli_cmd_available))
  2743. (pring->lpfc_sli_cmd_available) (phba, pring);
  2744. }
  2745. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2746. return;
  2747. }
  2748. /**
  2749. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  2750. * @phba: Pointer to HBA context object.
  2751. * @pring: Pointer to driver SLI ring object.
  2752. * @mask: Host attention register mask for this ring.
  2753. *
  2754. * This function is called from the worker thread when there is a pending
  2755. * ELS response iocb on the driver internal slow-path response iocb worker
  2756. * queue. The caller does not hold any lock. The function will remove each
  2757. * response iocb from the response worker queue and calls the handle
  2758. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2759. **/
  2760. static void
  2761. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  2762. struct lpfc_sli_ring *pring, uint32_t mask)
  2763. {
  2764. struct lpfc_iocbq *irspiocbq;
  2765. unsigned long iflag;
  2766. while (!list_empty(&phba->sli4_hba.sp_rspiocb_work_queue)) {
  2767. /* Get the response iocb from the head of work queue */
  2768. spin_lock_irqsave(&phba->hbalock, iflag);
  2769. list_remove_head(&phba->sli4_hba.sp_rspiocb_work_queue,
  2770. irspiocbq, struct lpfc_iocbq, list);
  2771. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2772. /* Process the response iocb */
  2773. lpfc_sli_sp_handle_rspiocb(phba, pring, irspiocbq);
  2774. }
  2775. }
  2776. /**
  2777. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  2778. * @phba: Pointer to HBA context object.
  2779. * @pring: Pointer to driver SLI ring object.
  2780. *
  2781. * This function aborts all iocbs in the given ring and frees all the iocb
  2782. * objects in txq. This function issues an abort iocb for all the iocb commands
  2783. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  2784. * the return of this function. The caller is not required to hold any locks.
  2785. **/
  2786. void
  2787. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2788. {
  2789. LIST_HEAD(completions);
  2790. struct lpfc_iocbq *iocb, *next_iocb;
  2791. if (pring->ringno == LPFC_ELS_RING) {
  2792. lpfc_fabric_abort_hba(phba);
  2793. }
  2794. /* Error everything on txq and txcmplq
  2795. * First do the txq.
  2796. */
  2797. spin_lock_irq(&phba->hbalock);
  2798. list_splice_init(&pring->txq, &completions);
  2799. pring->txq_cnt = 0;
  2800. /* Next issue ABTS for everything on the txcmplq */
  2801. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  2802. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  2803. spin_unlock_irq(&phba->hbalock);
  2804. /* Cancel all the IOCBs from the completions list */
  2805. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  2806. IOERR_SLI_ABORTED);
  2807. }
  2808. /**
  2809. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  2810. * @phba: Pointer to HBA context object.
  2811. *
  2812. * This function flushes all iocbs in the fcp ring and frees all the iocb
  2813. * objects in txq and txcmplq. This function will not issue abort iocbs
  2814. * for all the iocb commands in txcmplq, they will just be returned with
  2815. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  2816. * slot has been permanently disabled.
  2817. **/
  2818. void
  2819. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  2820. {
  2821. LIST_HEAD(txq);
  2822. LIST_HEAD(txcmplq);
  2823. struct lpfc_sli *psli = &phba->sli;
  2824. struct lpfc_sli_ring *pring;
  2825. /* Currently, only one fcp ring */
  2826. pring = &psli->ring[psli->fcp_ring];
  2827. spin_lock_irq(&phba->hbalock);
  2828. /* Retrieve everything on txq */
  2829. list_splice_init(&pring->txq, &txq);
  2830. pring->txq_cnt = 0;
  2831. /* Retrieve everything on the txcmplq */
  2832. list_splice_init(&pring->txcmplq, &txcmplq);
  2833. pring->txcmplq_cnt = 0;
  2834. spin_unlock_irq(&phba->hbalock);
  2835. /* Flush the txq */
  2836. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  2837. IOERR_SLI_DOWN);
  2838. /* Flush the txcmpq */
  2839. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  2840. IOERR_SLI_DOWN);
  2841. }
  2842. /**
  2843. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  2844. * @phba: Pointer to HBA context object.
  2845. * @mask: Bit mask to be checked.
  2846. *
  2847. * This function reads the host status register and compares
  2848. * with the provided bit mask to check if HBA completed
  2849. * the restart. This function will wait in a loop for the
  2850. * HBA to complete restart. If the HBA does not restart within
  2851. * 15 iterations, the function will reset the HBA again. The
  2852. * function returns 1 when HBA fail to restart otherwise returns
  2853. * zero.
  2854. **/
  2855. static int
  2856. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  2857. {
  2858. uint32_t status;
  2859. int i = 0;
  2860. int retval = 0;
  2861. /* Read the HBA Host Status Register */
  2862. status = readl(phba->HSregaddr);
  2863. /*
  2864. * Check status register every 100ms for 5 retries, then every
  2865. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  2866. * every 2.5 sec for 4.
  2867. * Break our of the loop if errors occurred during init.
  2868. */
  2869. while (((status & mask) != mask) &&
  2870. !(status & HS_FFERM) &&
  2871. i++ < 20) {
  2872. if (i <= 5)
  2873. msleep(10);
  2874. else if (i <= 10)
  2875. msleep(500);
  2876. else
  2877. msleep(2500);
  2878. if (i == 15) {
  2879. /* Do post */
  2880. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2881. lpfc_sli_brdrestart(phba);
  2882. }
  2883. /* Read the HBA Host Status Register */
  2884. status = readl(phba->HSregaddr);
  2885. }
  2886. /* Check to see if any errors occurred during init */
  2887. if ((status & HS_FFERM) || (i >= 20)) {
  2888. phba->link_state = LPFC_HBA_ERROR;
  2889. retval = 1;
  2890. }
  2891. return retval;
  2892. }
  2893. /**
  2894. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  2895. * @phba: Pointer to HBA context object.
  2896. * @mask: Bit mask to be checked.
  2897. *
  2898. * This function checks the host status register to check if HBA is
  2899. * ready. This function will wait in a loop for the HBA to be ready
  2900. * If the HBA is not ready , the function will will reset the HBA PCI
  2901. * function again. The function returns 1 when HBA fail to be ready
  2902. * otherwise returns zero.
  2903. **/
  2904. static int
  2905. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  2906. {
  2907. uint32_t status;
  2908. int retval = 0;
  2909. /* Read the HBA Host Status Register */
  2910. status = lpfc_sli4_post_status_check(phba);
  2911. if (status) {
  2912. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2913. lpfc_sli_brdrestart(phba);
  2914. status = lpfc_sli4_post_status_check(phba);
  2915. }
  2916. /* Check to see if any errors occurred during init */
  2917. if (status) {
  2918. phba->link_state = LPFC_HBA_ERROR;
  2919. retval = 1;
  2920. } else
  2921. phba->sli4_hba.intr_enable = 0;
  2922. return retval;
  2923. }
  2924. /**
  2925. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  2926. * @phba: Pointer to HBA context object.
  2927. * @mask: Bit mask to be checked.
  2928. *
  2929. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  2930. * from the API jump table function pointer from the lpfc_hba struct.
  2931. **/
  2932. int
  2933. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  2934. {
  2935. return phba->lpfc_sli_brdready(phba, mask);
  2936. }
  2937. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  2938. /**
  2939. * lpfc_reset_barrier - Make HBA ready for HBA reset
  2940. * @phba: Pointer to HBA context object.
  2941. *
  2942. * This function is called before resetting an HBA. This
  2943. * function requests HBA to quiesce DMAs before a reset.
  2944. **/
  2945. void lpfc_reset_barrier(struct lpfc_hba *phba)
  2946. {
  2947. uint32_t __iomem *resp_buf;
  2948. uint32_t __iomem *mbox_buf;
  2949. volatile uint32_t mbox;
  2950. uint32_t hc_copy;
  2951. int i;
  2952. uint8_t hdrtype;
  2953. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  2954. if (hdrtype != 0x80 ||
  2955. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  2956. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  2957. return;
  2958. /*
  2959. * Tell the other part of the chip to suspend temporarily all
  2960. * its DMA activity.
  2961. */
  2962. resp_buf = phba->MBslimaddr;
  2963. /* Disable the error attention */
  2964. hc_copy = readl(phba->HCregaddr);
  2965. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  2966. readl(phba->HCregaddr); /* flush */
  2967. phba->link_flag |= LS_IGNORE_ERATT;
  2968. if (readl(phba->HAregaddr) & HA_ERATT) {
  2969. /* Clear Chip error bit */
  2970. writel(HA_ERATT, phba->HAregaddr);
  2971. phba->pport->stopped = 1;
  2972. }
  2973. mbox = 0;
  2974. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  2975. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  2976. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  2977. mbox_buf = phba->MBslimaddr;
  2978. writel(mbox, mbox_buf);
  2979. for (i = 0;
  2980. readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
  2981. mdelay(1);
  2982. if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
  2983. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  2984. phba->pport->stopped)
  2985. goto restore_hc;
  2986. else
  2987. goto clear_errat;
  2988. }
  2989. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  2990. for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
  2991. mdelay(1);
  2992. clear_errat:
  2993. while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
  2994. mdelay(1);
  2995. if (readl(phba->HAregaddr) & HA_ERATT) {
  2996. writel(HA_ERATT, phba->HAregaddr);
  2997. phba->pport->stopped = 1;
  2998. }
  2999. restore_hc:
  3000. phba->link_flag &= ~LS_IGNORE_ERATT;
  3001. writel(hc_copy, phba->HCregaddr);
  3002. readl(phba->HCregaddr); /* flush */
  3003. }
  3004. /**
  3005. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3006. * @phba: Pointer to HBA context object.
  3007. *
  3008. * This function issues a kill_board mailbox command and waits for
  3009. * the error attention interrupt. This function is called for stopping
  3010. * the firmware processing. The caller is not required to hold any
  3011. * locks. This function calls lpfc_hba_down_post function to free
  3012. * any pending commands after the kill. The function will return 1 when it
  3013. * fails to kill the board else will return 0.
  3014. **/
  3015. int
  3016. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3017. {
  3018. struct lpfc_sli *psli;
  3019. LPFC_MBOXQ_t *pmb;
  3020. uint32_t status;
  3021. uint32_t ha_copy;
  3022. int retval;
  3023. int i = 0;
  3024. psli = &phba->sli;
  3025. /* Kill HBA */
  3026. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3027. "0329 Kill HBA Data: x%x x%x\n",
  3028. phba->pport->port_state, psli->sli_flag);
  3029. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3030. if (!pmb)
  3031. return 1;
  3032. /* Disable the error attention */
  3033. spin_lock_irq(&phba->hbalock);
  3034. status = readl(phba->HCregaddr);
  3035. status &= ~HC_ERINT_ENA;
  3036. writel(status, phba->HCregaddr);
  3037. readl(phba->HCregaddr); /* flush */
  3038. phba->link_flag |= LS_IGNORE_ERATT;
  3039. spin_unlock_irq(&phba->hbalock);
  3040. lpfc_kill_board(phba, pmb);
  3041. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3042. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3043. if (retval != MBX_SUCCESS) {
  3044. if (retval != MBX_BUSY)
  3045. mempool_free(pmb, phba->mbox_mem_pool);
  3046. spin_lock_irq(&phba->hbalock);
  3047. phba->link_flag &= ~LS_IGNORE_ERATT;
  3048. spin_unlock_irq(&phba->hbalock);
  3049. return 1;
  3050. }
  3051. spin_lock_irq(&phba->hbalock);
  3052. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3053. spin_unlock_irq(&phba->hbalock);
  3054. mempool_free(pmb, phba->mbox_mem_pool);
  3055. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3056. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3057. * 3 seconds we still set HBA_ERROR state because the status of the
  3058. * board is now undefined.
  3059. */
  3060. ha_copy = readl(phba->HAregaddr);
  3061. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3062. mdelay(100);
  3063. ha_copy = readl(phba->HAregaddr);
  3064. }
  3065. del_timer_sync(&psli->mbox_tmo);
  3066. if (ha_copy & HA_ERATT) {
  3067. writel(HA_ERATT, phba->HAregaddr);
  3068. phba->pport->stopped = 1;
  3069. }
  3070. spin_lock_irq(&phba->hbalock);
  3071. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3072. psli->mbox_active = NULL;
  3073. phba->link_flag &= ~LS_IGNORE_ERATT;
  3074. spin_unlock_irq(&phba->hbalock);
  3075. lpfc_hba_down_post(phba);
  3076. phba->link_state = LPFC_HBA_ERROR;
  3077. return ha_copy & HA_ERATT ? 0 : 1;
  3078. }
  3079. /**
  3080. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3081. * @phba: Pointer to HBA context object.
  3082. *
  3083. * This function resets the HBA by writing HC_INITFF to the control
  3084. * register. After the HBA resets, this function resets all the iocb ring
  3085. * indices. This function disables PCI layer parity checking during
  3086. * the reset.
  3087. * This function returns 0 always.
  3088. * The caller is not required to hold any locks.
  3089. **/
  3090. int
  3091. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3092. {
  3093. struct lpfc_sli *psli;
  3094. struct lpfc_sli_ring *pring;
  3095. uint16_t cfg_value;
  3096. int i;
  3097. psli = &phba->sli;
  3098. /* Reset HBA */
  3099. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3100. "0325 Reset HBA Data: x%x x%x\n",
  3101. phba->pport->port_state, psli->sli_flag);
  3102. /* perform board reset */
  3103. phba->fc_eventTag = 0;
  3104. phba->pport->fc_myDID = 0;
  3105. phba->pport->fc_prevDID = 0;
  3106. /* Turn off parity checking and serr during the physical reset */
  3107. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3108. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3109. (cfg_value &
  3110. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3111. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3112. /* Now toggle INITFF bit in the Host Control Register */
  3113. writel(HC_INITFF, phba->HCregaddr);
  3114. mdelay(1);
  3115. readl(phba->HCregaddr); /* flush */
  3116. writel(0, phba->HCregaddr);
  3117. readl(phba->HCregaddr); /* flush */
  3118. /* Restore PCI cmd register */
  3119. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3120. /* Initialize relevant SLI info */
  3121. for (i = 0; i < psli->num_rings; i++) {
  3122. pring = &psli->ring[i];
  3123. pring->flag = 0;
  3124. pring->rspidx = 0;
  3125. pring->next_cmdidx = 0;
  3126. pring->local_getidx = 0;
  3127. pring->cmdidx = 0;
  3128. pring->missbufcnt = 0;
  3129. }
  3130. phba->link_state = LPFC_WARM_START;
  3131. return 0;
  3132. }
  3133. /**
  3134. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3135. * @phba: Pointer to HBA context object.
  3136. *
  3137. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3138. * checking during resets the device. The caller is not required to hold
  3139. * any locks.
  3140. *
  3141. * This function returns 0 always.
  3142. **/
  3143. int
  3144. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3145. {
  3146. struct lpfc_sli *psli = &phba->sli;
  3147. uint16_t cfg_value;
  3148. uint8_t qindx;
  3149. /* Reset HBA */
  3150. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3151. "0295 Reset HBA Data: x%x x%x\n",
  3152. phba->pport->port_state, psli->sli_flag);
  3153. /* perform board reset */
  3154. phba->fc_eventTag = 0;
  3155. phba->pport->fc_myDID = 0;
  3156. phba->pport->fc_prevDID = 0;
  3157. /* Turn off parity checking and serr during the physical reset */
  3158. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3159. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3160. (cfg_value &
  3161. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3162. spin_lock_irq(&phba->hbalock);
  3163. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3164. phba->fcf.fcf_flag = 0;
  3165. /* Clean up the child queue list for the CQs */
  3166. list_del_init(&phba->sli4_hba.mbx_wq->list);
  3167. list_del_init(&phba->sli4_hba.els_wq->list);
  3168. list_del_init(&phba->sli4_hba.hdr_rq->list);
  3169. list_del_init(&phba->sli4_hba.dat_rq->list);
  3170. list_del_init(&phba->sli4_hba.mbx_cq->list);
  3171. list_del_init(&phba->sli4_hba.els_cq->list);
  3172. list_del_init(&phba->sli4_hba.rxq_cq->list);
  3173. for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
  3174. list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
  3175. for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
  3176. list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
  3177. spin_unlock_irq(&phba->hbalock);
  3178. /* Now physically reset the device */
  3179. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3180. "0389 Performing PCI function reset!\n");
  3181. /* Perform FCoE PCI function reset */
  3182. lpfc_pci_function_reset(phba);
  3183. return 0;
  3184. }
  3185. /**
  3186. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3187. * @phba: Pointer to HBA context object.
  3188. *
  3189. * This function is called in the SLI initialization code path to
  3190. * restart the HBA. The caller is not required to hold any lock.
  3191. * This function writes MBX_RESTART mailbox command to the SLIM and
  3192. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3193. * function to free any pending commands. The function enables
  3194. * POST only during the first initialization. The function returns zero.
  3195. * The function does not guarantee completion of MBX_RESTART mailbox
  3196. * command before the return of this function.
  3197. **/
  3198. static int
  3199. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3200. {
  3201. MAILBOX_t *mb;
  3202. struct lpfc_sli *psli;
  3203. volatile uint32_t word0;
  3204. void __iomem *to_slim;
  3205. spin_lock_irq(&phba->hbalock);
  3206. psli = &phba->sli;
  3207. /* Restart HBA */
  3208. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3209. "0337 Restart HBA Data: x%x x%x\n",
  3210. phba->pport->port_state, psli->sli_flag);
  3211. word0 = 0;
  3212. mb = (MAILBOX_t *) &word0;
  3213. mb->mbxCommand = MBX_RESTART;
  3214. mb->mbxHc = 1;
  3215. lpfc_reset_barrier(phba);
  3216. to_slim = phba->MBslimaddr;
  3217. writel(*(uint32_t *) mb, to_slim);
  3218. readl(to_slim); /* flush */
  3219. /* Only skip post after fc_ffinit is completed */
  3220. if (phba->pport->port_state)
  3221. word0 = 1; /* This is really setting up word1 */
  3222. else
  3223. word0 = 0; /* This is really setting up word1 */
  3224. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3225. writel(*(uint32_t *) mb, to_slim);
  3226. readl(to_slim); /* flush */
  3227. lpfc_sli_brdreset(phba);
  3228. phba->pport->stopped = 0;
  3229. phba->link_state = LPFC_INIT_START;
  3230. phba->hba_flag = 0;
  3231. spin_unlock_irq(&phba->hbalock);
  3232. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3233. psli->stats_start = get_seconds();
  3234. /* Give the INITFF and Post time to settle. */
  3235. mdelay(100);
  3236. lpfc_hba_down_post(phba);
  3237. return 0;
  3238. }
  3239. /**
  3240. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3241. * @phba: Pointer to HBA context object.
  3242. *
  3243. * This function is called in the SLI initialization code path to restart
  3244. * a SLI4 HBA. The caller is not required to hold any lock.
  3245. * At the end of the function, it calls lpfc_hba_down_post function to
  3246. * free any pending commands.
  3247. **/
  3248. static int
  3249. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3250. {
  3251. struct lpfc_sli *psli = &phba->sli;
  3252. /* Restart HBA */
  3253. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3254. "0296 Restart HBA Data: x%x x%x\n",
  3255. phba->pport->port_state, psli->sli_flag);
  3256. lpfc_sli4_brdreset(phba);
  3257. spin_lock_irq(&phba->hbalock);
  3258. phba->pport->stopped = 0;
  3259. phba->link_state = LPFC_INIT_START;
  3260. phba->hba_flag = 0;
  3261. spin_unlock_irq(&phba->hbalock);
  3262. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3263. psli->stats_start = get_seconds();
  3264. lpfc_hba_down_post(phba);
  3265. return 0;
  3266. }
  3267. /**
  3268. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3269. * @phba: Pointer to HBA context object.
  3270. *
  3271. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3272. * API jump table function pointer from the lpfc_hba struct.
  3273. **/
  3274. int
  3275. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3276. {
  3277. return phba->lpfc_sli_brdrestart(phba);
  3278. }
  3279. /**
  3280. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3281. * @phba: Pointer to HBA context object.
  3282. *
  3283. * This function is called after a HBA restart to wait for successful
  3284. * restart of the HBA. Successful restart of the HBA is indicated by
  3285. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3286. * iteration, the function will restart the HBA again. The function returns
  3287. * zero if HBA successfully restarted else returns negative error code.
  3288. **/
  3289. static int
  3290. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3291. {
  3292. uint32_t status, i = 0;
  3293. /* Read the HBA Host Status Register */
  3294. status = readl(phba->HSregaddr);
  3295. /* Check status register to see what current state is */
  3296. i = 0;
  3297. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3298. /* Check every 100ms for 5 retries, then every 500ms for 5, then
  3299. * every 2.5 sec for 5, then reset board and every 2.5 sec for
  3300. * 4.
  3301. */
  3302. if (i++ >= 20) {
  3303. /* Adapter failed to init, timeout, status reg
  3304. <status> */
  3305. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3306. "0436 Adapter failed to init, "
  3307. "timeout, status reg x%x, "
  3308. "FW Data: A8 x%x AC x%x\n", status,
  3309. readl(phba->MBslimaddr + 0xa8),
  3310. readl(phba->MBslimaddr + 0xac));
  3311. phba->link_state = LPFC_HBA_ERROR;
  3312. return -ETIMEDOUT;
  3313. }
  3314. /* Check to see if any errors occurred during init */
  3315. if (status & HS_FFERM) {
  3316. /* ERROR: During chipset initialization */
  3317. /* Adapter failed to init, chipset, status reg
  3318. <status> */
  3319. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3320. "0437 Adapter failed to init, "
  3321. "chipset, status reg x%x, "
  3322. "FW Data: A8 x%x AC x%x\n", status,
  3323. readl(phba->MBslimaddr + 0xa8),
  3324. readl(phba->MBslimaddr + 0xac));
  3325. phba->link_state = LPFC_HBA_ERROR;
  3326. return -EIO;
  3327. }
  3328. if (i <= 5) {
  3329. msleep(10);
  3330. } else if (i <= 10) {
  3331. msleep(500);
  3332. } else {
  3333. msleep(2500);
  3334. }
  3335. if (i == 15) {
  3336. /* Do post */
  3337. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3338. lpfc_sli_brdrestart(phba);
  3339. }
  3340. /* Read the HBA Host Status Register */
  3341. status = readl(phba->HSregaddr);
  3342. }
  3343. /* Check to see if any errors occurred during init */
  3344. if (status & HS_FFERM) {
  3345. /* ERROR: During chipset initialization */
  3346. /* Adapter failed to init, chipset, status reg <status> */
  3347. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3348. "0438 Adapter failed to init, chipset, "
  3349. "status reg x%x, "
  3350. "FW Data: A8 x%x AC x%x\n", status,
  3351. readl(phba->MBslimaddr + 0xa8),
  3352. readl(phba->MBslimaddr + 0xac));
  3353. phba->link_state = LPFC_HBA_ERROR;
  3354. return -EIO;
  3355. }
  3356. /* Clear all interrupt enable conditions */
  3357. writel(0, phba->HCregaddr);
  3358. readl(phba->HCregaddr); /* flush */
  3359. /* setup host attn register */
  3360. writel(0xffffffff, phba->HAregaddr);
  3361. readl(phba->HAregaddr); /* flush */
  3362. return 0;
  3363. }
  3364. /**
  3365. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3366. *
  3367. * This function calculates and returns the number of HBQs required to be
  3368. * configured.
  3369. **/
  3370. int
  3371. lpfc_sli_hbq_count(void)
  3372. {
  3373. return ARRAY_SIZE(lpfc_hbq_defs);
  3374. }
  3375. /**
  3376. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3377. *
  3378. * This function adds the number of hbq entries in every HBQ to get
  3379. * the total number of hbq entries required for the HBA and returns
  3380. * the total count.
  3381. **/
  3382. static int
  3383. lpfc_sli_hbq_entry_count(void)
  3384. {
  3385. int hbq_count = lpfc_sli_hbq_count();
  3386. int count = 0;
  3387. int i;
  3388. for (i = 0; i < hbq_count; ++i)
  3389. count += lpfc_hbq_defs[i]->entry_count;
  3390. return count;
  3391. }
  3392. /**
  3393. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3394. *
  3395. * This function calculates amount of memory required for all hbq entries
  3396. * to be configured and returns the total memory required.
  3397. **/
  3398. int
  3399. lpfc_sli_hbq_size(void)
  3400. {
  3401. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3402. }
  3403. /**
  3404. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3405. * @phba: Pointer to HBA context object.
  3406. *
  3407. * This function is called during the SLI initialization to configure
  3408. * all the HBQs and post buffers to the HBQ. The caller is not
  3409. * required to hold any locks. This function will return zero if successful
  3410. * else it will return negative error code.
  3411. **/
  3412. static int
  3413. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3414. {
  3415. int hbq_count = lpfc_sli_hbq_count();
  3416. LPFC_MBOXQ_t *pmb;
  3417. MAILBOX_t *pmbox;
  3418. uint32_t hbqno;
  3419. uint32_t hbq_entry_index;
  3420. /* Get a Mailbox buffer to setup mailbox
  3421. * commands for HBA initialization
  3422. */
  3423. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3424. if (!pmb)
  3425. return -ENOMEM;
  3426. pmbox = &pmb->u.mb;
  3427. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3428. phba->link_state = LPFC_INIT_MBX_CMDS;
  3429. phba->hbq_in_use = 1;
  3430. hbq_entry_index = 0;
  3431. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3432. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3433. phba->hbqs[hbqno].hbqPutIdx = 0;
  3434. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3435. phba->hbqs[hbqno].entry_count =
  3436. lpfc_hbq_defs[hbqno]->entry_count;
  3437. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3438. hbq_entry_index, pmb);
  3439. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3440. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3441. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3442. mbxStatus <status>, ring <num> */
  3443. lpfc_printf_log(phba, KERN_ERR,
  3444. LOG_SLI | LOG_VPORT,
  3445. "1805 Adapter failed to init. "
  3446. "Data: x%x x%x x%x\n",
  3447. pmbox->mbxCommand,
  3448. pmbox->mbxStatus, hbqno);
  3449. phba->link_state = LPFC_HBA_ERROR;
  3450. mempool_free(pmb, phba->mbox_mem_pool);
  3451. return ENXIO;
  3452. }
  3453. }
  3454. phba->hbq_count = hbq_count;
  3455. mempool_free(pmb, phba->mbox_mem_pool);
  3456. /* Initially populate or replenish the HBQs */
  3457. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3458. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3459. return 0;
  3460. }
  3461. /**
  3462. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3463. * @phba: Pointer to HBA context object.
  3464. *
  3465. * This function is called during the SLI initialization to configure
  3466. * all the HBQs and post buffers to the HBQ. The caller is not
  3467. * required to hold any locks. This function will return zero if successful
  3468. * else it will return negative error code.
  3469. **/
  3470. static int
  3471. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3472. {
  3473. phba->hbq_in_use = 1;
  3474. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3475. phba->hbq_count = 1;
  3476. /* Initially populate or replenish the HBQs */
  3477. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3478. return 0;
  3479. }
  3480. /**
  3481. * lpfc_sli_config_port - Issue config port mailbox command
  3482. * @phba: Pointer to HBA context object.
  3483. * @sli_mode: sli mode - 2/3
  3484. *
  3485. * This function is called by the sli intialization code path
  3486. * to issue config_port mailbox command. This function restarts the
  3487. * HBA firmware and issues a config_port mailbox command to configure
  3488. * the SLI interface in the sli mode specified by sli_mode
  3489. * variable. The caller is not required to hold any locks.
  3490. * The function returns 0 if successful, else returns negative error
  3491. * code.
  3492. **/
  3493. int
  3494. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3495. {
  3496. LPFC_MBOXQ_t *pmb;
  3497. uint32_t resetcount = 0, rc = 0, done = 0;
  3498. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3499. if (!pmb) {
  3500. phba->link_state = LPFC_HBA_ERROR;
  3501. return -ENOMEM;
  3502. }
  3503. phba->sli_rev = sli_mode;
  3504. while (resetcount < 2 && !done) {
  3505. spin_lock_irq(&phba->hbalock);
  3506. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3507. spin_unlock_irq(&phba->hbalock);
  3508. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3509. lpfc_sli_brdrestart(phba);
  3510. rc = lpfc_sli_chipset_init(phba);
  3511. if (rc)
  3512. break;
  3513. spin_lock_irq(&phba->hbalock);
  3514. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3515. spin_unlock_irq(&phba->hbalock);
  3516. resetcount++;
  3517. /* Call pre CONFIG_PORT mailbox command initialization. A
  3518. * value of 0 means the call was successful. Any other
  3519. * nonzero value is a failure, but if ERESTART is returned,
  3520. * the driver may reset the HBA and try again.
  3521. */
  3522. rc = lpfc_config_port_prep(phba);
  3523. if (rc == -ERESTART) {
  3524. phba->link_state = LPFC_LINK_UNKNOWN;
  3525. continue;
  3526. } else if (rc)
  3527. break;
  3528. phba->link_state = LPFC_INIT_MBX_CMDS;
  3529. lpfc_config_port(phba, pmb);
  3530. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3531. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3532. LPFC_SLI3_HBQ_ENABLED |
  3533. LPFC_SLI3_CRP_ENABLED |
  3534. LPFC_SLI3_INB_ENABLED |
  3535. LPFC_SLI3_BG_ENABLED);
  3536. if (rc != MBX_SUCCESS) {
  3537. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3538. "0442 Adapter failed to init, mbxCmd x%x "
  3539. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3540. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3541. spin_lock_irq(&phba->hbalock);
  3542. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3543. spin_unlock_irq(&phba->hbalock);
  3544. rc = -ENXIO;
  3545. } else {
  3546. /* Allow asynchronous mailbox command to go through */
  3547. spin_lock_irq(&phba->hbalock);
  3548. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3549. spin_unlock_irq(&phba->hbalock);
  3550. done = 1;
  3551. }
  3552. }
  3553. if (!done) {
  3554. rc = -EINVAL;
  3555. goto do_prep_failed;
  3556. }
  3557. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3558. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3559. rc = -ENXIO;
  3560. goto do_prep_failed;
  3561. }
  3562. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3563. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3564. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3565. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3566. phba->max_vpi : phba->max_vports;
  3567. } else
  3568. phba->max_vpi = 0;
  3569. if (pmb->u.mb.un.varCfgPort.gdss)
  3570. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3571. if (pmb->u.mb.un.varCfgPort.gerbm)
  3572. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3573. if (pmb->u.mb.un.varCfgPort.gcrp)
  3574. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3575. if (pmb->u.mb.un.varCfgPort.ginb) {
  3576. phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
  3577. phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
  3578. phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
  3579. phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
  3580. phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
  3581. phba->inb_last_counter =
  3582. phba->mbox->us.s3_inb_pgp.counter;
  3583. } else {
  3584. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3585. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3586. phba->inb_ha_copy = NULL;
  3587. phba->inb_counter = NULL;
  3588. }
  3589. if (phba->cfg_enable_bg) {
  3590. if (pmb->u.mb.un.varCfgPort.gbg)
  3591. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3592. else
  3593. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3594. "0443 Adapter did not grant "
  3595. "BlockGuard\n");
  3596. }
  3597. } else {
  3598. phba->hbq_get = NULL;
  3599. phba->port_gp = phba->mbox->us.s2.port;
  3600. phba->inb_ha_copy = NULL;
  3601. phba->inb_counter = NULL;
  3602. phba->max_vpi = 0;
  3603. }
  3604. do_prep_failed:
  3605. mempool_free(pmb, phba->mbox_mem_pool);
  3606. return rc;
  3607. }
  3608. /**
  3609. * lpfc_sli_hba_setup - SLI intialization function
  3610. * @phba: Pointer to HBA context object.
  3611. *
  3612. * This function is the main SLI intialization function. This function
  3613. * is called by the HBA intialization code, HBA reset code and HBA
  3614. * error attention handler code. Caller is not required to hold any
  3615. * locks. This function issues config_port mailbox command to configure
  3616. * the SLI, setup iocb rings and HBQ rings. In the end the function
  3617. * calls the config_port_post function to issue init_link mailbox
  3618. * command and to start the discovery. The function will return zero
  3619. * if successful, else it will return negative error code.
  3620. **/
  3621. int
  3622. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  3623. {
  3624. uint32_t rc;
  3625. int mode = 3;
  3626. switch (lpfc_sli_mode) {
  3627. case 2:
  3628. if (phba->cfg_enable_npiv) {
  3629. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  3630. "1824 NPIV enabled: Override lpfc_sli_mode "
  3631. "parameter (%d) to auto (0).\n",
  3632. lpfc_sli_mode);
  3633. break;
  3634. }
  3635. mode = 2;
  3636. break;
  3637. case 0:
  3638. case 3:
  3639. break;
  3640. default:
  3641. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  3642. "1819 Unrecognized lpfc_sli_mode "
  3643. "parameter: %d.\n", lpfc_sli_mode);
  3644. break;
  3645. }
  3646. rc = lpfc_sli_config_port(phba, mode);
  3647. if (rc && lpfc_sli_mode == 3)
  3648. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  3649. "1820 Unable to select SLI-3. "
  3650. "Not supported by adapter.\n");
  3651. if (rc && mode != 2)
  3652. rc = lpfc_sli_config_port(phba, 2);
  3653. if (rc)
  3654. goto lpfc_sli_hba_setup_error;
  3655. if (phba->sli_rev == 3) {
  3656. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  3657. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  3658. } else {
  3659. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  3660. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  3661. phba->sli3_options = 0;
  3662. }
  3663. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3664. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  3665. phba->sli_rev, phba->max_vpi);
  3666. rc = lpfc_sli_ring_map(phba);
  3667. if (rc)
  3668. goto lpfc_sli_hba_setup_error;
  3669. /* Init HBQs */
  3670. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  3671. rc = lpfc_sli_hbq_setup(phba);
  3672. if (rc)
  3673. goto lpfc_sli_hba_setup_error;
  3674. }
  3675. spin_lock_irq(&phba->hbalock);
  3676. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  3677. spin_unlock_irq(&phba->hbalock);
  3678. rc = lpfc_config_port_post(phba);
  3679. if (rc)
  3680. goto lpfc_sli_hba_setup_error;
  3681. return rc;
  3682. lpfc_sli_hba_setup_error:
  3683. phba->link_state = LPFC_HBA_ERROR;
  3684. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3685. "0445 Firmware initialization failed\n");
  3686. return rc;
  3687. }
  3688. /**
  3689. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  3690. * @phba: Pointer to HBA context object.
  3691. * @mboxq: mailbox pointer.
  3692. * This function issue a dump mailbox command to read config region
  3693. * 23 and parse the records in the region and populate driver
  3694. * data structure.
  3695. **/
  3696. static int
  3697. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
  3698. LPFC_MBOXQ_t *mboxq)
  3699. {
  3700. struct lpfc_dmabuf *mp;
  3701. struct lpfc_mqe *mqe;
  3702. uint32_t data_length;
  3703. int rc;
  3704. /* Program the default value of vlan_id and fc_map */
  3705. phba->valid_vlan = 0;
  3706. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  3707. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  3708. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  3709. mqe = &mboxq->u.mqe;
  3710. if (lpfc_dump_fcoe_param(phba, mboxq))
  3711. return -ENOMEM;
  3712. mp = (struct lpfc_dmabuf *) mboxq->context1;
  3713. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  3714. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3715. "(%d):2571 Mailbox cmd x%x Status x%x "
  3716. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  3717. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  3718. "CQ: x%x x%x x%x x%x\n",
  3719. mboxq->vport ? mboxq->vport->vpi : 0,
  3720. bf_get(lpfc_mqe_command, mqe),
  3721. bf_get(lpfc_mqe_status, mqe),
  3722. mqe->un.mb_words[0], mqe->un.mb_words[1],
  3723. mqe->un.mb_words[2], mqe->un.mb_words[3],
  3724. mqe->un.mb_words[4], mqe->un.mb_words[5],
  3725. mqe->un.mb_words[6], mqe->un.mb_words[7],
  3726. mqe->un.mb_words[8], mqe->un.mb_words[9],
  3727. mqe->un.mb_words[10], mqe->un.mb_words[11],
  3728. mqe->un.mb_words[12], mqe->un.mb_words[13],
  3729. mqe->un.mb_words[14], mqe->un.mb_words[15],
  3730. mqe->un.mb_words[16], mqe->un.mb_words[50],
  3731. mboxq->mcqe.word0,
  3732. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  3733. mboxq->mcqe.trailer);
  3734. if (rc) {
  3735. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3736. kfree(mp);
  3737. return -EIO;
  3738. }
  3739. data_length = mqe->un.mb_words[5];
  3740. if (data_length > DMP_FCOEPARAM_RGN_SIZE) {
  3741. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3742. kfree(mp);
  3743. return -EIO;
  3744. }
  3745. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  3746. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3747. kfree(mp);
  3748. return 0;
  3749. }
  3750. /**
  3751. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  3752. * @phba: pointer to lpfc hba data structure.
  3753. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  3754. * @vpd: pointer to the memory to hold resulting port vpd data.
  3755. * @vpd_size: On input, the number of bytes allocated to @vpd.
  3756. * On output, the number of data bytes in @vpd.
  3757. *
  3758. * This routine executes a READ_REV SLI4 mailbox command. In
  3759. * addition, this routine gets the port vpd data.
  3760. *
  3761. * Return codes
  3762. * 0 - sucessful
  3763. * ENOMEM - could not allocated memory.
  3764. **/
  3765. static int
  3766. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  3767. uint8_t *vpd, uint32_t *vpd_size)
  3768. {
  3769. int rc = 0;
  3770. uint32_t dma_size;
  3771. struct lpfc_dmabuf *dmabuf;
  3772. struct lpfc_mqe *mqe;
  3773. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  3774. if (!dmabuf)
  3775. return -ENOMEM;
  3776. /*
  3777. * Get a DMA buffer for the vpd data resulting from the READ_REV
  3778. * mailbox command.
  3779. */
  3780. dma_size = *vpd_size;
  3781. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  3782. dma_size,
  3783. &dmabuf->phys,
  3784. GFP_KERNEL);
  3785. if (!dmabuf->virt) {
  3786. kfree(dmabuf);
  3787. return -ENOMEM;
  3788. }
  3789. memset(dmabuf->virt, 0, dma_size);
  3790. /*
  3791. * The SLI4 implementation of READ_REV conflicts at word1,
  3792. * bits 31:16 and SLI4 adds vpd functionality not present
  3793. * in SLI3. This code corrects the conflicts.
  3794. */
  3795. lpfc_read_rev(phba, mboxq);
  3796. mqe = &mboxq->u.mqe;
  3797. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  3798. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  3799. mqe->un.read_rev.word1 &= 0x0000FFFF;
  3800. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  3801. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  3802. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  3803. if (rc) {
  3804. dma_free_coherent(&phba->pcidev->dev, dma_size,
  3805. dmabuf->virt, dmabuf->phys);
  3806. return -EIO;
  3807. }
  3808. /*
  3809. * The available vpd length cannot be bigger than the
  3810. * DMA buffer passed to the port. Catch the less than
  3811. * case and update the caller's size.
  3812. */
  3813. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  3814. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  3815. lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
  3816. dma_free_coherent(&phba->pcidev->dev, dma_size,
  3817. dmabuf->virt, dmabuf->phys);
  3818. kfree(dmabuf);
  3819. return 0;
  3820. }
  3821. /**
  3822. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  3823. * @phba: pointer to lpfc hba data structure.
  3824. *
  3825. * This routine is called to explicitly arm the SLI4 device's completion and
  3826. * event queues
  3827. **/
  3828. static void
  3829. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  3830. {
  3831. uint8_t fcp_eqidx;
  3832. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  3833. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  3834. lpfc_sli4_cq_release(phba->sli4_hba.rxq_cq, LPFC_QUEUE_REARM);
  3835. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
  3836. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  3837. LPFC_QUEUE_REARM);
  3838. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  3839. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
  3840. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  3841. LPFC_QUEUE_REARM);
  3842. }
  3843. /**
  3844. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  3845. * @phba: Pointer to HBA context object.
  3846. *
  3847. * This function is the main SLI4 device intialization PCI function. This
  3848. * function is called by the HBA intialization code, HBA reset code and
  3849. * HBA error attention handler code. Caller is not required to hold any
  3850. * locks.
  3851. **/
  3852. int
  3853. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  3854. {
  3855. int rc;
  3856. LPFC_MBOXQ_t *mboxq;
  3857. struct lpfc_mqe *mqe;
  3858. uint8_t *vpd;
  3859. uint32_t vpd_size;
  3860. uint32_t ftr_rsp = 0;
  3861. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  3862. struct lpfc_vport *vport = phba->pport;
  3863. struct lpfc_dmabuf *mp;
  3864. /* Perform a PCI function reset to start from clean */
  3865. rc = lpfc_pci_function_reset(phba);
  3866. if (unlikely(rc))
  3867. return -ENODEV;
  3868. /* Check the HBA Host Status Register for readyness */
  3869. rc = lpfc_sli4_post_status_check(phba);
  3870. if (unlikely(rc))
  3871. return -ENODEV;
  3872. else {
  3873. spin_lock_irq(&phba->hbalock);
  3874. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  3875. spin_unlock_irq(&phba->hbalock);
  3876. }
  3877. /*
  3878. * Allocate a single mailbox container for initializing the
  3879. * port.
  3880. */
  3881. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3882. if (!mboxq)
  3883. return -ENOMEM;
  3884. /*
  3885. * Continue initialization with default values even if driver failed
  3886. * to read FCoE param config regions
  3887. */
  3888. if (lpfc_sli4_read_fcoe_params(phba, mboxq))
  3889. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  3890. "2570 Failed to read FCoE parameters \n");
  3891. /* Issue READ_REV to collect vpd and FW information. */
  3892. vpd_size = PAGE_SIZE;
  3893. vpd = kzalloc(vpd_size, GFP_KERNEL);
  3894. if (!vpd) {
  3895. rc = -ENOMEM;
  3896. goto out_free_mbox;
  3897. }
  3898. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  3899. if (unlikely(rc))
  3900. goto out_free_vpd;
  3901. mqe = &mboxq->u.mqe;
  3902. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  3903. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  3904. phba->hba_flag |= HBA_FCOE_SUPPORT;
  3905. if (phba->sli_rev != LPFC_SLI_REV4 ||
  3906. !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
  3907. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  3908. "0376 READ_REV Error. SLI Level %d "
  3909. "FCoE enabled %d\n",
  3910. phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
  3911. rc = -EIO;
  3912. goto out_free_vpd;
  3913. }
  3914. /*
  3915. * Evaluate the read rev and vpd data. Populate the driver
  3916. * state with the results. If this routine fails, the failure
  3917. * is not fatal as the driver will use generic values.
  3918. */
  3919. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  3920. if (unlikely(!rc)) {
  3921. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  3922. "0377 Error %d parsing vpd. "
  3923. "Using defaults.\n", rc);
  3924. rc = 0;
  3925. }
  3926. /* Save information as VPD data */
  3927. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  3928. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  3929. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  3930. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  3931. &mqe->un.read_rev);
  3932. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  3933. &mqe->un.read_rev);
  3934. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  3935. &mqe->un.read_rev);
  3936. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  3937. &mqe->un.read_rev);
  3938. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  3939. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  3940. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  3941. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  3942. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  3943. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  3944. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3945. "(%d):0380 READ_REV Status x%x "
  3946. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  3947. mboxq->vport ? mboxq->vport->vpi : 0,
  3948. bf_get(lpfc_mqe_status, mqe),
  3949. phba->vpd.rev.opFwName,
  3950. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  3951. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  3952. /*
  3953. * Discover the port's supported feature set and match it against the
  3954. * hosts requests.
  3955. */
  3956. lpfc_request_features(phba, mboxq);
  3957. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  3958. if (unlikely(rc)) {
  3959. rc = -EIO;
  3960. goto out_free_vpd;
  3961. }
  3962. /*
  3963. * The port must support FCP initiator mode as this is the
  3964. * only mode running in the host.
  3965. */
  3966. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  3967. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  3968. "0378 No support for fcpi mode.\n");
  3969. ftr_rsp++;
  3970. }
  3971. /*
  3972. * If the port cannot support the host's requested features
  3973. * then turn off the global config parameters to disable the
  3974. * feature in the driver. This is not a fatal error.
  3975. */
  3976. if ((phba->cfg_enable_bg) &&
  3977. !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  3978. ftr_rsp++;
  3979. if (phba->max_vpi && phba->cfg_enable_npiv &&
  3980. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  3981. ftr_rsp++;
  3982. if (ftr_rsp) {
  3983. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  3984. "0379 Feature Mismatch Data: x%08x %08x "
  3985. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  3986. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  3987. phba->cfg_enable_npiv, phba->max_vpi);
  3988. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  3989. phba->cfg_enable_bg = 0;
  3990. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  3991. phba->cfg_enable_npiv = 0;
  3992. }
  3993. /* These SLI3 features are assumed in SLI4 */
  3994. spin_lock_irq(&phba->hbalock);
  3995. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  3996. spin_unlock_irq(&phba->hbalock);
  3997. /* Read the port's service parameters. */
  3998. lpfc_read_sparam(phba, mboxq, vport->vpi);
  3999. mboxq->vport = vport;
  4000. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4001. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4002. if (rc == MBX_SUCCESS) {
  4003. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  4004. rc = 0;
  4005. }
  4006. /*
  4007. * This memory was allocated by the lpfc_read_sparam routine. Release
  4008. * it to the mbuf pool.
  4009. */
  4010. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4011. kfree(mp);
  4012. mboxq->context1 = NULL;
  4013. if (unlikely(rc)) {
  4014. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4015. "0382 READ_SPARAM command failed "
  4016. "status %d, mbxStatus x%x\n",
  4017. rc, bf_get(lpfc_mqe_status, mqe));
  4018. phba->link_state = LPFC_HBA_ERROR;
  4019. rc = -EIO;
  4020. goto out_free_vpd;
  4021. }
  4022. if (phba->cfg_soft_wwnn)
  4023. u64_to_wwn(phba->cfg_soft_wwnn,
  4024. vport->fc_sparam.nodeName.u.wwn);
  4025. if (phba->cfg_soft_wwpn)
  4026. u64_to_wwn(phba->cfg_soft_wwpn,
  4027. vport->fc_sparam.portName.u.wwn);
  4028. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  4029. sizeof(struct lpfc_name));
  4030. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  4031. sizeof(struct lpfc_name));
  4032. /* Update the fc_host data structures with new wwn. */
  4033. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  4034. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  4035. /* Register SGL pool to the device using non-embedded mailbox command */
  4036. rc = lpfc_sli4_post_sgl_list(phba);
  4037. if (unlikely(rc)) {
  4038. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4039. "0582 Error %d during sgl post operation", rc);
  4040. rc = -ENODEV;
  4041. goto out_free_vpd;
  4042. }
  4043. /* Register SCSI SGL pool to the device */
  4044. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  4045. if (unlikely(rc)) {
  4046. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  4047. "0383 Error %d during scsi sgl post opeation",
  4048. rc);
  4049. /* Some Scsi buffers were moved to the abort scsi list */
  4050. /* A pci function reset will repost them */
  4051. rc = -ENODEV;
  4052. goto out_free_vpd;
  4053. }
  4054. /* Post the rpi header region to the device. */
  4055. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  4056. if (unlikely(rc)) {
  4057. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4058. "0393 Error %d during rpi post operation\n",
  4059. rc);
  4060. rc = -ENODEV;
  4061. goto out_free_vpd;
  4062. }
  4063. if (phba->cfg_enable_fip)
  4064. bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 1);
  4065. else
  4066. bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
  4067. /* Set up all the queues to the device */
  4068. rc = lpfc_sli4_queue_setup(phba);
  4069. if (unlikely(rc)) {
  4070. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4071. "0381 Error %d during queue setup.\n ", rc);
  4072. goto out_stop_timers;
  4073. }
  4074. /* Arm the CQs and then EQs on device */
  4075. lpfc_sli4_arm_cqeq_intr(phba);
  4076. /* Indicate device interrupt mode */
  4077. phba->sli4_hba.intr_enable = 1;
  4078. /* Allow asynchronous mailbox command to go through */
  4079. spin_lock_irq(&phba->hbalock);
  4080. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4081. spin_unlock_irq(&phba->hbalock);
  4082. /* Post receive buffers to the device */
  4083. lpfc_sli4_rb_setup(phba);
  4084. /* Start the ELS watchdog timer */
  4085. mod_timer(&vport->els_tmofunc,
  4086. jiffies + HZ * (phba->fc_ratov * 2));
  4087. /* Start heart beat timer */
  4088. mod_timer(&phba->hb_tmofunc,
  4089. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  4090. phba->hb_outstanding = 0;
  4091. phba->last_completion_time = jiffies;
  4092. /* Start error attention (ERATT) polling timer */
  4093. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  4094. /*
  4095. * The port is ready, set the host's link state to LINK_DOWN
  4096. * in preparation for link interrupts.
  4097. */
  4098. lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
  4099. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4100. lpfc_set_loopback_flag(phba);
  4101. /* Change driver state to LPFC_LINK_DOWN right before init link */
  4102. spin_lock_irq(&phba->hbalock);
  4103. phba->link_state = LPFC_LINK_DOWN;
  4104. spin_unlock_irq(&phba->hbalock);
  4105. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  4106. if (unlikely(rc != MBX_NOT_FINISHED)) {
  4107. kfree(vpd);
  4108. return 0;
  4109. } else
  4110. rc = -EIO;
  4111. /* Unset all the queues set up in this routine when error out */
  4112. if (rc)
  4113. lpfc_sli4_queue_unset(phba);
  4114. out_stop_timers:
  4115. if (rc)
  4116. lpfc_stop_hba_timers(phba);
  4117. out_free_vpd:
  4118. kfree(vpd);
  4119. out_free_mbox:
  4120. mempool_free(mboxq, phba->mbox_mem_pool);
  4121. return rc;
  4122. }
  4123. /**
  4124. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  4125. * @ptr: context object - pointer to hba structure.
  4126. *
  4127. * This is the callback function for mailbox timer. The mailbox
  4128. * timer is armed when a new mailbox command is issued and the timer
  4129. * is deleted when the mailbox complete. The function is called by
  4130. * the kernel timer code when a mailbox does not complete within
  4131. * expected time. This function wakes up the worker thread to
  4132. * process the mailbox timeout and returns. All the processing is
  4133. * done by the worker thread function lpfc_mbox_timeout_handler.
  4134. **/
  4135. void
  4136. lpfc_mbox_timeout(unsigned long ptr)
  4137. {
  4138. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  4139. unsigned long iflag;
  4140. uint32_t tmo_posted;
  4141. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  4142. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  4143. if (!tmo_posted)
  4144. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  4145. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  4146. if (!tmo_posted)
  4147. lpfc_worker_wake_up(phba);
  4148. return;
  4149. }
  4150. /**
  4151. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  4152. * @phba: Pointer to HBA context object.
  4153. *
  4154. * This function is called from worker thread when a mailbox command times out.
  4155. * The caller is not required to hold any locks. This function will reset the
  4156. * HBA and recover all the pending commands.
  4157. **/
  4158. void
  4159. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  4160. {
  4161. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  4162. MAILBOX_t *mb = &pmbox->u.mb;
  4163. struct lpfc_sli *psli = &phba->sli;
  4164. struct lpfc_sli_ring *pring;
  4165. /* Check the pmbox pointer first. There is a race condition
  4166. * between the mbox timeout handler getting executed in the
  4167. * worklist and the mailbox actually completing. When this
  4168. * race condition occurs, the mbox_active will be NULL.
  4169. */
  4170. spin_lock_irq(&phba->hbalock);
  4171. if (pmbox == NULL) {
  4172. lpfc_printf_log(phba, KERN_WARNING,
  4173. LOG_MBOX | LOG_SLI,
  4174. "0353 Active Mailbox cleared - mailbox timeout "
  4175. "exiting\n");
  4176. spin_unlock_irq(&phba->hbalock);
  4177. return;
  4178. }
  4179. /* Mbox cmd <mbxCommand> timeout */
  4180. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4181. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  4182. mb->mbxCommand,
  4183. phba->pport->port_state,
  4184. phba->sli.sli_flag,
  4185. phba->sli.mbox_active);
  4186. spin_unlock_irq(&phba->hbalock);
  4187. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  4188. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  4189. * it to fail all oustanding SCSI IO.
  4190. */
  4191. spin_lock_irq(&phba->pport->work_port_lock);
  4192. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  4193. spin_unlock_irq(&phba->pport->work_port_lock);
  4194. spin_lock_irq(&phba->hbalock);
  4195. phba->link_state = LPFC_LINK_UNKNOWN;
  4196. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  4197. spin_unlock_irq(&phba->hbalock);
  4198. pring = &psli->ring[psli->fcp_ring];
  4199. lpfc_sli_abort_iocb_ring(phba, pring);
  4200. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4201. "0345 Resetting board due to mailbox timeout\n");
  4202. /* Reset the HBA device */
  4203. lpfc_reset_hba(phba);
  4204. }
  4205. /**
  4206. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  4207. * @phba: Pointer to HBA context object.
  4208. * @pmbox: Pointer to mailbox object.
  4209. * @flag: Flag indicating how the mailbox need to be processed.
  4210. *
  4211. * This function is called by discovery code and HBA management code
  4212. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  4213. * function gets the hbalock to protect the data structures.
  4214. * The mailbox command can be submitted in polling mode, in which case
  4215. * this function will wait in a polling loop for the completion of the
  4216. * mailbox.
  4217. * If the mailbox is submitted in no_wait mode (not polling) the
  4218. * function will submit the command and returns immediately without waiting
  4219. * for the mailbox completion. The no_wait is supported only when HBA
  4220. * is in SLI2/SLI3 mode - interrupts are enabled.
  4221. * The SLI interface allows only one mailbox pending at a time. If the
  4222. * mailbox is issued in polling mode and there is already a mailbox
  4223. * pending, then the function will return an error. If the mailbox is issued
  4224. * in NO_WAIT mode and there is a mailbox pending already, the function
  4225. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  4226. * The sli layer owns the mailbox object until the completion of mailbox
  4227. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  4228. * return codes the caller owns the mailbox command after the return of
  4229. * the function.
  4230. **/
  4231. static int
  4232. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  4233. uint32_t flag)
  4234. {
  4235. MAILBOX_t *mb;
  4236. struct lpfc_sli *psli = &phba->sli;
  4237. uint32_t status, evtctr;
  4238. uint32_t ha_copy;
  4239. int i;
  4240. unsigned long timeout;
  4241. unsigned long drvr_flag = 0;
  4242. uint32_t word0, ldata;
  4243. void __iomem *to_slim;
  4244. int processing_queue = 0;
  4245. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  4246. if (!pmbox) {
  4247. /* processing mbox queue from intr_handler */
  4248. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  4249. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4250. return MBX_SUCCESS;
  4251. }
  4252. processing_queue = 1;
  4253. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4254. pmbox = lpfc_mbox_get(phba);
  4255. if (!pmbox) {
  4256. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4257. return MBX_SUCCESS;
  4258. }
  4259. }
  4260. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  4261. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  4262. if(!pmbox->vport) {
  4263. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4264. lpfc_printf_log(phba, KERN_ERR,
  4265. LOG_MBOX | LOG_VPORT,
  4266. "1806 Mbox x%x failed. No vport\n",
  4267. pmbox->u.mb.mbxCommand);
  4268. dump_stack();
  4269. goto out_not_finished;
  4270. }
  4271. }
  4272. /* If the PCI channel is in offline state, do not post mbox. */
  4273. if (unlikely(pci_channel_offline(phba->pcidev))) {
  4274. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4275. goto out_not_finished;
  4276. }
  4277. /* If HBA has a deferred error attention, fail the iocb. */
  4278. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  4279. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4280. goto out_not_finished;
  4281. }
  4282. psli = &phba->sli;
  4283. mb = &pmbox->u.mb;
  4284. status = MBX_SUCCESS;
  4285. if (phba->link_state == LPFC_HBA_ERROR) {
  4286. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4287. /* Mbox command <mbxCommand> cannot issue */
  4288. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4289. "(%d):0311 Mailbox command x%x cannot "
  4290. "issue Data: x%x x%x\n",
  4291. pmbox->vport ? pmbox->vport->vpi : 0,
  4292. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  4293. goto out_not_finished;
  4294. }
  4295. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
  4296. !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
  4297. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4298. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4299. "(%d):2528 Mailbox command x%x cannot "
  4300. "issue Data: x%x x%x\n",
  4301. pmbox->vport ? pmbox->vport->vpi : 0,
  4302. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  4303. goto out_not_finished;
  4304. }
  4305. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  4306. /* Polling for a mbox command when another one is already active
  4307. * is not allowed in SLI. Also, the driver must have established
  4308. * SLI2 mode to queue and process multiple mbox commands.
  4309. */
  4310. if (flag & MBX_POLL) {
  4311. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4312. /* Mbox command <mbxCommand> cannot issue */
  4313. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4314. "(%d):2529 Mailbox command x%x "
  4315. "cannot issue Data: x%x x%x\n",
  4316. pmbox->vport ? pmbox->vport->vpi : 0,
  4317. pmbox->u.mb.mbxCommand,
  4318. psli->sli_flag, flag);
  4319. goto out_not_finished;
  4320. }
  4321. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  4322. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4323. /* Mbox command <mbxCommand> cannot issue */
  4324. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4325. "(%d):2530 Mailbox command x%x "
  4326. "cannot issue Data: x%x x%x\n",
  4327. pmbox->vport ? pmbox->vport->vpi : 0,
  4328. pmbox->u.mb.mbxCommand,
  4329. psli->sli_flag, flag);
  4330. goto out_not_finished;
  4331. }
  4332. /* Another mailbox command is still being processed, queue this
  4333. * command to be processed later.
  4334. */
  4335. lpfc_mbox_put(phba, pmbox);
  4336. /* Mbox cmd issue - BUSY */
  4337. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4338. "(%d):0308 Mbox cmd issue - BUSY Data: "
  4339. "x%x x%x x%x x%x\n",
  4340. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  4341. mb->mbxCommand, phba->pport->port_state,
  4342. psli->sli_flag, flag);
  4343. psli->slistat.mbox_busy++;
  4344. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4345. if (pmbox->vport) {
  4346. lpfc_debugfs_disc_trc(pmbox->vport,
  4347. LPFC_DISC_TRC_MBOX_VPORT,
  4348. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  4349. (uint32_t)mb->mbxCommand,
  4350. mb->un.varWords[0], mb->un.varWords[1]);
  4351. }
  4352. else {
  4353. lpfc_debugfs_disc_trc(phba->pport,
  4354. LPFC_DISC_TRC_MBOX,
  4355. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  4356. (uint32_t)mb->mbxCommand,
  4357. mb->un.varWords[0], mb->un.varWords[1]);
  4358. }
  4359. return MBX_BUSY;
  4360. }
  4361. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4362. /* If we are not polling, we MUST be in SLI2 mode */
  4363. if (flag != MBX_POLL) {
  4364. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  4365. (mb->mbxCommand != MBX_KILL_BOARD)) {
  4366. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4367. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4368. /* Mbox command <mbxCommand> cannot issue */
  4369. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4370. "(%d):2531 Mailbox command x%x "
  4371. "cannot issue Data: x%x x%x\n",
  4372. pmbox->vport ? pmbox->vport->vpi : 0,
  4373. pmbox->u.mb.mbxCommand,
  4374. psli->sli_flag, flag);
  4375. goto out_not_finished;
  4376. }
  4377. /* timeout active mbox command */
  4378. mod_timer(&psli->mbox_tmo, (jiffies +
  4379. (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
  4380. }
  4381. /* Mailbox cmd <cmd> issue */
  4382. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4383. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  4384. "x%x\n",
  4385. pmbox->vport ? pmbox->vport->vpi : 0,
  4386. mb->mbxCommand, phba->pport->port_state,
  4387. psli->sli_flag, flag);
  4388. if (mb->mbxCommand != MBX_HEARTBEAT) {
  4389. if (pmbox->vport) {
  4390. lpfc_debugfs_disc_trc(pmbox->vport,
  4391. LPFC_DISC_TRC_MBOX_VPORT,
  4392. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  4393. (uint32_t)mb->mbxCommand,
  4394. mb->un.varWords[0], mb->un.varWords[1]);
  4395. }
  4396. else {
  4397. lpfc_debugfs_disc_trc(phba->pport,
  4398. LPFC_DISC_TRC_MBOX,
  4399. "MBOX Send: cmd:x%x mb:x%x x%x",
  4400. (uint32_t)mb->mbxCommand,
  4401. mb->un.varWords[0], mb->un.varWords[1]);
  4402. }
  4403. }
  4404. psli->slistat.mbox_cmd++;
  4405. evtctr = psli->slistat.mbox_event;
  4406. /* next set own bit for the adapter and copy over command word */
  4407. mb->mbxOwner = OWN_CHIP;
  4408. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  4409. /* First copy command data to host SLIM area */
  4410. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  4411. } else {
  4412. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  4413. /* copy command data into host mbox for cmpl */
  4414. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  4415. }
  4416. /* First copy mbox command data to HBA SLIM, skip past first
  4417. word */
  4418. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  4419. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  4420. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  4421. /* Next copy over first word, with mbxOwner set */
  4422. ldata = *((uint32_t *)mb);
  4423. to_slim = phba->MBslimaddr;
  4424. writel(ldata, to_slim);
  4425. readl(to_slim); /* flush */
  4426. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  4427. /* switch over to host mailbox */
  4428. psli->sli_flag |= LPFC_SLI_ACTIVE;
  4429. }
  4430. }
  4431. wmb();
  4432. switch (flag) {
  4433. case MBX_NOWAIT:
  4434. /* Set up reference to mailbox command */
  4435. psli->mbox_active = pmbox;
  4436. /* Interrupt board to do it */
  4437. writel(CA_MBATT, phba->CAregaddr);
  4438. readl(phba->CAregaddr); /* flush */
  4439. /* Don't wait for it to finish, just return */
  4440. break;
  4441. case MBX_POLL:
  4442. /* Set up null reference to mailbox command */
  4443. psli->mbox_active = NULL;
  4444. /* Interrupt board to do it */
  4445. writel(CA_MBATT, phba->CAregaddr);
  4446. readl(phba->CAregaddr); /* flush */
  4447. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  4448. /* First read mbox status word */
  4449. word0 = *((uint32_t *)phba->mbox);
  4450. word0 = le32_to_cpu(word0);
  4451. } else {
  4452. /* First read mbox status word */
  4453. word0 = readl(phba->MBslimaddr);
  4454. }
  4455. /* Read the HBA Host Attention Register */
  4456. ha_copy = readl(phba->HAregaddr);
  4457. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  4458. mb->mbxCommand) *
  4459. 1000) + jiffies;
  4460. i = 0;
  4461. /* Wait for command to complete */
  4462. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  4463. (!(ha_copy & HA_MBATT) &&
  4464. (phba->link_state > LPFC_WARM_START))) {
  4465. if (time_after(jiffies, timeout)) {
  4466. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4467. spin_unlock_irqrestore(&phba->hbalock,
  4468. drvr_flag);
  4469. goto out_not_finished;
  4470. }
  4471. /* Check if we took a mbox interrupt while we were
  4472. polling */
  4473. if (((word0 & OWN_CHIP) != OWN_CHIP)
  4474. && (evtctr != psli->slistat.mbox_event))
  4475. break;
  4476. if (i++ > 10) {
  4477. spin_unlock_irqrestore(&phba->hbalock,
  4478. drvr_flag);
  4479. msleep(1);
  4480. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  4481. }
  4482. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  4483. /* First copy command data */
  4484. word0 = *((uint32_t *)phba->mbox);
  4485. word0 = le32_to_cpu(word0);
  4486. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  4487. MAILBOX_t *slimmb;
  4488. uint32_t slimword0;
  4489. /* Check real SLIM for any errors */
  4490. slimword0 = readl(phba->MBslimaddr);
  4491. slimmb = (MAILBOX_t *) & slimword0;
  4492. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  4493. && slimmb->mbxStatus) {
  4494. psli->sli_flag &=
  4495. ~LPFC_SLI_ACTIVE;
  4496. word0 = slimword0;
  4497. }
  4498. }
  4499. } else {
  4500. /* First copy command data */
  4501. word0 = readl(phba->MBslimaddr);
  4502. }
  4503. /* Read the HBA Host Attention Register */
  4504. ha_copy = readl(phba->HAregaddr);
  4505. }
  4506. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  4507. /* copy results back to user */
  4508. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  4509. } else {
  4510. /* First copy command data */
  4511. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  4512. MAILBOX_CMD_SIZE);
  4513. if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
  4514. pmbox->context2) {
  4515. lpfc_memcpy_from_slim((void *)pmbox->context2,
  4516. phba->MBslimaddr + DMP_RSP_OFFSET,
  4517. mb->un.varDmp.word_cnt);
  4518. }
  4519. }
  4520. writel(HA_MBATT, phba->HAregaddr);
  4521. readl(phba->HAregaddr); /* flush */
  4522. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4523. status = mb->mbxStatus;
  4524. }
  4525. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4526. return status;
  4527. out_not_finished:
  4528. if (processing_queue) {
  4529. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  4530. lpfc_mbox_cmpl_put(phba, pmbox);
  4531. }
  4532. return MBX_NOT_FINISHED;
  4533. }
  4534. /**
  4535. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  4536. * @phba: Pointer to HBA context object.
  4537. *
  4538. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  4539. * the driver internal pending mailbox queue. It will then try to wait out the
  4540. * possible outstanding mailbox command before return.
  4541. *
  4542. * Returns:
  4543. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  4544. * the outstanding mailbox command timed out.
  4545. **/
  4546. static int
  4547. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  4548. {
  4549. struct lpfc_sli *psli = &phba->sli;
  4550. uint8_t actcmd = MBX_HEARTBEAT;
  4551. int rc = 0;
  4552. unsigned long timeout;
  4553. /* Mark the asynchronous mailbox command posting as blocked */
  4554. spin_lock_irq(&phba->hbalock);
  4555. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  4556. if (phba->sli.mbox_active)
  4557. actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
  4558. spin_unlock_irq(&phba->hbalock);
  4559. /* Determine how long we might wait for the active mailbox
  4560. * command to be gracefully completed by firmware.
  4561. */
  4562. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
  4563. jiffies;
  4564. /* Wait for the outstnading mailbox command to complete */
  4565. while (phba->sli.mbox_active) {
  4566. /* Check active mailbox complete status every 2ms */
  4567. msleep(2);
  4568. if (time_after(jiffies, timeout)) {
  4569. /* Timeout, marked the outstanding cmd not complete */
  4570. rc = 1;
  4571. break;
  4572. }
  4573. }
  4574. /* Can not cleanly block async mailbox command, fails it */
  4575. if (rc) {
  4576. spin_lock_irq(&phba->hbalock);
  4577. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4578. spin_unlock_irq(&phba->hbalock);
  4579. }
  4580. return rc;
  4581. }
  4582. /**
  4583. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  4584. * @phba: Pointer to HBA context object.
  4585. *
  4586. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  4587. * commands from the driver internal pending mailbox queue. It makes sure
  4588. * that there is no outstanding mailbox command before resuming posting
  4589. * asynchronous mailbox commands. If, for any reason, there is outstanding
  4590. * mailbox command, it will try to wait it out before resuming asynchronous
  4591. * mailbox command posting.
  4592. **/
  4593. static void
  4594. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  4595. {
  4596. struct lpfc_sli *psli = &phba->sli;
  4597. spin_lock_irq(&phba->hbalock);
  4598. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  4599. /* Asynchronous mailbox posting is not blocked, do nothing */
  4600. spin_unlock_irq(&phba->hbalock);
  4601. return;
  4602. }
  4603. /* Outstanding synchronous mailbox command is guaranteed to be done,
  4604. * successful or timeout, after timing-out the outstanding mailbox
  4605. * command shall always be removed, so just unblock posting async
  4606. * mailbox command and resume
  4607. */
  4608. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4609. spin_unlock_irq(&phba->hbalock);
  4610. /* wake up worker thread to post asynchronlous mailbox command */
  4611. lpfc_worker_wake_up(phba);
  4612. }
  4613. /**
  4614. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  4615. * @phba: Pointer to HBA context object.
  4616. * @mboxq: Pointer to mailbox object.
  4617. *
  4618. * The function posts a mailbox to the port. The mailbox is expected
  4619. * to be comletely filled in and ready for the port to operate on it.
  4620. * This routine executes a synchronous completion operation on the
  4621. * mailbox by polling for its completion.
  4622. *
  4623. * The caller must not be holding any locks when calling this routine.
  4624. *
  4625. * Returns:
  4626. * MBX_SUCCESS - mailbox posted successfully
  4627. * Any of the MBX error values.
  4628. **/
  4629. static int
  4630. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  4631. {
  4632. int rc = MBX_SUCCESS;
  4633. unsigned long iflag;
  4634. uint32_t db_ready;
  4635. uint32_t mcqe_status;
  4636. uint32_t mbx_cmnd;
  4637. unsigned long timeout;
  4638. struct lpfc_sli *psli = &phba->sli;
  4639. struct lpfc_mqe *mb = &mboxq->u.mqe;
  4640. struct lpfc_bmbx_create *mbox_rgn;
  4641. struct dma_address *dma_address;
  4642. struct lpfc_register bmbx_reg;
  4643. /*
  4644. * Only one mailbox can be active to the bootstrap mailbox region
  4645. * at a time and there is no queueing provided.
  4646. */
  4647. spin_lock_irqsave(&phba->hbalock, iflag);
  4648. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  4649. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4650. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4651. "(%d):2532 Mailbox command x%x (x%x) "
  4652. "cannot issue Data: x%x x%x\n",
  4653. mboxq->vport ? mboxq->vport->vpi : 0,
  4654. mboxq->u.mb.mbxCommand,
  4655. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4656. psli->sli_flag, MBX_POLL);
  4657. return MBXERR_ERROR;
  4658. }
  4659. /* The server grabs the token and owns it until release */
  4660. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4661. phba->sli.mbox_active = mboxq;
  4662. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4663. /*
  4664. * Initialize the bootstrap memory region to avoid stale data areas
  4665. * in the mailbox post. Then copy the caller's mailbox contents to
  4666. * the bmbx mailbox region.
  4667. */
  4668. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  4669. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  4670. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  4671. sizeof(struct lpfc_mqe));
  4672. /* Post the high mailbox dma address to the port and wait for ready. */
  4673. dma_address = &phba->sli4_hba.bmbx.dma_address;
  4674. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  4675. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
  4676. * 1000) + jiffies;
  4677. do {
  4678. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  4679. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  4680. if (!db_ready)
  4681. msleep(2);
  4682. if (time_after(jiffies, timeout)) {
  4683. rc = MBXERR_ERROR;
  4684. goto exit;
  4685. }
  4686. } while (!db_ready);
  4687. /* Post the low mailbox dma address to the port. */
  4688. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  4689. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
  4690. * 1000) + jiffies;
  4691. do {
  4692. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  4693. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  4694. if (!db_ready)
  4695. msleep(2);
  4696. if (time_after(jiffies, timeout)) {
  4697. rc = MBXERR_ERROR;
  4698. goto exit;
  4699. }
  4700. } while (!db_ready);
  4701. /*
  4702. * Read the CQ to ensure the mailbox has completed.
  4703. * If so, update the mailbox status so that the upper layers
  4704. * can complete the request normally.
  4705. */
  4706. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  4707. sizeof(struct lpfc_mqe));
  4708. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  4709. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  4710. sizeof(struct lpfc_mcqe));
  4711. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  4712. /* Prefix the mailbox status with range x4000 to note SLI4 status. */
  4713. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  4714. bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
  4715. rc = MBXERR_ERROR;
  4716. }
  4717. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4718. "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
  4719. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  4720. " x%x x%x CQ: x%x x%x x%x x%x\n",
  4721. mboxq->vport ? mboxq->vport->vpi : 0,
  4722. mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4723. bf_get(lpfc_mqe_status, mb),
  4724. mb->un.mb_words[0], mb->un.mb_words[1],
  4725. mb->un.mb_words[2], mb->un.mb_words[3],
  4726. mb->un.mb_words[4], mb->un.mb_words[5],
  4727. mb->un.mb_words[6], mb->un.mb_words[7],
  4728. mb->un.mb_words[8], mb->un.mb_words[9],
  4729. mb->un.mb_words[10], mb->un.mb_words[11],
  4730. mb->un.mb_words[12], mboxq->mcqe.word0,
  4731. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4732. mboxq->mcqe.trailer);
  4733. exit:
  4734. /* We are holding the token, no needed for lock when release */
  4735. spin_lock_irqsave(&phba->hbalock, iflag);
  4736. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4737. phba->sli.mbox_active = NULL;
  4738. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4739. return rc;
  4740. }
  4741. /**
  4742. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  4743. * @phba: Pointer to HBA context object.
  4744. * @pmbox: Pointer to mailbox object.
  4745. * @flag: Flag indicating how the mailbox need to be processed.
  4746. *
  4747. * This function is called by discovery code and HBA management code to submit
  4748. * a mailbox command to firmware with SLI-4 interface spec.
  4749. *
  4750. * Return codes the caller owns the mailbox command after the return of the
  4751. * function.
  4752. **/
  4753. static int
  4754. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4755. uint32_t flag)
  4756. {
  4757. struct lpfc_sli *psli = &phba->sli;
  4758. unsigned long iflags;
  4759. int rc;
  4760. rc = lpfc_mbox_dev_check(phba);
  4761. if (unlikely(rc)) {
  4762. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4763. "(%d):2544 Mailbox command x%x (x%x) "
  4764. "cannot issue Data: x%x x%x\n",
  4765. mboxq->vport ? mboxq->vport->vpi : 0,
  4766. mboxq->u.mb.mbxCommand,
  4767. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4768. psli->sli_flag, flag);
  4769. goto out_not_finished;
  4770. }
  4771. /* Detect polling mode and jump to a handler */
  4772. if (!phba->sli4_hba.intr_enable) {
  4773. if (flag == MBX_POLL)
  4774. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  4775. else
  4776. rc = -EIO;
  4777. if (rc != MBX_SUCCESS)
  4778. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4779. "(%d):2541 Mailbox command x%x "
  4780. "(x%x) cannot issue Data: x%x x%x\n",
  4781. mboxq->vport ? mboxq->vport->vpi : 0,
  4782. mboxq->u.mb.mbxCommand,
  4783. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4784. psli->sli_flag, flag);
  4785. return rc;
  4786. } else if (flag == MBX_POLL) {
  4787. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  4788. "(%d):2542 Try to issue mailbox command "
  4789. "x%x (x%x) synchronously ahead of async"
  4790. "mailbox command queue: x%x x%x\n",
  4791. mboxq->vport ? mboxq->vport->vpi : 0,
  4792. mboxq->u.mb.mbxCommand,
  4793. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4794. psli->sli_flag, flag);
  4795. /* Try to block the asynchronous mailbox posting */
  4796. rc = lpfc_sli4_async_mbox_block(phba);
  4797. if (!rc) {
  4798. /* Successfully blocked, now issue sync mbox cmd */
  4799. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  4800. if (rc != MBX_SUCCESS)
  4801. lpfc_printf_log(phba, KERN_ERR,
  4802. LOG_MBOX | LOG_SLI,
  4803. "(%d):2597 Mailbox command "
  4804. "x%x (x%x) cannot issue "
  4805. "Data: x%x x%x\n",
  4806. mboxq->vport ?
  4807. mboxq->vport->vpi : 0,
  4808. mboxq->u.mb.mbxCommand,
  4809. lpfc_sli4_mbox_opcode_get(phba,
  4810. mboxq),
  4811. psli->sli_flag, flag);
  4812. /* Unblock the async mailbox posting afterward */
  4813. lpfc_sli4_async_mbox_unblock(phba);
  4814. }
  4815. return rc;
  4816. }
  4817. /* Now, interrupt mode asynchrous mailbox command */
  4818. rc = lpfc_mbox_cmd_check(phba, mboxq);
  4819. if (rc) {
  4820. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4821. "(%d):2543 Mailbox command x%x (x%x) "
  4822. "cannot issue Data: x%x x%x\n",
  4823. mboxq->vport ? mboxq->vport->vpi : 0,
  4824. mboxq->u.mb.mbxCommand,
  4825. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4826. psli->sli_flag, flag);
  4827. goto out_not_finished;
  4828. }
  4829. /* Put the mailbox command to the driver internal FIFO */
  4830. psli->slistat.mbox_busy++;
  4831. spin_lock_irqsave(&phba->hbalock, iflags);
  4832. lpfc_mbox_put(phba, mboxq);
  4833. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4834. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4835. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  4836. "x%x (x%x) x%x x%x x%x\n",
  4837. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  4838. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4839. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4840. phba->pport->port_state,
  4841. psli->sli_flag, MBX_NOWAIT);
  4842. /* Wake up worker thread to transport mailbox command from head */
  4843. lpfc_worker_wake_up(phba);
  4844. return MBX_BUSY;
  4845. out_not_finished:
  4846. return MBX_NOT_FINISHED;
  4847. }
  4848. /**
  4849. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  4850. * @phba: Pointer to HBA context object.
  4851. *
  4852. * This function is called by worker thread to send a mailbox command to
  4853. * SLI4 HBA firmware.
  4854. *
  4855. **/
  4856. int
  4857. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  4858. {
  4859. struct lpfc_sli *psli = &phba->sli;
  4860. LPFC_MBOXQ_t *mboxq;
  4861. int rc = MBX_SUCCESS;
  4862. unsigned long iflags;
  4863. struct lpfc_mqe *mqe;
  4864. uint32_t mbx_cmnd;
  4865. /* Check interrupt mode before post async mailbox command */
  4866. if (unlikely(!phba->sli4_hba.intr_enable))
  4867. return MBX_NOT_FINISHED;
  4868. /* Check for mailbox command service token */
  4869. spin_lock_irqsave(&phba->hbalock, iflags);
  4870. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  4871. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4872. return MBX_NOT_FINISHED;
  4873. }
  4874. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  4875. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4876. return MBX_NOT_FINISHED;
  4877. }
  4878. if (unlikely(phba->sli.mbox_active)) {
  4879. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4880. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4881. "0384 There is pending active mailbox cmd\n");
  4882. return MBX_NOT_FINISHED;
  4883. }
  4884. /* Take the mailbox command service token */
  4885. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4886. /* Get the next mailbox command from head of queue */
  4887. mboxq = lpfc_mbox_get(phba);
  4888. /* If no more mailbox command waiting for post, we're done */
  4889. if (!mboxq) {
  4890. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4891. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4892. return MBX_SUCCESS;
  4893. }
  4894. phba->sli.mbox_active = mboxq;
  4895. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4896. /* Check device readiness for posting mailbox command */
  4897. rc = lpfc_mbox_dev_check(phba);
  4898. if (unlikely(rc))
  4899. /* Driver clean routine will clean up pending mailbox */
  4900. goto out_not_finished;
  4901. /* Prepare the mbox command to be posted */
  4902. mqe = &mboxq->u.mqe;
  4903. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  4904. /* Start timer for the mbox_tmo and log some mailbox post messages */
  4905. mod_timer(&psli->mbox_tmo, (jiffies +
  4906. (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
  4907. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4908. "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
  4909. "x%x x%x\n",
  4910. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  4911. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4912. phba->pport->port_state, psli->sli_flag);
  4913. if (mbx_cmnd != MBX_HEARTBEAT) {
  4914. if (mboxq->vport) {
  4915. lpfc_debugfs_disc_trc(mboxq->vport,
  4916. LPFC_DISC_TRC_MBOX_VPORT,
  4917. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  4918. mbx_cmnd, mqe->un.mb_words[0],
  4919. mqe->un.mb_words[1]);
  4920. } else {
  4921. lpfc_debugfs_disc_trc(phba->pport,
  4922. LPFC_DISC_TRC_MBOX,
  4923. "MBOX Send: cmd:x%x mb:x%x x%x",
  4924. mbx_cmnd, mqe->un.mb_words[0],
  4925. mqe->un.mb_words[1]);
  4926. }
  4927. }
  4928. psli->slistat.mbox_cmd++;
  4929. /* Post the mailbox command to the port */
  4930. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  4931. if (rc != MBX_SUCCESS) {
  4932. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4933. "(%d):2533 Mailbox command x%x (x%x) "
  4934. "cannot issue Data: x%x x%x\n",
  4935. mboxq->vport ? mboxq->vport->vpi : 0,
  4936. mboxq->u.mb.mbxCommand,
  4937. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4938. psli->sli_flag, MBX_NOWAIT);
  4939. goto out_not_finished;
  4940. }
  4941. return rc;
  4942. out_not_finished:
  4943. spin_lock_irqsave(&phba->hbalock, iflags);
  4944. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  4945. __lpfc_mbox_cmpl_put(phba, mboxq);
  4946. /* Release the token */
  4947. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4948. phba->sli.mbox_active = NULL;
  4949. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4950. return MBX_NOT_FINISHED;
  4951. }
  4952. /**
  4953. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  4954. * @phba: Pointer to HBA context object.
  4955. * @pmbox: Pointer to mailbox object.
  4956. * @flag: Flag indicating how the mailbox need to be processed.
  4957. *
  4958. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  4959. * the API jump table function pointer from the lpfc_hba struct.
  4960. *
  4961. * Return codes the caller owns the mailbox command after the return of the
  4962. * function.
  4963. **/
  4964. int
  4965. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  4966. {
  4967. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  4968. }
  4969. /**
  4970. * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
  4971. * @phba: The hba struct for which this call is being executed.
  4972. * @dev_grp: The HBA PCI-Device group number.
  4973. *
  4974. * This routine sets up the mbox interface API function jump table in @phba
  4975. * struct.
  4976. * Returns: 0 - success, -ENODEV - failure.
  4977. **/
  4978. int
  4979. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  4980. {
  4981. switch (dev_grp) {
  4982. case LPFC_PCI_DEV_LP:
  4983. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  4984. phba->lpfc_sli_handle_slow_ring_event =
  4985. lpfc_sli_handle_slow_ring_event_s3;
  4986. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  4987. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  4988. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  4989. break;
  4990. case LPFC_PCI_DEV_OC:
  4991. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  4992. phba->lpfc_sli_handle_slow_ring_event =
  4993. lpfc_sli_handle_slow_ring_event_s4;
  4994. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  4995. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  4996. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  4997. break;
  4998. default:
  4999. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5000. "1420 Invalid HBA PCI-device group: 0x%x\n",
  5001. dev_grp);
  5002. return -ENODEV;
  5003. break;
  5004. }
  5005. return 0;
  5006. }
  5007. /**
  5008. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  5009. * @phba: Pointer to HBA context object.
  5010. * @pring: Pointer to driver SLI ring object.
  5011. * @piocb: Pointer to address of newly added command iocb.
  5012. *
  5013. * This function is called with hbalock held to add a command
  5014. * iocb to the txq when SLI layer cannot submit the command iocb
  5015. * to the ring.
  5016. **/
  5017. static void
  5018. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  5019. struct lpfc_iocbq *piocb)
  5020. {
  5021. /* Insert the caller's iocb in the txq tail for later processing. */
  5022. list_add_tail(&piocb->list, &pring->txq);
  5023. pring->txq_cnt++;
  5024. }
  5025. /**
  5026. * lpfc_sli_next_iocb - Get the next iocb in the txq
  5027. * @phba: Pointer to HBA context object.
  5028. * @pring: Pointer to driver SLI ring object.
  5029. * @piocb: Pointer to address of newly added command iocb.
  5030. *
  5031. * This function is called with hbalock held before a new
  5032. * iocb is submitted to the firmware. This function checks
  5033. * txq to flush the iocbs in txq to Firmware before
  5034. * submitting new iocbs to the Firmware.
  5035. * If there are iocbs in the txq which need to be submitted
  5036. * to firmware, lpfc_sli_next_iocb returns the first element
  5037. * of the txq after dequeuing it from txq.
  5038. * If there is no iocb in the txq then the function will return
  5039. * *piocb and *piocb is set to NULL. Caller needs to check
  5040. * *piocb to find if there are more commands in the txq.
  5041. **/
  5042. static struct lpfc_iocbq *
  5043. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  5044. struct lpfc_iocbq **piocb)
  5045. {
  5046. struct lpfc_iocbq * nextiocb;
  5047. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  5048. if (!nextiocb) {
  5049. nextiocb = *piocb;
  5050. *piocb = NULL;
  5051. }
  5052. return nextiocb;
  5053. }
  5054. /**
  5055. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  5056. * @phba: Pointer to HBA context object.
  5057. * @ring_number: SLI ring number to issue iocb on.
  5058. * @piocb: Pointer to command iocb.
  5059. * @flag: Flag indicating if this command can be put into txq.
  5060. *
  5061. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  5062. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  5063. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  5064. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  5065. * this function allows only iocbs for posting buffers. This function finds
  5066. * next available slot in the command ring and posts the command to the
  5067. * available slot and writes the port attention register to request HBA start
  5068. * processing new iocb. If there is no slot available in the ring and
  5069. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  5070. * the function returns IOCB_BUSY.
  5071. *
  5072. * This function is called with hbalock held. The function will return success
  5073. * after it successfully submit the iocb to firmware or after adding to the
  5074. * txq.
  5075. **/
  5076. static int
  5077. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  5078. struct lpfc_iocbq *piocb, uint32_t flag)
  5079. {
  5080. struct lpfc_iocbq *nextiocb;
  5081. IOCB_t *iocb;
  5082. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  5083. if (piocb->iocb_cmpl && (!piocb->vport) &&
  5084. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  5085. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  5086. lpfc_printf_log(phba, KERN_ERR,
  5087. LOG_SLI | LOG_VPORT,
  5088. "1807 IOCB x%x failed. No vport\n",
  5089. piocb->iocb.ulpCommand);
  5090. dump_stack();
  5091. return IOCB_ERROR;
  5092. }
  5093. /* If the PCI channel is in offline state, do not post iocbs. */
  5094. if (unlikely(pci_channel_offline(phba->pcidev)))
  5095. return IOCB_ERROR;
  5096. /* If HBA has a deferred error attention, fail the iocb. */
  5097. if (unlikely(phba->hba_flag & DEFER_ERATT))
  5098. return IOCB_ERROR;
  5099. /*
  5100. * We should never get an IOCB if we are in a < LINK_DOWN state
  5101. */
  5102. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  5103. return IOCB_ERROR;
  5104. /*
  5105. * Check to see if we are blocking IOCB processing because of a
  5106. * outstanding event.
  5107. */
  5108. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  5109. goto iocb_busy;
  5110. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  5111. /*
  5112. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  5113. * can be issued if the link is not up.
  5114. */
  5115. switch (piocb->iocb.ulpCommand) {
  5116. case CMD_GEN_REQUEST64_CR:
  5117. case CMD_GEN_REQUEST64_CX:
  5118. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  5119. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  5120. FC_FCP_CMND) ||
  5121. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  5122. MENLO_TRANSPORT_TYPE))
  5123. goto iocb_busy;
  5124. break;
  5125. case CMD_QUE_RING_BUF_CN:
  5126. case CMD_QUE_RING_BUF64_CN:
  5127. /*
  5128. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  5129. * completion, iocb_cmpl MUST be 0.
  5130. */
  5131. if (piocb->iocb_cmpl)
  5132. piocb->iocb_cmpl = NULL;
  5133. /*FALLTHROUGH*/
  5134. case CMD_CREATE_XRI_CR:
  5135. case CMD_CLOSE_XRI_CN:
  5136. case CMD_CLOSE_XRI_CX:
  5137. break;
  5138. default:
  5139. goto iocb_busy;
  5140. }
  5141. /*
  5142. * For FCP commands, we must be in a state where we can process link
  5143. * attention events.
  5144. */
  5145. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  5146. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  5147. goto iocb_busy;
  5148. }
  5149. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  5150. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  5151. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  5152. if (iocb)
  5153. lpfc_sli_update_ring(phba, pring);
  5154. else
  5155. lpfc_sli_update_full_ring(phba, pring);
  5156. if (!piocb)
  5157. return IOCB_SUCCESS;
  5158. goto out_busy;
  5159. iocb_busy:
  5160. pring->stats.iocb_cmd_delay++;
  5161. out_busy:
  5162. if (!(flag & SLI_IOCB_RET_IOCB)) {
  5163. __lpfc_sli_ringtx_put(phba, pring, piocb);
  5164. return IOCB_SUCCESS;
  5165. }
  5166. return IOCB_BUSY;
  5167. }
  5168. /**
  5169. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  5170. * @phba: Pointer to HBA context object.
  5171. * @piocb: Pointer to command iocb.
  5172. * @sglq: Pointer to the scatter gather queue object.
  5173. *
  5174. * This routine converts the bpl or bde that is in the IOCB
  5175. * to a sgl list for the sli4 hardware. The physical address
  5176. * of the bpl/bde is converted back to a virtual address.
  5177. * If the IOCB contains a BPL then the list of BDE's is
  5178. * converted to sli4_sge's. If the IOCB contains a single
  5179. * BDE then it is converted to a single sli_sge.
  5180. * The IOCB is still in cpu endianess so the contents of
  5181. * the bpl can be used without byte swapping.
  5182. *
  5183. * Returns valid XRI = Success, NO_XRI = Failure.
  5184. **/
  5185. static uint16_t
  5186. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  5187. struct lpfc_sglq *sglq)
  5188. {
  5189. uint16_t xritag = NO_XRI;
  5190. struct ulp_bde64 *bpl = NULL;
  5191. struct ulp_bde64 bde;
  5192. struct sli4_sge *sgl = NULL;
  5193. IOCB_t *icmd;
  5194. int numBdes = 0;
  5195. int i = 0;
  5196. if (!piocbq || !sglq)
  5197. return xritag;
  5198. sgl = (struct sli4_sge *)sglq->sgl;
  5199. icmd = &piocbq->iocb;
  5200. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  5201. numBdes = icmd->un.genreq64.bdl.bdeSize /
  5202. sizeof(struct ulp_bde64);
  5203. /* The addrHigh and addrLow fields within the IOCB
  5204. * have not been byteswapped yet so there is no
  5205. * need to swap them back.
  5206. */
  5207. bpl = (struct ulp_bde64 *)
  5208. ((struct lpfc_dmabuf *)piocbq->context3)->virt;
  5209. if (!bpl)
  5210. return xritag;
  5211. for (i = 0; i < numBdes; i++) {
  5212. /* Should already be byte swapped. */
  5213. sgl->addr_hi = bpl->addrHigh;
  5214. sgl->addr_lo = bpl->addrLow;
  5215. /* swap the size field back to the cpu so we
  5216. * can assign it to the sgl.
  5217. */
  5218. bde.tus.w = le32_to_cpu(bpl->tus.w);
  5219. bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
  5220. if ((i+1) == numBdes)
  5221. bf_set(lpfc_sli4_sge_last, sgl, 1);
  5222. else
  5223. bf_set(lpfc_sli4_sge_last, sgl, 0);
  5224. sgl->word2 = cpu_to_le32(sgl->word2);
  5225. sgl->word3 = cpu_to_le32(sgl->word3);
  5226. bpl++;
  5227. sgl++;
  5228. }
  5229. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  5230. /* The addrHigh and addrLow fields of the BDE have not
  5231. * been byteswapped yet so they need to be swapped
  5232. * before putting them in the sgl.
  5233. */
  5234. sgl->addr_hi =
  5235. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  5236. sgl->addr_lo =
  5237. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  5238. bf_set(lpfc_sli4_sge_len, sgl,
  5239. icmd->un.genreq64.bdl.bdeSize);
  5240. bf_set(lpfc_sli4_sge_last, sgl, 1);
  5241. sgl->word2 = cpu_to_le32(sgl->word2);
  5242. sgl->word3 = cpu_to_le32(sgl->word3);
  5243. }
  5244. return sglq->sli4_xritag;
  5245. }
  5246. /**
  5247. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  5248. * @phba: Pointer to HBA context object.
  5249. *
  5250. * This routine performs a round robin SCSI command to SLI4 FCP WQ index
  5251. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  5252. * held.
  5253. *
  5254. * Return: index into SLI4 fast-path FCP queue index.
  5255. **/
  5256. static uint32_t
  5257. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  5258. {
  5259. ++phba->fcp_qidx;
  5260. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  5261. phba->fcp_qidx = 0;
  5262. return phba->fcp_qidx;
  5263. }
  5264. /**
  5265. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  5266. * @phba: Pointer to HBA context object.
  5267. * @piocb: Pointer to command iocb.
  5268. * @wqe: Pointer to the work queue entry.
  5269. *
  5270. * This routine converts the iocb command to its Work Queue Entry
  5271. * equivalent. The wqe pointer should not have any fields set when
  5272. * this routine is called because it will memcpy over them.
  5273. * This routine does not set the CQ_ID or the WQEC bits in the
  5274. * wqe.
  5275. *
  5276. * Returns: 0 = Success, IOCB_ERROR = Failure.
  5277. **/
  5278. static int
  5279. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  5280. union lpfc_wqe *wqe)
  5281. {
  5282. uint32_t payload_len = 0;
  5283. uint8_t ct = 0;
  5284. uint32_t fip;
  5285. uint32_t abort_tag;
  5286. uint8_t command_type = ELS_COMMAND_NON_FIP;
  5287. uint8_t cmnd;
  5288. uint16_t xritag;
  5289. struct ulp_bde64 *bpl = NULL;
  5290. fip = bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags);
  5291. /* The fcp commands will set command type */
  5292. if (iocbq->iocb_flag & LPFC_IO_FCP)
  5293. command_type = FCP_COMMAND;
  5294. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS))
  5295. command_type = ELS_COMMAND_FIP;
  5296. else
  5297. command_type = ELS_COMMAND_NON_FIP;
  5298. /* Some of the fields are in the right position already */
  5299. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  5300. abort_tag = (uint32_t) iocbq->iotag;
  5301. xritag = iocbq->sli4_xritag;
  5302. wqe->words[7] = 0; /* The ct field has moved so reset */
  5303. /* words0-2 bpl convert bde */
  5304. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  5305. bpl = (struct ulp_bde64 *)
  5306. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  5307. if (!bpl)
  5308. return IOCB_ERROR;
  5309. /* Should already be byte swapped. */
  5310. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  5311. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  5312. /* swap the size field back to the cpu so we
  5313. * can assign it to the sgl.
  5314. */
  5315. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  5316. payload_len = wqe->generic.bde.tus.f.bdeSize;
  5317. } else
  5318. payload_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  5319. iocbq->iocb.ulpIoTag = iocbq->iotag;
  5320. cmnd = iocbq->iocb.ulpCommand;
  5321. switch (iocbq->iocb.ulpCommand) {
  5322. case CMD_ELS_REQUEST64_CR:
  5323. if (!iocbq->iocb.ulpLe) {
  5324. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5325. "2007 Only Limited Edition cmd Format"
  5326. " supported 0x%x\n",
  5327. iocbq->iocb.ulpCommand);
  5328. return IOCB_ERROR;
  5329. }
  5330. wqe->els_req.payload_len = payload_len;
  5331. /* Els_reguest64 has a TMO */
  5332. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  5333. iocbq->iocb.ulpTimeout);
  5334. /* Need a VF for word 4 set the vf bit*/
  5335. bf_set(els_req64_vf, &wqe->els_req, 0);
  5336. /* And a VFID for word 12 */
  5337. bf_set(els_req64_vfid, &wqe->els_req, 0);
  5338. /*
  5339. * Set ct field to 3, indicates that the context_tag field
  5340. * contains the FCFI and remote N_Port_ID is
  5341. * in word 5.
  5342. */
  5343. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  5344. bf_set(lpfc_wqe_gen_context, &wqe->generic,
  5345. iocbq->iocb.ulpContext);
  5346. bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
  5347. bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
  5348. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  5349. break;
  5350. case CMD_XMIT_SEQUENCE64_CR:
  5351. /* word3 iocb=io_tag32 wqe=payload_offset */
  5352. /* payload offset used for multilpe outstanding
  5353. * sequences on the same exchange
  5354. */
  5355. wqe->words[3] = 0;
  5356. /* word4 relative_offset memcpy */
  5357. /* word5 r_ctl/df_ctl memcpy */
  5358. bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
  5359. wqe->xmit_sequence.xmit_len = payload_len;
  5360. break;
  5361. case CMD_XMIT_BCAST64_CN:
  5362. /* word3 iocb=iotag32 wqe=payload_len */
  5363. wqe->words[3] = 0; /* no definition for this in wqe */
  5364. /* word4 iocb=rsvd wqe=rsvd */
  5365. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  5366. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  5367. bf_set(lpfc_wqe_gen_ct, &wqe->generic,
  5368. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  5369. break;
  5370. case CMD_FCP_IWRITE64_CR:
  5371. command_type = FCP_COMMAND_DATA_OUT;
  5372. /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
  5373. * confusing.
  5374. * word3 is payload_len: byte offset to the sgl entry for the
  5375. * fcp_command.
  5376. * word4 is total xfer len, same as the IOCB->ulpParameter.
  5377. * word5 is initial xfer len 0 = wait for xfer-ready
  5378. */
  5379. /* Always wait for xfer-ready before sending data */
  5380. wqe->fcp_iwrite.initial_xfer_len = 0;
  5381. /* word 4 (xfer length) should have been set on the memcpy */
  5382. /* allow write to fall through to read */
  5383. case CMD_FCP_IREAD64_CR:
  5384. /* FCP_CMD is always the 1st sgl entry */
  5385. wqe->fcp_iread.payload_len =
  5386. payload_len + sizeof(struct fcp_rsp);
  5387. /* word 4 (xfer length) should have been set on the memcpy */
  5388. bf_set(lpfc_wqe_gen_erp, &wqe->generic,
  5389. iocbq->iocb.ulpFCP2Rcvy);
  5390. bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
  5391. /* The XC bit and the XS bit are similar. The driver never
  5392. * tracked whether or not the exchange was previouslly open.
  5393. * XC = Exchange create, 0 is create. 1 is already open.
  5394. * XS = link cmd: 1 do not close the exchange after command.
  5395. * XS = 0 close exchange when command completes.
  5396. * The only time we would not set the XC bit is when the XS bit
  5397. * is set and we are sending our 2nd or greater command on
  5398. * this exchange.
  5399. */
  5400. /* Always open the exchange */
  5401. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  5402. wqe->words[10] &= 0xffff0000; /* zero out ebde count */
  5403. bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
  5404. break;
  5405. case CMD_FCP_ICMND64_CR:
  5406. /* Always open the exchange */
  5407. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  5408. wqe->words[4] = 0;
  5409. wqe->words[10] &= 0xffff0000; /* zero out ebde count */
  5410. bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
  5411. break;
  5412. case CMD_GEN_REQUEST64_CR:
  5413. /* word3 command length is described as byte offset to the
  5414. * rsp_data. Would always be 16, sizeof(struct sli4_sge)
  5415. * sgl[0] = cmnd
  5416. * sgl[1] = rsp.
  5417. *
  5418. */
  5419. wqe->gen_req.command_len = payload_len;
  5420. /* Word4 parameter copied in the memcpy */
  5421. /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
  5422. /* word6 context tag copied in memcpy */
  5423. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  5424. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  5425. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5426. "2015 Invalid CT %x command 0x%x\n",
  5427. ct, iocbq->iocb.ulpCommand);
  5428. return IOCB_ERROR;
  5429. }
  5430. bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
  5431. bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
  5432. iocbq->iocb.ulpTimeout);
  5433. bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
  5434. command_type = OTHER_COMMAND;
  5435. break;
  5436. case CMD_XMIT_ELS_RSP64_CX:
  5437. /* words0-2 BDE memcpy */
  5438. /* word3 iocb=iotag32 wqe=rsvd */
  5439. wqe->words[3] = 0;
  5440. /* word4 iocb=did wge=rsvd. */
  5441. wqe->words[4] = 0;
  5442. /* word5 iocb=rsvd wge=did */
  5443. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  5444. iocbq->iocb.un.elsreq64.remoteID);
  5445. bf_set(lpfc_wqe_gen_ct, &wqe->generic,
  5446. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  5447. bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
  5448. bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
  5449. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  5450. bf_set(lpfc_wqe_gen_context, &wqe->generic,
  5451. iocbq->vport->vpi + phba->vpi_base);
  5452. command_type = OTHER_COMMAND;
  5453. break;
  5454. case CMD_CLOSE_XRI_CN:
  5455. case CMD_ABORT_XRI_CN:
  5456. case CMD_ABORT_XRI_CX:
  5457. /* words 0-2 memcpy should be 0 rserved */
  5458. /* port will send abts */
  5459. if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  5460. /*
  5461. * The link is down so the fw does not need to send abts
  5462. * on the wire.
  5463. */
  5464. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  5465. else
  5466. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  5467. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  5468. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  5469. wqe->words[5] = 0;
  5470. bf_set(lpfc_wqe_gen_ct, &wqe->generic,
  5471. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  5472. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  5473. wqe->generic.abort_tag = abort_tag;
  5474. /*
  5475. * The abort handler will send us CMD_ABORT_XRI_CN or
  5476. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  5477. */
  5478. bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
  5479. cmnd = CMD_ABORT_XRI_CX;
  5480. command_type = OTHER_COMMAND;
  5481. xritag = 0;
  5482. break;
  5483. case CMD_XRI_ABORTED_CX:
  5484. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  5485. /* words0-2 are all 0's no bde */
  5486. /* word3 and word4 are rsvrd */
  5487. wqe->words[3] = 0;
  5488. wqe->words[4] = 0;
  5489. /* word5 iocb=rsvd wge=did */
  5490. /* There is no remote port id in the IOCB? */
  5491. /* Let this fall through and fail */
  5492. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  5493. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  5494. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  5495. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  5496. default:
  5497. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5498. "2014 Invalid command 0x%x\n",
  5499. iocbq->iocb.ulpCommand);
  5500. return IOCB_ERROR;
  5501. break;
  5502. }
  5503. bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
  5504. bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
  5505. wqe->generic.abort_tag = abort_tag;
  5506. bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
  5507. bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
  5508. bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
  5509. bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
  5510. return 0;
  5511. }
  5512. /**
  5513. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  5514. * @phba: Pointer to HBA context object.
  5515. * @ring_number: SLI ring number to issue iocb on.
  5516. * @piocb: Pointer to command iocb.
  5517. * @flag: Flag indicating if this command can be put into txq.
  5518. *
  5519. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  5520. * an iocb command to an HBA with SLI-4 interface spec.
  5521. *
  5522. * This function is called with hbalock held. The function will return success
  5523. * after it successfully submit the iocb to firmware or after adding to the
  5524. * txq.
  5525. **/
  5526. static int
  5527. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  5528. struct lpfc_iocbq *piocb, uint32_t flag)
  5529. {
  5530. struct lpfc_sglq *sglq;
  5531. uint16_t xritag;
  5532. union lpfc_wqe wqe;
  5533. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  5534. uint32_t fcp_wqidx;
  5535. if (piocb->sli4_xritag == NO_XRI) {
  5536. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  5537. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  5538. sglq = NULL;
  5539. else {
  5540. sglq = __lpfc_sli_get_sglq(phba);
  5541. if (!sglq)
  5542. return IOCB_ERROR;
  5543. piocb->sli4_xritag = sglq->sli4_xritag;
  5544. }
  5545. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  5546. sglq = NULL; /* These IO's already have an XRI and
  5547. * a mapped sgl.
  5548. */
  5549. } else {
  5550. /* This is a continuation of a commandi,(CX) so this
  5551. * sglq is on the active list
  5552. */
  5553. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  5554. if (!sglq)
  5555. return IOCB_ERROR;
  5556. }
  5557. if (sglq) {
  5558. xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
  5559. if (xritag != sglq->sli4_xritag)
  5560. return IOCB_ERROR;
  5561. }
  5562. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  5563. return IOCB_ERROR;
  5564. if (piocb->iocb_flag & LPFC_IO_FCP) {
  5565. fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  5566. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[fcp_wqidx], &wqe))
  5567. return IOCB_ERROR;
  5568. } else {
  5569. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  5570. return IOCB_ERROR;
  5571. }
  5572. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  5573. return 0;
  5574. }
  5575. /**
  5576. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  5577. *
  5578. * This routine wraps the actual lockless version for issusing IOCB function
  5579. * pointer from the lpfc_hba struct.
  5580. *
  5581. * Return codes:
  5582. * IOCB_ERROR - Error
  5583. * IOCB_SUCCESS - Success
  5584. * IOCB_BUSY - Busy
  5585. **/
  5586. static inline int
  5587. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  5588. struct lpfc_iocbq *piocb, uint32_t flag)
  5589. {
  5590. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  5591. }
  5592. /**
  5593. * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
  5594. * @phba: The hba struct for which this call is being executed.
  5595. * @dev_grp: The HBA PCI-Device group number.
  5596. *
  5597. * This routine sets up the SLI interface API function jump table in @phba
  5598. * struct.
  5599. * Returns: 0 - success, -ENODEV - failure.
  5600. **/
  5601. int
  5602. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  5603. {
  5604. switch (dev_grp) {
  5605. case LPFC_PCI_DEV_LP:
  5606. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  5607. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  5608. break;
  5609. case LPFC_PCI_DEV_OC:
  5610. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  5611. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  5612. break;
  5613. default:
  5614. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5615. "1419 Invalid HBA PCI-device group: 0x%x\n",
  5616. dev_grp);
  5617. return -ENODEV;
  5618. break;
  5619. }
  5620. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  5621. return 0;
  5622. }
  5623. /**
  5624. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  5625. * @phba: Pointer to HBA context object.
  5626. * @pring: Pointer to driver SLI ring object.
  5627. * @piocb: Pointer to command iocb.
  5628. * @flag: Flag indicating if this command can be put into txq.
  5629. *
  5630. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  5631. * function. This function gets the hbalock and calls
  5632. * __lpfc_sli_issue_iocb function and will return the error returned
  5633. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  5634. * functions which do not hold hbalock.
  5635. **/
  5636. int
  5637. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  5638. struct lpfc_iocbq *piocb, uint32_t flag)
  5639. {
  5640. unsigned long iflags;
  5641. int rc;
  5642. spin_lock_irqsave(&phba->hbalock, iflags);
  5643. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  5644. spin_unlock_irqrestore(&phba->hbalock, iflags);
  5645. return rc;
  5646. }
  5647. /**
  5648. * lpfc_extra_ring_setup - Extra ring setup function
  5649. * @phba: Pointer to HBA context object.
  5650. *
  5651. * This function is called while driver attaches with the
  5652. * HBA to setup the extra ring. The extra ring is used
  5653. * only when driver needs to support target mode functionality
  5654. * or IP over FC functionalities.
  5655. *
  5656. * This function is called with no lock held.
  5657. **/
  5658. static int
  5659. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  5660. {
  5661. struct lpfc_sli *psli;
  5662. struct lpfc_sli_ring *pring;
  5663. psli = &phba->sli;
  5664. /* Adjust cmd/rsp ring iocb entries more evenly */
  5665. /* Take some away from the FCP ring */
  5666. pring = &psli->ring[psli->fcp_ring];
  5667. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  5668. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  5669. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  5670. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  5671. /* and give them to the extra ring */
  5672. pring = &psli->ring[psli->extra_ring];
  5673. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  5674. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  5675. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  5676. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  5677. /* Setup default profile for this ring */
  5678. pring->iotag_max = 4096;
  5679. pring->num_mask = 1;
  5680. pring->prt[0].profile = 0; /* Mask 0 */
  5681. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  5682. pring->prt[0].type = phba->cfg_multi_ring_type;
  5683. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  5684. return 0;
  5685. }
  5686. /**
  5687. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  5688. * @phba: Pointer to HBA context object.
  5689. * @pring: Pointer to driver SLI ring object.
  5690. * @iocbq: Pointer to iocb object.
  5691. *
  5692. * This function is called by the slow ring event handler
  5693. * function when there is an ASYNC event iocb in the ring.
  5694. * This function is called with no lock held.
  5695. * Currently this function handles only temperature related
  5696. * ASYNC events. The function decodes the temperature sensor
  5697. * event message and posts events for the management applications.
  5698. **/
  5699. static void
  5700. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  5701. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  5702. {
  5703. IOCB_t *icmd;
  5704. uint16_t evt_code;
  5705. uint16_t temp;
  5706. struct temp_event temp_event_data;
  5707. struct Scsi_Host *shost;
  5708. uint32_t *iocb_w;
  5709. icmd = &iocbq->iocb;
  5710. evt_code = icmd->un.asyncstat.evt_code;
  5711. temp = icmd->ulpContext;
  5712. if ((evt_code != ASYNC_TEMP_WARN) &&
  5713. (evt_code != ASYNC_TEMP_SAFE)) {
  5714. iocb_w = (uint32_t *) icmd;
  5715. lpfc_printf_log(phba,
  5716. KERN_ERR,
  5717. LOG_SLI,
  5718. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  5719. " evt_code 0x%x \n"
  5720. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  5721. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  5722. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  5723. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  5724. pring->ringno,
  5725. icmd->un.asyncstat.evt_code,
  5726. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  5727. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  5728. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  5729. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  5730. return;
  5731. }
  5732. temp_event_data.data = (uint32_t)temp;
  5733. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  5734. if (evt_code == ASYNC_TEMP_WARN) {
  5735. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  5736. lpfc_printf_log(phba,
  5737. KERN_ERR,
  5738. LOG_TEMP,
  5739. "0347 Adapter is very hot, please take "
  5740. "corrective action. temperature : %d Celsius\n",
  5741. temp);
  5742. }
  5743. if (evt_code == ASYNC_TEMP_SAFE) {
  5744. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  5745. lpfc_printf_log(phba,
  5746. KERN_ERR,
  5747. LOG_TEMP,
  5748. "0340 Adapter temperature is OK now. "
  5749. "temperature : %d Celsius\n",
  5750. temp);
  5751. }
  5752. /* Send temperature change event to applications */
  5753. shost = lpfc_shost_from_vport(phba->pport);
  5754. fc_host_post_vendor_event(shost, fc_get_event_number(),
  5755. sizeof(temp_event_data), (char *) &temp_event_data,
  5756. LPFC_NL_VENDOR_ID);
  5757. }
  5758. /**
  5759. * lpfc_sli_setup - SLI ring setup function
  5760. * @phba: Pointer to HBA context object.
  5761. *
  5762. * lpfc_sli_setup sets up rings of the SLI interface with
  5763. * number of iocbs per ring and iotags. This function is
  5764. * called while driver attach to the HBA and before the
  5765. * interrupts are enabled. So there is no need for locking.
  5766. *
  5767. * This function always returns 0.
  5768. **/
  5769. int
  5770. lpfc_sli_setup(struct lpfc_hba *phba)
  5771. {
  5772. int i, totiocbsize = 0;
  5773. struct lpfc_sli *psli = &phba->sli;
  5774. struct lpfc_sli_ring *pring;
  5775. psli->num_rings = MAX_CONFIGURED_RINGS;
  5776. psli->sli_flag = 0;
  5777. psli->fcp_ring = LPFC_FCP_RING;
  5778. psli->next_ring = LPFC_FCP_NEXT_RING;
  5779. psli->extra_ring = LPFC_EXTRA_RING;
  5780. psli->iocbq_lookup = NULL;
  5781. psli->iocbq_lookup_len = 0;
  5782. psli->last_iotag = 0;
  5783. for (i = 0; i < psli->num_rings; i++) {
  5784. pring = &psli->ring[i];
  5785. switch (i) {
  5786. case LPFC_FCP_RING: /* ring 0 - FCP */
  5787. /* numCiocb and numRiocb are used in config_port */
  5788. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  5789. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  5790. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  5791. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  5792. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  5793. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  5794. pring->sizeCiocb = (phba->sli_rev == 3) ?
  5795. SLI3_IOCB_CMD_SIZE :
  5796. SLI2_IOCB_CMD_SIZE;
  5797. pring->sizeRiocb = (phba->sli_rev == 3) ?
  5798. SLI3_IOCB_RSP_SIZE :
  5799. SLI2_IOCB_RSP_SIZE;
  5800. pring->iotag_ctr = 0;
  5801. pring->iotag_max =
  5802. (phba->cfg_hba_queue_depth * 2);
  5803. pring->fast_iotag = pring->iotag_max;
  5804. pring->num_mask = 0;
  5805. break;
  5806. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  5807. /* numCiocb and numRiocb are used in config_port */
  5808. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  5809. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  5810. pring->sizeCiocb = (phba->sli_rev == 3) ?
  5811. SLI3_IOCB_CMD_SIZE :
  5812. SLI2_IOCB_CMD_SIZE;
  5813. pring->sizeRiocb = (phba->sli_rev == 3) ?
  5814. SLI3_IOCB_RSP_SIZE :
  5815. SLI2_IOCB_RSP_SIZE;
  5816. pring->iotag_max = phba->cfg_hba_queue_depth;
  5817. pring->num_mask = 0;
  5818. break;
  5819. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  5820. /* numCiocb and numRiocb are used in config_port */
  5821. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  5822. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  5823. pring->sizeCiocb = (phba->sli_rev == 3) ?
  5824. SLI3_IOCB_CMD_SIZE :
  5825. SLI2_IOCB_CMD_SIZE;
  5826. pring->sizeRiocb = (phba->sli_rev == 3) ?
  5827. SLI3_IOCB_RSP_SIZE :
  5828. SLI2_IOCB_RSP_SIZE;
  5829. pring->fast_iotag = 0;
  5830. pring->iotag_ctr = 0;
  5831. pring->iotag_max = 4096;
  5832. pring->lpfc_sli_rcv_async_status =
  5833. lpfc_sli_async_event_handler;
  5834. pring->num_mask = 4;
  5835. pring->prt[0].profile = 0; /* Mask 0 */
  5836. pring->prt[0].rctl = FC_ELS_REQ;
  5837. pring->prt[0].type = FC_ELS_DATA;
  5838. pring->prt[0].lpfc_sli_rcv_unsol_event =
  5839. lpfc_els_unsol_event;
  5840. pring->prt[1].profile = 0; /* Mask 1 */
  5841. pring->prt[1].rctl = FC_ELS_RSP;
  5842. pring->prt[1].type = FC_ELS_DATA;
  5843. pring->prt[1].lpfc_sli_rcv_unsol_event =
  5844. lpfc_els_unsol_event;
  5845. pring->prt[2].profile = 0; /* Mask 2 */
  5846. /* NameServer Inquiry */
  5847. pring->prt[2].rctl = FC_UNSOL_CTL;
  5848. /* NameServer */
  5849. pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
  5850. pring->prt[2].lpfc_sli_rcv_unsol_event =
  5851. lpfc_ct_unsol_event;
  5852. pring->prt[3].profile = 0; /* Mask 3 */
  5853. /* NameServer response */
  5854. pring->prt[3].rctl = FC_SOL_CTL;
  5855. /* NameServer */
  5856. pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
  5857. pring->prt[3].lpfc_sli_rcv_unsol_event =
  5858. lpfc_ct_unsol_event;
  5859. break;
  5860. }
  5861. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  5862. (pring->numRiocb * pring->sizeRiocb);
  5863. }
  5864. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  5865. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  5866. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  5867. "SLI2 SLIM Data: x%x x%lx\n",
  5868. phba->brd_no, totiocbsize,
  5869. (unsigned long) MAX_SLIM_IOCB_SIZE);
  5870. }
  5871. if (phba->cfg_multi_ring_support == 2)
  5872. lpfc_extra_ring_setup(phba);
  5873. return 0;
  5874. }
  5875. /**
  5876. * lpfc_sli_queue_setup - Queue initialization function
  5877. * @phba: Pointer to HBA context object.
  5878. *
  5879. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  5880. * ring. This function also initializes ring indices of each ring.
  5881. * This function is called during the initialization of the SLI
  5882. * interface of an HBA.
  5883. * This function is called with no lock held and always returns
  5884. * 1.
  5885. **/
  5886. int
  5887. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  5888. {
  5889. struct lpfc_sli *psli;
  5890. struct lpfc_sli_ring *pring;
  5891. int i;
  5892. psli = &phba->sli;
  5893. spin_lock_irq(&phba->hbalock);
  5894. INIT_LIST_HEAD(&psli->mboxq);
  5895. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  5896. /* Initialize list headers for txq and txcmplq as double linked lists */
  5897. for (i = 0; i < psli->num_rings; i++) {
  5898. pring = &psli->ring[i];
  5899. pring->ringno = i;
  5900. pring->next_cmdidx = 0;
  5901. pring->local_getidx = 0;
  5902. pring->cmdidx = 0;
  5903. INIT_LIST_HEAD(&pring->txq);
  5904. INIT_LIST_HEAD(&pring->txcmplq);
  5905. INIT_LIST_HEAD(&pring->iocb_continueq);
  5906. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  5907. INIT_LIST_HEAD(&pring->postbufq);
  5908. }
  5909. spin_unlock_irq(&phba->hbalock);
  5910. return 1;
  5911. }
  5912. /**
  5913. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  5914. * @phba: Pointer to HBA context object.
  5915. *
  5916. * This routine flushes the mailbox command subsystem. It will unconditionally
  5917. * flush all the mailbox commands in the three possible stages in the mailbox
  5918. * command sub-system: pending mailbox command queue; the outstanding mailbox
  5919. * command; and completed mailbox command queue. It is caller's responsibility
  5920. * to make sure that the driver is in the proper state to flush the mailbox
  5921. * command sub-system. Namely, the posting of mailbox commands into the
  5922. * pending mailbox command queue from the various clients must be stopped;
  5923. * either the HBA is in a state that it will never works on the outstanding
  5924. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  5925. * mailbox command has been completed.
  5926. **/
  5927. static void
  5928. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  5929. {
  5930. LIST_HEAD(completions);
  5931. struct lpfc_sli *psli = &phba->sli;
  5932. LPFC_MBOXQ_t *pmb;
  5933. unsigned long iflag;
  5934. /* Flush all the mailbox commands in the mbox system */
  5935. spin_lock_irqsave(&phba->hbalock, iflag);
  5936. /* The pending mailbox command queue */
  5937. list_splice_init(&phba->sli.mboxq, &completions);
  5938. /* The outstanding active mailbox command */
  5939. if (psli->mbox_active) {
  5940. list_add_tail(&psli->mbox_active->list, &completions);
  5941. psli->mbox_active = NULL;
  5942. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5943. }
  5944. /* The completed mailbox command queue */
  5945. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  5946. spin_unlock_irqrestore(&phba->hbalock, iflag);
  5947. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  5948. while (!list_empty(&completions)) {
  5949. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  5950. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  5951. if (pmb->mbox_cmpl)
  5952. pmb->mbox_cmpl(phba, pmb);
  5953. }
  5954. }
  5955. /**
  5956. * lpfc_sli_host_down - Vport cleanup function
  5957. * @vport: Pointer to virtual port object.
  5958. *
  5959. * lpfc_sli_host_down is called to clean up the resources
  5960. * associated with a vport before destroying virtual
  5961. * port data structures.
  5962. * This function does following operations:
  5963. * - Free discovery resources associated with this virtual
  5964. * port.
  5965. * - Free iocbs associated with this virtual port in
  5966. * the txq.
  5967. * - Send abort for all iocb commands associated with this
  5968. * vport in txcmplq.
  5969. *
  5970. * This function is called with no lock held and always returns 1.
  5971. **/
  5972. int
  5973. lpfc_sli_host_down(struct lpfc_vport *vport)
  5974. {
  5975. LIST_HEAD(completions);
  5976. struct lpfc_hba *phba = vport->phba;
  5977. struct lpfc_sli *psli = &phba->sli;
  5978. struct lpfc_sli_ring *pring;
  5979. struct lpfc_iocbq *iocb, *next_iocb;
  5980. int i;
  5981. unsigned long flags = 0;
  5982. uint16_t prev_pring_flag;
  5983. lpfc_cleanup_discovery_resources(vport);
  5984. spin_lock_irqsave(&phba->hbalock, flags);
  5985. for (i = 0; i < psli->num_rings; i++) {
  5986. pring = &psli->ring[i];
  5987. prev_pring_flag = pring->flag;
  5988. /* Only slow rings */
  5989. if (pring->ringno == LPFC_ELS_RING) {
  5990. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  5991. /* Set the lpfc data pending flag */
  5992. set_bit(LPFC_DATA_READY, &phba->data_flags);
  5993. }
  5994. /*
  5995. * Error everything on the txq since these iocbs have not been
  5996. * given to the FW yet.
  5997. */
  5998. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  5999. if (iocb->vport != vport)
  6000. continue;
  6001. list_move_tail(&iocb->list, &completions);
  6002. pring->txq_cnt--;
  6003. }
  6004. /* Next issue ABTS for everything on the txcmplq */
  6005. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  6006. list) {
  6007. if (iocb->vport != vport)
  6008. continue;
  6009. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  6010. }
  6011. pring->flag = prev_pring_flag;
  6012. }
  6013. spin_unlock_irqrestore(&phba->hbalock, flags);
  6014. /* Cancel all the IOCBs from the completions list */
  6015. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  6016. IOERR_SLI_DOWN);
  6017. return 1;
  6018. }
  6019. /**
  6020. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  6021. * @phba: Pointer to HBA context object.
  6022. *
  6023. * This function cleans up all iocb, buffers, mailbox commands
  6024. * while shutting down the HBA. This function is called with no
  6025. * lock held and always returns 1.
  6026. * This function does the following to cleanup driver resources:
  6027. * - Free discovery resources for each virtual port
  6028. * - Cleanup any pending fabric iocbs
  6029. * - Iterate through the iocb txq and free each entry
  6030. * in the list.
  6031. * - Free up any buffer posted to the HBA
  6032. * - Free mailbox commands in the mailbox queue.
  6033. **/
  6034. int
  6035. lpfc_sli_hba_down(struct lpfc_hba *phba)
  6036. {
  6037. LIST_HEAD(completions);
  6038. struct lpfc_sli *psli = &phba->sli;
  6039. struct lpfc_sli_ring *pring;
  6040. struct lpfc_dmabuf *buf_ptr;
  6041. unsigned long flags = 0;
  6042. int i;
  6043. /* Shutdown the mailbox command sub-system */
  6044. lpfc_sli_mbox_sys_shutdown(phba);
  6045. lpfc_hba_down_prep(phba);
  6046. lpfc_fabric_abort_hba(phba);
  6047. spin_lock_irqsave(&phba->hbalock, flags);
  6048. for (i = 0; i < psli->num_rings; i++) {
  6049. pring = &psli->ring[i];
  6050. /* Only slow rings */
  6051. if (pring->ringno == LPFC_ELS_RING) {
  6052. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  6053. /* Set the lpfc data pending flag */
  6054. set_bit(LPFC_DATA_READY, &phba->data_flags);
  6055. }
  6056. /*
  6057. * Error everything on the txq since these iocbs have not been
  6058. * given to the FW yet.
  6059. */
  6060. list_splice_init(&pring->txq, &completions);
  6061. pring->txq_cnt = 0;
  6062. }
  6063. spin_unlock_irqrestore(&phba->hbalock, flags);
  6064. /* Cancel all the IOCBs from the completions list */
  6065. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  6066. IOERR_SLI_DOWN);
  6067. spin_lock_irqsave(&phba->hbalock, flags);
  6068. list_splice_init(&phba->elsbuf, &completions);
  6069. phba->elsbuf_cnt = 0;
  6070. phba->elsbuf_prev_cnt = 0;
  6071. spin_unlock_irqrestore(&phba->hbalock, flags);
  6072. while (!list_empty(&completions)) {
  6073. list_remove_head(&completions, buf_ptr,
  6074. struct lpfc_dmabuf, list);
  6075. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  6076. kfree(buf_ptr);
  6077. }
  6078. /* Return any active mbox cmds */
  6079. del_timer_sync(&psli->mbox_tmo);
  6080. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  6081. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  6082. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  6083. return 1;
  6084. }
  6085. /**
  6086. * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
  6087. * @phba: Pointer to HBA context object.
  6088. *
  6089. * This function cleans up all queues, iocb, buffers, mailbox commands while
  6090. * shutting down the SLI4 HBA FCoE function. This function is called with no
  6091. * lock held and always returns 1.
  6092. *
  6093. * This function does the following to cleanup driver FCoE function resources:
  6094. * - Free discovery resources for each virtual port
  6095. * - Cleanup any pending fabric iocbs
  6096. * - Iterate through the iocb txq and free each entry in the list.
  6097. * - Free up any buffer posted to the HBA.
  6098. * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
  6099. * - Free mailbox commands in the mailbox queue.
  6100. **/
  6101. int
  6102. lpfc_sli4_hba_down(struct lpfc_hba *phba)
  6103. {
  6104. /* Stop the SLI4 device port */
  6105. lpfc_stop_port(phba);
  6106. /* Tear down the queues in the HBA */
  6107. lpfc_sli4_queue_unset(phba);
  6108. /* unregister default FCFI from the HBA */
  6109. lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
  6110. return 1;
  6111. }
  6112. /**
  6113. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  6114. * @srcp: Source memory pointer.
  6115. * @destp: Destination memory pointer.
  6116. * @cnt: Number of words required to be copied.
  6117. *
  6118. * This function is used for copying data between driver memory
  6119. * and the SLI memory. This function also changes the endianness
  6120. * of each word if native endianness is different from SLI
  6121. * endianness. This function can be called with or without
  6122. * lock.
  6123. **/
  6124. void
  6125. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  6126. {
  6127. uint32_t *src = srcp;
  6128. uint32_t *dest = destp;
  6129. uint32_t ldata;
  6130. int i;
  6131. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  6132. ldata = *src;
  6133. ldata = le32_to_cpu(ldata);
  6134. *dest = ldata;
  6135. src++;
  6136. dest++;
  6137. }
  6138. }
  6139. /**
  6140. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  6141. * @phba: Pointer to HBA context object.
  6142. * @pring: Pointer to driver SLI ring object.
  6143. * @mp: Pointer to driver buffer object.
  6144. *
  6145. * This function is called with no lock held.
  6146. * It always return zero after adding the buffer to the postbufq
  6147. * buffer list.
  6148. **/
  6149. int
  6150. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6151. struct lpfc_dmabuf *mp)
  6152. {
  6153. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  6154. later */
  6155. spin_lock_irq(&phba->hbalock);
  6156. list_add_tail(&mp->list, &pring->postbufq);
  6157. pring->postbufq_cnt++;
  6158. spin_unlock_irq(&phba->hbalock);
  6159. return 0;
  6160. }
  6161. /**
  6162. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  6163. * @phba: Pointer to HBA context object.
  6164. *
  6165. * When HBQ is enabled, buffers are searched based on tags. This function
  6166. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  6167. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  6168. * does not conflict with tags of buffer posted for unsolicited events.
  6169. * The function returns the allocated tag. The function is called with
  6170. * no locks held.
  6171. **/
  6172. uint32_t
  6173. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  6174. {
  6175. spin_lock_irq(&phba->hbalock);
  6176. phba->buffer_tag_count++;
  6177. /*
  6178. * Always set the QUE_BUFTAG_BIT to distiguish between
  6179. * a tag assigned by HBQ.
  6180. */
  6181. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  6182. spin_unlock_irq(&phba->hbalock);
  6183. return phba->buffer_tag_count;
  6184. }
  6185. /**
  6186. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  6187. * @phba: Pointer to HBA context object.
  6188. * @pring: Pointer to driver SLI ring object.
  6189. * @tag: Buffer tag.
  6190. *
  6191. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  6192. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  6193. * iocb is posted to the response ring with the tag of the buffer.
  6194. * This function searches the pring->postbufq list using the tag
  6195. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  6196. * iocb. If the buffer is found then lpfc_dmabuf object of the
  6197. * buffer is returned to the caller else NULL is returned.
  6198. * This function is called with no lock held.
  6199. **/
  6200. struct lpfc_dmabuf *
  6201. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6202. uint32_t tag)
  6203. {
  6204. struct lpfc_dmabuf *mp, *next_mp;
  6205. struct list_head *slp = &pring->postbufq;
  6206. /* Search postbufq, from the begining, looking for a match on tag */
  6207. spin_lock_irq(&phba->hbalock);
  6208. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  6209. if (mp->buffer_tag == tag) {
  6210. list_del_init(&mp->list);
  6211. pring->postbufq_cnt--;
  6212. spin_unlock_irq(&phba->hbalock);
  6213. return mp;
  6214. }
  6215. }
  6216. spin_unlock_irq(&phba->hbalock);
  6217. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6218. "0402 Cannot find virtual addr for buffer tag on "
  6219. "ring %d Data x%lx x%p x%p x%x\n",
  6220. pring->ringno, (unsigned long) tag,
  6221. slp->next, slp->prev, pring->postbufq_cnt);
  6222. return NULL;
  6223. }
  6224. /**
  6225. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  6226. * @phba: Pointer to HBA context object.
  6227. * @pring: Pointer to driver SLI ring object.
  6228. * @phys: DMA address of the buffer.
  6229. *
  6230. * This function searches the buffer list using the dma_address
  6231. * of unsolicited event to find the driver's lpfc_dmabuf object
  6232. * corresponding to the dma_address. The function returns the
  6233. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  6234. * This function is called by the ct and els unsolicited event
  6235. * handlers to get the buffer associated with the unsolicited
  6236. * event.
  6237. *
  6238. * This function is called with no lock held.
  6239. **/
  6240. struct lpfc_dmabuf *
  6241. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6242. dma_addr_t phys)
  6243. {
  6244. struct lpfc_dmabuf *mp, *next_mp;
  6245. struct list_head *slp = &pring->postbufq;
  6246. /* Search postbufq, from the begining, looking for a match on phys */
  6247. spin_lock_irq(&phba->hbalock);
  6248. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  6249. if (mp->phys == phys) {
  6250. list_del_init(&mp->list);
  6251. pring->postbufq_cnt--;
  6252. spin_unlock_irq(&phba->hbalock);
  6253. return mp;
  6254. }
  6255. }
  6256. spin_unlock_irq(&phba->hbalock);
  6257. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6258. "0410 Cannot find virtual addr for mapped buf on "
  6259. "ring %d Data x%llx x%p x%p x%x\n",
  6260. pring->ringno, (unsigned long long)phys,
  6261. slp->next, slp->prev, pring->postbufq_cnt);
  6262. return NULL;
  6263. }
  6264. /**
  6265. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  6266. * @phba: Pointer to HBA context object.
  6267. * @cmdiocb: Pointer to driver command iocb object.
  6268. * @rspiocb: Pointer to driver response iocb object.
  6269. *
  6270. * This function is the completion handler for the abort iocbs for
  6271. * ELS commands. This function is called from the ELS ring event
  6272. * handler with no lock held. This function frees memory resources
  6273. * associated with the abort iocb.
  6274. **/
  6275. static void
  6276. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  6277. struct lpfc_iocbq *rspiocb)
  6278. {
  6279. IOCB_t *irsp = &rspiocb->iocb;
  6280. uint16_t abort_iotag, abort_context;
  6281. struct lpfc_iocbq *abort_iocb;
  6282. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  6283. abort_iocb = NULL;
  6284. if (irsp->ulpStatus) {
  6285. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  6286. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  6287. spin_lock_irq(&phba->hbalock);
  6288. if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
  6289. abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
  6290. lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
  6291. "0327 Cannot abort els iocb %p "
  6292. "with tag %x context %x, abort status %x, "
  6293. "abort code %x\n",
  6294. abort_iocb, abort_iotag, abort_context,
  6295. irsp->ulpStatus, irsp->un.ulpWord[4]);
  6296. /*
  6297. * If the iocb is not found in Firmware queue the iocb
  6298. * might have completed already. Do not free it again.
  6299. */
  6300. if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  6301. spin_unlock_irq(&phba->hbalock);
  6302. lpfc_sli_release_iocbq(phba, cmdiocb);
  6303. return;
  6304. }
  6305. /*
  6306. * make sure we have the right iocbq before taking it
  6307. * off the txcmplq and try to call completion routine.
  6308. */
  6309. if (!abort_iocb ||
  6310. abort_iocb->iocb.ulpContext != abort_context ||
  6311. (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
  6312. spin_unlock_irq(&phba->hbalock);
  6313. else {
  6314. list_del_init(&abort_iocb->list);
  6315. pring->txcmplq_cnt--;
  6316. spin_unlock_irq(&phba->hbalock);
  6317. /* Firmware could still be in progress of DMAing
  6318. * payload, so don't free data buffer till after
  6319. * a hbeat.
  6320. */
  6321. abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
  6322. abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  6323. abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  6324. abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
  6325. (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
  6326. }
  6327. }
  6328. lpfc_sli_release_iocbq(phba, cmdiocb);
  6329. return;
  6330. }
  6331. /**
  6332. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  6333. * @phba: Pointer to HBA context object.
  6334. * @cmdiocb: Pointer to driver command iocb object.
  6335. * @rspiocb: Pointer to driver response iocb object.
  6336. *
  6337. * The function is called from SLI ring event handler with no
  6338. * lock held. This function is the completion handler for ELS commands
  6339. * which are aborted. The function frees memory resources used for
  6340. * the aborted ELS commands.
  6341. **/
  6342. static void
  6343. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  6344. struct lpfc_iocbq *rspiocb)
  6345. {
  6346. IOCB_t *irsp = &rspiocb->iocb;
  6347. /* ELS cmd tag <ulpIoTag> completes */
  6348. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  6349. "0139 Ignoring ELS cmd tag x%x completion Data: "
  6350. "x%x x%x x%x\n",
  6351. irsp->ulpIoTag, irsp->ulpStatus,
  6352. irsp->un.ulpWord[4], irsp->ulpTimeout);
  6353. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  6354. lpfc_ct_free_iocb(phba, cmdiocb);
  6355. else
  6356. lpfc_els_free_iocb(phba, cmdiocb);
  6357. return;
  6358. }
  6359. /**
  6360. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  6361. * @phba: Pointer to HBA context object.
  6362. * @pring: Pointer to driver SLI ring object.
  6363. * @cmdiocb: Pointer to driver command iocb object.
  6364. *
  6365. * This function issues an abort iocb for the provided command
  6366. * iocb. This function is called with hbalock held.
  6367. * The function returns 0 when it fails due to memory allocation
  6368. * failure or when the command iocb is an abort request.
  6369. **/
  6370. int
  6371. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6372. struct lpfc_iocbq *cmdiocb)
  6373. {
  6374. struct lpfc_vport *vport = cmdiocb->vport;
  6375. struct lpfc_iocbq *abtsiocbp;
  6376. IOCB_t *icmd = NULL;
  6377. IOCB_t *iabt = NULL;
  6378. int retval = IOCB_ERROR;
  6379. /*
  6380. * There are certain command types we don't want to abort. And we
  6381. * don't want to abort commands that are already in the process of
  6382. * being aborted.
  6383. */
  6384. icmd = &cmdiocb->iocb;
  6385. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  6386. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  6387. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  6388. return 0;
  6389. /* If we're unloading, don't abort iocb on the ELS ring, but change the
  6390. * callback so that nothing happens when it finishes.
  6391. */
  6392. if ((vport->load_flag & FC_UNLOADING) &&
  6393. (pring->ringno == LPFC_ELS_RING)) {
  6394. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  6395. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  6396. else
  6397. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  6398. goto abort_iotag_exit;
  6399. }
  6400. /* issue ABTS for this IOCB based on iotag */
  6401. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  6402. if (abtsiocbp == NULL)
  6403. return 0;
  6404. /* This signals the response to set the correct status
  6405. * before calling the completion handler.
  6406. */
  6407. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  6408. iabt = &abtsiocbp->iocb;
  6409. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  6410. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  6411. if (phba->sli_rev == LPFC_SLI_REV4)
  6412. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  6413. else
  6414. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  6415. iabt->ulpLe = 1;
  6416. iabt->ulpClass = icmd->ulpClass;
  6417. if (phba->link_state >= LPFC_LINK_UP)
  6418. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  6419. else
  6420. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  6421. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  6422. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  6423. "0339 Abort xri x%x, original iotag x%x, "
  6424. "abort cmd iotag x%x\n",
  6425. iabt->un.acxri.abortContextTag,
  6426. iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
  6427. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  6428. if (retval)
  6429. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  6430. abort_iotag_exit:
  6431. /*
  6432. * Caller to this routine should check for IOCB_ERROR
  6433. * and handle it properly. This routine no longer removes
  6434. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  6435. */
  6436. return retval;
  6437. }
  6438. /**
  6439. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  6440. * @iocbq: Pointer to driver iocb object.
  6441. * @vport: Pointer to driver virtual port object.
  6442. * @tgt_id: SCSI ID of the target.
  6443. * @lun_id: LUN ID of the scsi device.
  6444. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  6445. *
  6446. * This function acts as an iocb filter for functions which abort or count
  6447. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  6448. * 0 if the filtering criteria is met for the given iocb and will return
  6449. * 1 if the filtering criteria is not met.
  6450. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  6451. * given iocb is for the SCSI device specified by vport, tgt_id and
  6452. * lun_id parameter.
  6453. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  6454. * given iocb is for the SCSI target specified by vport and tgt_id
  6455. * parameters.
  6456. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  6457. * given iocb is for the SCSI host associated with the given vport.
  6458. * This function is called with no locks held.
  6459. **/
  6460. static int
  6461. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  6462. uint16_t tgt_id, uint64_t lun_id,
  6463. lpfc_ctx_cmd ctx_cmd)
  6464. {
  6465. struct lpfc_scsi_buf *lpfc_cmd;
  6466. int rc = 1;
  6467. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  6468. return rc;
  6469. if (iocbq->vport != vport)
  6470. return rc;
  6471. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  6472. if (lpfc_cmd->pCmd == NULL)
  6473. return rc;
  6474. switch (ctx_cmd) {
  6475. case LPFC_CTX_LUN:
  6476. if ((lpfc_cmd->rdata->pnode) &&
  6477. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  6478. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  6479. rc = 0;
  6480. break;
  6481. case LPFC_CTX_TGT:
  6482. if ((lpfc_cmd->rdata->pnode) &&
  6483. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  6484. rc = 0;
  6485. break;
  6486. case LPFC_CTX_HOST:
  6487. rc = 0;
  6488. break;
  6489. default:
  6490. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  6491. __func__, ctx_cmd);
  6492. break;
  6493. }
  6494. return rc;
  6495. }
  6496. /**
  6497. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  6498. * @vport: Pointer to virtual port.
  6499. * @tgt_id: SCSI ID of the target.
  6500. * @lun_id: LUN ID of the scsi device.
  6501. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  6502. *
  6503. * This function returns number of FCP commands pending for the vport.
  6504. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  6505. * commands pending on the vport associated with SCSI device specified
  6506. * by tgt_id and lun_id parameters.
  6507. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  6508. * commands pending on the vport associated with SCSI target specified
  6509. * by tgt_id parameter.
  6510. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  6511. * commands pending on the vport.
  6512. * This function returns the number of iocbs which satisfy the filter.
  6513. * This function is called without any lock held.
  6514. **/
  6515. int
  6516. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  6517. lpfc_ctx_cmd ctx_cmd)
  6518. {
  6519. struct lpfc_hba *phba = vport->phba;
  6520. struct lpfc_iocbq *iocbq;
  6521. int sum, i;
  6522. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  6523. iocbq = phba->sli.iocbq_lookup[i];
  6524. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  6525. ctx_cmd) == 0)
  6526. sum++;
  6527. }
  6528. return sum;
  6529. }
  6530. /**
  6531. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  6532. * @phba: Pointer to HBA context object
  6533. * @cmdiocb: Pointer to command iocb object.
  6534. * @rspiocb: Pointer to response iocb object.
  6535. *
  6536. * This function is called when an aborted FCP iocb completes. This
  6537. * function is called by the ring event handler with no lock held.
  6538. * This function frees the iocb.
  6539. **/
  6540. void
  6541. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  6542. struct lpfc_iocbq *rspiocb)
  6543. {
  6544. lpfc_sli_release_iocbq(phba, cmdiocb);
  6545. return;
  6546. }
  6547. /**
  6548. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  6549. * @vport: Pointer to virtual port.
  6550. * @pring: Pointer to driver SLI ring object.
  6551. * @tgt_id: SCSI ID of the target.
  6552. * @lun_id: LUN ID of the scsi device.
  6553. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  6554. *
  6555. * This function sends an abort command for every SCSI command
  6556. * associated with the given virtual port pending on the ring
  6557. * filtered by lpfc_sli_validate_fcp_iocb function.
  6558. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  6559. * FCP iocbs associated with lun specified by tgt_id and lun_id
  6560. * parameters
  6561. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  6562. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  6563. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  6564. * FCP iocbs associated with virtual port.
  6565. * This function returns number of iocbs it failed to abort.
  6566. * This function is called with no locks held.
  6567. **/
  6568. int
  6569. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  6570. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  6571. {
  6572. struct lpfc_hba *phba = vport->phba;
  6573. struct lpfc_iocbq *iocbq;
  6574. struct lpfc_iocbq *abtsiocb;
  6575. IOCB_t *cmd = NULL;
  6576. int errcnt = 0, ret_val = 0;
  6577. int i;
  6578. for (i = 1; i <= phba->sli.last_iotag; i++) {
  6579. iocbq = phba->sli.iocbq_lookup[i];
  6580. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  6581. abort_cmd) != 0)
  6582. continue;
  6583. /* issue ABTS for this IOCB based on iotag */
  6584. abtsiocb = lpfc_sli_get_iocbq(phba);
  6585. if (abtsiocb == NULL) {
  6586. errcnt++;
  6587. continue;
  6588. }
  6589. cmd = &iocbq->iocb;
  6590. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  6591. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  6592. if (phba->sli_rev == LPFC_SLI_REV4)
  6593. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  6594. else
  6595. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  6596. abtsiocb->iocb.ulpLe = 1;
  6597. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  6598. abtsiocb->vport = phba->pport;
  6599. if (lpfc_is_link_up(phba))
  6600. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  6601. else
  6602. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  6603. /* Setup callback routine and issue the command. */
  6604. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  6605. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  6606. abtsiocb, 0);
  6607. if (ret_val == IOCB_ERROR) {
  6608. lpfc_sli_release_iocbq(phba, abtsiocb);
  6609. errcnt++;
  6610. continue;
  6611. }
  6612. }
  6613. return errcnt;
  6614. }
  6615. /**
  6616. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  6617. * @phba: Pointer to HBA context object.
  6618. * @cmdiocbq: Pointer to command iocb.
  6619. * @rspiocbq: Pointer to response iocb.
  6620. *
  6621. * This function is the completion handler for iocbs issued using
  6622. * lpfc_sli_issue_iocb_wait function. This function is called by the
  6623. * ring event handler function without any lock held. This function
  6624. * can be called from both worker thread context and interrupt
  6625. * context. This function also can be called from other thread which
  6626. * cleans up the SLI layer objects.
  6627. * This function copy the contents of the response iocb to the
  6628. * response iocb memory object provided by the caller of
  6629. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  6630. * sleeps for the iocb completion.
  6631. **/
  6632. static void
  6633. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  6634. struct lpfc_iocbq *cmdiocbq,
  6635. struct lpfc_iocbq *rspiocbq)
  6636. {
  6637. wait_queue_head_t *pdone_q;
  6638. unsigned long iflags;
  6639. spin_lock_irqsave(&phba->hbalock, iflags);
  6640. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  6641. if (cmdiocbq->context2 && rspiocbq)
  6642. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  6643. &rspiocbq->iocb, sizeof(IOCB_t));
  6644. pdone_q = cmdiocbq->context_un.wait_queue;
  6645. if (pdone_q)
  6646. wake_up(pdone_q);
  6647. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6648. return;
  6649. }
  6650. /**
  6651. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  6652. * @phba: Pointer to HBA context object..
  6653. * @piocbq: Pointer to command iocb.
  6654. * @flag: Flag to test.
  6655. *
  6656. * This routine grabs the hbalock and then test the iocb_flag to
  6657. * see if the passed in flag is set.
  6658. * Returns:
  6659. * 1 if flag is set.
  6660. * 0 if flag is not set.
  6661. **/
  6662. static int
  6663. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  6664. struct lpfc_iocbq *piocbq, uint32_t flag)
  6665. {
  6666. unsigned long iflags;
  6667. int ret;
  6668. spin_lock_irqsave(&phba->hbalock, iflags);
  6669. ret = piocbq->iocb_flag & flag;
  6670. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6671. return ret;
  6672. }
  6673. /**
  6674. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  6675. * @phba: Pointer to HBA context object..
  6676. * @pring: Pointer to sli ring.
  6677. * @piocb: Pointer to command iocb.
  6678. * @prspiocbq: Pointer to response iocb.
  6679. * @timeout: Timeout in number of seconds.
  6680. *
  6681. * This function issues the iocb to firmware and waits for the
  6682. * iocb to complete. If the iocb command is not
  6683. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  6684. * Caller should not free the iocb resources if this function
  6685. * returns IOCB_TIMEDOUT.
  6686. * The function waits for the iocb completion using an
  6687. * non-interruptible wait.
  6688. * This function will sleep while waiting for iocb completion.
  6689. * So, this function should not be called from any context which
  6690. * does not allow sleeping. Due to the same reason, this function
  6691. * cannot be called with interrupt disabled.
  6692. * This function assumes that the iocb completions occur while
  6693. * this function sleep. So, this function cannot be called from
  6694. * the thread which process iocb completion for this ring.
  6695. * This function clears the iocb_flag of the iocb object before
  6696. * issuing the iocb and the iocb completion handler sets this
  6697. * flag and wakes this thread when the iocb completes.
  6698. * The contents of the response iocb will be copied to prspiocbq
  6699. * by the completion handler when the command completes.
  6700. * This function returns IOCB_SUCCESS when success.
  6701. * This function is called with no lock held.
  6702. **/
  6703. int
  6704. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  6705. uint32_t ring_number,
  6706. struct lpfc_iocbq *piocb,
  6707. struct lpfc_iocbq *prspiocbq,
  6708. uint32_t timeout)
  6709. {
  6710. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  6711. long timeleft, timeout_req = 0;
  6712. int retval = IOCB_SUCCESS;
  6713. uint32_t creg_val;
  6714. /*
  6715. * If the caller has provided a response iocbq buffer, then context2
  6716. * is NULL or its an error.
  6717. */
  6718. if (prspiocbq) {
  6719. if (piocb->context2)
  6720. return IOCB_ERROR;
  6721. piocb->context2 = prspiocbq;
  6722. }
  6723. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  6724. piocb->context_un.wait_queue = &done_q;
  6725. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  6726. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  6727. creg_val = readl(phba->HCregaddr);
  6728. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  6729. writel(creg_val, phba->HCregaddr);
  6730. readl(phba->HCregaddr); /* flush */
  6731. }
  6732. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
  6733. if (retval == IOCB_SUCCESS) {
  6734. timeout_req = timeout * HZ;
  6735. timeleft = wait_event_timeout(done_q,
  6736. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  6737. timeout_req);
  6738. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  6739. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  6740. "0331 IOCB wake signaled\n");
  6741. } else if (timeleft == 0) {
  6742. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6743. "0338 IOCB wait timeout error - no "
  6744. "wake response Data x%x\n", timeout);
  6745. retval = IOCB_TIMEDOUT;
  6746. } else {
  6747. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6748. "0330 IOCB wake NOT set, "
  6749. "Data x%x x%lx\n",
  6750. timeout, (timeleft / jiffies));
  6751. retval = IOCB_TIMEDOUT;
  6752. }
  6753. } else {
  6754. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  6755. "0332 IOCB wait issue failed, Data x%x\n",
  6756. retval);
  6757. retval = IOCB_ERROR;
  6758. }
  6759. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  6760. creg_val = readl(phba->HCregaddr);
  6761. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  6762. writel(creg_val, phba->HCregaddr);
  6763. readl(phba->HCregaddr); /* flush */
  6764. }
  6765. if (prspiocbq)
  6766. piocb->context2 = NULL;
  6767. piocb->context_un.wait_queue = NULL;
  6768. piocb->iocb_cmpl = NULL;
  6769. return retval;
  6770. }
  6771. /**
  6772. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  6773. * @phba: Pointer to HBA context object.
  6774. * @pmboxq: Pointer to driver mailbox object.
  6775. * @timeout: Timeout in number of seconds.
  6776. *
  6777. * This function issues the mailbox to firmware and waits for the
  6778. * mailbox command to complete. If the mailbox command is not
  6779. * completed within timeout seconds, it returns MBX_TIMEOUT.
  6780. * The function waits for the mailbox completion using an
  6781. * interruptible wait. If the thread is woken up due to a
  6782. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  6783. * should not free the mailbox resources, if this function returns
  6784. * MBX_TIMEOUT.
  6785. * This function will sleep while waiting for mailbox completion.
  6786. * So, this function should not be called from any context which
  6787. * does not allow sleeping. Due to the same reason, this function
  6788. * cannot be called with interrupt disabled.
  6789. * This function assumes that the mailbox completion occurs while
  6790. * this function sleep. So, this function cannot be called from
  6791. * the worker thread which processes mailbox completion.
  6792. * This function is called in the context of HBA management
  6793. * applications.
  6794. * This function returns MBX_SUCCESS when successful.
  6795. * This function is called with no lock held.
  6796. **/
  6797. int
  6798. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  6799. uint32_t timeout)
  6800. {
  6801. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  6802. int retval;
  6803. unsigned long flag;
  6804. /* The caller must leave context1 empty. */
  6805. if (pmboxq->context1)
  6806. return MBX_NOT_FINISHED;
  6807. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  6808. /* setup wake call as IOCB callback */
  6809. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  6810. /* setup context field to pass wait_queue pointer to wake function */
  6811. pmboxq->context1 = &done_q;
  6812. /* now issue the command */
  6813. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  6814. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  6815. wait_event_interruptible_timeout(done_q,
  6816. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  6817. timeout * HZ);
  6818. spin_lock_irqsave(&phba->hbalock, flag);
  6819. pmboxq->context1 = NULL;
  6820. /*
  6821. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  6822. * else do not free the resources.
  6823. */
  6824. if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
  6825. retval = MBX_SUCCESS;
  6826. else {
  6827. retval = MBX_TIMEOUT;
  6828. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  6829. }
  6830. spin_unlock_irqrestore(&phba->hbalock, flag);
  6831. }
  6832. return retval;
  6833. }
  6834. /**
  6835. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  6836. * @phba: Pointer to HBA context.
  6837. *
  6838. * This function is called to shutdown the driver's mailbox sub-system.
  6839. * It first marks the mailbox sub-system is in a block state to prevent
  6840. * the asynchronous mailbox command from issued off the pending mailbox
  6841. * command queue. If the mailbox command sub-system shutdown is due to
  6842. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  6843. * the mailbox sub-system flush routine to forcefully bring down the
  6844. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  6845. * as with offline or HBA function reset), this routine will wait for the
  6846. * outstanding mailbox command to complete before invoking the mailbox
  6847. * sub-system flush routine to gracefully bring down mailbox sub-system.
  6848. **/
  6849. void
  6850. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  6851. {
  6852. struct lpfc_sli *psli = &phba->sli;
  6853. uint8_t actcmd = MBX_HEARTBEAT;
  6854. unsigned long timeout;
  6855. spin_lock_irq(&phba->hbalock);
  6856. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6857. spin_unlock_irq(&phba->hbalock);
  6858. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6859. spin_lock_irq(&phba->hbalock);
  6860. if (phba->sli.mbox_active)
  6861. actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
  6862. spin_unlock_irq(&phba->hbalock);
  6863. /* Determine how long we might wait for the active mailbox
  6864. * command to be gracefully completed by firmware.
  6865. */
  6866. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
  6867. 1000) + jiffies;
  6868. while (phba->sli.mbox_active) {
  6869. /* Check active mailbox complete status every 2ms */
  6870. msleep(2);
  6871. if (time_after(jiffies, timeout))
  6872. /* Timeout, let the mailbox flush routine to
  6873. * forcefully release active mailbox command
  6874. */
  6875. break;
  6876. }
  6877. }
  6878. lpfc_sli_mbox_sys_flush(phba);
  6879. }
  6880. /**
  6881. * lpfc_sli_eratt_read - read sli-3 error attention events
  6882. * @phba: Pointer to HBA context.
  6883. *
  6884. * This function is called to read the SLI3 device error attention registers
  6885. * for possible error attention events. The caller must hold the hostlock
  6886. * with spin_lock_irq().
  6887. *
  6888. * This fucntion returns 1 when there is Error Attention in the Host Attention
  6889. * Register and returns 0 otherwise.
  6890. **/
  6891. static int
  6892. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  6893. {
  6894. uint32_t ha_copy;
  6895. /* Read chip Host Attention (HA) register */
  6896. ha_copy = readl(phba->HAregaddr);
  6897. if (ha_copy & HA_ERATT) {
  6898. /* Read host status register to retrieve error event */
  6899. lpfc_sli_read_hs(phba);
  6900. /* Check if there is a deferred error condition is active */
  6901. if ((HS_FFER1 & phba->work_hs) &&
  6902. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  6903. HS_FFER6 | HS_FFER7) & phba->work_hs)) {
  6904. phba->hba_flag |= DEFER_ERATT;
  6905. /* Clear all interrupt enable conditions */
  6906. writel(0, phba->HCregaddr);
  6907. readl(phba->HCregaddr);
  6908. }
  6909. /* Set the driver HA work bitmap */
  6910. phba->work_ha |= HA_ERATT;
  6911. /* Indicate polling handles this ERATT */
  6912. phba->hba_flag |= HBA_ERATT_HANDLED;
  6913. return 1;
  6914. }
  6915. return 0;
  6916. }
  6917. /**
  6918. * lpfc_sli4_eratt_read - read sli-4 error attention events
  6919. * @phba: Pointer to HBA context.
  6920. *
  6921. * This function is called to read the SLI4 device error attention registers
  6922. * for possible error attention events. The caller must hold the hostlock
  6923. * with spin_lock_irq().
  6924. *
  6925. * This fucntion returns 1 when there is Error Attention in the Host Attention
  6926. * Register and returns 0 otherwise.
  6927. **/
  6928. static int
  6929. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  6930. {
  6931. uint32_t uerr_sta_hi, uerr_sta_lo;
  6932. uint32_t onlnreg0, onlnreg1;
  6933. /* For now, use the SLI4 device internal unrecoverable error
  6934. * registers for error attention. This can be changed later.
  6935. */
  6936. onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
  6937. onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
  6938. if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
  6939. uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
  6940. uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
  6941. if (uerr_sta_lo || uerr_sta_hi) {
  6942. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6943. "1423 HBA Unrecoverable error: "
  6944. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  6945. "online0_reg=0x%x, online1_reg=0x%x\n",
  6946. uerr_sta_lo, uerr_sta_hi,
  6947. onlnreg0, onlnreg1);
  6948. phba->work_status[0] = uerr_sta_lo;
  6949. phba->work_status[1] = uerr_sta_hi;
  6950. /* Set the driver HA work bitmap */
  6951. phba->work_ha |= HA_ERATT;
  6952. /* Indicate polling handles this ERATT */
  6953. phba->hba_flag |= HBA_ERATT_HANDLED;
  6954. return 1;
  6955. }
  6956. }
  6957. return 0;
  6958. }
  6959. /**
  6960. * lpfc_sli_check_eratt - check error attention events
  6961. * @phba: Pointer to HBA context.
  6962. *
  6963. * This function is called from timer soft interrupt context to check HBA's
  6964. * error attention register bit for error attention events.
  6965. *
  6966. * This fucntion returns 1 when there is Error Attention in the Host Attention
  6967. * Register and returns 0 otherwise.
  6968. **/
  6969. int
  6970. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  6971. {
  6972. uint32_t ha_copy;
  6973. /* If somebody is waiting to handle an eratt, don't process it
  6974. * here. The brdkill function will do this.
  6975. */
  6976. if (phba->link_flag & LS_IGNORE_ERATT)
  6977. return 0;
  6978. /* Check if interrupt handler handles this ERATT */
  6979. spin_lock_irq(&phba->hbalock);
  6980. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  6981. /* Interrupt handler has handled ERATT */
  6982. spin_unlock_irq(&phba->hbalock);
  6983. return 0;
  6984. }
  6985. /*
  6986. * If there is deferred error attention, do not check for error
  6987. * attention
  6988. */
  6989. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6990. spin_unlock_irq(&phba->hbalock);
  6991. return 0;
  6992. }
  6993. /* If PCI channel is offline, don't process it */
  6994. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6995. spin_unlock_irq(&phba->hbalock);
  6996. return 0;
  6997. }
  6998. switch (phba->sli_rev) {
  6999. case LPFC_SLI_REV2:
  7000. case LPFC_SLI_REV3:
  7001. /* Read chip Host Attention (HA) register */
  7002. ha_copy = lpfc_sli_eratt_read(phba);
  7003. break;
  7004. case LPFC_SLI_REV4:
  7005. /* Read devcie Uncoverable Error (UERR) registers */
  7006. ha_copy = lpfc_sli4_eratt_read(phba);
  7007. break;
  7008. default:
  7009. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7010. "0299 Invalid SLI revision (%d)\n",
  7011. phba->sli_rev);
  7012. ha_copy = 0;
  7013. break;
  7014. }
  7015. spin_unlock_irq(&phba->hbalock);
  7016. return ha_copy;
  7017. }
  7018. /**
  7019. * lpfc_intr_state_check - Check device state for interrupt handling
  7020. * @phba: Pointer to HBA context.
  7021. *
  7022. * This inline routine checks whether a device or its PCI slot is in a state
  7023. * that the interrupt should be handled.
  7024. *
  7025. * This function returns 0 if the device or the PCI slot is in a state that
  7026. * interrupt should be handled, otherwise -EIO.
  7027. */
  7028. static inline int
  7029. lpfc_intr_state_check(struct lpfc_hba *phba)
  7030. {
  7031. /* If the pci channel is offline, ignore all the interrupts */
  7032. if (unlikely(pci_channel_offline(phba->pcidev)))
  7033. return -EIO;
  7034. /* Update device level interrupt statistics */
  7035. phba->sli.slistat.sli_intr++;
  7036. /* Ignore all interrupts during initialization. */
  7037. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  7038. return -EIO;
  7039. return 0;
  7040. }
  7041. /**
  7042. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  7043. * @irq: Interrupt number.
  7044. * @dev_id: The device context pointer.
  7045. *
  7046. * This function is directly called from the PCI layer as an interrupt
  7047. * service routine when device with SLI-3 interface spec is enabled with
  7048. * MSI-X multi-message interrupt mode and there are slow-path events in
  7049. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  7050. * interrupt mode, this function is called as part of the device-level
  7051. * interrupt handler. When the PCI slot is in error recovery or the HBA
  7052. * is undergoing initialization, the interrupt handler will not process
  7053. * the interrupt. The link attention and ELS ring attention events are
  7054. * handled by the worker thread. The interrupt handler signals the worker
  7055. * thread and returns for these events. This function is called without
  7056. * any lock held. It gets the hbalock to access and update SLI data
  7057. * structures.
  7058. *
  7059. * This function returns IRQ_HANDLED when interrupt is handled else it
  7060. * returns IRQ_NONE.
  7061. **/
  7062. irqreturn_t
  7063. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  7064. {
  7065. struct lpfc_hba *phba;
  7066. uint32_t ha_copy;
  7067. uint32_t work_ha_copy;
  7068. unsigned long status;
  7069. unsigned long iflag;
  7070. uint32_t control;
  7071. MAILBOX_t *mbox, *pmbox;
  7072. struct lpfc_vport *vport;
  7073. struct lpfc_nodelist *ndlp;
  7074. struct lpfc_dmabuf *mp;
  7075. LPFC_MBOXQ_t *pmb;
  7076. int rc;
  7077. /*
  7078. * Get the driver's phba structure from the dev_id and
  7079. * assume the HBA is not interrupting.
  7080. */
  7081. phba = (struct lpfc_hba *)dev_id;
  7082. if (unlikely(!phba))
  7083. return IRQ_NONE;
  7084. /*
  7085. * Stuff needs to be attented to when this function is invoked as an
  7086. * individual interrupt handler in MSI-X multi-message interrupt mode
  7087. */
  7088. if (phba->intr_type == MSIX) {
  7089. /* Check device state for handling interrupt */
  7090. if (lpfc_intr_state_check(phba))
  7091. return IRQ_NONE;
  7092. /* Need to read HA REG for slow-path events */
  7093. spin_lock_irqsave(&phba->hbalock, iflag);
  7094. ha_copy = readl(phba->HAregaddr);
  7095. /* If somebody is waiting to handle an eratt don't process it
  7096. * here. The brdkill function will do this.
  7097. */
  7098. if (phba->link_flag & LS_IGNORE_ERATT)
  7099. ha_copy &= ~HA_ERATT;
  7100. /* Check the need for handling ERATT in interrupt handler */
  7101. if (ha_copy & HA_ERATT) {
  7102. if (phba->hba_flag & HBA_ERATT_HANDLED)
  7103. /* ERATT polling has handled ERATT */
  7104. ha_copy &= ~HA_ERATT;
  7105. else
  7106. /* Indicate interrupt handler handles ERATT */
  7107. phba->hba_flag |= HBA_ERATT_HANDLED;
  7108. }
  7109. /*
  7110. * If there is deferred error attention, do not check for any
  7111. * interrupt.
  7112. */
  7113. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  7114. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7115. return IRQ_NONE;
  7116. }
  7117. /* Clear up only attention source related to slow-path */
  7118. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  7119. phba->HAregaddr);
  7120. readl(phba->HAregaddr); /* flush */
  7121. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7122. } else
  7123. ha_copy = phba->ha_copy;
  7124. work_ha_copy = ha_copy & phba->work_ha_mask;
  7125. if (work_ha_copy) {
  7126. if (work_ha_copy & HA_LATT) {
  7127. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  7128. /*
  7129. * Turn off Link Attention interrupts
  7130. * until CLEAR_LA done
  7131. */
  7132. spin_lock_irqsave(&phba->hbalock, iflag);
  7133. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  7134. control = readl(phba->HCregaddr);
  7135. control &= ~HC_LAINT_ENA;
  7136. writel(control, phba->HCregaddr);
  7137. readl(phba->HCregaddr); /* flush */
  7138. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7139. }
  7140. else
  7141. work_ha_copy &= ~HA_LATT;
  7142. }
  7143. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  7144. /*
  7145. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  7146. * the only slow ring.
  7147. */
  7148. status = (work_ha_copy &
  7149. (HA_RXMASK << (4*LPFC_ELS_RING)));
  7150. status >>= (4*LPFC_ELS_RING);
  7151. if (status & HA_RXMASK) {
  7152. spin_lock_irqsave(&phba->hbalock, iflag);
  7153. control = readl(phba->HCregaddr);
  7154. lpfc_debugfs_slow_ring_trc(phba,
  7155. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  7156. control, status,
  7157. (uint32_t)phba->sli.slistat.sli_intr);
  7158. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  7159. lpfc_debugfs_slow_ring_trc(phba,
  7160. "ISR Disable ring:"
  7161. "pwork:x%x hawork:x%x wait:x%x",
  7162. phba->work_ha, work_ha_copy,
  7163. (uint32_t)((unsigned long)
  7164. &phba->work_waitq));
  7165. control &=
  7166. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  7167. writel(control, phba->HCregaddr);
  7168. readl(phba->HCregaddr); /* flush */
  7169. }
  7170. else {
  7171. lpfc_debugfs_slow_ring_trc(phba,
  7172. "ISR slow ring: pwork:"
  7173. "x%x hawork:x%x wait:x%x",
  7174. phba->work_ha, work_ha_copy,
  7175. (uint32_t)((unsigned long)
  7176. &phba->work_waitq));
  7177. }
  7178. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7179. }
  7180. }
  7181. spin_lock_irqsave(&phba->hbalock, iflag);
  7182. if (work_ha_copy & HA_ERATT) {
  7183. lpfc_sli_read_hs(phba);
  7184. /*
  7185. * Check if there is a deferred error condition
  7186. * is active
  7187. */
  7188. if ((HS_FFER1 & phba->work_hs) &&
  7189. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  7190. HS_FFER6 | HS_FFER7) & phba->work_hs)) {
  7191. phba->hba_flag |= DEFER_ERATT;
  7192. /* Clear all interrupt enable conditions */
  7193. writel(0, phba->HCregaddr);
  7194. readl(phba->HCregaddr);
  7195. }
  7196. }
  7197. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  7198. pmb = phba->sli.mbox_active;
  7199. pmbox = &pmb->u.mb;
  7200. mbox = phba->mbox;
  7201. vport = pmb->vport;
  7202. /* First check out the status word */
  7203. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  7204. if (pmbox->mbxOwner != OWN_HOST) {
  7205. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7206. /*
  7207. * Stray Mailbox Interrupt, mbxCommand <cmd>
  7208. * mbxStatus <status>
  7209. */
  7210. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  7211. LOG_SLI,
  7212. "(%d):0304 Stray Mailbox "
  7213. "Interrupt mbxCommand x%x "
  7214. "mbxStatus x%x\n",
  7215. (vport ? vport->vpi : 0),
  7216. pmbox->mbxCommand,
  7217. pmbox->mbxStatus);
  7218. /* clear mailbox attention bit */
  7219. work_ha_copy &= ~HA_MBATT;
  7220. } else {
  7221. phba->sli.mbox_active = NULL;
  7222. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7223. phba->last_completion_time = jiffies;
  7224. del_timer(&phba->sli.mbox_tmo);
  7225. if (pmb->mbox_cmpl) {
  7226. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  7227. MAILBOX_CMD_SIZE);
  7228. }
  7229. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  7230. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  7231. lpfc_debugfs_disc_trc(vport,
  7232. LPFC_DISC_TRC_MBOX_VPORT,
  7233. "MBOX dflt rpi: : "
  7234. "status:x%x rpi:x%x",
  7235. (uint32_t)pmbox->mbxStatus,
  7236. pmbox->un.varWords[0], 0);
  7237. if (!pmbox->mbxStatus) {
  7238. mp = (struct lpfc_dmabuf *)
  7239. (pmb->context1);
  7240. ndlp = (struct lpfc_nodelist *)
  7241. pmb->context2;
  7242. /* Reg_LOGIN of dflt RPI was
  7243. * successful. new lets get
  7244. * rid of the RPI using the
  7245. * same mbox buffer.
  7246. */
  7247. lpfc_unreg_login(phba,
  7248. vport->vpi,
  7249. pmbox->un.varWords[0],
  7250. pmb);
  7251. pmb->mbox_cmpl =
  7252. lpfc_mbx_cmpl_dflt_rpi;
  7253. pmb->context1 = mp;
  7254. pmb->context2 = ndlp;
  7255. pmb->vport = vport;
  7256. rc = lpfc_sli_issue_mbox(phba,
  7257. pmb,
  7258. MBX_NOWAIT);
  7259. if (rc != MBX_BUSY)
  7260. lpfc_printf_log(phba,
  7261. KERN_ERR,
  7262. LOG_MBOX | LOG_SLI,
  7263. "0350 rc should have"
  7264. "been MBX_BUSY");
  7265. if (rc != MBX_NOT_FINISHED)
  7266. goto send_current_mbox;
  7267. }
  7268. }
  7269. spin_lock_irqsave(
  7270. &phba->pport->work_port_lock,
  7271. iflag);
  7272. phba->pport->work_port_events &=
  7273. ~WORKER_MBOX_TMO;
  7274. spin_unlock_irqrestore(
  7275. &phba->pport->work_port_lock,
  7276. iflag);
  7277. lpfc_mbox_cmpl_put(phba, pmb);
  7278. }
  7279. } else
  7280. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7281. if ((work_ha_copy & HA_MBATT) &&
  7282. (phba->sli.mbox_active == NULL)) {
  7283. send_current_mbox:
  7284. /* Process next mailbox command if there is one */
  7285. do {
  7286. rc = lpfc_sli_issue_mbox(phba, NULL,
  7287. MBX_NOWAIT);
  7288. } while (rc == MBX_NOT_FINISHED);
  7289. if (rc != MBX_SUCCESS)
  7290. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  7291. LOG_SLI, "0349 rc should be "
  7292. "MBX_SUCCESS");
  7293. }
  7294. spin_lock_irqsave(&phba->hbalock, iflag);
  7295. phba->work_ha |= work_ha_copy;
  7296. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7297. lpfc_worker_wake_up(phba);
  7298. }
  7299. return IRQ_HANDLED;
  7300. } /* lpfc_sli_sp_intr_handler */
  7301. /**
  7302. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  7303. * @irq: Interrupt number.
  7304. * @dev_id: The device context pointer.
  7305. *
  7306. * This function is directly called from the PCI layer as an interrupt
  7307. * service routine when device with SLI-3 interface spec is enabled with
  7308. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  7309. * ring event in the HBA. However, when the device is enabled with either
  7310. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  7311. * device-level interrupt handler. When the PCI slot is in error recovery
  7312. * or the HBA is undergoing initialization, the interrupt handler will not
  7313. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  7314. * the intrrupt context. This function is called without any lock held.
  7315. * It gets the hbalock to access and update SLI data structures.
  7316. *
  7317. * This function returns IRQ_HANDLED when interrupt is handled else it
  7318. * returns IRQ_NONE.
  7319. **/
  7320. irqreturn_t
  7321. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  7322. {
  7323. struct lpfc_hba *phba;
  7324. uint32_t ha_copy;
  7325. unsigned long status;
  7326. unsigned long iflag;
  7327. /* Get the driver's phba structure from the dev_id and
  7328. * assume the HBA is not interrupting.
  7329. */
  7330. phba = (struct lpfc_hba *) dev_id;
  7331. if (unlikely(!phba))
  7332. return IRQ_NONE;
  7333. /*
  7334. * Stuff needs to be attented to when this function is invoked as an
  7335. * individual interrupt handler in MSI-X multi-message interrupt mode
  7336. */
  7337. if (phba->intr_type == MSIX) {
  7338. /* Check device state for handling interrupt */
  7339. if (lpfc_intr_state_check(phba))
  7340. return IRQ_NONE;
  7341. /* Need to read HA REG for FCP ring and other ring events */
  7342. ha_copy = readl(phba->HAregaddr);
  7343. /* Clear up only attention source related to fast-path */
  7344. spin_lock_irqsave(&phba->hbalock, iflag);
  7345. /*
  7346. * If there is deferred error attention, do not check for
  7347. * any interrupt.
  7348. */
  7349. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  7350. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7351. return IRQ_NONE;
  7352. }
  7353. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  7354. phba->HAregaddr);
  7355. readl(phba->HAregaddr); /* flush */
  7356. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7357. } else
  7358. ha_copy = phba->ha_copy;
  7359. /*
  7360. * Process all events on FCP ring. Take the optimized path for FCP IO.
  7361. */
  7362. ha_copy &= ~(phba->work_ha_mask);
  7363. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  7364. status >>= (4*LPFC_FCP_RING);
  7365. if (status & HA_RXMASK)
  7366. lpfc_sli_handle_fast_ring_event(phba,
  7367. &phba->sli.ring[LPFC_FCP_RING],
  7368. status);
  7369. if (phba->cfg_multi_ring_support == 2) {
  7370. /*
  7371. * Process all events on extra ring. Take the optimized path
  7372. * for extra ring IO.
  7373. */
  7374. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  7375. status >>= (4*LPFC_EXTRA_RING);
  7376. if (status & HA_RXMASK) {
  7377. lpfc_sli_handle_fast_ring_event(phba,
  7378. &phba->sli.ring[LPFC_EXTRA_RING],
  7379. status);
  7380. }
  7381. }
  7382. return IRQ_HANDLED;
  7383. } /* lpfc_sli_fp_intr_handler */
  7384. /**
  7385. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  7386. * @irq: Interrupt number.
  7387. * @dev_id: The device context pointer.
  7388. *
  7389. * This function is the HBA device-level interrupt handler to device with
  7390. * SLI-3 interface spec, called from the PCI layer when either MSI or
  7391. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  7392. * requires driver attention. This function invokes the slow-path interrupt
  7393. * attention handling function and fast-path interrupt attention handling
  7394. * function in turn to process the relevant HBA attention events. This
  7395. * function is called without any lock held. It gets the hbalock to access
  7396. * and update SLI data structures.
  7397. *
  7398. * This function returns IRQ_HANDLED when interrupt is handled, else it
  7399. * returns IRQ_NONE.
  7400. **/
  7401. irqreturn_t
  7402. lpfc_sli_intr_handler(int irq, void *dev_id)
  7403. {
  7404. struct lpfc_hba *phba;
  7405. irqreturn_t sp_irq_rc, fp_irq_rc;
  7406. unsigned long status1, status2;
  7407. /*
  7408. * Get the driver's phba structure from the dev_id and
  7409. * assume the HBA is not interrupting.
  7410. */
  7411. phba = (struct lpfc_hba *) dev_id;
  7412. if (unlikely(!phba))
  7413. return IRQ_NONE;
  7414. /* Check device state for handling interrupt */
  7415. if (lpfc_intr_state_check(phba))
  7416. return IRQ_NONE;
  7417. spin_lock(&phba->hbalock);
  7418. phba->ha_copy = readl(phba->HAregaddr);
  7419. if (unlikely(!phba->ha_copy)) {
  7420. spin_unlock(&phba->hbalock);
  7421. return IRQ_NONE;
  7422. } else if (phba->ha_copy & HA_ERATT) {
  7423. if (phba->hba_flag & HBA_ERATT_HANDLED)
  7424. /* ERATT polling has handled ERATT */
  7425. phba->ha_copy &= ~HA_ERATT;
  7426. else
  7427. /* Indicate interrupt handler handles ERATT */
  7428. phba->hba_flag |= HBA_ERATT_HANDLED;
  7429. }
  7430. /*
  7431. * If there is deferred error attention, do not check for any interrupt.
  7432. */
  7433. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  7434. spin_unlock_irq(&phba->hbalock);
  7435. return IRQ_NONE;
  7436. }
  7437. /* Clear attention sources except link and error attentions */
  7438. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  7439. readl(phba->HAregaddr); /* flush */
  7440. spin_unlock(&phba->hbalock);
  7441. /*
  7442. * Invokes slow-path host attention interrupt handling as appropriate.
  7443. */
  7444. /* status of events with mailbox and link attention */
  7445. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  7446. /* status of events with ELS ring */
  7447. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  7448. status2 >>= (4*LPFC_ELS_RING);
  7449. if (status1 || (status2 & HA_RXMASK))
  7450. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  7451. else
  7452. sp_irq_rc = IRQ_NONE;
  7453. /*
  7454. * Invoke fast-path host attention interrupt handling as appropriate.
  7455. */
  7456. /* status of events with FCP ring */
  7457. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  7458. status1 >>= (4*LPFC_FCP_RING);
  7459. /* status of events with extra ring */
  7460. if (phba->cfg_multi_ring_support == 2) {
  7461. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  7462. status2 >>= (4*LPFC_EXTRA_RING);
  7463. } else
  7464. status2 = 0;
  7465. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  7466. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  7467. else
  7468. fp_irq_rc = IRQ_NONE;
  7469. /* Return device-level interrupt handling status */
  7470. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  7471. } /* lpfc_sli_intr_handler */
  7472. /**
  7473. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  7474. * @phba: pointer to lpfc hba data structure.
  7475. *
  7476. * This routine is invoked by the worker thread to process all the pending
  7477. * SLI4 FCP abort XRI events.
  7478. **/
  7479. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  7480. {
  7481. struct lpfc_cq_event *cq_event;
  7482. /* First, declare the fcp xri abort event has been handled */
  7483. spin_lock_irq(&phba->hbalock);
  7484. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  7485. spin_unlock_irq(&phba->hbalock);
  7486. /* Now, handle all the fcp xri abort events */
  7487. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  7488. /* Get the first event from the head of the event queue */
  7489. spin_lock_irq(&phba->hbalock);
  7490. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  7491. cq_event, struct lpfc_cq_event, list);
  7492. spin_unlock_irq(&phba->hbalock);
  7493. /* Notify aborted XRI for FCP work queue */
  7494. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  7495. /* Free the event processed back to the free pool */
  7496. lpfc_sli4_cq_event_release(phba, cq_event);
  7497. }
  7498. }
  7499. /**
  7500. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  7501. * @phba: pointer to lpfc hba data structure.
  7502. *
  7503. * This routine is invoked by the worker thread to process all the pending
  7504. * SLI4 els abort xri events.
  7505. **/
  7506. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  7507. {
  7508. struct lpfc_cq_event *cq_event;
  7509. /* First, declare the els xri abort event has been handled */
  7510. spin_lock_irq(&phba->hbalock);
  7511. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  7512. spin_unlock_irq(&phba->hbalock);
  7513. /* Now, handle all the els xri abort events */
  7514. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  7515. /* Get the first event from the head of the event queue */
  7516. spin_lock_irq(&phba->hbalock);
  7517. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  7518. cq_event, struct lpfc_cq_event, list);
  7519. spin_unlock_irq(&phba->hbalock);
  7520. /* Notify aborted XRI for ELS work queue */
  7521. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  7522. /* Free the event processed back to the free pool */
  7523. lpfc_sli4_cq_event_release(phba, cq_event);
  7524. }
  7525. }
  7526. static void
  7527. lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
  7528. struct lpfc_iocbq *pIocbOut,
  7529. struct lpfc_wcqe_complete *wcqe)
  7530. {
  7531. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  7532. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  7533. sizeof(struct lpfc_iocbq) - offset);
  7534. memset(&pIocbIn->sli4_info, 0,
  7535. sizeof(struct lpfc_sli4_rspiocb_info));
  7536. /* Map WCQE parameters into irspiocb parameters */
  7537. pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
  7538. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  7539. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  7540. pIocbIn->iocb.un.fcpi.fcpi_parm =
  7541. pIocbOut->iocb.un.fcpi.fcpi_parm -
  7542. wcqe->total_data_placed;
  7543. else
  7544. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  7545. else
  7546. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  7547. /* Load in additional WCQE parameters */
  7548. pIocbIn->sli4_info.hw_status = bf_get(lpfc_wcqe_c_hw_status, wcqe);
  7549. pIocbIn->sli4_info.bfield = 0;
  7550. if (bf_get(lpfc_wcqe_c_xb, wcqe))
  7551. pIocbIn->sli4_info.bfield |= LPFC_XB;
  7552. if (bf_get(lpfc_wcqe_c_pv, wcqe)) {
  7553. pIocbIn->sli4_info.bfield |= LPFC_PV;
  7554. pIocbIn->sli4_info.priority =
  7555. bf_get(lpfc_wcqe_c_priority, wcqe);
  7556. }
  7557. }
  7558. /**
  7559. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  7560. * @phba: Pointer to HBA context object.
  7561. * @cqe: Pointer to mailbox completion queue entry.
  7562. *
  7563. * This routine process a mailbox completion queue entry with asynchrous
  7564. * event.
  7565. *
  7566. * Return: true if work posted to worker thread, otherwise false.
  7567. **/
  7568. static bool
  7569. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  7570. {
  7571. struct lpfc_cq_event *cq_event;
  7572. unsigned long iflags;
  7573. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7574. "0392 Async Event: word0:x%x, word1:x%x, "
  7575. "word2:x%x, word3:x%x\n", mcqe->word0,
  7576. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  7577. /* Allocate a new internal CQ_EVENT entry */
  7578. cq_event = lpfc_sli4_cq_event_alloc(phba);
  7579. if (!cq_event) {
  7580. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7581. "0394 Failed to allocate CQ_EVENT entry\n");
  7582. return false;
  7583. }
  7584. /* Move the CQE into an asynchronous event entry */
  7585. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  7586. spin_lock_irqsave(&phba->hbalock, iflags);
  7587. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  7588. /* Set the async event flag */
  7589. phba->hba_flag |= ASYNC_EVENT;
  7590. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7591. return true;
  7592. }
  7593. /**
  7594. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  7595. * @phba: Pointer to HBA context object.
  7596. * @cqe: Pointer to mailbox completion queue entry.
  7597. *
  7598. * This routine process a mailbox completion queue entry with mailbox
  7599. * completion event.
  7600. *
  7601. * Return: true if work posted to worker thread, otherwise false.
  7602. **/
  7603. static bool
  7604. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  7605. {
  7606. uint32_t mcqe_status;
  7607. MAILBOX_t *mbox, *pmbox;
  7608. struct lpfc_mqe *mqe;
  7609. struct lpfc_vport *vport;
  7610. struct lpfc_nodelist *ndlp;
  7611. struct lpfc_dmabuf *mp;
  7612. unsigned long iflags;
  7613. LPFC_MBOXQ_t *pmb;
  7614. bool workposted = false;
  7615. int rc;
  7616. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  7617. if (!bf_get(lpfc_trailer_completed, mcqe))
  7618. goto out_no_mqe_complete;
  7619. /* Get the reference to the active mbox command */
  7620. spin_lock_irqsave(&phba->hbalock, iflags);
  7621. pmb = phba->sli.mbox_active;
  7622. if (unlikely(!pmb)) {
  7623. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  7624. "1832 No pending MBOX command to handle\n");
  7625. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7626. goto out_no_mqe_complete;
  7627. }
  7628. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7629. mqe = &pmb->u.mqe;
  7630. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  7631. mbox = phba->mbox;
  7632. vport = pmb->vport;
  7633. /* Reset heartbeat timer */
  7634. phba->last_completion_time = jiffies;
  7635. del_timer(&phba->sli.mbox_tmo);
  7636. /* Move mbox data to caller's mailbox region, do endian swapping */
  7637. if (pmb->mbox_cmpl && mbox)
  7638. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  7639. /* Set the mailbox status with SLI4 range 0x4000 */
  7640. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  7641. if (mcqe_status != MB_CQE_STATUS_SUCCESS)
  7642. bf_set(lpfc_mqe_status, mqe,
  7643. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  7644. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  7645. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  7646. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  7647. "MBOX dflt rpi: status:x%x rpi:x%x",
  7648. mcqe_status,
  7649. pmbox->un.varWords[0], 0);
  7650. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  7651. mp = (struct lpfc_dmabuf *)(pmb->context1);
  7652. ndlp = (struct lpfc_nodelist *)pmb->context2;
  7653. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  7654. * RID of the PPI using the same mbox buffer.
  7655. */
  7656. lpfc_unreg_login(phba, vport->vpi,
  7657. pmbox->un.varWords[0], pmb);
  7658. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  7659. pmb->context1 = mp;
  7660. pmb->context2 = ndlp;
  7661. pmb->vport = vport;
  7662. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  7663. if (rc != MBX_BUSY)
  7664. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  7665. LOG_SLI, "0385 rc should "
  7666. "have been MBX_BUSY\n");
  7667. if (rc != MBX_NOT_FINISHED)
  7668. goto send_current_mbox;
  7669. }
  7670. }
  7671. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  7672. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  7673. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  7674. /* There is mailbox completion work to do */
  7675. spin_lock_irqsave(&phba->hbalock, iflags);
  7676. __lpfc_mbox_cmpl_put(phba, pmb);
  7677. phba->work_ha |= HA_MBATT;
  7678. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7679. workposted = true;
  7680. send_current_mbox:
  7681. spin_lock_irqsave(&phba->hbalock, iflags);
  7682. /* Release the mailbox command posting token */
  7683. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7684. /* Setting active mailbox pointer need to be in sync to flag clear */
  7685. phba->sli.mbox_active = NULL;
  7686. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7687. /* Wake up worker thread to post the next pending mailbox command */
  7688. lpfc_worker_wake_up(phba);
  7689. out_no_mqe_complete:
  7690. if (bf_get(lpfc_trailer_consumed, mcqe))
  7691. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  7692. return workposted;
  7693. }
  7694. /**
  7695. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  7696. * @phba: Pointer to HBA context object.
  7697. * @cqe: Pointer to mailbox completion queue entry.
  7698. *
  7699. * This routine process a mailbox completion queue entry, it invokes the
  7700. * proper mailbox complete handling or asynchrous event handling routine
  7701. * according to the MCQE's async bit.
  7702. *
  7703. * Return: true if work posted to worker thread, otherwise false.
  7704. **/
  7705. static bool
  7706. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  7707. {
  7708. struct lpfc_mcqe mcqe;
  7709. bool workposted;
  7710. /* Copy the mailbox MCQE and convert endian order as needed */
  7711. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  7712. /* Invoke the proper event handling routine */
  7713. if (!bf_get(lpfc_trailer_async, &mcqe))
  7714. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  7715. else
  7716. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  7717. return workposted;
  7718. }
  7719. /**
  7720. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  7721. * @phba: Pointer to HBA context object.
  7722. * @wcqe: Pointer to work-queue completion queue entry.
  7723. *
  7724. * This routine handles an ELS work-queue completion event.
  7725. *
  7726. * Return: true if work posted to worker thread, otherwise false.
  7727. **/
  7728. static bool
  7729. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  7730. struct lpfc_wcqe_complete *wcqe)
  7731. {
  7732. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  7733. struct lpfc_iocbq *cmdiocbq;
  7734. struct lpfc_iocbq *irspiocbq;
  7735. unsigned long iflags;
  7736. bool workposted = false;
  7737. spin_lock_irqsave(&phba->hbalock, iflags);
  7738. pring->stats.iocb_event++;
  7739. /* Look up the ELS command IOCB and create pseudo response IOCB */
  7740. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  7741. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  7742. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7743. if (unlikely(!cmdiocbq)) {
  7744. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7745. "0386 ELS complete with no corresponding "
  7746. "cmdiocb: iotag (%d)\n",
  7747. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  7748. return workposted;
  7749. }
  7750. /* Fake the irspiocbq and copy necessary response information */
  7751. irspiocbq = lpfc_sli_get_iocbq(phba);
  7752. if (!irspiocbq) {
  7753. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7754. "0387 Failed to allocate an iocbq\n");
  7755. return workposted;
  7756. }
  7757. lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
  7758. /* Add the irspiocb to the response IOCB work list */
  7759. spin_lock_irqsave(&phba->hbalock, iflags);
  7760. list_add_tail(&irspiocbq->list, &phba->sli4_hba.sp_rspiocb_work_queue);
  7761. /* Indicate ELS ring attention */
  7762. phba->work_ha |= (HA_R0ATT << (4*LPFC_ELS_RING));
  7763. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7764. workposted = true;
  7765. return workposted;
  7766. }
  7767. /**
  7768. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  7769. * @phba: Pointer to HBA context object.
  7770. * @wcqe: Pointer to work-queue completion queue entry.
  7771. *
  7772. * This routine handles slow-path WQ entry comsumed event by invoking the
  7773. * proper WQ release routine to the slow-path WQ.
  7774. **/
  7775. static void
  7776. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  7777. struct lpfc_wcqe_release *wcqe)
  7778. {
  7779. /* Check for the slow-path ELS work queue */
  7780. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  7781. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  7782. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  7783. else
  7784. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7785. "2579 Slow-path wqe consume event carries "
  7786. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  7787. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  7788. phba->sli4_hba.els_wq->queue_id);
  7789. }
  7790. /**
  7791. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  7792. * @phba: Pointer to HBA context object.
  7793. * @cq: Pointer to a WQ completion queue.
  7794. * @wcqe: Pointer to work-queue completion queue entry.
  7795. *
  7796. * This routine handles an XRI abort event.
  7797. *
  7798. * Return: true if work posted to worker thread, otherwise false.
  7799. **/
  7800. static bool
  7801. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  7802. struct lpfc_queue *cq,
  7803. struct sli4_wcqe_xri_aborted *wcqe)
  7804. {
  7805. bool workposted = false;
  7806. struct lpfc_cq_event *cq_event;
  7807. unsigned long iflags;
  7808. /* Allocate a new internal CQ_EVENT entry */
  7809. cq_event = lpfc_sli4_cq_event_alloc(phba);
  7810. if (!cq_event) {
  7811. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7812. "0602 Failed to allocate CQ_EVENT entry\n");
  7813. return false;
  7814. }
  7815. /* Move the CQE into the proper xri abort event list */
  7816. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  7817. switch (cq->subtype) {
  7818. case LPFC_FCP:
  7819. spin_lock_irqsave(&phba->hbalock, iflags);
  7820. list_add_tail(&cq_event->list,
  7821. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  7822. /* Set the fcp xri abort event flag */
  7823. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  7824. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7825. workposted = true;
  7826. break;
  7827. case LPFC_ELS:
  7828. spin_lock_irqsave(&phba->hbalock, iflags);
  7829. list_add_tail(&cq_event->list,
  7830. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  7831. /* Set the els xri abort event flag */
  7832. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  7833. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7834. workposted = true;
  7835. break;
  7836. default:
  7837. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7838. "0603 Invalid work queue CQE subtype (x%x)\n",
  7839. cq->subtype);
  7840. workposted = false;
  7841. break;
  7842. }
  7843. return workposted;
  7844. }
  7845. /**
  7846. * lpfc_sli4_sp_handle_wcqe - Process a work-queue completion queue entry
  7847. * @phba: Pointer to HBA context object.
  7848. * @cq: Pointer to the completion queue.
  7849. * @wcqe: Pointer to a completion queue entry.
  7850. *
  7851. * This routine process a slow-path work-queue completion queue entry.
  7852. *
  7853. * Return: true if work posted to worker thread, otherwise false.
  7854. **/
  7855. static bool
  7856. lpfc_sli4_sp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  7857. struct lpfc_cqe *cqe)
  7858. {
  7859. struct lpfc_wcqe_complete wcqe;
  7860. bool workposted = false;
  7861. /* Copy the work queue CQE and convert endian order if needed */
  7862. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  7863. /* Check and process for different type of WCQE and dispatch */
  7864. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  7865. case CQE_CODE_COMPL_WQE:
  7866. /* Process the WQ complete event */
  7867. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  7868. (struct lpfc_wcqe_complete *)&wcqe);
  7869. break;
  7870. case CQE_CODE_RELEASE_WQE:
  7871. /* Process the WQ release event */
  7872. lpfc_sli4_sp_handle_rel_wcqe(phba,
  7873. (struct lpfc_wcqe_release *)&wcqe);
  7874. break;
  7875. case CQE_CODE_XRI_ABORTED:
  7876. /* Process the WQ XRI abort event */
  7877. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  7878. (struct sli4_wcqe_xri_aborted *)&wcqe);
  7879. break;
  7880. default:
  7881. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7882. "0388 Not a valid WCQE code: x%x\n",
  7883. bf_get(lpfc_wcqe_c_code, &wcqe));
  7884. break;
  7885. }
  7886. return workposted;
  7887. }
  7888. /**
  7889. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  7890. * @phba: Pointer to HBA context object.
  7891. * @rcqe: Pointer to receive-queue completion queue entry.
  7892. *
  7893. * This routine process a receive-queue completion queue entry.
  7894. *
  7895. * Return: true if work posted to worker thread, otherwise false.
  7896. **/
  7897. static bool
  7898. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  7899. {
  7900. struct lpfc_rcqe rcqe;
  7901. bool workposted = false;
  7902. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  7903. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  7904. struct hbq_dmabuf *dma_buf;
  7905. uint32_t status;
  7906. unsigned long iflags;
  7907. /* Copy the receive queue CQE and convert endian order if needed */
  7908. lpfc_sli_pcimem_bcopy(cqe, &rcqe, sizeof(struct lpfc_rcqe));
  7909. lpfc_sli4_rq_release(hrq, drq);
  7910. if (bf_get(lpfc_rcqe_code, &rcqe) != CQE_CODE_RECEIVE)
  7911. goto out;
  7912. if (bf_get(lpfc_rcqe_rq_id, &rcqe) != hrq->queue_id)
  7913. goto out;
  7914. status = bf_get(lpfc_rcqe_status, &rcqe);
  7915. switch (status) {
  7916. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  7917. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7918. "2537 Receive Frame Truncated!!\n");
  7919. case FC_STATUS_RQ_SUCCESS:
  7920. spin_lock_irqsave(&phba->hbalock, iflags);
  7921. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  7922. if (!dma_buf) {
  7923. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7924. goto out;
  7925. }
  7926. memcpy(&dma_buf->rcqe, &rcqe, sizeof(rcqe));
  7927. /* save off the frame for the word thread to process */
  7928. list_add_tail(&dma_buf->dbuf.list, &phba->rb_pend_list);
  7929. /* Frame received */
  7930. phba->hba_flag |= HBA_RECEIVE_BUFFER;
  7931. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7932. workposted = true;
  7933. break;
  7934. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  7935. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  7936. /* Post more buffers if possible */
  7937. spin_lock_irqsave(&phba->hbalock, iflags);
  7938. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  7939. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7940. workposted = true;
  7941. break;
  7942. }
  7943. out:
  7944. return workposted;
  7945. }
  7946. /**
  7947. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  7948. * @phba: Pointer to HBA context object.
  7949. * @eqe: Pointer to fast-path event queue entry.
  7950. *
  7951. * This routine process a event queue entry from the slow-path event queue.
  7952. * It will check the MajorCode and MinorCode to determine this is for a
  7953. * completion event on a completion queue, if not, an error shall be logged
  7954. * and just return. Otherwise, it will get to the corresponding completion
  7955. * queue and process all the entries on that completion queue, rearm the
  7956. * completion queue, and then return.
  7957. *
  7958. **/
  7959. static void
  7960. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  7961. {
  7962. struct lpfc_queue *cq = NULL, *childq, *speq;
  7963. struct lpfc_cqe *cqe;
  7964. bool workposted = false;
  7965. int ecount = 0;
  7966. uint16_t cqid;
  7967. if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
  7968. bf_get(lpfc_eqe_minor_code, eqe) != 0) {
  7969. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7970. "0359 Not a valid slow-path completion "
  7971. "event: majorcode=x%x, minorcode=x%x\n",
  7972. bf_get(lpfc_eqe_major_code, eqe),
  7973. bf_get(lpfc_eqe_minor_code, eqe));
  7974. return;
  7975. }
  7976. /* Get the reference to the corresponding CQ */
  7977. cqid = bf_get(lpfc_eqe_resource_id, eqe);
  7978. /* Search for completion queue pointer matching this cqid */
  7979. speq = phba->sli4_hba.sp_eq;
  7980. list_for_each_entry(childq, &speq->child_list, list) {
  7981. if (childq->queue_id == cqid) {
  7982. cq = childq;
  7983. break;
  7984. }
  7985. }
  7986. if (unlikely(!cq)) {
  7987. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7988. "0365 Slow-path CQ identifier (%d) does "
  7989. "not exist\n", cqid);
  7990. return;
  7991. }
  7992. /* Process all the entries to the CQ */
  7993. switch (cq->type) {
  7994. case LPFC_MCQ:
  7995. while ((cqe = lpfc_sli4_cq_get(cq))) {
  7996. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  7997. if (!(++ecount % LPFC_GET_QE_REL_INT))
  7998. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  7999. }
  8000. break;
  8001. case LPFC_WCQ:
  8002. while ((cqe = lpfc_sli4_cq_get(cq))) {
  8003. workposted |= lpfc_sli4_sp_handle_wcqe(phba, cq, cqe);
  8004. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8005. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  8006. }
  8007. break;
  8008. case LPFC_RCQ:
  8009. while ((cqe = lpfc_sli4_cq_get(cq))) {
  8010. workposted |= lpfc_sli4_sp_handle_rcqe(phba, cqe);
  8011. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8012. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  8013. }
  8014. break;
  8015. default:
  8016. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8017. "0370 Invalid completion queue type (%d)\n",
  8018. cq->type);
  8019. return;
  8020. }
  8021. /* Catch the no cq entry condition, log an error */
  8022. if (unlikely(ecount == 0))
  8023. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8024. "0371 No entry from the CQ: identifier "
  8025. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  8026. /* In any case, flash and re-arm the RCQ */
  8027. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  8028. /* wake up worker thread if there are works to be done */
  8029. if (workposted)
  8030. lpfc_worker_wake_up(phba);
  8031. }
  8032. /**
  8033. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  8034. * @eqe: Pointer to fast-path completion queue entry.
  8035. *
  8036. * This routine process a fast-path work queue completion entry from fast-path
  8037. * event queue for FCP command response completion.
  8038. **/
  8039. static void
  8040. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  8041. struct lpfc_wcqe_complete *wcqe)
  8042. {
  8043. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  8044. struct lpfc_iocbq *cmdiocbq;
  8045. struct lpfc_iocbq irspiocbq;
  8046. unsigned long iflags;
  8047. spin_lock_irqsave(&phba->hbalock, iflags);
  8048. pring->stats.iocb_event++;
  8049. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8050. /* Check for response status */
  8051. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  8052. /* If resource errors reported from HBA, reduce queue
  8053. * depth of the SCSI device.
  8054. */
  8055. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  8056. IOSTAT_LOCAL_REJECT) &&
  8057. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  8058. phba->lpfc_rampdown_queue_depth(phba);
  8059. }
  8060. /* Log the error status */
  8061. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8062. "0373 FCP complete error: status=x%x, "
  8063. "hw_status=x%x, total_data_specified=%d, "
  8064. "parameter=x%x, word3=x%x\n",
  8065. bf_get(lpfc_wcqe_c_status, wcqe),
  8066. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  8067. wcqe->total_data_placed, wcqe->parameter,
  8068. wcqe->word3);
  8069. }
  8070. /* Look up the FCP command IOCB and create pseudo response IOCB */
  8071. spin_lock_irqsave(&phba->hbalock, iflags);
  8072. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  8073. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  8074. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8075. if (unlikely(!cmdiocbq)) {
  8076. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8077. "0374 FCP complete with no corresponding "
  8078. "cmdiocb: iotag (%d)\n",
  8079. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  8080. return;
  8081. }
  8082. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  8083. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8084. "0375 FCP cmdiocb not callback function "
  8085. "iotag: (%d)\n",
  8086. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  8087. return;
  8088. }
  8089. /* Fake the irspiocb and copy necessary response information */
  8090. lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
  8091. /* Pass the cmd_iocb and the rsp state to the upper layer */
  8092. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  8093. }
  8094. /**
  8095. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  8096. * @phba: Pointer to HBA context object.
  8097. * @cq: Pointer to completion queue.
  8098. * @wcqe: Pointer to work-queue completion queue entry.
  8099. *
  8100. * This routine handles an fast-path WQ entry comsumed event by invoking the
  8101. * proper WQ release routine to the slow-path WQ.
  8102. **/
  8103. static void
  8104. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  8105. struct lpfc_wcqe_release *wcqe)
  8106. {
  8107. struct lpfc_queue *childwq;
  8108. bool wqid_matched = false;
  8109. uint16_t fcp_wqid;
  8110. /* Check for fast-path FCP work queue release */
  8111. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  8112. list_for_each_entry(childwq, &cq->child_list, list) {
  8113. if (childwq->queue_id == fcp_wqid) {
  8114. lpfc_sli4_wq_release(childwq,
  8115. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  8116. wqid_matched = true;
  8117. break;
  8118. }
  8119. }
  8120. /* Report warning log message if no match found */
  8121. if (wqid_matched != true)
  8122. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8123. "2580 Fast-path wqe consume event carries "
  8124. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  8125. }
  8126. /**
  8127. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  8128. * @cq: Pointer to the completion queue.
  8129. * @eqe: Pointer to fast-path completion queue entry.
  8130. *
  8131. * This routine process a fast-path work queue completion entry from fast-path
  8132. * event queue for FCP command response completion.
  8133. **/
  8134. static int
  8135. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  8136. struct lpfc_cqe *cqe)
  8137. {
  8138. struct lpfc_wcqe_release wcqe;
  8139. bool workposted = false;
  8140. /* Copy the work queue CQE and convert endian order if needed */
  8141. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  8142. /* Check and process for different type of WCQE and dispatch */
  8143. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  8144. case CQE_CODE_COMPL_WQE:
  8145. /* Process the WQ complete event */
  8146. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  8147. (struct lpfc_wcqe_complete *)&wcqe);
  8148. break;
  8149. case CQE_CODE_RELEASE_WQE:
  8150. /* Process the WQ release event */
  8151. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  8152. (struct lpfc_wcqe_release *)&wcqe);
  8153. break;
  8154. case CQE_CODE_XRI_ABORTED:
  8155. /* Process the WQ XRI abort event */
  8156. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  8157. (struct sli4_wcqe_xri_aborted *)&wcqe);
  8158. break;
  8159. default:
  8160. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8161. "0144 Not a valid WCQE code: x%x\n",
  8162. bf_get(lpfc_wcqe_c_code, &wcqe));
  8163. break;
  8164. }
  8165. return workposted;
  8166. }
  8167. /**
  8168. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  8169. * @phba: Pointer to HBA context object.
  8170. * @eqe: Pointer to fast-path event queue entry.
  8171. *
  8172. * This routine process a event queue entry from the fast-path event queue.
  8173. * It will check the MajorCode and MinorCode to determine this is for a
  8174. * completion event on a completion queue, if not, an error shall be logged
  8175. * and just return. Otherwise, it will get to the corresponding completion
  8176. * queue and process all the entries on the completion queue, rearm the
  8177. * completion queue, and then return.
  8178. **/
  8179. static void
  8180. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  8181. uint32_t fcp_cqidx)
  8182. {
  8183. struct lpfc_queue *cq;
  8184. struct lpfc_cqe *cqe;
  8185. bool workposted = false;
  8186. uint16_t cqid;
  8187. int ecount = 0;
  8188. if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
  8189. unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
  8190. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8191. "0366 Not a valid fast-path completion "
  8192. "event: majorcode=x%x, minorcode=x%x\n",
  8193. bf_get(lpfc_eqe_major_code, eqe),
  8194. bf_get(lpfc_eqe_minor_code, eqe));
  8195. return;
  8196. }
  8197. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  8198. if (unlikely(!cq)) {
  8199. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8200. "0367 Fast-path completion queue does not "
  8201. "exist\n");
  8202. return;
  8203. }
  8204. /* Get the reference to the corresponding CQ */
  8205. cqid = bf_get(lpfc_eqe_resource_id, eqe);
  8206. if (unlikely(cqid != cq->queue_id)) {
  8207. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8208. "0368 Miss-matched fast-path completion "
  8209. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  8210. cqid, cq->queue_id);
  8211. return;
  8212. }
  8213. /* Process all the entries to the CQ */
  8214. while ((cqe = lpfc_sli4_cq_get(cq))) {
  8215. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  8216. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8217. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  8218. }
  8219. /* Catch the no cq entry condition */
  8220. if (unlikely(ecount == 0))
  8221. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8222. "0369 No entry from fast-path completion "
  8223. "queue fcpcqid=%d\n", cq->queue_id);
  8224. /* In any case, flash and re-arm the CQ */
  8225. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  8226. /* wake up worker thread if there are works to be done */
  8227. if (workposted)
  8228. lpfc_worker_wake_up(phba);
  8229. }
  8230. static void
  8231. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  8232. {
  8233. struct lpfc_eqe *eqe;
  8234. /* walk all the EQ entries and drop on the floor */
  8235. while ((eqe = lpfc_sli4_eq_get(eq)))
  8236. ;
  8237. /* Clear and re-arm the EQ */
  8238. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  8239. }
  8240. /**
  8241. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  8242. * @irq: Interrupt number.
  8243. * @dev_id: The device context pointer.
  8244. *
  8245. * This function is directly called from the PCI layer as an interrupt
  8246. * service routine when device with SLI-4 interface spec is enabled with
  8247. * MSI-X multi-message interrupt mode and there are slow-path events in
  8248. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  8249. * interrupt mode, this function is called as part of the device-level
  8250. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  8251. * undergoing initialization, the interrupt handler will not process the
  8252. * interrupt. The link attention and ELS ring attention events are handled
  8253. * by the worker thread. The interrupt handler signals the worker thread
  8254. * and returns for these events. This function is called without any lock
  8255. * held. It gets the hbalock to access and update SLI data structures.
  8256. *
  8257. * This function returns IRQ_HANDLED when interrupt is handled else it
  8258. * returns IRQ_NONE.
  8259. **/
  8260. irqreturn_t
  8261. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  8262. {
  8263. struct lpfc_hba *phba;
  8264. struct lpfc_queue *speq;
  8265. struct lpfc_eqe *eqe;
  8266. unsigned long iflag;
  8267. int ecount = 0;
  8268. /*
  8269. * Get the driver's phba structure from the dev_id
  8270. */
  8271. phba = (struct lpfc_hba *)dev_id;
  8272. if (unlikely(!phba))
  8273. return IRQ_NONE;
  8274. /* Get to the EQ struct associated with this vector */
  8275. speq = phba->sli4_hba.sp_eq;
  8276. /* Check device state for handling interrupt */
  8277. if (unlikely(lpfc_intr_state_check(phba))) {
  8278. /* Check again for link_state with lock held */
  8279. spin_lock_irqsave(&phba->hbalock, iflag);
  8280. if (phba->link_state < LPFC_LINK_DOWN)
  8281. /* Flush, clear interrupt, and rearm the EQ */
  8282. lpfc_sli4_eq_flush(phba, speq);
  8283. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8284. return IRQ_NONE;
  8285. }
  8286. /*
  8287. * Process all the event on FCP slow-path EQ
  8288. */
  8289. while ((eqe = lpfc_sli4_eq_get(speq))) {
  8290. lpfc_sli4_sp_handle_eqe(phba, eqe);
  8291. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8292. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  8293. }
  8294. /* Always clear and re-arm the slow-path EQ */
  8295. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  8296. /* Catch the no cq entry condition */
  8297. if (unlikely(ecount == 0)) {
  8298. if (phba->intr_type == MSIX)
  8299. /* MSI-X treated interrupt served as no EQ share INT */
  8300. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8301. "0357 MSI-X interrupt with no EQE\n");
  8302. else
  8303. /* Non MSI-X treated on interrupt as EQ share INT */
  8304. return IRQ_NONE;
  8305. }
  8306. return IRQ_HANDLED;
  8307. } /* lpfc_sli4_sp_intr_handler */
  8308. /**
  8309. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  8310. * @irq: Interrupt number.
  8311. * @dev_id: The device context pointer.
  8312. *
  8313. * This function is directly called from the PCI layer as an interrupt
  8314. * service routine when device with SLI-4 interface spec is enabled with
  8315. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  8316. * ring event in the HBA. However, when the device is enabled with either
  8317. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  8318. * device-level interrupt handler. When the PCI slot is in error recovery
  8319. * or the HBA is undergoing initialization, the interrupt handler will not
  8320. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  8321. * the intrrupt context. This function is called without any lock held.
  8322. * It gets the hbalock to access and update SLI data structures. Note that,
  8323. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  8324. * equal to that of FCP CQ index.
  8325. *
  8326. * This function returns IRQ_HANDLED when interrupt is handled else it
  8327. * returns IRQ_NONE.
  8328. **/
  8329. irqreturn_t
  8330. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  8331. {
  8332. struct lpfc_hba *phba;
  8333. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  8334. struct lpfc_queue *fpeq;
  8335. struct lpfc_eqe *eqe;
  8336. unsigned long iflag;
  8337. int ecount = 0;
  8338. uint32_t fcp_eqidx;
  8339. /* Get the driver's phba structure from the dev_id */
  8340. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  8341. phba = fcp_eq_hdl->phba;
  8342. fcp_eqidx = fcp_eq_hdl->idx;
  8343. if (unlikely(!phba))
  8344. return IRQ_NONE;
  8345. /* Get to the EQ struct associated with this vector */
  8346. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  8347. /* Check device state for handling interrupt */
  8348. if (unlikely(lpfc_intr_state_check(phba))) {
  8349. /* Check again for link_state with lock held */
  8350. spin_lock_irqsave(&phba->hbalock, iflag);
  8351. if (phba->link_state < LPFC_LINK_DOWN)
  8352. /* Flush, clear interrupt, and rearm the EQ */
  8353. lpfc_sli4_eq_flush(phba, fpeq);
  8354. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8355. return IRQ_NONE;
  8356. }
  8357. /*
  8358. * Process all the event on FCP fast-path EQ
  8359. */
  8360. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  8361. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  8362. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8363. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  8364. }
  8365. /* Always clear and re-arm the fast-path EQ */
  8366. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  8367. if (unlikely(ecount == 0)) {
  8368. if (phba->intr_type == MSIX)
  8369. /* MSI-X treated interrupt served as no EQ share INT */
  8370. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8371. "0358 MSI-X interrupt with no EQE\n");
  8372. else
  8373. /* Non MSI-X treated on interrupt as EQ share INT */
  8374. return IRQ_NONE;
  8375. }
  8376. return IRQ_HANDLED;
  8377. } /* lpfc_sli4_fp_intr_handler */
  8378. /**
  8379. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  8380. * @irq: Interrupt number.
  8381. * @dev_id: The device context pointer.
  8382. *
  8383. * This function is the device-level interrupt handler to device with SLI-4
  8384. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  8385. * interrupt mode is enabled and there is an event in the HBA which requires
  8386. * driver attention. This function invokes the slow-path interrupt attention
  8387. * handling function and fast-path interrupt attention handling function in
  8388. * turn to process the relevant HBA attention events. This function is called
  8389. * without any lock held. It gets the hbalock to access and update SLI data
  8390. * structures.
  8391. *
  8392. * This function returns IRQ_HANDLED when interrupt is handled, else it
  8393. * returns IRQ_NONE.
  8394. **/
  8395. irqreturn_t
  8396. lpfc_sli4_intr_handler(int irq, void *dev_id)
  8397. {
  8398. struct lpfc_hba *phba;
  8399. irqreturn_t sp_irq_rc, fp_irq_rc;
  8400. bool fp_handled = false;
  8401. uint32_t fcp_eqidx;
  8402. /* Get the driver's phba structure from the dev_id */
  8403. phba = (struct lpfc_hba *)dev_id;
  8404. if (unlikely(!phba))
  8405. return IRQ_NONE;
  8406. /*
  8407. * Invokes slow-path host attention interrupt handling as appropriate.
  8408. */
  8409. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  8410. /*
  8411. * Invoke fast-path host attention interrupt handling as appropriate.
  8412. */
  8413. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  8414. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  8415. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  8416. if (fp_irq_rc == IRQ_HANDLED)
  8417. fp_handled |= true;
  8418. }
  8419. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  8420. } /* lpfc_sli4_intr_handler */
  8421. /**
  8422. * lpfc_sli4_queue_free - free a queue structure and associated memory
  8423. * @queue: The queue structure to free.
  8424. *
  8425. * This function frees a queue structure and the DMAable memeory used for
  8426. * the host resident queue. This function must be called after destroying the
  8427. * queue on the HBA.
  8428. **/
  8429. void
  8430. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  8431. {
  8432. struct lpfc_dmabuf *dmabuf;
  8433. if (!queue)
  8434. return;
  8435. while (!list_empty(&queue->page_list)) {
  8436. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  8437. list);
  8438. dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
  8439. dmabuf->virt, dmabuf->phys);
  8440. kfree(dmabuf);
  8441. }
  8442. kfree(queue);
  8443. return;
  8444. }
  8445. /**
  8446. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  8447. * @phba: The HBA that this queue is being created on.
  8448. * @entry_size: The size of each queue entry for this queue.
  8449. * @entry count: The number of entries that this queue will handle.
  8450. *
  8451. * This function allocates a queue structure and the DMAable memory used for
  8452. * the host resident queue. This function must be called before creating the
  8453. * queue on the HBA.
  8454. **/
  8455. struct lpfc_queue *
  8456. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  8457. uint32_t entry_count)
  8458. {
  8459. struct lpfc_queue *queue;
  8460. struct lpfc_dmabuf *dmabuf;
  8461. int x, total_qe_count;
  8462. void *dma_pointer;
  8463. queue = kzalloc(sizeof(struct lpfc_queue) +
  8464. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  8465. if (!queue)
  8466. return NULL;
  8467. queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
  8468. INIT_LIST_HEAD(&queue->list);
  8469. INIT_LIST_HEAD(&queue->page_list);
  8470. INIT_LIST_HEAD(&queue->child_list);
  8471. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  8472. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  8473. if (!dmabuf)
  8474. goto out_fail;
  8475. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  8476. PAGE_SIZE, &dmabuf->phys,
  8477. GFP_KERNEL);
  8478. if (!dmabuf->virt) {
  8479. kfree(dmabuf);
  8480. goto out_fail;
  8481. }
  8482. memset(dmabuf->virt, 0, PAGE_SIZE);
  8483. dmabuf->buffer_tag = x;
  8484. list_add_tail(&dmabuf->list, &queue->page_list);
  8485. /* initialize queue's entry array */
  8486. dma_pointer = dmabuf->virt;
  8487. for (; total_qe_count < entry_count &&
  8488. dma_pointer < (PAGE_SIZE + dmabuf->virt);
  8489. total_qe_count++, dma_pointer += entry_size) {
  8490. queue->qe[total_qe_count].address = dma_pointer;
  8491. }
  8492. }
  8493. queue->entry_size = entry_size;
  8494. queue->entry_count = entry_count;
  8495. queue->phba = phba;
  8496. return queue;
  8497. out_fail:
  8498. lpfc_sli4_queue_free(queue);
  8499. return NULL;
  8500. }
  8501. /**
  8502. * lpfc_eq_create - Create an Event Queue on the HBA
  8503. * @phba: HBA structure that indicates port to create a queue on.
  8504. * @eq: The queue structure to use to create the event queue.
  8505. * @imax: The maximum interrupt per second limit.
  8506. *
  8507. * This function creates an event queue, as detailed in @eq, on a port,
  8508. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  8509. *
  8510. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  8511. * is used to get the entry count and entry size that are necessary to
  8512. * determine the number of pages to allocate and use for this queue. This
  8513. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  8514. * event queue. This function is asynchronous and will wait for the mailbox
  8515. * command to finish before continuing.
  8516. *
  8517. * On success this function will return a zero. If unable to allocate enough
  8518. * memory this function will return ENOMEM. If the queue create mailbox command
  8519. * fails this function will return ENXIO.
  8520. **/
  8521. uint32_t
  8522. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  8523. {
  8524. struct lpfc_mbx_eq_create *eq_create;
  8525. LPFC_MBOXQ_t *mbox;
  8526. int rc, length, status = 0;
  8527. struct lpfc_dmabuf *dmabuf;
  8528. uint32_t shdr_status, shdr_add_status;
  8529. union lpfc_sli4_cfg_shdr *shdr;
  8530. uint16_t dmult;
  8531. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8532. if (!mbox)
  8533. return -ENOMEM;
  8534. length = (sizeof(struct lpfc_mbx_eq_create) -
  8535. sizeof(struct lpfc_sli4_cfg_mhdr));
  8536. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  8537. LPFC_MBOX_OPCODE_EQ_CREATE,
  8538. length, LPFC_SLI4_MBX_EMBED);
  8539. eq_create = &mbox->u.mqe.un.eq_create;
  8540. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  8541. eq->page_count);
  8542. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  8543. LPFC_EQE_SIZE);
  8544. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  8545. /* Calculate delay multiper from maximum interrupt per second */
  8546. dmult = LPFC_DMULT_CONST/imax - 1;
  8547. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  8548. dmult);
  8549. switch (eq->entry_count) {
  8550. default:
  8551. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8552. "0360 Unsupported EQ count. (%d)\n",
  8553. eq->entry_count);
  8554. if (eq->entry_count < 256)
  8555. return -EINVAL;
  8556. /* otherwise default to smallest count (drop through) */
  8557. case 256:
  8558. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8559. LPFC_EQ_CNT_256);
  8560. break;
  8561. case 512:
  8562. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8563. LPFC_EQ_CNT_512);
  8564. break;
  8565. case 1024:
  8566. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8567. LPFC_EQ_CNT_1024);
  8568. break;
  8569. case 2048:
  8570. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8571. LPFC_EQ_CNT_2048);
  8572. break;
  8573. case 4096:
  8574. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8575. LPFC_EQ_CNT_4096);
  8576. break;
  8577. }
  8578. list_for_each_entry(dmabuf, &eq->page_list, list) {
  8579. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8580. putPaddrLow(dmabuf->phys);
  8581. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8582. putPaddrHigh(dmabuf->phys);
  8583. }
  8584. mbox->vport = phba->pport;
  8585. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  8586. mbox->context1 = NULL;
  8587. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8588. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  8589. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8590. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8591. if (shdr_status || shdr_add_status || rc) {
  8592. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8593. "2500 EQ_CREATE mailbox failed with "
  8594. "status x%x add_status x%x, mbx status x%x\n",
  8595. shdr_status, shdr_add_status, rc);
  8596. status = -ENXIO;
  8597. }
  8598. eq->type = LPFC_EQ;
  8599. eq->subtype = LPFC_NONE;
  8600. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  8601. if (eq->queue_id == 0xFFFF)
  8602. status = -ENXIO;
  8603. eq->host_index = 0;
  8604. eq->hba_index = 0;
  8605. mempool_free(mbox, phba->mbox_mem_pool);
  8606. return status;
  8607. }
  8608. /**
  8609. * lpfc_cq_create - Create a Completion Queue on the HBA
  8610. * @phba: HBA structure that indicates port to create a queue on.
  8611. * @cq: The queue structure to use to create the completion queue.
  8612. * @eq: The event queue to bind this completion queue to.
  8613. *
  8614. * This function creates a completion queue, as detailed in @wq, on a port,
  8615. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  8616. *
  8617. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  8618. * is used to get the entry count and entry size that are necessary to
  8619. * determine the number of pages to allocate and use for this queue. The @eq
  8620. * is used to indicate which event queue to bind this completion queue to. This
  8621. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  8622. * completion queue. This function is asynchronous and will wait for the mailbox
  8623. * command to finish before continuing.
  8624. *
  8625. * On success this function will return a zero. If unable to allocate enough
  8626. * memory this function will return ENOMEM. If the queue create mailbox command
  8627. * fails this function will return ENXIO.
  8628. **/
  8629. uint32_t
  8630. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  8631. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  8632. {
  8633. struct lpfc_mbx_cq_create *cq_create;
  8634. struct lpfc_dmabuf *dmabuf;
  8635. LPFC_MBOXQ_t *mbox;
  8636. int rc, length, status = 0;
  8637. uint32_t shdr_status, shdr_add_status;
  8638. union lpfc_sli4_cfg_shdr *shdr;
  8639. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8640. if (!mbox)
  8641. return -ENOMEM;
  8642. length = (sizeof(struct lpfc_mbx_cq_create) -
  8643. sizeof(struct lpfc_sli4_cfg_mhdr));
  8644. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  8645. LPFC_MBOX_OPCODE_CQ_CREATE,
  8646. length, LPFC_SLI4_MBX_EMBED);
  8647. cq_create = &mbox->u.mqe.un.cq_create;
  8648. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  8649. cq->page_count);
  8650. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  8651. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  8652. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
  8653. switch (cq->entry_count) {
  8654. default:
  8655. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8656. "0361 Unsupported CQ count. (%d)\n",
  8657. cq->entry_count);
  8658. if (cq->entry_count < 256)
  8659. return -EINVAL;
  8660. /* otherwise default to smallest count (drop through) */
  8661. case 256:
  8662. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  8663. LPFC_CQ_CNT_256);
  8664. break;
  8665. case 512:
  8666. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  8667. LPFC_CQ_CNT_512);
  8668. break;
  8669. case 1024:
  8670. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  8671. LPFC_CQ_CNT_1024);
  8672. break;
  8673. }
  8674. list_for_each_entry(dmabuf, &cq->page_list, list) {
  8675. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8676. putPaddrLow(dmabuf->phys);
  8677. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8678. putPaddrHigh(dmabuf->phys);
  8679. }
  8680. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8681. /* The IOCTL status is embedded in the mailbox subheader. */
  8682. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  8683. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8684. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8685. if (shdr_status || shdr_add_status || rc) {
  8686. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8687. "2501 CQ_CREATE mailbox failed with "
  8688. "status x%x add_status x%x, mbx status x%x\n",
  8689. shdr_status, shdr_add_status, rc);
  8690. status = -ENXIO;
  8691. goto out;
  8692. }
  8693. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  8694. if (cq->queue_id == 0xFFFF) {
  8695. status = -ENXIO;
  8696. goto out;
  8697. }
  8698. /* link the cq onto the parent eq child list */
  8699. list_add_tail(&cq->list, &eq->child_list);
  8700. /* Set up completion queue's type and subtype */
  8701. cq->type = type;
  8702. cq->subtype = subtype;
  8703. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  8704. cq->host_index = 0;
  8705. cq->hba_index = 0;
  8706. out:
  8707. mempool_free(mbox, phba->mbox_mem_pool);
  8708. return status;
  8709. }
  8710. /**
  8711. * lpfc_mq_create - Create a mailbox Queue on the HBA
  8712. * @phba: HBA structure that indicates port to create a queue on.
  8713. * @mq: The queue structure to use to create the mailbox queue.
  8714. *
  8715. * This function creates a mailbox queue, as detailed in @mq, on a port,
  8716. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  8717. *
  8718. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  8719. * is used to get the entry count and entry size that are necessary to
  8720. * determine the number of pages to allocate and use for this queue. This
  8721. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  8722. * mailbox queue. This function is asynchronous and will wait for the mailbox
  8723. * command to finish before continuing.
  8724. *
  8725. * On success this function will return a zero. If unable to allocate enough
  8726. * memory this function will return ENOMEM. If the queue create mailbox command
  8727. * fails this function will return ENXIO.
  8728. **/
  8729. uint32_t
  8730. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  8731. struct lpfc_queue *cq, uint32_t subtype)
  8732. {
  8733. struct lpfc_mbx_mq_create *mq_create;
  8734. struct lpfc_dmabuf *dmabuf;
  8735. LPFC_MBOXQ_t *mbox;
  8736. int rc, length, status = 0;
  8737. uint32_t shdr_status, shdr_add_status;
  8738. union lpfc_sli4_cfg_shdr *shdr;
  8739. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8740. if (!mbox)
  8741. return -ENOMEM;
  8742. length = (sizeof(struct lpfc_mbx_mq_create) -
  8743. sizeof(struct lpfc_sli4_cfg_mhdr));
  8744. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  8745. LPFC_MBOX_OPCODE_MQ_CREATE,
  8746. length, LPFC_SLI4_MBX_EMBED);
  8747. mq_create = &mbox->u.mqe.un.mq_create;
  8748. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  8749. mq->page_count);
  8750. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  8751. cq->queue_id);
  8752. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  8753. switch (mq->entry_count) {
  8754. default:
  8755. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8756. "0362 Unsupported MQ count. (%d)\n",
  8757. mq->entry_count);
  8758. if (mq->entry_count < 16)
  8759. return -EINVAL;
  8760. /* otherwise default to smallest count (drop through) */
  8761. case 16:
  8762. bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
  8763. LPFC_MQ_CNT_16);
  8764. break;
  8765. case 32:
  8766. bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
  8767. LPFC_MQ_CNT_32);
  8768. break;
  8769. case 64:
  8770. bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
  8771. LPFC_MQ_CNT_64);
  8772. break;
  8773. case 128:
  8774. bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
  8775. LPFC_MQ_CNT_128);
  8776. break;
  8777. }
  8778. list_for_each_entry(dmabuf, &mq->page_list, list) {
  8779. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8780. putPaddrLow(dmabuf->phys);
  8781. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8782. putPaddrHigh(dmabuf->phys);
  8783. }
  8784. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8785. /* The IOCTL status is embedded in the mailbox subheader. */
  8786. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  8787. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8788. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8789. if (shdr_status || shdr_add_status || rc) {
  8790. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8791. "2502 MQ_CREATE mailbox failed with "
  8792. "status x%x add_status x%x, mbx status x%x\n",
  8793. shdr_status, shdr_add_status, rc);
  8794. status = -ENXIO;
  8795. goto out;
  8796. }
  8797. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
  8798. if (mq->queue_id == 0xFFFF) {
  8799. status = -ENXIO;
  8800. goto out;
  8801. }
  8802. mq->type = LPFC_MQ;
  8803. mq->subtype = subtype;
  8804. mq->host_index = 0;
  8805. mq->hba_index = 0;
  8806. /* link the mq onto the parent cq child list */
  8807. list_add_tail(&mq->list, &cq->child_list);
  8808. out:
  8809. mempool_free(mbox, phba->mbox_mem_pool);
  8810. return status;
  8811. }
  8812. /**
  8813. * lpfc_wq_create - Create a Work Queue on the HBA
  8814. * @phba: HBA structure that indicates port to create a queue on.
  8815. * @wq: The queue structure to use to create the work queue.
  8816. * @cq: The completion queue to bind this work queue to.
  8817. * @subtype: The subtype of the work queue indicating its functionality.
  8818. *
  8819. * This function creates a work queue, as detailed in @wq, on a port, described
  8820. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  8821. *
  8822. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  8823. * is used to get the entry count and entry size that are necessary to
  8824. * determine the number of pages to allocate and use for this queue. The @cq
  8825. * is used to indicate which completion queue to bind this work queue to. This
  8826. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  8827. * work queue. This function is asynchronous and will wait for the mailbox
  8828. * command to finish before continuing.
  8829. *
  8830. * On success this function will return a zero. If unable to allocate enough
  8831. * memory this function will return ENOMEM. If the queue create mailbox command
  8832. * fails this function will return ENXIO.
  8833. **/
  8834. uint32_t
  8835. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  8836. struct lpfc_queue *cq, uint32_t subtype)
  8837. {
  8838. struct lpfc_mbx_wq_create *wq_create;
  8839. struct lpfc_dmabuf *dmabuf;
  8840. LPFC_MBOXQ_t *mbox;
  8841. int rc, length, status = 0;
  8842. uint32_t shdr_status, shdr_add_status;
  8843. union lpfc_sli4_cfg_shdr *shdr;
  8844. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8845. if (!mbox)
  8846. return -ENOMEM;
  8847. length = (sizeof(struct lpfc_mbx_wq_create) -
  8848. sizeof(struct lpfc_sli4_cfg_mhdr));
  8849. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  8850. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  8851. length, LPFC_SLI4_MBX_EMBED);
  8852. wq_create = &mbox->u.mqe.un.wq_create;
  8853. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  8854. wq->page_count);
  8855. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  8856. cq->queue_id);
  8857. list_for_each_entry(dmabuf, &wq->page_list, list) {
  8858. wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8859. putPaddrLow(dmabuf->phys);
  8860. wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8861. putPaddrHigh(dmabuf->phys);
  8862. }
  8863. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8864. /* The IOCTL status is embedded in the mailbox subheader. */
  8865. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  8866. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8867. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8868. if (shdr_status || shdr_add_status || rc) {
  8869. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8870. "2503 WQ_CREATE mailbox failed with "
  8871. "status x%x add_status x%x, mbx status x%x\n",
  8872. shdr_status, shdr_add_status, rc);
  8873. status = -ENXIO;
  8874. goto out;
  8875. }
  8876. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  8877. if (wq->queue_id == 0xFFFF) {
  8878. status = -ENXIO;
  8879. goto out;
  8880. }
  8881. wq->type = LPFC_WQ;
  8882. wq->subtype = subtype;
  8883. wq->host_index = 0;
  8884. wq->hba_index = 0;
  8885. /* link the wq onto the parent cq child list */
  8886. list_add_tail(&wq->list, &cq->child_list);
  8887. out:
  8888. mempool_free(mbox, phba->mbox_mem_pool);
  8889. return status;
  8890. }
  8891. /**
  8892. * lpfc_rq_create - Create a Receive Queue on the HBA
  8893. * @phba: HBA structure that indicates port to create a queue on.
  8894. * @hrq: The queue structure to use to create the header receive queue.
  8895. * @drq: The queue structure to use to create the data receive queue.
  8896. * @cq: The completion queue to bind this work queue to.
  8897. *
  8898. * This function creates a receive buffer queue pair , as detailed in @hrq and
  8899. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  8900. * to the HBA.
  8901. *
  8902. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  8903. * struct is used to get the entry count that is necessary to determine the
  8904. * number of pages to use for this queue. The @cq is used to indicate which
  8905. * completion queue to bind received buffers that are posted to these queues to.
  8906. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  8907. * receive queue pair. This function is asynchronous and will wait for the
  8908. * mailbox command to finish before continuing.
  8909. *
  8910. * On success this function will return a zero. If unable to allocate enough
  8911. * memory this function will return ENOMEM. If the queue create mailbox command
  8912. * fails this function will return ENXIO.
  8913. **/
  8914. uint32_t
  8915. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  8916. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  8917. {
  8918. struct lpfc_mbx_rq_create *rq_create;
  8919. struct lpfc_dmabuf *dmabuf;
  8920. LPFC_MBOXQ_t *mbox;
  8921. int rc, length, status = 0;
  8922. uint32_t shdr_status, shdr_add_status;
  8923. union lpfc_sli4_cfg_shdr *shdr;
  8924. if (hrq->entry_count != drq->entry_count)
  8925. return -EINVAL;
  8926. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8927. if (!mbox)
  8928. return -ENOMEM;
  8929. length = (sizeof(struct lpfc_mbx_rq_create) -
  8930. sizeof(struct lpfc_sli4_cfg_mhdr));
  8931. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  8932. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  8933. length, LPFC_SLI4_MBX_EMBED);
  8934. rq_create = &mbox->u.mqe.un.rq_create;
  8935. switch (hrq->entry_count) {
  8936. default:
  8937. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8938. "2535 Unsupported RQ count. (%d)\n",
  8939. hrq->entry_count);
  8940. if (hrq->entry_count < 512)
  8941. return -EINVAL;
  8942. /* otherwise default to smallest count (drop through) */
  8943. case 512:
  8944. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  8945. LPFC_RQ_RING_SIZE_512);
  8946. break;
  8947. case 1024:
  8948. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  8949. LPFC_RQ_RING_SIZE_1024);
  8950. break;
  8951. case 2048:
  8952. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  8953. LPFC_RQ_RING_SIZE_2048);
  8954. break;
  8955. case 4096:
  8956. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  8957. LPFC_RQ_RING_SIZE_4096);
  8958. break;
  8959. }
  8960. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  8961. cq->queue_id);
  8962. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  8963. hrq->page_count);
  8964. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  8965. LPFC_HDR_BUF_SIZE);
  8966. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  8967. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8968. putPaddrLow(dmabuf->phys);
  8969. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8970. putPaddrHigh(dmabuf->phys);
  8971. }
  8972. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8973. /* The IOCTL status is embedded in the mailbox subheader. */
  8974. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  8975. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8976. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8977. if (shdr_status || shdr_add_status || rc) {
  8978. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8979. "2504 RQ_CREATE mailbox failed with "
  8980. "status x%x add_status x%x, mbx status x%x\n",
  8981. shdr_status, shdr_add_status, rc);
  8982. status = -ENXIO;
  8983. goto out;
  8984. }
  8985. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  8986. if (hrq->queue_id == 0xFFFF) {
  8987. status = -ENXIO;
  8988. goto out;
  8989. }
  8990. hrq->type = LPFC_HRQ;
  8991. hrq->subtype = subtype;
  8992. hrq->host_index = 0;
  8993. hrq->hba_index = 0;
  8994. /* now create the data queue */
  8995. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  8996. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  8997. length, LPFC_SLI4_MBX_EMBED);
  8998. switch (drq->entry_count) {
  8999. default:
  9000. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9001. "2536 Unsupported RQ count. (%d)\n",
  9002. drq->entry_count);
  9003. if (drq->entry_count < 512)
  9004. return -EINVAL;
  9005. /* otherwise default to smallest count (drop through) */
  9006. case 512:
  9007. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  9008. LPFC_RQ_RING_SIZE_512);
  9009. break;
  9010. case 1024:
  9011. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  9012. LPFC_RQ_RING_SIZE_1024);
  9013. break;
  9014. case 2048:
  9015. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  9016. LPFC_RQ_RING_SIZE_2048);
  9017. break;
  9018. case 4096:
  9019. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  9020. LPFC_RQ_RING_SIZE_4096);
  9021. break;
  9022. }
  9023. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  9024. cq->queue_id);
  9025. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  9026. drq->page_count);
  9027. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  9028. LPFC_DATA_BUF_SIZE);
  9029. list_for_each_entry(dmabuf, &drq->page_list, list) {
  9030. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  9031. putPaddrLow(dmabuf->phys);
  9032. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  9033. putPaddrHigh(dmabuf->phys);
  9034. }
  9035. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9036. /* The IOCTL status is embedded in the mailbox subheader. */
  9037. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  9038. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9039. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9040. if (shdr_status || shdr_add_status || rc) {
  9041. status = -ENXIO;
  9042. goto out;
  9043. }
  9044. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  9045. if (drq->queue_id == 0xFFFF) {
  9046. status = -ENXIO;
  9047. goto out;
  9048. }
  9049. drq->type = LPFC_DRQ;
  9050. drq->subtype = subtype;
  9051. drq->host_index = 0;
  9052. drq->hba_index = 0;
  9053. /* link the header and data RQs onto the parent cq child list */
  9054. list_add_tail(&hrq->list, &cq->child_list);
  9055. list_add_tail(&drq->list, &cq->child_list);
  9056. out:
  9057. mempool_free(mbox, phba->mbox_mem_pool);
  9058. return status;
  9059. }
  9060. /**
  9061. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  9062. * @eq: The queue structure associated with the queue to destroy.
  9063. *
  9064. * This function destroys a queue, as detailed in @eq by sending an mailbox
  9065. * command, specific to the type of queue, to the HBA.
  9066. *
  9067. * The @eq struct is used to get the queue ID of the queue to destroy.
  9068. *
  9069. * On success this function will return a zero. If the queue destroy mailbox
  9070. * command fails this function will return ENXIO.
  9071. **/
  9072. uint32_t
  9073. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  9074. {
  9075. LPFC_MBOXQ_t *mbox;
  9076. int rc, length, status = 0;
  9077. uint32_t shdr_status, shdr_add_status;
  9078. union lpfc_sli4_cfg_shdr *shdr;
  9079. if (!eq)
  9080. return -ENODEV;
  9081. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  9082. if (!mbox)
  9083. return -ENOMEM;
  9084. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  9085. sizeof(struct lpfc_sli4_cfg_mhdr));
  9086. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  9087. LPFC_MBOX_OPCODE_EQ_DESTROY,
  9088. length, LPFC_SLI4_MBX_EMBED);
  9089. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  9090. eq->queue_id);
  9091. mbox->vport = eq->phba->pport;
  9092. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9093. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  9094. /* The IOCTL status is embedded in the mailbox subheader. */
  9095. shdr = (union lpfc_sli4_cfg_shdr *)
  9096. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  9097. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9098. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9099. if (shdr_status || shdr_add_status || rc) {
  9100. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9101. "2505 EQ_DESTROY mailbox failed with "
  9102. "status x%x add_status x%x, mbx status x%x\n",
  9103. shdr_status, shdr_add_status, rc);
  9104. status = -ENXIO;
  9105. }
  9106. /* Remove eq from any list */
  9107. list_del_init(&eq->list);
  9108. mempool_free(mbox, eq->phba->mbox_mem_pool);
  9109. return status;
  9110. }
  9111. /**
  9112. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  9113. * @cq: The queue structure associated with the queue to destroy.
  9114. *
  9115. * This function destroys a queue, as detailed in @cq by sending an mailbox
  9116. * command, specific to the type of queue, to the HBA.
  9117. *
  9118. * The @cq struct is used to get the queue ID of the queue to destroy.
  9119. *
  9120. * On success this function will return a zero. If the queue destroy mailbox
  9121. * command fails this function will return ENXIO.
  9122. **/
  9123. uint32_t
  9124. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  9125. {
  9126. LPFC_MBOXQ_t *mbox;
  9127. int rc, length, status = 0;
  9128. uint32_t shdr_status, shdr_add_status;
  9129. union lpfc_sli4_cfg_shdr *shdr;
  9130. if (!cq)
  9131. return -ENODEV;
  9132. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  9133. if (!mbox)
  9134. return -ENOMEM;
  9135. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  9136. sizeof(struct lpfc_sli4_cfg_mhdr));
  9137. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  9138. LPFC_MBOX_OPCODE_CQ_DESTROY,
  9139. length, LPFC_SLI4_MBX_EMBED);
  9140. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  9141. cq->queue_id);
  9142. mbox->vport = cq->phba->pport;
  9143. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9144. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  9145. /* The IOCTL status is embedded in the mailbox subheader. */
  9146. shdr = (union lpfc_sli4_cfg_shdr *)
  9147. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  9148. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9149. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9150. if (shdr_status || shdr_add_status || rc) {
  9151. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9152. "2506 CQ_DESTROY mailbox failed with "
  9153. "status x%x add_status x%x, mbx status x%x\n",
  9154. shdr_status, shdr_add_status, rc);
  9155. status = -ENXIO;
  9156. }
  9157. /* Remove cq from any list */
  9158. list_del_init(&cq->list);
  9159. mempool_free(mbox, cq->phba->mbox_mem_pool);
  9160. return status;
  9161. }
  9162. /**
  9163. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  9164. * @qm: The queue structure associated with the queue to destroy.
  9165. *
  9166. * This function destroys a queue, as detailed in @mq by sending an mailbox
  9167. * command, specific to the type of queue, to the HBA.
  9168. *
  9169. * The @mq struct is used to get the queue ID of the queue to destroy.
  9170. *
  9171. * On success this function will return a zero. If the queue destroy mailbox
  9172. * command fails this function will return ENXIO.
  9173. **/
  9174. uint32_t
  9175. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  9176. {
  9177. LPFC_MBOXQ_t *mbox;
  9178. int rc, length, status = 0;
  9179. uint32_t shdr_status, shdr_add_status;
  9180. union lpfc_sli4_cfg_shdr *shdr;
  9181. if (!mq)
  9182. return -ENODEV;
  9183. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  9184. if (!mbox)
  9185. return -ENOMEM;
  9186. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  9187. sizeof(struct lpfc_sli4_cfg_mhdr));
  9188. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  9189. LPFC_MBOX_OPCODE_MQ_DESTROY,
  9190. length, LPFC_SLI4_MBX_EMBED);
  9191. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  9192. mq->queue_id);
  9193. mbox->vport = mq->phba->pport;
  9194. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9195. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  9196. /* The IOCTL status is embedded in the mailbox subheader. */
  9197. shdr = (union lpfc_sli4_cfg_shdr *)
  9198. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  9199. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9200. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9201. if (shdr_status || shdr_add_status || rc) {
  9202. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9203. "2507 MQ_DESTROY mailbox failed with "
  9204. "status x%x add_status x%x, mbx status x%x\n",
  9205. shdr_status, shdr_add_status, rc);
  9206. status = -ENXIO;
  9207. }
  9208. /* Remove mq from any list */
  9209. list_del_init(&mq->list);
  9210. mempool_free(mbox, mq->phba->mbox_mem_pool);
  9211. return status;
  9212. }
  9213. /**
  9214. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  9215. * @wq: The queue structure associated with the queue to destroy.
  9216. *
  9217. * This function destroys a queue, as detailed in @wq by sending an mailbox
  9218. * command, specific to the type of queue, to the HBA.
  9219. *
  9220. * The @wq struct is used to get the queue ID of the queue to destroy.
  9221. *
  9222. * On success this function will return a zero. If the queue destroy mailbox
  9223. * command fails this function will return ENXIO.
  9224. **/
  9225. uint32_t
  9226. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  9227. {
  9228. LPFC_MBOXQ_t *mbox;
  9229. int rc, length, status = 0;
  9230. uint32_t shdr_status, shdr_add_status;
  9231. union lpfc_sli4_cfg_shdr *shdr;
  9232. if (!wq)
  9233. return -ENODEV;
  9234. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  9235. if (!mbox)
  9236. return -ENOMEM;
  9237. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  9238. sizeof(struct lpfc_sli4_cfg_mhdr));
  9239. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9240. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  9241. length, LPFC_SLI4_MBX_EMBED);
  9242. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  9243. wq->queue_id);
  9244. mbox->vport = wq->phba->pport;
  9245. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9246. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  9247. shdr = (union lpfc_sli4_cfg_shdr *)
  9248. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  9249. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9250. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9251. if (shdr_status || shdr_add_status || rc) {
  9252. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9253. "2508 WQ_DESTROY mailbox failed with "
  9254. "status x%x add_status x%x, mbx status x%x\n",
  9255. shdr_status, shdr_add_status, rc);
  9256. status = -ENXIO;
  9257. }
  9258. /* Remove wq from any list */
  9259. list_del_init(&wq->list);
  9260. mempool_free(mbox, wq->phba->mbox_mem_pool);
  9261. return status;
  9262. }
  9263. /**
  9264. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  9265. * @rq: The queue structure associated with the queue to destroy.
  9266. *
  9267. * This function destroys a queue, as detailed in @rq by sending an mailbox
  9268. * command, specific to the type of queue, to the HBA.
  9269. *
  9270. * The @rq struct is used to get the queue ID of the queue to destroy.
  9271. *
  9272. * On success this function will return a zero. If the queue destroy mailbox
  9273. * command fails this function will return ENXIO.
  9274. **/
  9275. uint32_t
  9276. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  9277. struct lpfc_queue *drq)
  9278. {
  9279. LPFC_MBOXQ_t *mbox;
  9280. int rc, length, status = 0;
  9281. uint32_t shdr_status, shdr_add_status;
  9282. union lpfc_sli4_cfg_shdr *shdr;
  9283. if (!hrq || !drq)
  9284. return -ENODEV;
  9285. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  9286. if (!mbox)
  9287. return -ENOMEM;
  9288. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  9289. sizeof(struct mbox_header));
  9290. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9291. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  9292. length, LPFC_SLI4_MBX_EMBED);
  9293. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  9294. hrq->queue_id);
  9295. mbox->vport = hrq->phba->pport;
  9296. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9297. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  9298. /* The IOCTL status is embedded in the mailbox subheader. */
  9299. shdr = (union lpfc_sli4_cfg_shdr *)
  9300. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  9301. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9302. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9303. if (shdr_status || shdr_add_status || rc) {
  9304. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9305. "2509 RQ_DESTROY mailbox failed with "
  9306. "status x%x add_status x%x, mbx status x%x\n",
  9307. shdr_status, shdr_add_status, rc);
  9308. if (rc != MBX_TIMEOUT)
  9309. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  9310. return -ENXIO;
  9311. }
  9312. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  9313. drq->queue_id);
  9314. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  9315. shdr = (union lpfc_sli4_cfg_shdr *)
  9316. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  9317. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9318. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9319. if (shdr_status || shdr_add_status || rc) {
  9320. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9321. "2510 RQ_DESTROY mailbox failed with "
  9322. "status x%x add_status x%x, mbx status x%x\n",
  9323. shdr_status, shdr_add_status, rc);
  9324. status = -ENXIO;
  9325. }
  9326. list_del_init(&hrq->list);
  9327. list_del_init(&drq->list);
  9328. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  9329. return status;
  9330. }
  9331. /**
  9332. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  9333. * @phba: The virtual port for which this call being executed.
  9334. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  9335. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  9336. * @xritag: the xritag that ties this io to the SGL pages.
  9337. *
  9338. * This routine will post the sgl pages for the IO that has the xritag
  9339. * that is in the iocbq structure. The xritag is assigned during iocbq
  9340. * creation and persists for as long as the driver is loaded.
  9341. * if the caller has fewer than 256 scatter gather segments to map then
  9342. * pdma_phys_addr1 should be 0.
  9343. * If the caller needs to map more than 256 scatter gather segment then
  9344. * pdma_phys_addr1 should be a valid physical address.
  9345. * physical address for SGLs must be 64 byte aligned.
  9346. * If you are going to map 2 SGL's then the first one must have 256 entries
  9347. * the second sgl can have between 1 and 256 entries.
  9348. *
  9349. * Return codes:
  9350. * 0 - Success
  9351. * -ENXIO, -ENOMEM - Failure
  9352. **/
  9353. int
  9354. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  9355. dma_addr_t pdma_phys_addr0,
  9356. dma_addr_t pdma_phys_addr1,
  9357. uint16_t xritag)
  9358. {
  9359. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  9360. LPFC_MBOXQ_t *mbox;
  9361. int rc;
  9362. uint32_t shdr_status, shdr_add_status;
  9363. union lpfc_sli4_cfg_shdr *shdr;
  9364. if (xritag == NO_XRI) {
  9365. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9366. "0364 Invalid param:\n");
  9367. return -EINVAL;
  9368. }
  9369. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9370. if (!mbox)
  9371. return -ENOMEM;
  9372. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9373. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  9374. sizeof(struct lpfc_mbx_post_sgl_pages) -
  9375. sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
  9376. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  9377. &mbox->u.mqe.un.post_sgl_pages;
  9378. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  9379. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  9380. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  9381. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  9382. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  9383. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  9384. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  9385. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  9386. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  9387. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  9388. if (!phba->sli4_hba.intr_enable)
  9389. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9390. else
  9391. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  9392. /* The IOCTL status is embedded in the mailbox subheader. */
  9393. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  9394. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9395. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9396. if (rc != MBX_TIMEOUT)
  9397. mempool_free(mbox, phba->mbox_mem_pool);
  9398. if (shdr_status || shdr_add_status || rc) {
  9399. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9400. "2511 POST_SGL mailbox failed with "
  9401. "status x%x add_status x%x, mbx status x%x\n",
  9402. shdr_status, shdr_add_status, rc);
  9403. rc = -ENXIO;
  9404. }
  9405. return 0;
  9406. }
  9407. /**
  9408. * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
  9409. * @phba: The virtual port for which this call being executed.
  9410. *
  9411. * This routine will remove all of the sgl pages registered with the hba.
  9412. *
  9413. * Return codes:
  9414. * 0 - Success
  9415. * -ENXIO, -ENOMEM - Failure
  9416. **/
  9417. int
  9418. lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
  9419. {
  9420. LPFC_MBOXQ_t *mbox;
  9421. int rc;
  9422. uint32_t shdr_status, shdr_add_status;
  9423. union lpfc_sli4_cfg_shdr *shdr;
  9424. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9425. if (!mbox)
  9426. return -ENOMEM;
  9427. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9428. LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
  9429. LPFC_SLI4_MBX_EMBED);
  9430. if (!phba->sli4_hba.intr_enable)
  9431. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9432. else
  9433. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  9434. /* The IOCTL status is embedded in the mailbox subheader. */
  9435. shdr = (union lpfc_sli4_cfg_shdr *)
  9436. &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
  9437. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9438. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9439. if (rc != MBX_TIMEOUT)
  9440. mempool_free(mbox, phba->mbox_mem_pool);
  9441. if (shdr_status || shdr_add_status || rc) {
  9442. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9443. "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
  9444. "status x%x add_status x%x, mbx status x%x\n",
  9445. shdr_status, shdr_add_status, rc);
  9446. rc = -ENXIO;
  9447. }
  9448. return rc;
  9449. }
  9450. /**
  9451. * lpfc_sli4_next_xritag - Get an xritag for the io
  9452. * @phba: Pointer to HBA context object.
  9453. *
  9454. * This function gets an xritag for the iocb. If there is no unused xritag
  9455. * it will return 0xffff.
  9456. * The function returns the allocated xritag if successful, else returns zero.
  9457. * Zero is not a valid xritag.
  9458. * The caller is not required to hold any lock.
  9459. **/
  9460. uint16_t
  9461. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  9462. {
  9463. uint16_t xritag;
  9464. spin_lock_irq(&phba->hbalock);
  9465. xritag = phba->sli4_hba.next_xri;
  9466. if ((xritag != (uint16_t) -1) && xritag <
  9467. (phba->sli4_hba.max_cfg_param.max_xri
  9468. + phba->sli4_hba.max_cfg_param.xri_base)) {
  9469. phba->sli4_hba.next_xri++;
  9470. phba->sli4_hba.max_cfg_param.xri_used++;
  9471. spin_unlock_irq(&phba->hbalock);
  9472. return xritag;
  9473. }
  9474. spin_unlock_irq(&phba->hbalock);
  9475. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9476. "2004 Failed to allocate XRI.last XRITAG is %d"
  9477. " Max XRI is %d, Used XRI is %d\n",
  9478. phba->sli4_hba.next_xri,
  9479. phba->sli4_hba.max_cfg_param.max_xri,
  9480. phba->sli4_hba.max_cfg_param.xri_used);
  9481. return -1;
  9482. }
  9483. /**
  9484. * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
  9485. * @phba: pointer to lpfc hba data structure.
  9486. *
  9487. * This routine is invoked to post a block of driver's sgl pages to the
  9488. * HBA using non-embedded mailbox command. No Lock is held. This routine
  9489. * is only called when the driver is loading and after all IO has been
  9490. * stopped.
  9491. **/
  9492. int
  9493. lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
  9494. {
  9495. struct lpfc_sglq *sglq_entry;
  9496. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  9497. struct sgl_page_pairs *sgl_pg_pairs;
  9498. void *viraddr;
  9499. LPFC_MBOXQ_t *mbox;
  9500. uint32_t reqlen, alloclen, pg_pairs;
  9501. uint32_t mbox_tmo;
  9502. uint16_t xritag_start = 0;
  9503. int els_xri_cnt, rc = 0;
  9504. uint32_t shdr_status, shdr_add_status;
  9505. union lpfc_sli4_cfg_shdr *shdr;
  9506. /* The number of sgls to be posted */
  9507. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  9508. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  9509. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  9510. if (reqlen > PAGE_SIZE) {
  9511. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  9512. "2559 Block sgl registration required DMA "
  9513. "size (%d) great than a page\n", reqlen);
  9514. return -ENOMEM;
  9515. }
  9516. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9517. if (!mbox) {
  9518. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9519. "2560 Failed to allocate mbox cmd memory\n");
  9520. return -ENOMEM;
  9521. }
  9522. /* Allocate DMA memory and set up the non-embedded mailbox command */
  9523. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9524. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  9525. LPFC_SLI4_MBX_NEMBED);
  9526. if (alloclen < reqlen) {
  9527. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9528. "0285 Allocated DMA memory size (%d) is "
  9529. "less than the requested DMA memory "
  9530. "size (%d)\n", alloclen, reqlen);
  9531. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9532. return -ENOMEM;
  9533. }
  9534. /* Get the first SGE entry from the non-embedded DMA memory */
  9535. if (unlikely(!mbox->sge_array)) {
  9536. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9537. "2525 Failed to get the non-embedded SGE "
  9538. "virtual address\n");
  9539. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9540. return -ENOMEM;
  9541. }
  9542. viraddr = mbox->sge_array->addr[0];
  9543. /* Set up the SGL pages in the non-embedded DMA pages */
  9544. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  9545. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  9546. for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
  9547. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
  9548. /* Set up the sge entry */
  9549. sgl_pg_pairs->sgl_pg0_addr_lo =
  9550. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  9551. sgl_pg_pairs->sgl_pg0_addr_hi =
  9552. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  9553. sgl_pg_pairs->sgl_pg1_addr_lo =
  9554. cpu_to_le32(putPaddrLow(0));
  9555. sgl_pg_pairs->sgl_pg1_addr_hi =
  9556. cpu_to_le32(putPaddrHigh(0));
  9557. /* Keep the first xritag on the list */
  9558. if (pg_pairs == 0)
  9559. xritag_start = sglq_entry->sli4_xritag;
  9560. sgl_pg_pairs++;
  9561. }
  9562. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  9563. pg_pairs = (pg_pairs > 0) ? (pg_pairs - 1) : pg_pairs;
  9564. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  9565. /* Perform endian conversion if necessary */
  9566. sgl->word0 = cpu_to_le32(sgl->word0);
  9567. if (!phba->sli4_hba.intr_enable)
  9568. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9569. else {
  9570. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  9571. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  9572. }
  9573. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  9574. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9575. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9576. if (rc != MBX_TIMEOUT)
  9577. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9578. if (shdr_status || shdr_add_status || rc) {
  9579. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9580. "2513 POST_SGL_BLOCK mailbox command failed "
  9581. "status x%x add_status x%x mbx status x%x\n",
  9582. shdr_status, shdr_add_status, rc);
  9583. rc = -ENXIO;
  9584. }
  9585. return rc;
  9586. }
  9587. /**
  9588. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  9589. * @phba: pointer to lpfc hba data structure.
  9590. * @sblist: pointer to scsi buffer list.
  9591. * @count: number of scsi buffers on the list.
  9592. *
  9593. * This routine is invoked to post a block of @count scsi sgl pages from a
  9594. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  9595. * No Lock is held.
  9596. *
  9597. **/
  9598. int
  9599. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
  9600. int cnt)
  9601. {
  9602. struct lpfc_scsi_buf *psb;
  9603. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  9604. struct sgl_page_pairs *sgl_pg_pairs;
  9605. void *viraddr;
  9606. LPFC_MBOXQ_t *mbox;
  9607. uint32_t reqlen, alloclen, pg_pairs;
  9608. uint32_t mbox_tmo;
  9609. uint16_t xritag_start = 0;
  9610. int rc = 0;
  9611. uint32_t shdr_status, shdr_add_status;
  9612. dma_addr_t pdma_phys_bpl1;
  9613. union lpfc_sli4_cfg_shdr *shdr;
  9614. /* Calculate the requested length of the dma memory */
  9615. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  9616. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  9617. if (reqlen > PAGE_SIZE) {
  9618. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  9619. "0217 Block sgl registration required DMA "
  9620. "size (%d) great than a page\n", reqlen);
  9621. return -ENOMEM;
  9622. }
  9623. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9624. if (!mbox) {
  9625. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9626. "0283 Failed to allocate mbox cmd memory\n");
  9627. return -ENOMEM;
  9628. }
  9629. /* Allocate DMA memory and set up the non-embedded mailbox command */
  9630. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9631. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  9632. LPFC_SLI4_MBX_NEMBED);
  9633. if (alloclen < reqlen) {
  9634. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9635. "2561 Allocated DMA memory size (%d) is "
  9636. "less than the requested DMA memory "
  9637. "size (%d)\n", alloclen, reqlen);
  9638. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9639. return -ENOMEM;
  9640. }
  9641. /* Get the first SGE entry from the non-embedded DMA memory */
  9642. if (unlikely(!mbox->sge_array)) {
  9643. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9644. "2565 Failed to get the non-embedded SGE "
  9645. "virtual address\n");
  9646. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9647. return -ENOMEM;
  9648. }
  9649. viraddr = mbox->sge_array->addr[0];
  9650. /* Set up the SGL pages in the non-embedded DMA pages */
  9651. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  9652. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  9653. pg_pairs = 0;
  9654. list_for_each_entry(psb, sblist, list) {
  9655. /* Set up the sge entry */
  9656. sgl_pg_pairs->sgl_pg0_addr_lo =
  9657. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  9658. sgl_pg_pairs->sgl_pg0_addr_hi =
  9659. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  9660. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  9661. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  9662. else
  9663. pdma_phys_bpl1 = 0;
  9664. sgl_pg_pairs->sgl_pg1_addr_lo =
  9665. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  9666. sgl_pg_pairs->sgl_pg1_addr_hi =
  9667. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  9668. /* Keep the first xritag on the list */
  9669. if (pg_pairs == 0)
  9670. xritag_start = psb->cur_iocbq.sli4_xritag;
  9671. sgl_pg_pairs++;
  9672. pg_pairs++;
  9673. }
  9674. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  9675. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  9676. /* Perform endian conversion if necessary */
  9677. sgl->word0 = cpu_to_le32(sgl->word0);
  9678. if (!phba->sli4_hba.intr_enable)
  9679. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9680. else {
  9681. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  9682. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  9683. }
  9684. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  9685. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9686. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9687. if (rc != MBX_TIMEOUT)
  9688. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9689. if (shdr_status || shdr_add_status || rc) {
  9690. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9691. "2564 POST_SGL_BLOCK mailbox command failed "
  9692. "status x%x add_status x%x mbx status x%x\n",
  9693. shdr_status, shdr_add_status, rc);
  9694. rc = -ENXIO;
  9695. }
  9696. return rc;
  9697. }
  9698. /**
  9699. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  9700. * @phba: pointer to lpfc_hba struct that the frame was received on
  9701. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  9702. *
  9703. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  9704. * valid type of frame that the LPFC driver will handle. This function will
  9705. * return a zero if the frame is a valid frame or a non zero value when the
  9706. * frame does not pass the check.
  9707. **/
  9708. static int
  9709. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  9710. {
  9711. char *rctl_names[] = FC_RCTL_NAMES_INIT;
  9712. char *type_names[] = FC_TYPE_NAMES_INIT;
  9713. struct fc_vft_header *fc_vft_hdr;
  9714. switch (fc_hdr->fh_r_ctl) {
  9715. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  9716. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  9717. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  9718. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  9719. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  9720. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  9721. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  9722. case FC_RCTL_DD_CMD_STATUS: /* command status */
  9723. case FC_RCTL_ELS_REQ: /* extended link services request */
  9724. case FC_RCTL_ELS_REP: /* extended link services reply */
  9725. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  9726. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  9727. case FC_RCTL_BA_NOP: /* basic link service NOP */
  9728. case FC_RCTL_BA_ABTS: /* basic link service abort */
  9729. case FC_RCTL_BA_RMC: /* remove connection */
  9730. case FC_RCTL_BA_ACC: /* basic accept */
  9731. case FC_RCTL_BA_RJT: /* basic reject */
  9732. case FC_RCTL_BA_PRMT:
  9733. case FC_RCTL_ACK_1: /* acknowledge_1 */
  9734. case FC_RCTL_ACK_0: /* acknowledge_0 */
  9735. case FC_RCTL_P_RJT: /* port reject */
  9736. case FC_RCTL_F_RJT: /* fabric reject */
  9737. case FC_RCTL_P_BSY: /* port busy */
  9738. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  9739. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  9740. case FC_RCTL_LCR: /* link credit reset */
  9741. case FC_RCTL_END: /* end */
  9742. break;
  9743. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  9744. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  9745. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  9746. return lpfc_fc_frame_check(phba, fc_hdr);
  9747. default:
  9748. goto drop;
  9749. }
  9750. switch (fc_hdr->fh_type) {
  9751. case FC_TYPE_BLS:
  9752. case FC_TYPE_ELS:
  9753. case FC_TYPE_FCP:
  9754. case FC_TYPE_CT:
  9755. break;
  9756. case FC_TYPE_IP:
  9757. case FC_TYPE_ILS:
  9758. default:
  9759. goto drop;
  9760. }
  9761. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  9762. "2538 Received frame rctl:%s type:%s\n",
  9763. rctl_names[fc_hdr->fh_r_ctl],
  9764. type_names[fc_hdr->fh_type]);
  9765. return 0;
  9766. drop:
  9767. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  9768. "2539 Dropped frame rctl:%s type:%s\n",
  9769. rctl_names[fc_hdr->fh_r_ctl],
  9770. type_names[fc_hdr->fh_type]);
  9771. return 1;
  9772. }
  9773. /**
  9774. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  9775. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  9776. *
  9777. * This function processes the FC header to retrieve the VFI from the VF
  9778. * header, if one exists. This function will return the VFI if one exists
  9779. * or 0 if no VSAN Header exists.
  9780. **/
  9781. static uint32_t
  9782. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  9783. {
  9784. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  9785. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  9786. return 0;
  9787. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  9788. }
  9789. /**
  9790. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  9791. * @phba: Pointer to the HBA structure to search for the vport on
  9792. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  9793. * @fcfi: The FC Fabric ID that the frame came from
  9794. *
  9795. * This function searches the @phba for a vport that matches the content of the
  9796. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  9797. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  9798. * returns the matching vport pointer or NULL if unable to match frame to a
  9799. * vport.
  9800. **/
  9801. static struct lpfc_vport *
  9802. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  9803. uint16_t fcfi)
  9804. {
  9805. struct lpfc_vport **vports;
  9806. struct lpfc_vport *vport = NULL;
  9807. int i;
  9808. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  9809. fc_hdr->fh_d_id[1] << 8 |
  9810. fc_hdr->fh_d_id[2]);
  9811. vports = lpfc_create_vport_work_array(phba);
  9812. if (vports != NULL)
  9813. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  9814. if (phba->fcf.fcfi == fcfi &&
  9815. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  9816. vports[i]->fc_myDID == did) {
  9817. vport = vports[i];
  9818. break;
  9819. }
  9820. }
  9821. lpfc_destroy_vport_work_array(phba, vports);
  9822. return vport;
  9823. }
  9824. /**
  9825. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  9826. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  9827. *
  9828. * This function searches through the existing incomplete sequences that have
  9829. * been sent to this @vport. If the frame matches one of the incomplete
  9830. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  9831. * make up that sequence. If no sequence is found that matches this frame then
  9832. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  9833. * This function returns a pointer to the first dmabuf in the sequence list that
  9834. * the frame was linked to.
  9835. **/
  9836. static struct hbq_dmabuf *
  9837. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  9838. {
  9839. struct fc_frame_header *new_hdr;
  9840. struct fc_frame_header *temp_hdr;
  9841. struct lpfc_dmabuf *d_buf;
  9842. struct lpfc_dmabuf *h_buf;
  9843. struct hbq_dmabuf *seq_dmabuf = NULL;
  9844. struct hbq_dmabuf *temp_dmabuf = NULL;
  9845. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  9846. /* Use the hdr_buf to find the sequence that this frame belongs to */
  9847. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  9848. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  9849. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  9850. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  9851. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  9852. continue;
  9853. /* found a pending sequence that matches this frame */
  9854. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  9855. break;
  9856. }
  9857. if (!seq_dmabuf) {
  9858. /*
  9859. * This indicates first frame received for this sequence.
  9860. * Queue the buffer on the vport's rcv_buffer_list.
  9861. */
  9862. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  9863. return dmabuf;
  9864. }
  9865. temp_hdr = seq_dmabuf->hbuf.virt;
  9866. if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
  9867. list_add(&seq_dmabuf->dbuf.list, &dmabuf->dbuf.list);
  9868. return dmabuf;
  9869. }
  9870. /* find the correct place in the sequence to insert this frame */
  9871. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  9872. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  9873. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  9874. /*
  9875. * If the frame's sequence count is greater than the frame on
  9876. * the list then insert the frame right after this frame
  9877. */
  9878. if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
  9879. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  9880. return seq_dmabuf;
  9881. }
  9882. }
  9883. return NULL;
  9884. }
  9885. /**
  9886. * lpfc_seq_complete - Indicates if a sequence is complete
  9887. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  9888. *
  9889. * This function checks the sequence, starting with the frame described by
  9890. * @dmabuf, to see if all the frames associated with this sequence are present.
  9891. * the frames associated with this sequence are linked to the @dmabuf using the
  9892. * dbuf list. This function looks for two major things. 1) That the first frame
  9893. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  9894. * set. 3) That there are no holes in the sequence count. The function will
  9895. * return 1 when the sequence is complete, otherwise it will return 0.
  9896. **/
  9897. static int
  9898. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  9899. {
  9900. struct fc_frame_header *hdr;
  9901. struct lpfc_dmabuf *d_buf;
  9902. struct hbq_dmabuf *seq_dmabuf;
  9903. uint32_t fctl;
  9904. int seq_count = 0;
  9905. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  9906. /* make sure first fame of sequence has a sequence count of zero */
  9907. if (hdr->fh_seq_cnt != seq_count)
  9908. return 0;
  9909. fctl = (hdr->fh_f_ctl[0] << 16 |
  9910. hdr->fh_f_ctl[1] << 8 |
  9911. hdr->fh_f_ctl[2]);
  9912. /* If last frame of sequence we can return success. */
  9913. if (fctl & FC_FC_END_SEQ)
  9914. return 1;
  9915. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  9916. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  9917. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  9918. /* If there is a hole in the sequence count then fail. */
  9919. if (++seq_count != hdr->fh_seq_cnt)
  9920. return 0;
  9921. fctl = (hdr->fh_f_ctl[0] << 16 |
  9922. hdr->fh_f_ctl[1] << 8 |
  9923. hdr->fh_f_ctl[2]);
  9924. /* If last frame of sequence we can return success. */
  9925. if (fctl & FC_FC_END_SEQ)
  9926. return 1;
  9927. }
  9928. return 0;
  9929. }
  9930. /**
  9931. * lpfc_prep_seq - Prep sequence for ULP processing
  9932. * @vport: Pointer to the vport on which this sequence was received
  9933. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  9934. *
  9935. * This function takes a sequence, described by a list of frames, and creates
  9936. * a list of iocbq structures to describe the sequence. This iocbq list will be
  9937. * used to issue to the generic unsolicited sequence handler. This routine
  9938. * returns a pointer to the first iocbq in the list. If the function is unable
  9939. * to allocate an iocbq then it throw out the received frames that were not
  9940. * able to be described and return a pointer to the first iocbq. If unable to
  9941. * allocate any iocbqs (including the first) this function will return NULL.
  9942. **/
  9943. static struct lpfc_iocbq *
  9944. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  9945. {
  9946. struct lpfc_dmabuf *d_buf, *n_buf;
  9947. struct lpfc_iocbq *first_iocbq, *iocbq;
  9948. struct fc_frame_header *fc_hdr;
  9949. uint32_t sid;
  9950. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  9951. /* remove from receive buffer list */
  9952. list_del_init(&seq_dmabuf->hbuf.list);
  9953. /* get the Remote Port's SID */
  9954. sid = (fc_hdr->fh_s_id[0] << 16 |
  9955. fc_hdr->fh_s_id[1] << 8 |
  9956. fc_hdr->fh_s_id[2]);
  9957. /* Get an iocbq struct to fill in. */
  9958. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  9959. if (first_iocbq) {
  9960. /* Initialize the first IOCB. */
  9961. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  9962. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  9963. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  9964. first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
  9965. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  9966. vport->vpi + vport->phba->vpi_base;
  9967. /* put the first buffer into the first IOCBq */
  9968. first_iocbq->context2 = &seq_dmabuf->dbuf;
  9969. first_iocbq->context3 = NULL;
  9970. first_iocbq->iocb.ulpBdeCount = 1;
  9971. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  9972. LPFC_DATA_BUF_SIZE;
  9973. first_iocbq->iocb.un.rcvels.remoteID = sid;
  9974. first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
  9975. bf_get(lpfc_rcqe_length, &seq_dmabuf->rcqe);
  9976. }
  9977. iocbq = first_iocbq;
  9978. /*
  9979. * Each IOCBq can have two Buffers assigned, so go through the list
  9980. * of buffers for this sequence and save two buffers in each IOCBq
  9981. */
  9982. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  9983. if (!iocbq) {
  9984. lpfc_in_buf_free(vport->phba, d_buf);
  9985. continue;
  9986. }
  9987. if (!iocbq->context3) {
  9988. iocbq->context3 = d_buf;
  9989. iocbq->iocb.ulpBdeCount++;
  9990. iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
  9991. LPFC_DATA_BUF_SIZE;
  9992. first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
  9993. bf_get(lpfc_rcqe_length, &seq_dmabuf->rcqe);
  9994. } else {
  9995. iocbq = lpfc_sli_get_iocbq(vport->phba);
  9996. if (!iocbq) {
  9997. if (first_iocbq) {
  9998. first_iocbq->iocb.ulpStatus =
  9999. IOSTAT_FCP_RSP_ERROR;
  10000. first_iocbq->iocb.un.ulpWord[4] =
  10001. IOERR_NO_RESOURCES;
  10002. }
  10003. lpfc_in_buf_free(vport->phba, d_buf);
  10004. continue;
  10005. }
  10006. iocbq->context2 = d_buf;
  10007. iocbq->context3 = NULL;
  10008. iocbq->iocb.ulpBdeCount = 1;
  10009. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  10010. LPFC_DATA_BUF_SIZE;
  10011. first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
  10012. bf_get(lpfc_rcqe_length, &seq_dmabuf->rcqe);
  10013. iocbq->iocb.un.rcvels.remoteID = sid;
  10014. list_add_tail(&iocbq->list, &first_iocbq->list);
  10015. }
  10016. }
  10017. return first_iocbq;
  10018. }
  10019. /**
  10020. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  10021. * @phba: Pointer to HBA context object.
  10022. *
  10023. * This function is called with no lock held. This function processes all
  10024. * the received buffers and gives it to upper layers when a received buffer
  10025. * indicates that it is the final frame in the sequence. The interrupt
  10026. * service routine processes received buffers at interrupt contexts and adds
  10027. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  10028. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  10029. * appropriate receive function when the final frame in a sequence is received.
  10030. **/
  10031. int
  10032. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba)
  10033. {
  10034. LIST_HEAD(cmplq);
  10035. struct hbq_dmabuf *dmabuf, *seq_dmabuf;
  10036. struct fc_frame_header *fc_hdr;
  10037. struct lpfc_vport *vport;
  10038. uint32_t fcfi;
  10039. struct lpfc_iocbq *iocbq;
  10040. /* Clear hba flag and get all received buffers into the cmplq */
  10041. spin_lock_irq(&phba->hbalock);
  10042. phba->hba_flag &= ~HBA_RECEIVE_BUFFER;
  10043. list_splice_init(&phba->rb_pend_list, &cmplq);
  10044. spin_unlock_irq(&phba->hbalock);
  10045. /* Process each received buffer */
  10046. while ((dmabuf = lpfc_sli_hbqbuf_get(&cmplq)) != NULL) {
  10047. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  10048. /* check to see if this a valid type of frame */
  10049. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  10050. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  10051. continue;
  10052. }
  10053. fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->rcqe);
  10054. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  10055. if (!vport) {
  10056. /* throw out the frame */
  10057. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  10058. continue;
  10059. }
  10060. /* Link this frame */
  10061. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  10062. if (!seq_dmabuf) {
  10063. /* unable to add frame to vport - throw it out */
  10064. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  10065. continue;
  10066. }
  10067. /* If not last frame in sequence continue processing frames. */
  10068. if (!lpfc_seq_complete(seq_dmabuf)) {
  10069. /*
  10070. * When saving off frames post a new one and mark this
  10071. * frame to be freed when it is finished.
  10072. **/
  10073. lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
  10074. dmabuf->tag = -1;
  10075. continue;
  10076. }
  10077. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  10078. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  10079. if (!lpfc_complete_unsol_iocb(phba,
  10080. &phba->sli.ring[LPFC_ELS_RING],
  10081. iocbq, fc_hdr->fh_r_ctl,
  10082. fc_hdr->fh_type))
  10083. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10084. "2540 Ring %d handler: unexpected Rctl "
  10085. "x%x Type x%x received\n",
  10086. LPFC_ELS_RING,
  10087. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  10088. };
  10089. return 0;
  10090. }
  10091. /**
  10092. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  10093. * @phba: pointer to lpfc hba data structure.
  10094. *
  10095. * This routine is invoked to post rpi header templates to the
  10096. * HBA consistent with the SLI-4 interface spec. This routine
  10097. * posts a PAGE_SIZE memory region to the port to hold up to
  10098. * PAGE_SIZE modulo 64 rpi context headers.
  10099. *
  10100. * This routine does not require any locks. It's usage is expected
  10101. * to be driver load or reset recovery when the driver is
  10102. * sequential.
  10103. *
  10104. * Return codes
  10105. * 0 - sucessful
  10106. * EIO - The mailbox failed to complete successfully.
  10107. * When this error occurs, the driver is not guaranteed
  10108. * to have any rpi regions posted to the device and
  10109. * must either attempt to repost the regions or take a
  10110. * fatal error.
  10111. **/
  10112. int
  10113. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  10114. {
  10115. struct lpfc_rpi_hdr *rpi_page;
  10116. uint32_t rc = 0;
  10117. /* Post all rpi memory regions to the port. */
  10118. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  10119. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  10120. if (rc != MBX_SUCCESS) {
  10121. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10122. "2008 Error %d posting all rpi "
  10123. "headers\n", rc);
  10124. rc = -EIO;
  10125. break;
  10126. }
  10127. }
  10128. return rc;
  10129. }
  10130. /**
  10131. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  10132. * @phba: pointer to lpfc hba data structure.
  10133. * @rpi_page: pointer to the rpi memory region.
  10134. *
  10135. * This routine is invoked to post a single rpi header to the
  10136. * HBA consistent with the SLI-4 interface spec. This memory region
  10137. * maps up to 64 rpi context regions.
  10138. *
  10139. * Return codes
  10140. * 0 - sucessful
  10141. * ENOMEM - No available memory
  10142. * EIO - The mailbox failed to complete successfully.
  10143. **/
  10144. int
  10145. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  10146. {
  10147. LPFC_MBOXQ_t *mboxq;
  10148. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  10149. uint32_t rc = 0;
  10150. uint32_t mbox_tmo;
  10151. uint32_t shdr_status, shdr_add_status;
  10152. union lpfc_sli4_cfg_shdr *shdr;
  10153. /* The port is notified of the header region via a mailbox command. */
  10154. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10155. if (!mboxq) {
  10156. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10157. "2001 Unable to allocate memory for issuing "
  10158. "SLI_CONFIG_SPECIAL mailbox command\n");
  10159. return -ENOMEM;
  10160. }
  10161. /* Post all rpi memory regions to the port. */
  10162. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  10163. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  10164. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  10165. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  10166. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  10167. sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
  10168. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  10169. hdr_tmpl, rpi_page->page_count);
  10170. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  10171. rpi_page->start_rpi);
  10172. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  10173. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  10174. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  10175. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  10176. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10177. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10178. if (rc != MBX_TIMEOUT)
  10179. mempool_free(mboxq, phba->mbox_mem_pool);
  10180. if (shdr_status || shdr_add_status || rc) {
  10181. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10182. "2514 POST_RPI_HDR mailbox failed with "
  10183. "status x%x add_status x%x, mbx status x%x\n",
  10184. shdr_status, shdr_add_status, rc);
  10185. rc = -ENXIO;
  10186. }
  10187. return rc;
  10188. }
  10189. /**
  10190. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  10191. * @phba: pointer to lpfc hba data structure.
  10192. *
  10193. * This routine is invoked to post rpi header templates to the
  10194. * HBA consistent with the SLI-4 interface spec. This routine
  10195. * posts a PAGE_SIZE memory region to the port to hold up to
  10196. * PAGE_SIZE modulo 64 rpi context headers.
  10197. *
  10198. * Returns
  10199. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if sucessful
  10200. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  10201. **/
  10202. int
  10203. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  10204. {
  10205. int rpi;
  10206. uint16_t max_rpi, rpi_base, rpi_limit;
  10207. uint16_t rpi_remaining;
  10208. struct lpfc_rpi_hdr *rpi_hdr;
  10209. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  10210. rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
  10211. rpi_limit = phba->sli4_hba.next_rpi;
  10212. /*
  10213. * The valid rpi range is not guaranteed to be zero-based. Start
  10214. * the search at the rpi_base as reported by the port.
  10215. */
  10216. spin_lock_irq(&phba->hbalock);
  10217. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
  10218. if (rpi >= rpi_limit || rpi < rpi_base)
  10219. rpi = LPFC_RPI_ALLOC_ERROR;
  10220. else {
  10221. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  10222. phba->sli4_hba.max_cfg_param.rpi_used++;
  10223. phba->sli4_hba.rpi_count++;
  10224. }
  10225. /*
  10226. * Don't try to allocate more rpi header regions if the device limit
  10227. * on available rpis max has been exhausted.
  10228. */
  10229. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  10230. (phba->sli4_hba.rpi_count >= max_rpi)) {
  10231. spin_unlock_irq(&phba->hbalock);
  10232. return rpi;
  10233. }
  10234. /*
  10235. * If the driver is running low on rpi resources, allocate another
  10236. * page now. Note that the next_rpi value is used because
  10237. * it represents how many are actually in use whereas max_rpi notes
  10238. * how many are supported max by the device.
  10239. */
  10240. rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
  10241. phba->sli4_hba.rpi_count;
  10242. spin_unlock_irq(&phba->hbalock);
  10243. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  10244. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  10245. if (!rpi_hdr) {
  10246. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10247. "2002 Error Could not grow rpi "
  10248. "count\n");
  10249. } else {
  10250. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  10251. }
  10252. }
  10253. return rpi;
  10254. }
  10255. /**
  10256. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  10257. * @phba: pointer to lpfc hba data structure.
  10258. *
  10259. * This routine is invoked to release an rpi to the pool of
  10260. * available rpis maintained by the driver.
  10261. **/
  10262. void
  10263. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  10264. {
  10265. spin_lock_irq(&phba->hbalock);
  10266. clear_bit(rpi, phba->sli4_hba.rpi_bmask);
  10267. phba->sli4_hba.rpi_count--;
  10268. phba->sli4_hba.max_cfg_param.rpi_used--;
  10269. spin_unlock_irq(&phba->hbalock);
  10270. }
  10271. /**
  10272. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  10273. * @phba: pointer to lpfc hba data structure.
  10274. *
  10275. * This routine is invoked to remove the memory region that
  10276. * provided rpi via a bitmask.
  10277. **/
  10278. void
  10279. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  10280. {
  10281. kfree(phba->sli4_hba.rpi_bmask);
  10282. }
  10283. /**
  10284. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  10285. * @phba: pointer to lpfc hba data structure.
  10286. *
  10287. * This routine is invoked to remove the memory region that
  10288. * provided rpi via a bitmask.
  10289. **/
  10290. int
  10291. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
  10292. {
  10293. LPFC_MBOXQ_t *mboxq;
  10294. struct lpfc_hba *phba = ndlp->phba;
  10295. int rc;
  10296. /* The port is notified of the header region via a mailbox command. */
  10297. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10298. if (!mboxq)
  10299. return -ENOMEM;
  10300. /* Post all rpi memory regions to the port. */
  10301. lpfc_resume_rpi(mboxq, ndlp);
  10302. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  10303. if (rc == MBX_NOT_FINISHED) {
  10304. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10305. "2010 Resume RPI Mailbox failed "
  10306. "status %d, mbxStatus x%x\n", rc,
  10307. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  10308. mempool_free(mboxq, phba->mbox_mem_pool);
  10309. return -EIO;
  10310. }
  10311. return 0;
  10312. }
  10313. /**
  10314. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  10315. * @phba: pointer to lpfc hba data structure.
  10316. * @vpi: vpi value to activate with the port.
  10317. *
  10318. * This routine is invoked to activate a vpi with the
  10319. * port when the host intends to use vports with a
  10320. * nonzero vpi.
  10321. *
  10322. * Returns:
  10323. * 0 success
  10324. * -Evalue otherwise
  10325. **/
  10326. int
  10327. lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
  10328. {
  10329. LPFC_MBOXQ_t *mboxq;
  10330. int rc = 0;
  10331. uint32_t mbox_tmo;
  10332. if (vpi == 0)
  10333. return -EINVAL;
  10334. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10335. if (!mboxq)
  10336. return -ENOMEM;
  10337. lpfc_init_vpi(mboxq, vpi);
  10338. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
  10339. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  10340. if (rc != MBX_TIMEOUT)
  10341. mempool_free(mboxq, phba->mbox_mem_pool);
  10342. if (rc != MBX_SUCCESS) {
  10343. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10344. "2022 INIT VPI Mailbox failed "
  10345. "status %d, mbxStatus x%x\n", rc,
  10346. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  10347. rc = -EIO;
  10348. }
  10349. return rc;
  10350. }
  10351. /**
  10352. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  10353. * @phba: pointer to lpfc hba data structure.
  10354. * @mboxq: Pointer to mailbox object.
  10355. *
  10356. * This routine is invoked to manually add a single FCF record. The caller
  10357. * must pass a completely initialized FCF_Record. This routine takes
  10358. * care of the nonembedded mailbox operations.
  10359. **/
  10360. static void
  10361. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  10362. {
  10363. void *virt_addr;
  10364. union lpfc_sli4_cfg_shdr *shdr;
  10365. uint32_t shdr_status, shdr_add_status;
  10366. virt_addr = mboxq->sge_array->addr[0];
  10367. /* The IOCTL status is embedded in the mailbox subheader. */
  10368. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  10369. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10370. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10371. if ((shdr_status || shdr_add_status) &&
  10372. (shdr_status != STATUS_FCF_IN_USE))
  10373. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10374. "2558 ADD_FCF_RECORD mailbox failed with "
  10375. "status x%x add_status x%x\n",
  10376. shdr_status, shdr_add_status);
  10377. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10378. }
  10379. /**
  10380. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  10381. * @phba: pointer to lpfc hba data structure.
  10382. * @fcf_record: pointer to the initialized fcf record to add.
  10383. *
  10384. * This routine is invoked to manually add a single FCF record. The caller
  10385. * must pass a completely initialized FCF_Record. This routine takes
  10386. * care of the nonembedded mailbox operations.
  10387. **/
  10388. int
  10389. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  10390. {
  10391. int rc = 0;
  10392. LPFC_MBOXQ_t *mboxq;
  10393. uint8_t *bytep;
  10394. void *virt_addr;
  10395. dma_addr_t phys_addr;
  10396. struct lpfc_mbx_sge sge;
  10397. uint32_t alloc_len, req_len;
  10398. uint32_t fcfindex;
  10399. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10400. if (!mboxq) {
  10401. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10402. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  10403. return -ENOMEM;
  10404. }
  10405. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  10406. sizeof(uint32_t);
  10407. /* Allocate DMA memory and set up the non-embedded mailbox command */
  10408. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  10409. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  10410. req_len, LPFC_SLI4_MBX_NEMBED);
  10411. if (alloc_len < req_len) {
  10412. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10413. "2523 Allocated DMA memory size (x%x) is "
  10414. "less than the requested DMA memory "
  10415. "size (x%x)\n", alloc_len, req_len);
  10416. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10417. return -ENOMEM;
  10418. }
  10419. /*
  10420. * Get the first SGE entry from the non-embedded DMA memory. This
  10421. * routine only uses a single SGE.
  10422. */
  10423. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  10424. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  10425. if (unlikely(!mboxq->sge_array)) {
  10426. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10427. "2526 Failed to get the non-embedded SGE "
  10428. "virtual address\n");
  10429. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10430. return -ENOMEM;
  10431. }
  10432. virt_addr = mboxq->sge_array->addr[0];
  10433. /*
  10434. * Configure the FCF record for FCFI 0. This is the driver's
  10435. * hardcoded default and gets used in nonFIP mode.
  10436. */
  10437. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  10438. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  10439. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  10440. /*
  10441. * Copy the fcf_index and the FCF Record Data. The data starts after
  10442. * the FCoE header plus word10. The data copy needs to be endian
  10443. * correct.
  10444. */
  10445. bytep += sizeof(uint32_t);
  10446. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  10447. mboxq->vport = phba->pport;
  10448. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  10449. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  10450. if (rc == MBX_NOT_FINISHED) {
  10451. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10452. "2515 ADD_FCF_RECORD mailbox failed with "
  10453. "status 0x%x\n", rc);
  10454. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10455. rc = -EIO;
  10456. } else
  10457. rc = 0;
  10458. return rc;
  10459. }
  10460. /**
  10461. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  10462. * @phba: pointer to lpfc hba data structure.
  10463. * @fcf_record: pointer to the fcf record to write the default data.
  10464. * @fcf_index: FCF table entry index.
  10465. *
  10466. * This routine is invoked to build the driver's default FCF record. The
  10467. * values used are hardcoded. This routine handles memory initialization.
  10468. *
  10469. **/
  10470. void
  10471. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  10472. struct fcf_record *fcf_record,
  10473. uint16_t fcf_index)
  10474. {
  10475. memset(fcf_record, 0, sizeof(struct fcf_record));
  10476. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  10477. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  10478. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  10479. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  10480. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  10481. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  10482. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  10483. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  10484. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  10485. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  10486. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  10487. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  10488. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  10489. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  10490. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  10491. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  10492. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  10493. /* Set the VLAN bit map */
  10494. if (phba->valid_vlan) {
  10495. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  10496. = 1 << (phba->vlan_id % 8);
  10497. }
  10498. }
  10499. /**
  10500. * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
  10501. * @phba: pointer to lpfc hba data structure.
  10502. * @fcf_index: FCF table entry offset.
  10503. *
  10504. * This routine is invoked to read up to @fcf_num of FCF record from the
  10505. * device starting with the given @fcf_index.
  10506. **/
  10507. int
  10508. lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
  10509. {
  10510. int rc = 0, error;
  10511. LPFC_MBOXQ_t *mboxq;
  10512. void *virt_addr;
  10513. dma_addr_t phys_addr;
  10514. uint8_t *bytep;
  10515. struct lpfc_mbx_sge sge;
  10516. uint32_t alloc_len, req_len;
  10517. struct lpfc_mbx_read_fcf_tbl *read_fcf;
  10518. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10519. if (!mboxq) {
  10520. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10521. "2000 Failed to allocate mbox for "
  10522. "READ_FCF cmd\n");
  10523. return -ENOMEM;
  10524. }
  10525. req_len = sizeof(struct fcf_record) +
  10526. sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
  10527. /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
  10528. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  10529. LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
  10530. LPFC_SLI4_MBX_NEMBED);
  10531. if (alloc_len < req_len) {
  10532. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10533. "0291 Allocated DMA memory size (x%x) is "
  10534. "less than the requested DMA memory "
  10535. "size (x%x)\n", alloc_len, req_len);
  10536. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10537. return -ENOMEM;
  10538. }
  10539. /* Get the first SGE entry from the non-embedded DMA memory. This
  10540. * routine only uses a single SGE.
  10541. */
  10542. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  10543. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  10544. if (unlikely(!mboxq->sge_array)) {
  10545. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10546. "2527 Failed to get the non-embedded SGE "
  10547. "virtual address\n");
  10548. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10549. return -ENOMEM;
  10550. }
  10551. virt_addr = mboxq->sge_array->addr[0];
  10552. read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
  10553. /* Set up command fields */
  10554. bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
  10555. /* Perform necessary endian conversion */
  10556. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  10557. lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
  10558. mboxq->vport = phba->pport;
  10559. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
  10560. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  10561. if (rc == MBX_NOT_FINISHED) {
  10562. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10563. error = -EIO;
  10564. } else
  10565. error = 0;
  10566. return error;
  10567. }