lpfc_scsi.c 197 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2017-2022 Broadcom. All Rights Reserved. The term *
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
  6. * Copyright (C) 2004-2016 Emulex. All rights reserved. *
  7. * EMULEX and SLI are trademarks of Emulex. *
  8. * www.broadcom.com *
  9. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  10. * *
  11. * This program is free software; you can redistribute it and/or *
  12. * modify it under the terms of version 2 of the GNU General *
  13. * Public License as published by the Free Software Foundation. *
  14. * This program is distributed in the hope that it will be useful. *
  15. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  16. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  17. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  18. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  20. * more details, a copy of which can be found in the file COPYING *
  21. * included with this package. *
  22. *******************************************************************/
  23. #include <linux/pci.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/export.h>
  27. #include <linux/delay.h>
  28. #include <asm/unaligned.h>
  29. #include <linux/t10-pi.h>
  30. #include <linux/crc-t10dif.h>
  31. #include <linux/blk-cgroup.h>
  32. #include <net/checksum.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_device.h>
  35. #include <scsi/scsi_eh.h>
  36. #include <scsi/scsi_host.h>
  37. #include <scsi/scsi_tcq.h>
  38. #include <scsi/scsi_transport_fc.h>
  39. #include "lpfc_version.h"
  40. #include "lpfc_hw4.h"
  41. #include "lpfc_hw.h"
  42. #include "lpfc_sli.h"
  43. #include "lpfc_sli4.h"
  44. #include "lpfc_nl.h"
  45. #include "lpfc_disc.h"
  46. #include "lpfc.h"
  47. #include "lpfc_scsi.h"
  48. #include "lpfc_logmsg.h"
  49. #include "lpfc_crtn.h"
  50. #include "lpfc_vport.h"
  51. #define LPFC_RESET_WAIT 2
  52. #define LPFC_ABORT_WAIT 2
  53. static char *dif_op_str[] = {
  54. "PROT_NORMAL",
  55. "PROT_READ_INSERT",
  56. "PROT_WRITE_STRIP",
  57. "PROT_READ_STRIP",
  58. "PROT_WRITE_INSERT",
  59. "PROT_READ_PASS",
  60. "PROT_WRITE_PASS",
  61. };
  62. struct scsi_dif_tuple {
  63. __be16 guard_tag; /* Checksum */
  64. __be16 app_tag; /* Opaque storage */
  65. __be32 ref_tag; /* Target LBA or indirect LBA */
  66. };
  67. static struct lpfc_rport_data *
  68. lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
  69. {
  70. struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
  71. if (vport->phba->cfg_fof)
  72. return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
  73. else
  74. return (struct lpfc_rport_data *)sdev->hostdata;
  75. }
  76. static void
  77. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
  78. static void
  79. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
  80. static int
  81. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
  82. /**
  83. * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
  84. * @phba: Pointer to HBA object.
  85. * @lpfc_cmd: lpfc scsi command object pointer.
  86. *
  87. * This function is called from the lpfc_prep_task_mgmt_cmd function to
  88. * set the last bit in the response sge entry.
  89. **/
  90. static void
  91. lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
  92. struct lpfc_io_buf *lpfc_cmd)
  93. {
  94. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
  95. if (sgl) {
  96. sgl += 1;
  97. sgl->word2 = le32_to_cpu(sgl->word2);
  98. bf_set(lpfc_sli4_sge_last, sgl, 1);
  99. sgl->word2 = cpu_to_le32(sgl->word2);
  100. }
  101. }
  102. /**
  103. * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
  104. * @phba: The Hba for which this call is being executed.
  105. *
  106. * This routine is called when there is resource error in driver or firmware.
  107. * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
  108. * posts at most 1 event each second. This routine wakes up worker thread of
  109. * @phba to process WORKER_RAM_DOWN_EVENT event.
  110. *
  111. * This routine should be called with no lock held.
  112. **/
  113. void
  114. lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
  115. {
  116. unsigned long flags;
  117. uint32_t evt_posted;
  118. unsigned long expires;
  119. spin_lock_irqsave(&phba->hbalock, flags);
  120. atomic_inc(&phba->num_rsrc_err);
  121. phba->last_rsrc_error_time = jiffies;
  122. expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
  123. if (time_after(expires, jiffies)) {
  124. spin_unlock_irqrestore(&phba->hbalock, flags);
  125. return;
  126. }
  127. phba->last_ramp_down_time = jiffies;
  128. spin_unlock_irqrestore(&phba->hbalock, flags);
  129. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  130. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  131. if (!evt_posted)
  132. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  133. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  134. if (!evt_posted)
  135. lpfc_worker_wake_up(phba);
  136. return;
  137. }
  138. /**
  139. * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
  140. * @phba: The Hba for which this call is being executed.
  141. *
  142. * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
  143. * thread.This routine reduces queue depth for all scsi device on each vport
  144. * associated with @phba.
  145. **/
  146. void
  147. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  148. {
  149. struct lpfc_vport **vports;
  150. struct Scsi_Host *shost;
  151. struct scsi_device *sdev;
  152. unsigned long new_queue_depth;
  153. unsigned long num_rsrc_err, num_cmd_success;
  154. int i;
  155. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  156. num_cmd_success = atomic_read(&phba->num_cmd_success);
  157. /*
  158. * The error and success command counters are global per
  159. * driver instance. If another handler has already
  160. * operated on this error event, just exit.
  161. */
  162. if (num_rsrc_err == 0)
  163. return;
  164. vports = lpfc_create_vport_work_array(phba);
  165. if (vports != NULL)
  166. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  167. shost = lpfc_shost_from_vport(vports[i]);
  168. shost_for_each_device(sdev, shost) {
  169. new_queue_depth =
  170. sdev->queue_depth * num_rsrc_err /
  171. (num_rsrc_err + num_cmd_success);
  172. if (!new_queue_depth)
  173. new_queue_depth = sdev->queue_depth - 1;
  174. else
  175. new_queue_depth = sdev->queue_depth -
  176. new_queue_depth;
  177. scsi_change_queue_depth(sdev, new_queue_depth);
  178. }
  179. }
  180. lpfc_destroy_vport_work_array(phba, vports);
  181. atomic_set(&phba->num_rsrc_err, 0);
  182. atomic_set(&phba->num_cmd_success, 0);
  183. }
  184. /**
  185. * lpfc_scsi_dev_block - set all scsi hosts to block state
  186. * @phba: Pointer to HBA context object.
  187. *
  188. * This function walks vport list and set each SCSI host to block state
  189. * by invoking fc_remote_port_delete() routine. This function is invoked
  190. * with EEH when device's PCI slot has been permanently disabled.
  191. **/
  192. void
  193. lpfc_scsi_dev_block(struct lpfc_hba *phba)
  194. {
  195. struct lpfc_vport **vports;
  196. struct Scsi_Host *shost;
  197. struct scsi_device *sdev;
  198. struct fc_rport *rport;
  199. int i;
  200. vports = lpfc_create_vport_work_array(phba);
  201. if (vports != NULL)
  202. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  203. shost = lpfc_shost_from_vport(vports[i]);
  204. shost_for_each_device(sdev, shost) {
  205. rport = starget_to_rport(scsi_target(sdev));
  206. fc_remote_port_delete(rport);
  207. }
  208. }
  209. lpfc_destroy_vport_work_array(phba, vports);
  210. }
  211. /**
  212. * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
  213. * @vport: The virtual port for which this call being executed.
  214. * @num_to_alloc: The requested number of buffers to allocate.
  215. *
  216. * This routine allocates a scsi buffer for device with SLI-3 interface spec,
  217. * the scsi buffer contains all the necessary information needed to initiate
  218. * a SCSI I/O. The non-DMAable buffer region contains information to build
  219. * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
  220. * and the initial BPL. In addition to allocating memory, the FCP CMND and
  221. * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
  222. *
  223. * Return codes:
  224. * int - number of scsi buffers that were allocated.
  225. * 0 = failure, less than num_to_alloc is a partial failure.
  226. **/
  227. static int
  228. lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
  229. {
  230. struct lpfc_hba *phba = vport->phba;
  231. struct lpfc_io_buf *psb;
  232. struct ulp_bde64 *bpl;
  233. IOCB_t *iocb;
  234. dma_addr_t pdma_phys_fcp_cmd;
  235. dma_addr_t pdma_phys_fcp_rsp;
  236. dma_addr_t pdma_phys_sgl;
  237. uint16_t iotag;
  238. int bcnt, bpl_size;
  239. bpl_size = phba->cfg_sg_dma_buf_size -
  240. (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  241. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  242. "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
  243. num_to_alloc, phba->cfg_sg_dma_buf_size,
  244. (int)sizeof(struct fcp_cmnd),
  245. (int)sizeof(struct fcp_rsp), bpl_size);
  246. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  247. psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
  248. if (!psb)
  249. break;
  250. /*
  251. * Get memory from the pci pool to map the virt space to pci
  252. * bus space for an I/O. The DMA buffer includes space for the
  253. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  254. * necessary to support the sg_tablesize.
  255. */
  256. psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
  257. GFP_KERNEL, &psb->dma_handle);
  258. if (!psb->data) {
  259. kfree(psb);
  260. break;
  261. }
  262. /* Allocate iotag for psb->cur_iocbq. */
  263. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  264. if (iotag == 0) {
  265. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  266. psb->data, psb->dma_handle);
  267. kfree(psb);
  268. break;
  269. }
  270. psb->cur_iocbq.cmd_flag |= LPFC_IO_FCP;
  271. psb->fcp_cmnd = psb->data;
  272. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  273. psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
  274. sizeof(struct fcp_rsp);
  275. /* Initialize local short-hand pointers. */
  276. bpl = (struct ulp_bde64 *)psb->dma_sgl;
  277. pdma_phys_fcp_cmd = psb->dma_handle;
  278. pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
  279. pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
  280. sizeof(struct fcp_rsp);
  281. /*
  282. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  283. * are sg list bdes. Initialize the first two and leave the
  284. * rest for queuecommand.
  285. */
  286. bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
  287. bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
  288. bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
  289. bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  290. bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
  291. /* Setup the physical region for the FCP RSP */
  292. bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
  293. bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
  294. bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
  295. bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  296. bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
  297. /*
  298. * Since the IOCB for the FCP I/O is built into this
  299. * lpfc_scsi_buf, initialize it with all known data now.
  300. */
  301. iocb = &psb->cur_iocbq.iocb;
  302. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  303. if ((phba->sli_rev == 3) &&
  304. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  305. /* fill in immediate fcp command BDE */
  306. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
  307. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  308. iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
  309. unsli3.fcp_ext.icd);
  310. iocb->un.fcpi64.bdl.addrHigh = 0;
  311. iocb->ulpBdeCount = 0;
  312. iocb->ulpLe = 0;
  313. /* fill in response BDE */
  314. iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
  315. BUFF_TYPE_BDE_64;
  316. iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
  317. sizeof(struct fcp_rsp);
  318. iocb->unsli3.fcp_ext.rbde.addrLow =
  319. putPaddrLow(pdma_phys_fcp_rsp);
  320. iocb->unsli3.fcp_ext.rbde.addrHigh =
  321. putPaddrHigh(pdma_phys_fcp_rsp);
  322. } else {
  323. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  324. iocb->un.fcpi64.bdl.bdeSize =
  325. (2 * sizeof(struct ulp_bde64));
  326. iocb->un.fcpi64.bdl.addrLow =
  327. putPaddrLow(pdma_phys_sgl);
  328. iocb->un.fcpi64.bdl.addrHigh =
  329. putPaddrHigh(pdma_phys_sgl);
  330. iocb->ulpBdeCount = 1;
  331. iocb->ulpLe = 1;
  332. }
  333. iocb->ulpClass = CLASS3;
  334. psb->status = IOSTAT_SUCCESS;
  335. /* Put it back into the SCSI buffer list */
  336. psb->cur_iocbq.io_buf = psb;
  337. spin_lock_init(&psb->buf_lock);
  338. lpfc_release_scsi_buf_s3(phba, psb);
  339. }
  340. return bcnt;
  341. }
  342. /**
  343. * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
  344. * @vport: pointer to lpfc vport data structure.
  345. *
  346. * This routine is invoked by the vport cleanup for deletions and the cleanup
  347. * for an ndlp on removal.
  348. **/
  349. void
  350. lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
  351. {
  352. struct lpfc_hba *phba = vport->phba;
  353. struct lpfc_io_buf *psb, *next_psb;
  354. struct lpfc_sli4_hdw_queue *qp;
  355. unsigned long iflag = 0;
  356. int idx;
  357. if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
  358. return;
  359. spin_lock_irqsave(&phba->hbalock, iflag);
  360. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  361. qp = &phba->sli4_hba.hdwq[idx];
  362. spin_lock(&qp->abts_io_buf_list_lock);
  363. list_for_each_entry_safe(psb, next_psb,
  364. &qp->lpfc_abts_io_buf_list, list) {
  365. if (psb->cur_iocbq.cmd_flag & LPFC_IO_NVME)
  366. continue;
  367. if (psb->rdata && psb->rdata->pnode &&
  368. psb->rdata->pnode->vport == vport)
  369. psb->rdata = NULL;
  370. }
  371. spin_unlock(&qp->abts_io_buf_list_lock);
  372. }
  373. spin_unlock_irqrestore(&phba->hbalock, iflag);
  374. }
  375. /**
  376. * lpfc_sli4_io_xri_aborted - Fast-path process of fcp xri abort
  377. * @phba: pointer to lpfc hba data structure.
  378. * @axri: pointer to the fcp xri abort wcqe structure.
  379. * @idx: index into hdwq
  380. *
  381. * This routine is invoked by the worker thread to process a SLI4 fast-path
  382. * FCP or NVME aborted xri.
  383. **/
  384. void
  385. lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
  386. struct sli4_wcqe_xri_aborted *axri, int idx)
  387. {
  388. u16 xri = 0;
  389. u16 rxid = 0;
  390. struct lpfc_io_buf *psb, *next_psb;
  391. struct lpfc_sli4_hdw_queue *qp;
  392. unsigned long iflag = 0;
  393. struct lpfc_iocbq *iocbq;
  394. int i;
  395. struct lpfc_nodelist *ndlp;
  396. int rrq_empty = 0;
  397. struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
  398. struct scsi_cmnd *cmd;
  399. int offline = 0;
  400. if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
  401. return;
  402. offline = pci_channel_offline(phba->pcidev);
  403. if (!offline) {
  404. xri = bf_get(lpfc_wcqe_xa_xri, axri);
  405. rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
  406. }
  407. qp = &phba->sli4_hba.hdwq[idx];
  408. spin_lock_irqsave(&phba->hbalock, iflag);
  409. spin_lock(&qp->abts_io_buf_list_lock);
  410. list_for_each_entry_safe(psb, next_psb,
  411. &qp->lpfc_abts_io_buf_list, list) {
  412. if (offline)
  413. xri = psb->cur_iocbq.sli4_xritag;
  414. if (psb->cur_iocbq.sli4_xritag == xri) {
  415. list_del_init(&psb->list);
  416. psb->flags &= ~LPFC_SBUF_XBUSY;
  417. psb->status = IOSTAT_SUCCESS;
  418. if (psb->cur_iocbq.cmd_flag & LPFC_IO_NVME) {
  419. qp->abts_nvme_io_bufs--;
  420. spin_unlock(&qp->abts_io_buf_list_lock);
  421. spin_unlock_irqrestore(&phba->hbalock, iflag);
  422. if (!offline) {
  423. lpfc_sli4_nvme_xri_aborted(phba, axri,
  424. psb);
  425. return;
  426. }
  427. lpfc_sli4_nvme_pci_offline_aborted(phba, psb);
  428. spin_lock_irqsave(&phba->hbalock, iflag);
  429. spin_lock(&qp->abts_io_buf_list_lock);
  430. continue;
  431. }
  432. qp->abts_scsi_io_bufs--;
  433. spin_unlock(&qp->abts_io_buf_list_lock);
  434. if (psb->rdata && psb->rdata->pnode)
  435. ndlp = psb->rdata->pnode;
  436. else
  437. ndlp = NULL;
  438. rrq_empty = list_empty(&phba->active_rrq_list);
  439. spin_unlock_irqrestore(&phba->hbalock, iflag);
  440. if (ndlp && !offline) {
  441. lpfc_set_rrq_active(phba, ndlp,
  442. psb->cur_iocbq.sli4_lxritag, rxid, 1);
  443. lpfc_sli4_abts_err_handler(phba, ndlp, axri);
  444. }
  445. if (phba->cfg_fcp_wait_abts_rsp || offline) {
  446. spin_lock_irqsave(&psb->buf_lock, iflag);
  447. cmd = psb->pCmd;
  448. psb->pCmd = NULL;
  449. spin_unlock_irqrestore(&psb->buf_lock, iflag);
  450. /* The sdev is not guaranteed to be valid post
  451. * scsi_done upcall.
  452. */
  453. if (cmd)
  454. scsi_done(cmd);
  455. /*
  456. * We expect there is an abort thread waiting
  457. * for command completion wake up the thread.
  458. */
  459. spin_lock_irqsave(&psb->buf_lock, iflag);
  460. psb->cur_iocbq.cmd_flag &=
  461. ~LPFC_DRIVER_ABORTED;
  462. if (psb->waitq)
  463. wake_up(psb->waitq);
  464. spin_unlock_irqrestore(&psb->buf_lock, iflag);
  465. }
  466. lpfc_release_scsi_buf_s4(phba, psb);
  467. if (rrq_empty)
  468. lpfc_worker_wake_up(phba);
  469. if (!offline)
  470. return;
  471. spin_lock_irqsave(&phba->hbalock, iflag);
  472. spin_lock(&qp->abts_io_buf_list_lock);
  473. continue;
  474. }
  475. }
  476. spin_unlock(&qp->abts_io_buf_list_lock);
  477. if (!offline) {
  478. for (i = 1; i <= phba->sli.last_iotag; i++) {
  479. iocbq = phba->sli.iocbq_lookup[i];
  480. if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
  481. (iocbq->cmd_flag & LPFC_IO_LIBDFC))
  482. continue;
  483. if (iocbq->sli4_xritag != xri)
  484. continue;
  485. psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
  486. psb->flags &= ~LPFC_SBUF_XBUSY;
  487. spin_unlock_irqrestore(&phba->hbalock, iflag);
  488. if (!list_empty(&pring->txq))
  489. lpfc_worker_wake_up(phba);
  490. return;
  491. }
  492. }
  493. spin_unlock_irqrestore(&phba->hbalock, iflag);
  494. }
  495. /**
  496. * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  497. * @phba: The HBA for which this call is being executed.
  498. * @ndlp: pointer to a node-list data structure.
  499. * @cmnd: Pointer to scsi_cmnd data structure.
  500. *
  501. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  502. * and returns to caller.
  503. *
  504. * Return codes:
  505. * NULL - Error
  506. * Pointer to lpfc_scsi_buf - Success
  507. **/
  508. static struct lpfc_io_buf *
  509. lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  510. struct scsi_cmnd *cmnd)
  511. {
  512. struct lpfc_io_buf *lpfc_cmd = NULL;
  513. struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
  514. unsigned long iflag = 0;
  515. spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
  516. list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
  517. list);
  518. if (!lpfc_cmd) {
  519. spin_lock(&phba->scsi_buf_list_put_lock);
  520. list_splice(&phba->lpfc_scsi_buf_list_put,
  521. &phba->lpfc_scsi_buf_list_get);
  522. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
  523. list_remove_head(scsi_buf_list_get, lpfc_cmd,
  524. struct lpfc_io_buf, list);
  525. spin_unlock(&phba->scsi_buf_list_put_lock);
  526. }
  527. spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
  528. if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
  529. atomic_inc(&ndlp->cmd_pending);
  530. lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
  531. }
  532. return lpfc_cmd;
  533. }
  534. /**
  535. * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
  536. * @phba: The HBA for which this call is being executed.
  537. * @ndlp: pointer to a node-list data structure.
  538. * @cmnd: Pointer to scsi_cmnd data structure.
  539. *
  540. * This routine removes a scsi buffer from head of @hdwq io_buf_list
  541. * and returns to caller.
  542. *
  543. * Return codes:
  544. * NULL - Error
  545. * Pointer to lpfc_scsi_buf - Success
  546. **/
  547. static struct lpfc_io_buf *
  548. lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  549. struct scsi_cmnd *cmnd)
  550. {
  551. struct lpfc_io_buf *lpfc_cmd;
  552. struct lpfc_sli4_hdw_queue *qp;
  553. struct sli4_sge *sgl;
  554. dma_addr_t pdma_phys_fcp_rsp;
  555. dma_addr_t pdma_phys_fcp_cmd;
  556. uint32_t cpu, idx;
  557. int tag;
  558. struct fcp_cmd_rsp_buf *tmp = NULL;
  559. cpu = raw_smp_processor_id();
  560. if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
  561. tag = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
  562. idx = blk_mq_unique_tag_to_hwq(tag);
  563. } else {
  564. idx = phba->sli4_hba.cpu_map[cpu].hdwq;
  565. }
  566. lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
  567. !phba->cfg_xri_rebalancing);
  568. if (!lpfc_cmd) {
  569. qp = &phba->sli4_hba.hdwq[idx];
  570. qp->empty_io_bufs++;
  571. return NULL;
  572. }
  573. /* Setup key fields in buffer that may have been changed
  574. * if other protocols used this buffer.
  575. */
  576. lpfc_cmd->cur_iocbq.cmd_flag = LPFC_IO_FCP;
  577. lpfc_cmd->prot_seg_cnt = 0;
  578. lpfc_cmd->seg_cnt = 0;
  579. lpfc_cmd->timeout = 0;
  580. lpfc_cmd->flags = 0;
  581. lpfc_cmd->start_time = jiffies;
  582. lpfc_cmd->waitq = NULL;
  583. lpfc_cmd->cpu = cpu;
  584. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  585. lpfc_cmd->prot_data_type = 0;
  586. #endif
  587. tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
  588. if (!tmp) {
  589. lpfc_release_io_buf(phba, lpfc_cmd, lpfc_cmd->hdwq);
  590. return NULL;
  591. }
  592. lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
  593. lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
  594. /*
  595. * The first two SGEs are the FCP_CMD and FCP_RSP.
  596. * The balance are sg list bdes. Initialize the
  597. * first two and leave the rest for queuecommand.
  598. */
  599. sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
  600. pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
  601. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
  602. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
  603. sgl->word2 = le32_to_cpu(sgl->word2);
  604. bf_set(lpfc_sli4_sge_last, sgl, 0);
  605. sgl->word2 = cpu_to_le32(sgl->word2);
  606. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
  607. sgl++;
  608. /* Setup the physical region for the FCP RSP */
  609. pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
  610. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
  611. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
  612. sgl->word2 = le32_to_cpu(sgl->word2);
  613. bf_set(lpfc_sli4_sge_last, sgl, 1);
  614. sgl->word2 = cpu_to_le32(sgl->word2);
  615. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
  616. if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
  617. atomic_inc(&ndlp->cmd_pending);
  618. lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
  619. }
  620. return lpfc_cmd;
  621. }
  622. /**
  623. * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  624. * @phba: The HBA for which this call is being executed.
  625. * @ndlp: pointer to a node-list data structure.
  626. * @cmnd: Pointer to scsi_cmnd data structure.
  627. *
  628. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  629. * and returns to caller.
  630. *
  631. * Return codes:
  632. * NULL - Error
  633. * Pointer to lpfc_scsi_buf - Success
  634. **/
  635. static struct lpfc_io_buf*
  636. lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  637. struct scsi_cmnd *cmnd)
  638. {
  639. return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
  640. }
  641. /**
  642. * lpfc_release_scsi_buf_s3 - Return a scsi buffer back to hba scsi buf list
  643. * @phba: The Hba for which this call is being executed.
  644. * @psb: The scsi buffer which is being released.
  645. *
  646. * This routine releases @psb scsi buffer by adding it to tail of @phba
  647. * lpfc_scsi_buf_list list.
  648. **/
  649. static void
  650. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
  651. {
  652. unsigned long iflag = 0;
  653. psb->seg_cnt = 0;
  654. psb->prot_seg_cnt = 0;
  655. spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
  656. psb->pCmd = NULL;
  657. psb->cur_iocbq.cmd_flag = LPFC_IO_FCP;
  658. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
  659. spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
  660. }
  661. /**
  662. * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
  663. * @phba: The Hba for which this call is being executed.
  664. * @psb: The scsi buffer which is being released.
  665. *
  666. * This routine releases @psb scsi buffer by adding it to tail of @hdwq
  667. * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
  668. * and cannot be reused for at least RA_TOV amount of time if it was
  669. * aborted.
  670. **/
  671. static void
  672. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
  673. {
  674. struct lpfc_sli4_hdw_queue *qp;
  675. unsigned long iflag = 0;
  676. psb->seg_cnt = 0;
  677. psb->prot_seg_cnt = 0;
  678. qp = psb->hdwq;
  679. if (psb->flags & LPFC_SBUF_XBUSY) {
  680. spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
  681. if (!phba->cfg_fcp_wait_abts_rsp)
  682. psb->pCmd = NULL;
  683. list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
  684. qp->abts_scsi_io_bufs++;
  685. spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
  686. } else {
  687. lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
  688. }
  689. }
  690. /**
  691. * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
  692. * @phba: The Hba for which this call is being executed.
  693. * @psb: The scsi buffer which is being released.
  694. *
  695. * This routine releases @psb scsi buffer by adding it to tail of @phba
  696. * lpfc_scsi_buf_list list.
  697. **/
  698. static void
  699. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
  700. {
  701. if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
  702. atomic_dec(&psb->ndlp->cmd_pending);
  703. psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
  704. phba->lpfc_release_scsi_buf(phba, psb);
  705. }
  706. /**
  707. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
  708. * @data: A pointer to the immediate command data portion of the IOCB.
  709. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  710. *
  711. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  712. * byte swapping the data to big endian format for transmission on the wire.
  713. **/
  714. static void
  715. lpfc_fcpcmd_to_iocb(u8 *data, struct fcp_cmnd *fcp_cmnd)
  716. {
  717. int i, j;
  718. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  719. i += sizeof(uint32_t), j++) {
  720. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  721. }
  722. }
  723. /**
  724. * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
  725. * @phba: The Hba for which this call is being executed.
  726. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  727. *
  728. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  729. * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
  730. * through sg elements and format the bde. This routine also initializes all
  731. * IOCB fields which are dependent on scsi command request buffer.
  732. *
  733. * Return codes:
  734. * 1 - Error
  735. * 0 - Success
  736. **/
  737. static int
  738. lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
  739. {
  740. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  741. struct scatterlist *sgel = NULL;
  742. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  743. struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
  744. struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
  745. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  746. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  747. dma_addr_t physaddr;
  748. uint32_t num_bde = 0;
  749. int nseg, datadir = scsi_cmnd->sc_data_direction;
  750. /*
  751. * There are three possibilities here - use scatter-gather segment, use
  752. * the single mapping, or neither. Start the lpfc command prep by
  753. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  754. * data bde entry.
  755. */
  756. bpl += 2;
  757. if (scsi_sg_count(scsi_cmnd)) {
  758. /*
  759. * The driver stores the segment count returned from dma_map_sg
  760. * because this a count of dma-mappings used to map the use_sg
  761. * pages. They are not guaranteed to be the same for those
  762. * architectures that implement an IOMMU.
  763. */
  764. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  765. scsi_sg_count(scsi_cmnd), datadir);
  766. if (unlikely(!nseg))
  767. return 1;
  768. lpfc_cmd->seg_cnt = nseg;
  769. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  770. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  771. "9064 BLKGRD: %s: Too many sg segments"
  772. " from dma_map_sg. Config %d, seg_cnt"
  773. " %d\n", __func__, phba->cfg_sg_seg_cnt,
  774. lpfc_cmd->seg_cnt);
  775. WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
  776. lpfc_cmd->seg_cnt = 0;
  777. scsi_dma_unmap(scsi_cmnd);
  778. return 2;
  779. }
  780. /*
  781. * The driver established a maximum scatter-gather segment count
  782. * during probe that limits the number of sg elements in any
  783. * single scsi command. Just run through the seg_cnt and format
  784. * the bde's.
  785. * When using SLI-3 the driver will try to fit all the BDEs into
  786. * the IOCB. If it can't then the BDEs get added to a BPL as it
  787. * does for SLI-2 mode.
  788. */
  789. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  790. physaddr = sg_dma_address(sgel);
  791. if (phba->sli_rev == 3 &&
  792. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  793. !(iocbq->cmd_flag & DSS_SECURITY_OP) &&
  794. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  795. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  796. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  797. data_bde->addrLow = putPaddrLow(physaddr);
  798. data_bde->addrHigh = putPaddrHigh(physaddr);
  799. data_bde++;
  800. } else {
  801. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  802. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  803. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  804. bpl->addrLow =
  805. le32_to_cpu(putPaddrLow(physaddr));
  806. bpl->addrHigh =
  807. le32_to_cpu(putPaddrHigh(physaddr));
  808. bpl++;
  809. }
  810. }
  811. }
  812. /*
  813. * Finish initializing those IOCB fields that are dependent on the
  814. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  815. * explicitly reinitialized and for SLI-3 the extended bde count is
  816. * explicitly reinitialized since all iocb memory resources are reused.
  817. */
  818. if (phba->sli_rev == 3 &&
  819. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  820. !(iocbq->cmd_flag & DSS_SECURITY_OP)) {
  821. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  822. /*
  823. * The extended IOCB format can only fit 3 BDE or a BPL.
  824. * This I/O has more than 3 BDE so the 1st data bde will
  825. * be a BPL that is filled in here.
  826. */
  827. physaddr = lpfc_cmd->dma_handle;
  828. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  829. data_bde->tus.f.bdeSize = (num_bde *
  830. sizeof(struct ulp_bde64));
  831. physaddr += (sizeof(struct fcp_cmnd) +
  832. sizeof(struct fcp_rsp) +
  833. (2 * sizeof(struct ulp_bde64)));
  834. data_bde->addrHigh = putPaddrHigh(physaddr);
  835. data_bde->addrLow = putPaddrLow(physaddr);
  836. /* ebde count includes the response bde and data bpl */
  837. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  838. } else {
  839. /* ebde count includes the response bde and data bdes */
  840. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  841. }
  842. } else {
  843. iocb_cmd->un.fcpi64.bdl.bdeSize =
  844. ((num_bde + 2) * sizeof(struct ulp_bde64));
  845. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  846. }
  847. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  848. /*
  849. * Due to difference in data length between DIF/non-DIF paths,
  850. * we need to set word 4 of IOCB here
  851. */
  852. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  853. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  854. return 0;
  855. }
  856. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  857. /* Return BG_ERR_INIT if error injection is detected by Initiator */
  858. #define BG_ERR_INIT 0x1
  859. /* Return BG_ERR_TGT if error injection is detected by Target */
  860. #define BG_ERR_TGT 0x2
  861. /* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
  862. #define BG_ERR_SWAP 0x10
  863. /*
  864. * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
  865. * error injection
  866. */
  867. #define BG_ERR_CHECK 0x20
  868. /**
  869. * lpfc_bg_err_inject - Determine if we should inject an error
  870. * @phba: The Hba for which this call is being executed.
  871. * @sc: The SCSI command to examine
  872. * @reftag: (out) BlockGuard reference tag for transmitted data
  873. * @apptag: (out) BlockGuard application tag for transmitted data
  874. * @new_guard: (in) Value to replace CRC with if needed
  875. *
  876. * Returns BG_ERR_* bit mask or 0 if request ignored
  877. **/
  878. static int
  879. lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  880. uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
  881. {
  882. struct scatterlist *sgpe; /* s/g prot entry */
  883. struct lpfc_io_buf *lpfc_cmd = NULL;
  884. struct scsi_dif_tuple *src = NULL;
  885. struct lpfc_nodelist *ndlp;
  886. struct lpfc_rport_data *rdata;
  887. uint32_t op = scsi_get_prot_op(sc);
  888. uint32_t blksize;
  889. uint32_t numblks;
  890. u32 lba;
  891. int rc = 0;
  892. int blockoff = 0;
  893. if (op == SCSI_PROT_NORMAL)
  894. return 0;
  895. sgpe = scsi_prot_sglist(sc);
  896. lba = scsi_prot_ref_tag(sc);
  897. /* First check if we need to match the LBA */
  898. if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
  899. blksize = scsi_prot_interval(sc);
  900. numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
  901. /* Make sure we have the right LBA if one is specified */
  902. if (phba->lpfc_injerr_lba < (u64)lba ||
  903. (phba->lpfc_injerr_lba >= (u64)(lba + numblks)))
  904. return 0;
  905. if (sgpe) {
  906. blockoff = phba->lpfc_injerr_lba - (u64)lba;
  907. numblks = sg_dma_len(sgpe) /
  908. sizeof(struct scsi_dif_tuple);
  909. if (numblks < blockoff)
  910. blockoff = numblks;
  911. }
  912. }
  913. /* Next check if we need to match the remote NPortID or WWPN */
  914. rdata = lpfc_rport_data_from_scsi_device(sc->device);
  915. if (rdata && rdata->pnode) {
  916. ndlp = rdata->pnode;
  917. /* Make sure we have the right NPortID if one is specified */
  918. if (phba->lpfc_injerr_nportid &&
  919. (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
  920. return 0;
  921. /*
  922. * Make sure we have the right WWPN if one is specified.
  923. * wwn[0] should be a non-zero NAA in a good WWPN.
  924. */
  925. if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
  926. (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
  927. sizeof(struct lpfc_name)) != 0))
  928. return 0;
  929. }
  930. /* Setup a ptr to the protection data if the SCSI host provides it */
  931. if (sgpe) {
  932. src = (struct scsi_dif_tuple *)sg_virt(sgpe);
  933. src += blockoff;
  934. lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
  935. }
  936. /* Should we change the Reference Tag */
  937. if (reftag) {
  938. if (phba->lpfc_injerr_wref_cnt) {
  939. switch (op) {
  940. case SCSI_PROT_WRITE_PASS:
  941. if (src) {
  942. /*
  943. * For WRITE_PASS, force the error
  944. * to be sent on the wire. It should
  945. * be detected by the Target.
  946. * If blockoff != 0 error will be
  947. * inserted in middle of the IO.
  948. */
  949. lpfc_printf_log(phba, KERN_ERR,
  950. LOG_TRACE_EVENT,
  951. "9076 BLKGRD: Injecting reftag error: "
  952. "write lba x%lx + x%x oldrefTag x%x\n",
  953. (unsigned long)lba, blockoff,
  954. be32_to_cpu(src->ref_tag));
  955. /*
  956. * Save the old ref_tag so we can
  957. * restore it on completion.
  958. */
  959. if (lpfc_cmd) {
  960. lpfc_cmd->prot_data_type =
  961. LPFC_INJERR_REFTAG;
  962. lpfc_cmd->prot_data_segment =
  963. src;
  964. lpfc_cmd->prot_data =
  965. src->ref_tag;
  966. }
  967. src->ref_tag = cpu_to_be32(0xDEADBEEF);
  968. phba->lpfc_injerr_wref_cnt--;
  969. if (phba->lpfc_injerr_wref_cnt == 0) {
  970. phba->lpfc_injerr_nportid = 0;
  971. phba->lpfc_injerr_lba =
  972. LPFC_INJERR_LBA_OFF;
  973. memset(&phba->lpfc_injerr_wwpn,
  974. 0, sizeof(struct lpfc_name));
  975. }
  976. rc = BG_ERR_TGT | BG_ERR_CHECK;
  977. break;
  978. }
  979. fallthrough;
  980. case SCSI_PROT_WRITE_INSERT:
  981. /*
  982. * For WRITE_INSERT, force the error
  983. * to be sent on the wire. It should be
  984. * detected by the Target.
  985. */
  986. /* DEADBEEF will be the reftag on the wire */
  987. *reftag = 0xDEADBEEF;
  988. phba->lpfc_injerr_wref_cnt--;
  989. if (phba->lpfc_injerr_wref_cnt == 0) {
  990. phba->lpfc_injerr_nportid = 0;
  991. phba->lpfc_injerr_lba =
  992. LPFC_INJERR_LBA_OFF;
  993. memset(&phba->lpfc_injerr_wwpn,
  994. 0, sizeof(struct lpfc_name));
  995. }
  996. rc = BG_ERR_TGT | BG_ERR_CHECK;
  997. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  998. "9078 BLKGRD: Injecting reftag error: "
  999. "write lba x%lx\n", (unsigned long)lba);
  1000. break;
  1001. case SCSI_PROT_WRITE_STRIP:
  1002. /*
  1003. * For WRITE_STRIP and WRITE_PASS,
  1004. * force the error on data
  1005. * being copied from SLI-Host to SLI-Port.
  1006. */
  1007. *reftag = 0xDEADBEEF;
  1008. phba->lpfc_injerr_wref_cnt--;
  1009. if (phba->lpfc_injerr_wref_cnt == 0) {
  1010. phba->lpfc_injerr_nportid = 0;
  1011. phba->lpfc_injerr_lba =
  1012. LPFC_INJERR_LBA_OFF;
  1013. memset(&phba->lpfc_injerr_wwpn,
  1014. 0, sizeof(struct lpfc_name));
  1015. }
  1016. rc = BG_ERR_INIT;
  1017. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1018. "9077 BLKGRD: Injecting reftag error: "
  1019. "write lba x%lx\n", (unsigned long)lba);
  1020. break;
  1021. }
  1022. }
  1023. if (phba->lpfc_injerr_rref_cnt) {
  1024. switch (op) {
  1025. case SCSI_PROT_READ_INSERT:
  1026. case SCSI_PROT_READ_STRIP:
  1027. case SCSI_PROT_READ_PASS:
  1028. /*
  1029. * For READ_STRIP and READ_PASS, force the
  1030. * error on data being read off the wire. It
  1031. * should force an IO error to the driver.
  1032. */
  1033. *reftag = 0xDEADBEEF;
  1034. phba->lpfc_injerr_rref_cnt--;
  1035. if (phba->lpfc_injerr_rref_cnt == 0) {
  1036. phba->lpfc_injerr_nportid = 0;
  1037. phba->lpfc_injerr_lba =
  1038. LPFC_INJERR_LBA_OFF;
  1039. memset(&phba->lpfc_injerr_wwpn,
  1040. 0, sizeof(struct lpfc_name));
  1041. }
  1042. rc = BG_ERR_INIT;
  1043. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1044. "9079 BLKGRD: Injecting reftag error: "
  1045. "read lba x%lx\n", (unsigned long)lba);
  1046. break;
  1047. }
  1048. }
  1049. }
  1050. /* Should we change the Application Tag */
  1051. if (apptag) {
  1052. if (phba->lpfc_injerr_wapp_cnt) {
  1053. switch (op) {
  1054. case SCSI_PROT_WRITE_PASS:
  1055. if (src) {
  1056. /*
  1057. * For WRITE_PASS, force the error
  1058. * to be sent on the wire. It should
  1059. * be detected by the Target.
  1060. * If blockoff != 0 error will be
  1061. * inserted in middle of the IO.
  1062. */
  1063. lpfc_printf_log(phba, KERN_ERR,
  1064. LOG_TRACE_EVENT,
  1065. "9080 BLKGRD: Injecting apptag error: "
  1066. "write lba x%lx + x%x oldappTag x%x\n",
  1067. (unsigned long)lba, blockoff,
  1068. be16_to_cpu(src->app_tag));
  1069. /*
  1070. * Save the old app_tag so we can
  1071. * restore it on completion.
  1072. */
  1073. if (lpfc_cmd) {
  1074. lpfc_cmd->prot_data_type =
  1075. LPFC_INJERR_APPTAG;
  1076. lpfc_cmd->prot_data_segment =
  1077. src;
  1078. lpfc_cmd->prot_data =
  1079. src->app_tag;
  1080. }
  1081. src->app_tag = cpu_to_be16(0xDEAD);
  1082. phba->lpfc_injerr_wapp_cnt--;
  1083. if (phba->lpfc_injerr_wapp_cnt == 0) {
  1084. phba->lpfc_injerr_nportid = 0;
  1085. phba->lpfc_injerr_lba =
  1086. LPFC_INJERR_LBA_OFF;
  1087. memset(&phba->lpfc_injerr_wwpn,
  1088. 0, sizeof(struct lpfc_name));
  1089. }
  1090. rc = BG_ERR_TGT | BG_ERR_CHECK;
  1091. break;
  1092. }
  1093. fallthrough;
  1094. case SCSI_PROT_WRITE_INSERT:
  1095. /*
  1096. * For WRITE_INSERT, force the
  1097. * error to be sent on the wire. It should be
  1098. * detected by the Target.
  1099. */
  1100. /* DEAD will be the apptag on the wire */
  1101. *apptag = 0xDEAD;
  1102. phba->lpfc_injerr_wapp_cnt--;
  1103. if (phba->lpfc_injerr_wapp_cnt == 0) {
  1104. phba->lpfc_injerr_nportid = 0;
  1105. phba->lpfc_injerr_lba =
  1106. LPFC_INJERR_LBA_OFF;
  1107. memset(&phba->lpfc_injerr_wwpn,
  1108. 0, sizeof(struct lpfc_name));
  1109. }
  1110. rc = BG_ERR_TGT | BG_ERR_CHECK;
  1111. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1112. "0813 BLKGRD: Injecting apptag error: "
  1113. "write lba x%lx\n", (unsigned long)lba);
  1114. break;
  1115. case SCSI_PROT_WRITE_STRIP:
  1116. /*
  1117. * For WRITE_STRIP and WRITE_PASS,
  1118. * force the error on data
  1119. * being copied from SLI-Host to SLI-Port.
  1120. */
  1121. *apptag = 0xDEAD;
  1122. phba->lpfc_injerr_wapp_cnt--;
  1123. if (phba->lpfc_injerr_wapp_cnt == 0) {
  1124. phba->lpfc_injerr_nportid = 0;
  1125. phba->lpfc_injerr_lba =
  1126. LPFC_INJERR_LBA_OFF;
  1127. memset(&phba->lpfc_injerr_wwpn,
  1128. 0, sizeof(struct lpfc_name));
  1129. }
  1130. rc = BG_ERR_INIT;
  1131. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1132. "0812 BLKGRD: Injecting apptag error: "
  1133. "write lba x%lx\n", (unsigned long)lba);
  1134. break;
  1135. }
  1136. }
  1137. if (phba->lpfc_injerr_rapp_cnt) {
  1138. switch (op) {
  1139. case SCSI_PROT_READ_INSERT:
  1140. case SCSI_PROT_READ_STRIP:
  1141. case SCSI_PROT_READ_PASS:
  1142. /*
  1143. * For READ_STRIP and READ_PASS, force the
  1144. * error on data being read off the wire. It
  1145. * should force an IO error to the driver.
  1146. */
  1147. *apptag = 0xDEAD;
  1148. phba->lpfc_injerr_rapp_cnt--;
  1149. if (phba->lpfc_injerr_rapp_cnt == 0) {
  1150. phba->lpfc_injerr_nportid = 0;
  1151. phba->lpfc_injerr_lba =
  1152. LPFC_INJERR_LBA_OFF;
  1153. memset(&phba->lpfc_injerr_wwpn,
  1154. 0, sizeof(struct lpfc_name));
  1155. }
  1156. rc = BG_ERR_INIT;
  1157. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1158. "0814 BLKGRD: Injecting apptag error: "
  1159. "read lba x%lx\n", (unsigned long)lba);
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. /* Should we change the Guard Tag */
  1165. if (new_guard) {
  1166. if (phba->lpfc_injerr_wgrd_cnt) {
  1167. switch (op) {
  1168. case SCSI_PROT_WRITE_PASS:
  1169. rc = BG_ERR_CHECK;
  1170. fallthrough;
  1171. case SCSI_PROT_WRITE_INSERT:
  1172. /*
  1173. * For WRITE_INSERT, force the
  1174. * error to be sent on the wire. It should be
  1175. * detected by the Target.
  1176. */
  1177. phba->lpfc_injerr_wgrd_cnt--;
  1178. if (phba->lpfc_injerr_wgrd_cnt == 0) {
  1179. phba->lpfc_injerr_nportid = 0;
  1180. phba->lpfc_injerr_lba =
  1181. LPFC_INJERR_LBA_OFF;
  1182. memset(&phba->lpfc_injerr_wwpn,
  1183. 0, sizeof(struct lpfc_name));
  1184. }
  1185. rc |= BG_ERR_TGT | BG_ERR_SWAP;
  1186. /* Signals the caller to swap CRC->CSUM */
  1187. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1188. "0817 BLKGRD: Injecting guard error: "
  1189. "write lba x%lx\n", (unsigned long)lba);
  1190. break;
  1191. case SCSI_PROT_WRITE_STRIP:
  1192. /*
  1193. * For WRITE_STRIP and WRITE_PASS,
  1194. * force the error on data
  1195. * being copied from SLI-Host to SLI-Port.
  1196. */
  1197. phba->lpfc_injerr_wgrd_cnt--;
  1198. if (phba->lpfc_injerr_wgrd_cnt == 0) {
  1199. phba->lpfc_injerr_nportid = 0;
  1200. phba->lpfc_injerr_lba =
  1201. LPFC_INJERR_LBA_OFF;
  1202. memset(&phba->lpfc_injerr_wwpn,
  1203. 0, sizeof(struct lpfc_name));
  1204. }
  1205. rc = BG_ERR_INIT | BG_ERR_SWAP;
  1206. /* Signals the caller to swap CRC->CSUM */
  1207. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1208. "0816 BLKGRD: Injecting guard error: "
  1209. "write lba x%lx\n", (unsigned long)lba);
  1210. break;
  1211. }
  1212. }
  1213. if (phba->lpfc_injerr_rgrd_cnt) {
  1214. switch (op) {
  1215. case SCSI_PROT_READ_INSERT:
  1216. case SCSI_PROT_READ_STRIP:
  1217. case SCSI_PROT_READ_PASS:
  1218. /*
  1219. * For READ_STRIP and READ_PASS, force the
  1220. * error on data being read off the wire. It
  1221. * should force an IO error to the driver.
  1222. */
  1223. phba->lpfc_injerr_rgrd_cnt--;
  1224. if (phba->lpfc_injerr_rgrd_cnt == 0) {
  1225. phba->lpfc_injerr_nportid = 0;
  1226. phba->lpfc_injerr_lba =
  1227. LPFC_INJERR_LBA_OFF;
  1228. memset(&phba->lpfc_injerr_wwpn,
  1229. 0, sizeof(struct lpfc_name));
  1230. }
  1231. rc = BG_ERR_INIT | BG_ERR_SWAP;
  1232. /* Signals the caller to swap CRC->CSUM */
  1233. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1234. "0818 BLKGRD: Injecting guard error: "
  1235. "read lba x%lx\n", (unsigned long)lba);
  1236. }
  1237. }
  1238. }
  1239. return rc;
  1240. }
  1241. #endif
  1242. /**
  1243. * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
  1244. * the specified SCSI command.
  1245. * @phba: The Hba for which this call is being executed.
  1246. * @sc: The SCSI command to examine
  1247. * @txop: (out) BlockGuard operation for transmitted data
  1248. * @rxop: (out) BlockGuard operation for received data
  1249. *
  1250. * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
  1251. *
  1252. **/
  1253. static int
  1254. lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1255. uint8_t *txop, uint8_t *rxop)
  1256. {
  1257. uint8_t ret = 0;
  1258. if (sc->prot_flags & SCSI_PROT_IP_CHECKSUM) {
  1259. switch (scsi_get_prot_op(sc)) {
  1260. case SCSI_PROT_READ_INSERT:
  1261. case SCSI_PROT_WRITE_STRIP:
  1262. *rxop = BG_OP_IN_NODIF_OUT_CSUM;
  1263. *txop = BG_OP_IN_CSUM_OUT_NODIF;
  1264. break;
  1265. case SCSI_PROT_READ_STRIP:
  1266. case SCSI_PROT_WRITE_INSERT:
  1267. *rxop = BG_OP_IN_CRC_OUT_NODIF;
  1268. *txop = BG_OP_IN_NODIF_OUT_CRC;
  1269. break;
  1270. case SCSI_PROT_READ_PASS:
  1271. case SCSI_PROT_WRITE_PASS:
  1272. *rxop = BG_OP_IN_CRC_OUT_CSUM;
  1273. *txop = BG_OP_IN_CSUM_OUT_CRC;
  1274. break;
  1275. case SCSI_PROT_NORMAL:
  1276. default:
  1277. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1278. "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
  1279. scsi_get_prot_op(sc));
  1280. ret = 1;
  1281. break;
  1282. }
  1283. } else {
  1284. switch (scsi_get_prot_op(sc)) {
  1285. case SCSI_PROT_READ_STRIP:
  1286. case SCSI_PROT_WRITE_INSERT:
  1287. *rxop = BG_OP_IN_CRC_OUT_NODIF;
  1288. *txop = BG_OP_IN_NODIF_OUT_CRC;
  1289. break;
  1290. case SCSI_PROT_READ_PASS:
  1291. case SCSI_PROT_WRITE_PASS:
  1292. *rxop = BG_OP_IN_CRC_OUT_CRC;
  1293. *txop = BG_OP_IN_CRC_OUT_CRC;
  1294. break;
  1295. case SCSI_PROT_READ_INSERT:
  1296. case SCSI_PROT_WRITE_STRIP:
  1297. *rxop = BG_OP_IN_NODIF_OUT_CRC;
  1298. *txop = BG_OP_IN_CRC_OUT_NODIF;
  1299. break;
  1300. case SCSI_PROT_NORMAL:
  1301. default:
  1302. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1303. "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
  1304. scsi_get_prot_op(sc));
  1305. ret = 1;
  1306. break;
  1307. }
  1308. }
  1309. return ret;
  1310. }
  1311. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1312. /**
  1313. * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
  1314. * the specified SCSI command in order to force a guard tag error.
  1315. * @phba: The Hba for which this call is being executed.
  1316. * @sc: The SCSI command to examine
  1317. * @txop: (out) BlockGuard operation for transmitted data
  1318. * @rxop: (out) BlockGuard operation for received data
  1319. *
  1320. * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
  1321. *
  1322. **/
  1323. static int
  1324. lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1325. uint8_t *txop, uint8_t *rxop)
  1326. {
  1327. if (sc->prot_flags & SCSI_PROT_IP_CHECKSUM) {
  1328. switch (scsi_get_prot_op(sc)) {
  1329. case SCSI_PROT_READ_INSERT:
  1330. case SCSI_PROT_WRITE_STRIP:
  1331. *rxop = BG_OP_IN_NODIF_OUT_CRC;
  1332. *txop = BG_OP_IN_CRC_OUT_NODIF;
  1333. break;
  1334. case SCSI_PROT_READ_STRIP:
  1335. case SCSI_PROT_WRITE_INSERT:
  1336. *rxop = BG_OP_IN_CSUM_OUT_NODIF;
  1337. *txop = BG_OP_IN_NODIF_OUT_CSUM;
  1338. break;
  1339. case SCSI_PROT_READ_PASS:
  1340. case SCSI_PROT_WRITE_PASS:
  1341. *rxop = BG_OP_IN_CSUM_OUT_CRC;
  1342. *txop = BG_OP_IN_CRC_OUT_CSUM;
  1343. break;
  1344. case SCSI_PROT_NORMAL:
  1345. default:
  1346. break;
  1347. }
  1348. } else {
  1349. switch (scsi_get_prot_op(sc)) {
  1350. case SCSI_PROT_READ_STRIP:
  1351. case SCSI_PROT_WRITE_INSERT:
  1352. *rxop = BG_OP_IN_CSUM_OUT_NODIF;
  1353. *txop = BG_OP_IN_NODIF_OUT_CSUM;
  1354. break;
  1355. case SCSI_PROT_READ_PASS:
  1356. case SCSI_PROT_WRITE_PASS:
  1357. *rxop = BG_OP_IN_CSUM_OUT_CSUM;
  1358. *txop = BG_OP_IN_CSUM_OUT_CSUM;
  1359. break;
  1360. case SCSI_PROT_READ_INSERT:
  1361. case SCSI_PROT_WRITE_STRIP:
  1362. *rxop = BG_OP_IN_NODIF_OUT_CSUM;
  1363. *txop = BG_OP_IN_CSUM_OUT_NODIF;
  1364. break;
  1365. case SCSI_PROT_NORMAL:
  1366. default:
  1367. break;
  1368. }
  1369. }
  1370. return 0;
  1371. }
  1372. #endif
  1373. /**
  1374. * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
  1375. * @phba: The Hba for which this call is being executed.
  1376. * @sc: pointer to scsi command we're working on
  1377. * @bpl: pointer to buffer list for protection groups
  1378. * @datasegcnt: number of segments of data that have been dma mapped
  1379. *
  1380. * This function sets up BPL buffer list for protection groups of
  1381. * type LPFC_PG_TYPE_NO_DIF
  1382. *
  1383. * This is usually used when the HBA is instructed to generate
  1384. * DIFs and insert them into data stream (or strip DIF from
  1385. * incoming data stream)
  1386. *
  1387. * The buffer list consists of just one protection group described
  1388. * below:
  1389. * +-------------------------+
  1390. * start of prot group --> | PDE_5 |
  1391. * +-------------------------+
  1392. * | PDE_6 |
  1393. * +-------------------------+
  1394. * | Data BDE |
  1395. * +-------------------------+
  1396. * |more Data BDE's ... (opt)|
  1397. * +-------------------------+
  1398. *
  1399. *
  1400. * Note: Data s/g buffers have been dma mapped
  1401. *
  1402. * Returns the number of BDEs added to the BPL.
  1403. **/
  1404. static int
  1405. lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1406. struct ulp_bde64 *bpl, int datasegcnt)
  1407. {
  1408. struct scatterlist *sgde = NULL; /* s/g data entry */
  1409. struct lpfc_pde5 *pde5 = NULL;
  1410. struct lpfc_pde6 *pde6 = NULL;
  1411. dma_addr_t physaddr;
  1412. int i = 0, num_bde = 0, status;
  1413. int datadir = sc->sc_data_direction;
  1414. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1415. uint32_t rc;
  1416. #endif
  1417. uint32_t checking = 1;
  1418. uint32_t reftag;
  1419. uint8_t txop, rxop;
  1420. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1421. if (status)
  1422. goto out;
  1423. /* extract some info from the scsi command for pde*/
  1424. reftag = scsi_prot_ref_tag(sc);
  1425. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1426. rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
  1427. if (rc) {
  1428. if (rc & BG_ERR_SWAP)
  1429. lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
  1430. if (rc & BG_ERR_CHECK)
  1431. checking = 0;
  1432. }
  1433. #endif
  1434. /* setup PDE5 with what we have */
  1435. pde5 = (struct lpfc_pde5 *) bpl;
  1436. memset(pde5, 0, sizeof(struct lpfc_pde5));
  1437. bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
  1438. /* Endianness conversion if necessary for PDE5 */
  1439. pde5->word0 = cpu_to_le32(pde5->word0);
  1440. pde5->reftag = cpu_to_le32(reftag);
  1441. /* advance bpl and increment bde count */
  1442. num_bde++;
  1443. bpl++;
  1444. pde6 = (struct lpfc_pde6 *) bpl;
  1445. /* setup PDE6 with the rest of the info */
  1446. memset(pde6, 0, sizeof(struct lpfc_pde6));
  1447. bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
  1448. bf_set(pde6_optx, pde6, txop);
  1449. bf_set(pde6_oprx, pde6, rxop);
  1450. /*
  1451. * We only need to check the data on READs, for WRITEs
  1452. * protection data is automatically generated, not checked.
  1453. */
  1454. if (datadir == DMA_FROM_DEVICE) {
  1455. if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
  1456. bf_set(pde6_ce, pde6, checking);
  1457. else
  1458. bf_set(pde6_ce, pde6, 0);
  1459. if (sc->prot_flags & SCSI_PROT_REF_CHECK)
  1460. bf_set(pde6_re, pde6, checking);
  1461. else
  1462. bf_set(pde6_re, pde6, 0);
  1463. }
  1464. bf_set(pde6_ai, pde6, 1);
  1465. bf_set(pde6_ae, pde6, 0);
  1466. bf_set(pde6_apptagval, pde6, 0);
  1467. /* Endianness conversion if necessary for PDE6 */
  1468. pde6->word0 = cpu_to_le32(pde6->word0);
  1469. pde6->word1 = cpu_to_le32(pde6->word1);
  1470. pde6->word2 = cpu_to_le32(pde6->word2);
  1471. /* advance bpl and increment bde count */
  1472. num_bde++;
  1473. bpl++;
  1474. /* assumption: caller has already run dma_map_sg on command data */
  1475. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  1476. physaddr = sg_dma_address(sgde);
  1477. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1478. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1479. bpl->tus.f.bdeSize = sg_dma_len(sgde);
  1480. if (datadir == DMA_TO_DEVICE)
  1481. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1482. else
  1483. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1484. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1485. bpl++;
  1486. num_bde++;
  1487. }
  1488. out:
  1489. return num_bde;
  1490. }
  1491. /**
  1492. * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
  1493. * @phba: The Hba for which this call is being executed.
  1494. * @sc: pointer to scsi command we're working on
  1495. * @bpl: pointer to buffer list for protection groups
  1496. * @datacnt: number of segments of data that have been dma mapped
  1497. * @protcnt: number of segment of protection data that have been dma mapped
  1498. *
  1499. * This function sets up BPL buffer list for protection groups of
  1500. * type LPFC_PG_TYPE_DIF
  1501. *
  1502. * This is usually used when DIFs are in their own buffers,
  1503. * separate from the data. The HBA can then by instructed
  1504. * to place the DIFs in the outgoing stream. For read operations,
  1505. * The HBA could extract the DIFs and place it in DIF buffers.
  1506. *
  1507. * The buffer list for this type consists of one or more of the
  1508. * protection groups described below:
  1509. * +-------------------------+
  1510. * start of first prot group --> | PDE_5 |
  1511. * +-------------------------+
  1512. * | PDE_6 |
  1513. * +-------------------------+
  1514. * | PDE_7 (Prot BDE) |
  1515. * +-------------------------+
  1516. * | Data BDE |
  1517. * +-------------------------+
  1518. * |more Data BDE's ... (opt)|
  1519. * +-------------------------+
  1520. * start of new prot group --> | PDE_5 |
  1521. * +-------------------------+
  1522. * | ... |
  1523. * +-------------------------+
  1524. *
  1525. * Note: It is assumed that both data and protection s/g buffers have been
  1526. * mapped for DMA
  1527. *
  1528. * Returns the number of BDEs added to the BPL.
  1529. **/
  1530. static int
  1531. lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1532. struct ulp_bde64 *bpl, int datacnt, int protcnt)
  1533. {
  1534. struct scatterlist *sgde = NULL; /* s/g data entry */
  1535. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  1536. struct lpfc_pde5 *pde5 = NULL;
  1537. struct lpfc_pde6 *pde6 = NULL;
  1538. struct lpfc_pde7 *pde7 = NULL;
  1539. dma_addr_t dataphysaddr, protphysaddr;
  1540. unsigned short curr_data = 0, curr_prot = 0;
  1541. unsigned int split_offset;
  1542. unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
  1543. unsigned int protgrp_blks, protgrp_bytes;
  1544. unsigned int remainder, subtotal;
  1545. int status;
  1546. int datadir = sc->sc_data_direction;
  1547. unsigned char pgdone = 0, alldone = 0;
  1548. unsigned blksize;
  1549. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1550. uint32_t rc;
  1551. #endif
  1552. uint32_t checking = 1;
  1553. uint32_t reftag;
  1554. uint8_t txop, rxop;
  1555. int num_bde = 0;
  1556. sgpe = scsi_prot_sglist(sc);
  1557. sgde = scsi_sglist(sc);
  1558. if (!sgpe || !sgde) {
  1559. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1560. "9020 Invalid s/g entry: data=x%px prot=x%px\n",
  1561. sgpe, sgde);
  1562. return 0;
  1563. }
  1564. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1565. if (status)
  1566. goto out;
  1567. /* extract some info from the scsi command */
  1568. blksize = scsi_prot_interval(sc);
  1569. reftag = scsi_prot_ref_tag(sc);
  1570. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1571. rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
  1572. if (rc) {
  1573. if (rc & BG_ERR_SWAP)
  1574. lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
  1575. if (rc & BG_ERR_CHECK)
  1576. checking = 0;
  1577. }
  1578. #endif
  1579. split_offset = 0;
  1580. do {
  1581. /* Check to see if we ran out of space */
  1582. if (num_bde >= (phba->cfg_total_seg_cnt - 2))
  1583. return num_bde + 3;
  1584. /* setup PDE5 with what we have */
  1585. pde5 = (struct lpfc_pde5 *) bpl;
  1586. memset(pde5, 0, sizeof(struct lpfc_pde5));
  1587. bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
  1588. /* Endianness conversion if necessary for PDE5 */
  1589. pde5->word0 = cpu_to_le32(pde5->word0);
  1590. pde5->reftag = cpu_to_le32(reftag);
  1591. /* advance bpl and increment bde count */
  1592. num_bde++;
  1593. bpl++;
  1594. pde6 = (struct lpfc_pde6 *) bpl;
  1595. /* setup PDE6 with the rest of the info */
  1596. memset(pde6, 0, sizeof(struct lpfc_pde6));
  1597. bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
  1598. bf_set(pde6_optx, pde6, txop);
  1599. bf_set(pde6_oprx, pde6, rxop);
  1600. if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
  1601. bf_set(pde6_ce, pde6, checking);
  1602. else
  1603. bf_set(pde6_ce, pde6, 0);
  1604. if (sc->prot_flags & SCSI_PROT_REF_CHECK)
  1605. bf_set(pde6_re, pde6, checking);
  1606. else
  1607. bf_set(pde6_re, pde6, 0);
  1608. bf_set(pde6_ai, pde6, 1);
  1609. bf_set(pde6_ae, pde6, 0);
  1610. bf_set(pde6_apptagval, pde6, 0);
  1611. /* Endianness conversion if necessary for PDE6 */
  1612. pde6->word0 = cpu_to_le32(pde6->word0);
  1613. pde6->word1 = cpu_to_le32(pde6->word1);
  1614. pde6->word2 = cpu_to_le32(pde6->word2);
  1615. /* advance bpl and increment bde count */
  1616. num_bde++;
  1617. bpl++;
  1618. /* setup the first BDE that points to protection buffer */
  1619. protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
  1620. protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
  1621. /* must be integer multiple of the DIF block length */
  1622. BUG_ON(protgroup_len % 8);
  1623. pde7 = (struct lpfc_pde7 *) bpl;
  1624. memset(pde7, 0, sizeof(struct lpfc_pde7));
  1625. bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
  1626. pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
  1627. pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
  1628. protgrp_blks = protgroup_len / 8;
  1629. protgrp_bytes = protgrp_blks * blksize;
  1630. /* check if this pde is crossing the 4K boundary; if so split */
  1631. if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
  1632. protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
  1633. protgroup_offset += protgroup_remainder;
  1634. protgrp_blks = protgroup_remainder / 8;
  1635. protgrp_bytes = protgrp_blks * blksize;
  1636. } else {
  1637. protgroup_offset = 0;
  1638. curr_prot++;
  1639. }
  1640. num_bde++;
  1641. /* setup BDE's for data blocks associated with DIF data */
  1642. pgdone = 0;
  1643. subtotal = 0; /* total bytes processed for current prot grp */
  1644. while (!pgdone) {
  1645. /* Check to see if we ran out of space */
  1646. if (num_bde >= phba->cfg_total_seg_cnt)
  1647. return num_bde + 1;
  1648. if (!sgde) {
  1649. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1650. "9065 BLKGRD:%s Invalid data segment\n",
  1651. __func__);
  1652. return 0;
  1653. }
  1654. bpl++;
  1655. dataphysaddr = sg_dma_address(sgde) + split_offset;
  1656. bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
  1657. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
  1658. remainder = sg_dma_len(sgde) - split_offset;
  1659. if ((subtotal + remainder) <= protgrp_bytes) {
  1660. /* we can use this whole buffer */
  1661. bpl->tus.f.bdeSize = remainder;
  1662. split_offset = 0;
  1663. if ((subtotal + remainder) == protgrp_bytes)
  1664. pgdone = 1;
  1665. } else {
  1666. /* must split this buffer with next prot grp */
  1667. bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
  1668. split_offset += bpl->tus.f.bdeSize;
  1669. }
  1670. subtotal += bpl->tus.f.bdeSize;
  1671. if (datadir == DMA_TO_DEVICE)
  1672. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1673. else
  1674. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1675. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1676. num_bde++;
  1677. curr_data++;
  1678. if (split_offset)
  1679. break;
  1680. /* Move to the next s/g segment if possible */
  1681. sgde = sg_next(sgde);
  1682. }
  1683. if (protgroup_offset) {
  1684. /* update the reference tag */
  1685. reftag += protgrp_blks;
  1686. bpl++;
  1687. continue;
  1688. }
  1689. /* are we done ? */
  1690. if (curr_prot == protcnt) {
  1691. alldone = 1;
  1692. } else if (curr_prot < protcnt) {
  1693. /* advance to next prot buffer */
  1694. sgpe = sg_next(sgpe);
  1695. bpl++;
  1696. /* update the reference tag */
  1697. reftag += protgrp_blks;
  1698. } else {
  1699. /* if we're here, we have a bug */
  1700. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1701. "9054 BLKGRD: bug in %s\n", __func__);
  1702. }
  1703. } while (!alldone);
  1704. out:
  1705. return num_bde;
  1706. }
  1707. /**
  1708. * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
  1709. * @phba: The Hba for which this call is being executed.
  1710. * @sc: pointer to scsi command we're working on
  1711. * @sgl: pointer to buffer list for protection groups
  1712. * @datasegcnt: number of segments of data that have been dma mapped
  1713. * @lpfc_cmd: lpfc scsi command object pointer.
  1714. *
  1715. * This function sets up SGL buffer list for protection groups of
  1716. * type LPFC_PG_TYPE_NO_DIF
  1717. *
  1718. * This is usually used when the HBA is instructed to generate
  1719. * DIFs and insert them into data stream (or strip DIF from
  1720. * incoming data stream)
  1721. *
  1722. * The buffer list consists of just one protection group described
  1723. * below:
  1724. * +-------------------------+
  1725. * start of prot group --> | DI_SEED |
  1726. * +-------------------------+
  1727. * | Data SGE |
  1728. * +-------------------------+
  1729. * |more Data SGE's ... (opt)|
  1730. * +-------------------------+
  1731. *
  1732. *
  1733. * Note: Data s/g buffers have been dma mapped
  1734. *
  1735. * Returns the number of SGEs added to the SGL.
  1736. **/
  1737. static int
  1738. lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1739. struct sli4_sge *sgl, int datasegcnt,
  1740. struct lpfc_io_buf *lpfc_cmd)
  1741. {
  1742. struct scatterlist *sgde = NULL; /* s/g data entry */
  1743. struct sli4_sge_diseed *diseed = NULL;
  1744. dma_addr_t physaddr;
  1745. int i = 0, num_sge = 0, status;
  1746. uint32_t reftag;
  1747. uint8_t txop, rxop;
  1748. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1749. uint32_t rc;
  1750. #endif
  1751. uint32_t checking = 1;
  1752. uint32_t dma_len;
  1753. uint32_t dma_offset = 0;
  1754. struct sli4_hybrid_sgl *sgl_xtra = NULL;
  1755. int j;
  1756. bool lsp_just_set = false;
  1757. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1758. if (status)
  1759. goto out;
  1760. /* extract some info from the scsi command for pde*/
  1761. reftag = scsi_prot_ref_tag(sc);
  1762. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1763. rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
  1764. if (rc) {
  1765. if (rc & BG_ERR_SWAP)
  1766. lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
  1767. if (rc & BG_ERR_CHECK)
  1768. checking = 0;
  1769. }
  1770. #endif
  1771. /* setup DISEED with what we have */
  1772. diseed = (struct sli4_sge_diseed *) sgl;
  1773. memset(diseed, 0, sizeof(struct sli4_sge_diseed));
  1774. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
  1775. /* Endianness conversion if necessary */
  1776. diseed->ref_tag = cpu_to_le32(reftag);
  1777. diseed->ref_tag_tran = diseed->ref_tag;
  1778. /*
  1779. * We only need to check the data on READs, for WRITEs
  1780. * protection data is automatically generated, not checked.
  1781. */
  1782. if (sc->sc_data_direction == DMA_FROM_DEVICE) {
  1783. if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
  1784. bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
  1785. else
  1786. bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
  1787. if (sc->prot_flags & SCSI_PROT_REF_CHECK)
  1788. bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
  1789. else
  1790. bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
  1791. }
  1792. /* setup DISEED with the rest of the info */
  1793. bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
  1794. bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
  1795. bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
  1796. bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
  1797. /* Endianness conversion if necessary for DISEED */
  1798. diseed->word2 = cpu_to_le32(diseed->word2);
  1799. diseed->word3 = cpu_to_le32(diseed->word3);
  1800. /* advance bpl and increment sge count */
  1801. num_sge++;
  1802. sgl++;
  1803. /* assumption: caller has already run dma_map_sg on command data */
  1804. sgde = scsi_sglist(sc);
  1805. j = 3;
  1806. for (i = 0; i < datasegcnt; i++) {
  1807. /* clear it */
  1808. sgl->word2 = 0;
  1809. /* do we need to expand the segment */
  1810. if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
  1811. ((datasegcnt - 1) != i)) {
  1812. /* set LSP type */
  1813. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
  1814. sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
  1815. if (unlikely(!sgl_xtra)) {
  1816. lpfc_cmd->seg_cnt = 0;
  1817. return 0;
  1818. }
  1819. sgl->addr_lo = cpu_to_le32(putPaddrLow(
  1820. sgl_xtra->dma_phys_sgl));
  1821. sgl->addr_hi = cpu_to_le32(putPaddrHigh(
  1822. sgl_xtra->dma_phys_sgl));
  1823. } else {
  1824. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
  1825. }
  1826. if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
  1827. if ((datasegcnt - 1) == i)
  1828. bf_set(lpfc_sli4_sge_last, sgl, 1);
  1829. physaddr = sg_dma_address(sgde);
  1830. dma_len = sg_dma_len(sgde);
  1831. sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
  1832. sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
  1833. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  1834. sgl->word2 = cpu_to_le32(sgl->word2);
  1835. sgl->sge_len = cpu_to_le32(dma_len);
  1836. dma_offset += dma_len;
  1837. sgde = sg_next(sgde);
  1838. sgl++;
  1839. num_sge++;
  1840. lsp_just_set = false;
  1841. } else {
  1842. sgl->word2 = cpu_to_le32(sgl->word2);
  1843. sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
  1844. sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
  1845. i = i - 1;
  1846. lsp_just_set = true;
  1847. }
  1848. j++;
  1849. }
  1850. out:
  1851. return num_sge;
  1852. }
  1853. /**
  1854. * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
  1855. * @phba: The Hba for which this call is being executed.
  1856. * @sc: pointer to scsi command we're working on
  1857. * @sgl: pointer to buffer list for protection groups
  1858. * @datacnt: number of segments of data that have been dma mapped
  1859. * @protcnt: number of segment of protection data that have been dma mapped
  1860. * @lpfc_cmd: lpfc scsi command object pointer.
  1861. *
  1862. * This function sets up SGL buffer list for protection groups of
  1863. * type LPFC_PG_TYPE_DIF
  1864. *
  1865. * This is usually used when DIFs are in their own buffers,
  1866. * separate from the data. The HBA can then by instructed
  1867. * to place the DIFs in the outgoing stream. For read operations,
  1868. * The HBA could extract the DIFs and place it in DIF buffers.
  1869. *
  1870. * The buffer list for this type consists of one or more of the
  1871. * protection groups described below:
  1872. * +-------------------------+
  1873. * start of first prot group --> | DISEED |
  1874. * +-------------------------+
  1875. * | DIF (Prot SGE) |
  1876. * +-------------------------+
  1877. * | Data SGE |
  1878. * +-------------------------+
  1879. * |more Data SGE's ... (opt)|
  1880. * +-------------------------+
  1881. * start of new prot group --> | DISEED |
  1882. * +-------------------------+
  1883. * | ... |
  1884. * +-------------------------+
  1885. *
  1886. * Note: It is assumed that both data and protection s/g buffers have been
  1887. * mapped for DMA
  1888. *
  1889. * Returns the number of SGEs added to the SGL.
  1890. **/
  1891. static int
  1892. lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1893. struct sli4_sge *sgl, int datacnt, int protcnt,
  1894. struct lpfc_io_buf *lpfc_cmd)
  1895. {
  1896. struct scatterlist *sgde = NULL; /* s/g data entry */
  1897. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  1898. struct sli4_sge_diseed *diseed = NULL;
  1899. dma_addr_t dataphysaddr, protphysaddr;
  1900. unsigned short curr_data = 0, curr_prot = 0;
  1901. unsigned int split_offset;
  1902. unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
  1903. unsigned int protgrp_blks, protgrp_bytes;
  1904. unsigned int remainder, subtotal;
  1905. int status;
  1906. unsigned char pgdone = 0, alldone = 0;
  1907. unsigned blksize;
  1908. uint32_t reftag;
  1909. uint8_t txop, rxop;
  1910. uint32_t dma_len;
  1911. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1912. uint32_t rc;
  1913. #endif
  1914. uint32_t checking = 1;
  1915. uint32_t dma_offset = 0;
  1916. int num_sge = 0, j = 2;
  1917. struct sli4_hybrid_sgl *sgl_xtra = NULL;
  1918. sgpe = scsi_prot_sglist(sc);
  1919. sgde = scsi_sglist(sc);
  1920. if (!sgpe || !sgde) {
  1921. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1922. "9082 Invalid s/g entry: data=x%px prot=x%px\n",
  1923. sgpe, sgde);
  1924. return 0;
  1925. }
  1926. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1927. if (status)
  1928. goto out;
  1929. /* extract some info from the scsi command */
  1930. blksize = scsi_prot_interval(sc);
  1931. reftag = scsi_prot_ref_tag(sc);
  1932. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1933. rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
  1934. if (rc) {
  1935. if (rc & BG_ERR_SWAP)
  1936. lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
  1937. if (rc & BG_ERR_CHECK)
  1938. checking = 0;
  1939. }
  1940. #endif
  1941. split_offset = 0;
  1942. do {
  1943. /* Check to see if we ran out of space */
  1944. if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
  1945. !(phba->cfg_xpsgl))
  1946. return num_sge + 3;
  1947. /* DISEED and DIF have to be together */
  1948. if (!((j + 1) % phba->border_sge_num) ||
  1949. !((j + 2) % phba->border_sge_num) ||
  1950. !((j + 3) % phba->border_sge_num)) {
  1951. sgl->word2 = 0;
  1952. /* set LSP type */
  1953. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
  1954. sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
  1955. if (unlikely(!sgl_xtra)) {
  1956. goto out;
  1957. } else {
  1958. sgl->addr_lo = cpu_to_le32(putPaddrLow(
  1959. sgl_xtra->dma_phys_sgl));
  1960. sgl->addr_hi = cpu_to_le32(putPaddrHigh(
  1961. sgl_xtra->dma_phys_sgl));
  1962. }
  1963. sgl->word2 = cpu_to_le32(sgl->word2);
  1964. sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
  1965. sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
  1966. j = 0;
  1967. }
  1968. /* setup DISEED with what we have */
  1969. diseed = (struct sli4_sge_diseed *) sgl;
  1970. memset(diseed, 0, sizeof(struct sli4_sge_diseed));
  1971. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
  1972. /* Endianness conversion if necessary */
  1973. diseed->ref_tag = cpu_to_le32(reftag);
  1974. diseed->ref_tag_tran = diseed->ref_tag;
  1975. if (sc->prot_flags & SCSI_PROT_GUARD_CHECK) {
  1976. bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
  1977. } else {
  1978. bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
  1979. /*
  1980. * When in this mode, the hardware will replace
  1981. * the guard tag from the host with a
  1982. * newly generated good CRC for the wire.
  1983. * Switch to raw mode here to avoid this
  1984. * behavior. What the host sends gets put on the wire.
  1985. */
  1986. if (txop == BG_OP_IN_CRC_OUT_CRC) {
  1987. txop = BG_OP_RAW_MODE;
  1988. rxop = BG_OP_RAW_MODE;
  1989. }
  1990. }
  1991. if (sc->prot_flags & SCSI_PROT_REF_CHECK)
  1992. bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
  1993. else
  1994. bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
  1995. /* setup DISEED with the rest of the info */
  1996. bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
  1997. bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
  1998. bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
  1999. bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
  2000. /* Endianness conversion if necessary for DISEED */
  2001. diseed->word2 = cpu_to_le32(diseed->word2);
  2002. diseed->word3 = cpu_to_le32(diseed->word3);
  2003. /* advance sgl and increment bde count */
  2004. num_sge++;
  2005. sgl++;
  2006. j++;
  2007. /* setup the first BDE that points to protection buffer */
  2008. protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
  2009. protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
  2010. /* must be integer multiple of the DIF block length */
  2011. BUG_ON(protgroup_len % 8);
  2012. /* Now setup DIF SGE */
  2013. sgl->word2 = 0;
  2014. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
  2015. sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
  2016. sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
  2017. sgl->word2 = cpu_to_le32(sgl->word2);
  2018. sgl->sge_len = 0;
  2019. protgrp_blks = protgroup_len / 8;
  2020. protgrp_bytes = protgrp_blks * blksize;
  2021. /* check if DIF SGE is crossing the 4K boundary; if so split */
  2022. if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
  2023. protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
  2024. protgroup_offset += protgroup_remainder;
  2025. protgrp_blks = protgroup_remainder / 8;
  2026. protgrp_bytes = protgrp_blks * blksize;
  2027. } else {
  2028. protgroup_offset = 0;
  2029. curr_prot++;
  2030. }
  2031. num_sge++;
  2032. /* setup SGE's for data blocks associated with DIF data */
  2033. pgdone = 0;
  2034. subtotal = 0; /* total bytes processed for current prot grp */
  2035. sgl++;
  2036. j++;
  2037. while (!pgdone) {
  2038. /* Check to see if we ran out of space */
  2039. if ((num_sge >= phba->cfg_total_seg_cnt) &&
  2040. !phba->cfg_xpsgl)
  2041. return num_sge + 1;
  2042. if (!sgde) {
  2043. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  2044. "9086 BLKGRD:%s Invalid data segment\n",
  2045. __func__);
  2046. return 0;
  2047. }
  2048. if (!((j + 1) % phba->border_sge_num)) {
  2049. sgl->word2 = 0;
  2050. /* set LSP type */
  2051. bf_set(lpfc_sli4_sge_type, sgl,
  2052. LPFC_SGE_TYPE_LSP);
  2053. sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
  2054. lpfc_cmd);
  2055. if (unlikely(!sgl_xtra)) {
  2056. goto out;
  2057. } else {
  2058. sgl->addr_lo = cpu_to_le32(
  2059. putPaddrLow(sgl_xtra->dma_phys_sgl));
  2060. sgl->addr_hi = cpu_to_le32(
  2061. putPaddrHigh(sgl_xtra->dma_phys_sgl));
  2062. }
  2063. sgl->word2 = cpu_to_le32(sgl->word2);
  2064. sgl->sge_len = cpu_to_le32(
  2065. phba->cfg_sg_dma_buf_size);
  2066. sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
  2067. } else {
  2068. dataphysaddr = sg_dma_address(sgde) +
  2069. split_offset;
  2070. remainder = sg_dma_len(sgde) - split_offset;
  2071. if ((subtotal + remainder) <= protgrp_bytes) {
  2072. /* we can use this whole buffer */
  2073. dma_len = remainder;
  2074. split_offset = 0;
  2075. if ((subtotal + remainder) ==
  2076. protgrp_bytes)
  2077. pgdone = 1;
  2078. } else {
  2079. /* must split this buffer with next
  2080. * prot grp
  2081. */
  2082. dma_len = protgrp_bytes - subtotal;
  2083. split_offset += dma_len;
  2084. }
  2085. subtotal += dma_len;
  2086. sgl->word2 = 0;
  2087. sgl->addr_lo = cpu_to_le32(putPaddrLow(
  2088. dataphysaddr));
  2089. sgl->addr_hi = cpu_to_le32(putPaddrHigh(
  2090. dataphysaddr));
  2091. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2092. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  2093. bf_set(lpfc_sli4_sge_type, sgl,
  2094. LPFC_SGE_TYPE_DATA);
  2095. sgl->sge_len = cpu_to_le32(dma_len);
  2096. dma_offset += dma_len;
  2097. num_sge++;
  2098. curr_data++;
  2099. if (split_offset) {
  2100. sgl++;
  2101. j++;
  2102. break;
  2103. }
  2104. /* Move to the next s/g segment if possible */
  2105. sgde = sg_next(sgde);
  2106. sgl++;
  2107. }
  2108. j++;
  2109. }
  2110. if (protgroup_offset) {
  2111. /* update the reference tag */
  2112. reftag += protgrp_blks;
  2113. continue;
  2114. }
  2115. /* are we done ? */
  2116. if (curr_prot == protcnt) {
  2117. /* mark the last SGL */
  2118. sgl--;
  2119. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2120. alldone = 1;
  2121. } else if (curr_prot < protcnt) {
  2122. /* advance to next prot buffer */
  2123. sgpe = sg_next(sgpe);
  2124. /* update the reference tag */
  2125. reftag += protgrp_blks;
  2126. } else {
  2127. /* if we're here, we have a bug */
  2128. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  2129. "9085 BLKGRD: bug in %s\n", __func__);
  2130. }
  2131. } while (!alldone);
  2132. out:
  2133. return num_sge;
  2134. }
  2135. /**
  2136. * lpfc_prot_group_type - Get prtotection group type of SCSI command
  2137. * @phba: The Hba for which this call is being executed.
  2138. * @sc: pointer to scsi command we're working on
  2139. *
  2140. * Given a SCSI command that supports DIF, determine composition of protection
  2141. * groups involved in setting up buffer lists
  2142. *
  2143. * Returns: Protection group type (with or without DIF)
  2144. *
  2145. **/
  2146. static int
  2147. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  2148. {
  2149. int ret = LPFC_PG_TYPE_INVALID;
  2150. unsigned char op = scsi_get_prot_op(sc);
  2151. switch (op) {
  2152. case SCSI_PROT_READ_STRIP:
  2153. case SCSI_PROT_WRITE_INSERT:
  2154. ret = LPFC_PG_TYPE_NO_DIF;
  2155. break;
  2156. case SCSI_PROT_READ_INSERT:
  2157. case SCSI_PROT_WRITE_STRIP:
  2158. case SCSI_PROT_READ_PASS:
  2159. case SCSI_PROT_WRITE_PASS:
  2160. ret = LPFC_PG_TYPE_DIF_BUF;
  2161. break;
  2162. default:
  2163. if (phba)
  2164. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  2165. "9021 Unsupported protection op:%d\n",
  2166. op);
  2167. break;
  2168. }
  2169. return ret;
  2170. }
  2171. /**
  2172. * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
  2173. * @phba: The Hba for which this call is being executed.
  2174. * @lpfc_cmd: The scsi buffer which is going to be adjusted.
  2175. *
  2176. * Adjust the data length to account for how much data
  2177. * is actually on the wire.
  2178. *
  2179. * returns the adjusted data length
  2180. **/
  2181. static int
  2182. lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
  2183. struct lpfc_io_buf *lpfc_cmd)
  2184. {
  2185. struct scsi_cmnd *sc = lpfc_cmd->pCmd;
  2186. int fcpdl;
  2187. fcpdl = scsi_bufflen(sc);
  2188. /* Check if there is protection data on the wire */
  2189. if (sc->sc_data_direction == DMA_FROM_DEVICE) {
  2190. /* Read check for protection data */
  2191. if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
  2192. return fcpdl;
  2193. } else {
  2194. /* Write check for protection data */
  2195. if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
  2196. return fcpdl;
  2197. }
  2198. /*
  2199. * If we are in DIF Type 1 mode every data block has a 8 byte
  2200. * DIF (trailer) attached to it. Must ajust FCP data length
  2201. * to account for the protection data.
  2202. */
  2203. fcpdl += (fcpdl / scsi_prot_interval(sc)) * 8;
  2204. return fcpdl;
  2205. }
  2206. /**
  2207. * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
  2208. * @phba: The Hba for which this call is being executed.
  2209. * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
  2210. *
  2211. * This is the protection/DIF aware version of
  2212. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  2213. * two functions eventually, but for now, it's here.
  2214. * RETURNS 0 - SUCCESS,
  2215. * 1 - Failed DMA map, retry.
  2216. * 2 - Invalid scsi cmd or prot-type. Do not rety.
  2217. **/
  2218. static int
  2219. lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
  2220. struct lpfc_io_buf *lpfc_cmd)
  2221. {
  2222. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2223. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2224. struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
  2225. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  2226. uint32_t num_bde = 0;
  2227. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  2228. int prot_group_type = 0;
  2229. int fcpdl;
  2230. int ret = 1;
  2231. struct lpfc_vport *vport = phba->pport;
  2232. /*
  2233. * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
  2234. * fcp_rsp regions to the first data bde entry
  2235. */
  2236. bpl += 2;
  2237. if (scsi_sg_count(scsi_cmnd)) {
  2238. /*
  2239. * The driver stores the segment count returned from dma_map_sg
  2240. * because this a count of dma-mappings used to map the use_sg
  2241. * pages. They are not guaranteed to be the same for those
  2242. * architectures that implement an IOMMU.
  2243. */
  2244. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  2245. scsi_sglist(scsi_cmnd),
  2246. scsi_sg_count(scsi_cmnd), datadir);
  2247. if (unlikely(!datasegcnt))
  2248. return 1;
  2249. lpfc_cmd->seg_cnt = datasegcnt;
  2250. /* First check if data segment count from SCSI Layer is good */
  2251. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  2252. WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
  2253. ret = 2;
  2254. goto err;
  2255. }
  2256. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  2257. switch (prot_group_type) {
  2258. case LPFC_PG_TYPE_NO_DIF:
  2259. /* Here we need to add a PDE5 and PDE6 to the count */
  2260. if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
  2261. ret = 2;
  2262. goto err;
  2263. }
  2264. num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
  2265. datasegcnt);
  2266. /* we should have 2 or more entries in buffer list */
  2267. if (num_bde < 2) {
  2268. ret = 2;
  2269. goto err;
  2270. }
  2271. break;
  2272. case LPFC_PG_TYPE_DIF_BUF:
  2273. /*
  2274. * This type indicates that protection buffers are
  2275. * passed to the driver, so that needs to be prepared
  2276. * for DMA
  2277. */
  2278. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  2279. scsi_prot_sglist(scsi_cmnd),
  2280. scsi_prot_sg_count(scsi_cmnd), datadir);
  2281. if (unlikely(!protsegcnt)) {
  2282. scsi_dma_unmap(scsi_cmnd);
  2283. return 1;
  2284. }
  2285. lpfc_cmd->prot_seg_cnt = protsegcnt;
  2286. /*
  2287. * There is a minimun of 4 BPLs used for every
  2288. * protection data segment.
  2289. */
  2290. if ((lpfc_cmd->prot_seg_cnt * 4) >
  2291. (phba->cfg_total_seg_cnt - 2)) {
  2292. ret = 2;
  2293. goto err;
  2294. }
  2295. num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
  2296. datasegcnt, protsegcnt);
  2297. /* we should have 3 or more entries in buffer list */
  2298. if ((num_bde < 3) ||
  2299. (num_bde > phba->cfg_total_seg_cnt)) {
  2300. ret = 2;
  2301. goto err;
  2302. }
  2303. break;
  2304. case LPFC_PG_TYPE_INVALID:
  2305. default:
  2306. scsi_dma_unmap(scsi_cmnd);
  2307. lpfc_cmd->seg_cnt = 0;
  2308. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  2309. "9022 Unexpected protection group %i\n",
  2310. prot_group_type);
  2311. return 2;
  2312. }
  2313. }
  2314. /*
  2315. * Finish initializing those IOCB fields that are dependent on the
  2316. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  2317. * reinitialized since all iocb memory resources are used many times
  2318. * for transmit, receive, and continuation bpl's.
  2319. */
  2320. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  2321. iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
  2322. iocb_cmd->ulpBdeCount = 1;
  2323. iocb_cmd->ulpLe = 1;
  2324. fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
  2325. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  2326. /*
  2327. * Due to difference in data length between DIF/non-DIF paths,
  2328. * we need to set word 4 of IOCB here
  2329. */
  2330. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  2331. /*
  2332. * For First burst, we may need to adjust the initial transfer
  2333. * length for DIF
  2334. */
  2335. if (iocb_cmd->un.fcpi.fcpi_XRdy &&
  2336. (fcpdl < vport->cfg_first_burst_size))
  2337. iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
  2338. return 0;
  2339. err:
  2340. if (lpfc_cmd->seg_cnt)
  2341. scsi_dma_unmap(scsi_cmnd);
  2342. if (lpfc_cmd->prot_seg_cnt)
  2343. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
  2344. scsi_prot_sg_count(scsi_cmnd),
  2345. scsi_cmnd->sc_data_direction);
  2346. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  2347. "9023 Cannot setup S/G List for HBA"
  2348. "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
  2349. lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
  2350. phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
  2351. prot_group_type, num_bde);
  2352. lpfc_cmd->seg_cnt = 0;
  2353. lpfc_cmd->prot_seg_cnt = 0;
  2354. return ret;
  2355. }
  2356. /*
  2357. * This function calcuates the T10 DIF guard tag
  2358. * on the specified data using a CRC algorithmn
  2359. * using crc_t10dif.
  2360. */
  2361. static uint16_t
  2362. lpfc_bg_crc(uint8_t *data, int count)
  2363. {
  2364. uint16_t crc = 0;
  2365. uint16_t x;
  2366. crc = crc_t10dif(data, count);
  2367. x = cpu_to_be16(crc);
  2368. return x;
  2369. }
  2370. /*
  2371. * This function calcuates the T10 DIF guard tag
  2372. * on the specified data using a CSUM algorithmn
  2373. * using ip_compute_csum.
  2374. */
  2375. static uint16_t
  2376. lpfc_bg_csum(uint8_t *data, int count)
  2377. {
  2378. uint16_t ret;
  2379. ret = ip_compute_csum(data, count);
  2380. return ret;
  2381. }
  2382. /*
  2383. * This function examines the protection data to try to determine
  2384. * what type of T10-DIF error occurred.
  2385. */
  2386. static void
  2387. lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
  2388. {
  2389. struct scatterlist *sgpe; /* s/g prot entry */
  2390. struct scatterlist *sgde; /* s/g data entry */
  2391. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  2392. struct scsi_dif_tuple *src = NULL;
  2393. uint8_t *data_src = NULL;
  2394. uint16_t guard_tag;
  2395. uint16_t start_app_tag, app_tag;
  2396. uint32_t start_ref_tag, ref_tag;
  2397. int prot, protsegcnt;
  2398. int err_type, len, data_len;
  2399. int chk_ref, chk_app, chk_guard;
  2400. uint16_t sum;
  2401. unsigned blksize;
  2402. err_type = BGS_GUARD_ERR_MASK;
  2403. sum = 0;
  2404. guard_tag = 0;
  2405. /* First check to see if there is protection data to examine */
  2406. prot = scsi_get_prot_op(cmd);
  2407. if ((prot == SCSI_PROT_READ_STRIP) ||
  2408. (prot == SCSI_PROT_WRITE_INSERT) ||
  2409. (prot == SCSI_PROT_NORMAL))
  2410. goto out;
  2411. /* Currently the driver just supports ref_tag and guard_tag checking */
  2412. chk_ref = 1;
  2413. chk_app = 0;
  2414. chk_guard = 0;
  2415. /* Setup a ptr to the protection data provided by the SCSI host */
  2416. sgpe = scsi_prot_sglist(cmd);
  2417. protsegcnt = lpfc_cmd->prot_seg_cnt;
  2418. if (sgpe && protsegcnt) {
  2419. /*
  2420. * We will only try to verify guard tag if the segment
  2421. * data length is a multiple of the blksize.
  2422. */
  2423. sgde = scsi_sglist(cmd);
  2424. blksize = scsi_prot_interval(cmd);
  2425. data_src = (uint8_t *)sg_virt(sgde);
  2426. data_len = sgde->length;
  2427. if ((data_len & (blksize - 1)) == 0)
  2428. chk_guard = 1;
  2429. src = (struct scsi_dif_tuple *)sg_virt(sgpe);
  2430. start_ref_tag = scsi_prot_ref_tag(cmd);
  2431. start_app_tag = src->app_tag;
  2432. len = sgpe->length;
  2433. while (src && protsegcnt) {
  2434. while (len) {
  2435. /*
  2436. * First check to see if a protection data
  2437. * check is valid
  2438. */
  2439. if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
  2440. (src->app_tag == T10_PI_APP_ESCAPE)) {
  2441. start_ref_tag++;
  2442. goto skipit;
  2443. }
  2444. /* First Guard Tag checking */
  2445. if (chk_guard) {
  2446. guard_tag = src->guard_tag;
  2447. if (cmd->prot_flags
  2448. & SCSI_PROT_IP_CHECKSUM)
  2449. sum = lpfc_bg_csum(data_src,
  2450. blksize);
  2451. else
  2452. sum = lpfc_bg_crc(data_src,
  2453. blksize);
  2454. if ((guard_tag != sum)) {
  2455. err_type = BGS_GUARD_ERR_MASK;
  2456. goto out;
  2457. }
  2458. }
  2459. /* Reference Tag checking */
  2460. ref_tag = be32_to_cpu(src->ref_tag);
  2461. if (chk_ref && (ref_tag != start_ref_tag)) {
  2462. err_type = BGS_REFTAG_ERR_MASK;
  2463. goto out;
  2464. }
  2465. start_ref_tag++;
  2466. /* App Tag checking */
  2467. app_tag = src->app_tag;
  2468. if (chk_app && (app_tag != start_app_tag)) {
  2469. err_type = BGS_APPTAG_ERR_MASK;
  2470. goto out;
  2471. }
  2472. skipit:
  2473. len -= sizeof(struct scsi_dif_tuple);
  2474. if (len < 0)
  2475. len = 0;
  2476. src++;
  2477. data_src += blksize;
  2478. data_len -= blksize;
  2479. /*
  2480. * Are we at the end of the Data segment?
  2481. * The data segment is only used for Guard
  2482. * tag checking.
  2483. */
  2484. if (chk_guard && (data_len == 0)) {
  2485. chk_guard = 0;
  2486. sgde = sg_next(sgde);
  2487. if (!sgde)
  2488. goto out;
  2489. data_src = (uint8_t *)sg_virt(sgde);
  2490. data_len = sgde->length;
  2491. if ((data_len & (blksize - 1)) == 0)
  2492. chk_guard = 1;
  2493. }
  2494. }
  2495. /* Goto the next Protection data segment */
  2496. sgpe = sg_next(sgpe);
  2497. if (sgpe) {
  2498. src = (struct scsi_dif_tuple *)sg_virt(sgpe);
  2499. len = sgpe->length;
  2500. } else {
  2501. src = NULL;
  2502. }
  2503. protsegcnt--;
  2504. }
  2505. }
  2506. out:
  2507. if (err_type == BGS_GUARD_ERR_MASK) {
  2508. scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
  2509. set_host_byte(cmd, DID_ABORT);
  2510. phba->bg_guard_err_cnt++;
  2511. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2512. "9069 BLKGRD: reftag %x grd_tag err %x != %x\n",
  2513. scsi_prot_ref_tag(cmd),
  2514. sum, guard_tag);
  2515. } else if (err_type == BGS_REFTAG_ERR_MASK) {
  2516. scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
  2517. set_host_byte(cmd, DID_ABORT);
  2518. phba->bg_reftag_err_cnt++;
  2519. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2520. "9066 BLKGRD: reftag %x ref_tag err %x != %x\n",
  2521. scsi_prot_ref_tag(cmd),
  2522. ref_tag, start_ref_tag);
  2523. } else if (err_type == BGS_APPTAG_ERR_MASK) {
  2524. scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
  2525. set_host_byte(cmd, DID_ABORT);
  2526. phba->bg_apptag_err_cnt++;
  2527. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2528. "9041 BLKGRD: reftag %x app_tag err %x != %x\n",
  2529. scsi_prot_ref_tag(cmd),
  2530. app_tag, start_app_tag);
  2531. }
  2532. }
  2533. /*
  2534. * This function checks for BlockGuard errors detected by
  2535. * the HBA. In case of errors, the ASC/ASCQ fields in the
  2536. * sense buffer will be set accordingly, paired with
  2537. * ILLEGAL_REQUEST to signal to the kernel that the HBA
  2538. * detected corruption.
  2539. *
  2540. * Returns:
  2541. * 0 - No error found
  2542. * 1 - BlockGuard error found
  2543. * -1 - Internal error (bad profile, ...etc)
  2544. */
  2545. static int
  2546. lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
  2547. struct lpfc_iocbq *pIocbOut)
  2548. {
  2549. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  2550. struct sli3_bg_fields *bgf;
  2551. int ret = 0;
  2552. struct lpfc_wcqe_complete *wcqe;
  2553. u32 status;
  2554. u32 bghm = 0;
  2555. u32 bgstat = 0;
  2556. u64 failing_sector = 0;
  2557. if (phba->sli_rev == LPFC_SLI_REV4) {
  2558. wcqe = &pIocbOut->wcqe_cmpl;
  2559. status = bf_get(lpfc_wcqe_c_status, wcqe);
  2560. if (status == CQE_STATUS_DI_ERROR) {
  2561. /* Guard Check failed */
  2562. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe))
  2563. bgstat |= BGS_GUARD_ERR_MASK;
  2564. /* AppTag Check failed */
  2565. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe))
  2566. bgstat |= BGS_APPTAG_ERR_MASK;
  2567. /* RefTag Check failed */
  2568. if (bf_get(lpfc_wcqe_c_bg_re, wcqe))
  2569. bgstat |= BGS_REFTAG_ERR_MASK;
  2570. /* Check to see if there was any good data before the
  2571. * error
  2572. */
  2573. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  2574. bgstat |= BGS_HI_WATER_MARK_PRESENT_MASK;
  2575. bghm = wcqe->total_data_placed;
  2576. }
  2577. /*
  2578. * Set ALL the error bits to indicate we don't know what
  2579. * type of error it is.
  2580. */
  2581. if (!bgstat)
  2582. bgstat |= (BGS_REFTAG_ERR_MASK |
  2583. BGS_APPTAG_ERR_MASK |
  2584. BGS_GUARD_ERR_MASK);
  2585. }
  2586. } else {
  2587. bgf = &pIocbOut->iocb.unsli3.sli3_bg;
  2588. bghm = bgf->bghm;
  2589. bgstat = bgf->bgstat;
  2590. }
  2591. if (lpfc_bgs_get_invalid_prof(bgstat)) {
  2592. cmd->result = DID_ERROR << 16;
  2593. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2594. "9072 BLKGRD: Invalid BG Profile in cmd "
  2595. "0x%x reftag 0x%x blk cnt 0x%x "
  2596. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2597. scsi_prot_ref_tag(cmd),
  2598. scsi_logical_block_count(cmd), bgstat, bghm);
  2599. ret = (-1);
  2600. goto out;
  2601. }
  2602. if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
  2603. cmd->result = DID_ERROR << 16;
  2604. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2605. "9073 BLKGRD: Invalid BG PDIF Block in cmd "
  2606. "0x%x reftag 0x%x blk cnt 0x%x "
  2607. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2608. scsi_prot_ref_tag(cmd),
  2609. scsi_logical_block_count(cmd), bgstat, bghm);
  2610. ret = (-1);
  2611. goto out;
  2612. }
  2613. if (lpfc_bgs_get_guard_err(bgstat)) {
  2614. ret = 1;
  2615. scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
  2616. set_host_byte(cmd, DID_ABORT);
  2617. phba->bg_guard_err_cnt++;
  2618. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2619. "9055 BLKGRD: Guard Tag error in cmd "
  2620. "0x%x reftag 0x%x blk cnt 0x%x "
  2621. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2622. scsi_prot_ref_tag(cmd),
  2623. scsi_logical_block_count(cmd), bgstat, bghm);
  2624. }
  2625. if (lpfc_bgs_get_reftag_err(bgstat)) {
  2626. ret = 1;
  2627. scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
  2628. set_host_byte(cmd, DID_ABORT);
  2629. phba->bg_reftag_err_cnt++;
  2630. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2631. "9056 BLKGRD: Ref Tag error in cmd "
  2632. "0x%x reftag 0x%x blk cnt 0x%x "
  2633. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2634. scsi_prot_ref_tag(cmd),
  2635. scsi_logical_block_count(cmd), bgstat, bghm);
  2636. }
  2637. if (lpfc_bgs_get_apptag_err(bgstat)) {
  2638. ret = 1;
  2639. scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
  2640. set_host_byte(cmd, DID_ABORT);
  2641. phba->bg_apptag_err_cnt++;
  2642. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2643. "9061 BLKGRD: App Tag error in cmd "
  2644. "0x%x reftag 0x%x blk cnt 0x%x "
  2645. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2646. scsi_prot_ref_tag(cmd),
  2647. scsi_logical_block_count(cmd), bgstat, bghm);
  2648. }
  2649. if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
  2650. /*
  2651. * setup sense data descriptor 0 per SPC-4 as an information
  2652. * field, and put the failing LBA in it.
  2653. * This code assumes there was also a guard/app/ref tag error
  2654. * indication.
  2655. */
  2656. cmd->sense_buffer[7] = 0xc; /* Additional sense length */
  2657. cmd->sense_buffer[8] = 0; /* Information descriptor type */
  2658. cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
  2659. cmd->sense_buffer[10] = 0x80; /* Validity bit */
  2660. /* bghm is a "on the wire" FC frame based count */
  2661. switch (scsi_get_prot_op(cmd)) {
  2662. case SCSI_PROT_READ_INSERT:
  2663. case SCSI_PROT_WRITE_STRIP:
  2664. bghm /= cmd->device->sector_size;
  2665. break;
  2666. case SCSI_PROT_READ_STRIP:
  2667. case SCSI_PROT_WRITE_INSERT:
  2668. case SCSI_PROT_READ_PASS:
  2669. case SCSI_PROT_WRITE_PASS:
  2670. bghm /= (cmd->device->sector_size +
  2671. sizeof(struct scsi_dif_tuple));
  2672. break;
  2673. }
  2674. failing_sector = scsi_get_lba(cmd);
  2675. failing_sector += bghm;
  2676. /* Descriptor Information */
  2677. put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
  2678. }
  2679. if (!ret) {
  2680. /* No error was reported - problem in FW? */
  2681. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2682. "9057 BLKGRD: Unknown error in cmd "
  2683. "0x%x reftag 0x%x blk cnt 0x%x "
  2684. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2685. scsi_prot_ref_tag(cmd),
  2686. scsi_logical_block_count(cmd), bgstat, bghm);
  2687. /* Calculate what type of error it was */
  2688. lpfc_calc_bg_err(phba, lpfc_cmd);
  2689. }
  2690. out:
  2691. return ret;
  2692. }
  2693. /**
  2694. * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
  2695. * @phba: The Hba for which this call is being executed.
  2696. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  2697. *
  2698. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  2699. * field of @lpfc_cmd for device with SLI-4 interface spec.
  2700. *
  2701. * Return codes:
  2702. * 2 - Error - Do not retry
  2703. * 1 - Error - Retry
  2704. * 0 - Success
  2705. **/
  2706. static int
  2707. lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
  2708. {
  2709. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2710. struct scatterlist *sgel = NULL;
  2711. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2712. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
  2713. struct sli4_sge *first_data_sgl;
  2714. struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
  2715. struct lpfc_vport *vport = phba->pport;
  2716. union lpfc_wqe128 *wqe = &pwqeq->wqe;
  2717. dma_addr_t physaddr;
  2718. uint32_t dma_len;
  2719. uint32_t dma_offset = 0;
  2720. int nseg, i, j;
  2721. struct ulp_bde64 *bde;
  2722. bool lsp_just_set = false;
  2723. struct sli4_hybrid_sgl *sgl_xtra = NULL;
  2724. /*
  2725. * There are three possibilities here - use scatter-gather segment, use
  2726. * the single mapping, or neither. Start the lpfc command prep by
  2727. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  2728. * data bde entry.
  2729. */
  2730. if (scsi_sg_count(scsi_cmnd)) {
  2731. /*
  2732. * The driver stores the segment count returned from dma_map_sg
  2733. * because this a count of dma-mappings used to map the use_sg
  2734. * pages. They are not guaranteed to be the same for those
  2735. * architectures that implement an IOMMU.
  2736. */
  2737. nseg = scsi_dma_map(scsi_cmnd);
  2738. if (unlikely(nseg <= 0))
  2739. return 1;
  2740. sgl += 1;
  2741. /* clear the last flag in the fcp_rsp map entry */
  2742. sgl->word2 = le32_to_cpu(sgl->word2);
  2743. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2744. sgl->word2 = cpu_to_le32(sgl->word2);
  2745. sgl += 1;
  2746. first_data_sgl = sgl;
  2747. lpfc_cmd->seg_cnt = nseg;
  2748. if (!phba->cfg_xpsgl &&
  2749. lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  2750. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  2751. "9074 BLKGRD:"
  2752. " %s: Too many sg segments from "
  2753. "dma_map_sg. Config %d, seg_cnt %d\n",
  2754. __func__, phba->cfg_sg_seg_cnt,
  2755. lpfc_cmd->seg_cnt);
  2756. WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
  2757. lpfc_cmd->seg_cnt = 0;
  2758. scsi_dma_unmap(scsi_cmnd);
  2759. return 2;
  2760. }
  2761. /*
  2762. * The driver established a maximum scatter-gather segment count
  2763. * during probe that limits the number of sg elements in any
  2764. * single scsi command. Just run through the seg_cnt and format
  2765. * the sge's.
  2766. * When using SLI-3 the driver will try to fit all the BDEs into
  2767. * the IOCB. If it can't then the BDEs get added to a BPL as it
  2768. * does for SLI-2 mode.
  2769. */
  2770. /* for tracking segment boundaries */
  2771. sgel = scsi_sglist(scsi_cmnd);
  2772. j = 2;
  2773. for (i = 0; i < nseg; i++) {
  2774. sgl->word2 = 0;
  2775. if (nseg == 1) {
  2776. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2777. bf_set(lpfc_sli4_sge_type, sgl,
  2778. LPFC_SGE_TYPE_DATA);
  2779. } else {
  2780. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2781. /* do we need to expand the segment */
  2782. if (!lsp_just_set &&
  2783. !((j + 1) % phba->border_sge_num) &&
  2784. ((nseg - 1) != i)) {
  2785. /* set LSP type */
  2786. bf_set(lpfc_sli4_sge_type, sgl,
  2787. LPFC_SGE_TYPE_LSP);
  2788. sgl_xtra = lpfc_get_sgl_per_hdwq(
  2789. phba, lpfc_cmd);
  2790. if (unlikely(!sgl_xtra)) {
  2791. lpfc_cmd->seg_cnt = 0;
  2792. scsi_dma_unmap(scsi_cmnd);
  2793. return 1;
  2794. }
  2795. sgl->addr_lo = cpu_to_le32(putPaddrLow(
  2796. sgl_xtra->dma_phys_sgl));
  2797. sgl->addr_hi = cpu_to_le32(putPaddrHigh(
  2798. sgl_xtra->dma_phys_sgl));
  2799. } else {
  2800. bf_set(lpfc_sli4_sge_type, sgl,
  2801. LPFC_SGE_TYPE_DATA);
  2802. }
  2803. }
  2804. if (!(bf_get(lpfc_sli4_sge_type, sgl) &
  2805. LPFC_SGE_TYPE_LSP)) {
  2806. if ((nseg - 1) == i)
  2807. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2808. physaddr = sg_dma_address(sgel);
  2809. dma_len = sg_dma_len(sgel);
  2810. sgl->addr_lo = cpu_to_le32(putPaddrLow(
  2811. physaddr));
  2812. sgl->addr_hi = cpu_to_le32(putPaddrHigh(
  2813. physaddr));
  2814. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  2815. sgl->word2 = cpu_to_le32(sgl->word2);
  2816. sgl->sge_len = cpu_to_le32(dma_len);
  2817. dma_offset += dma_len;
  2818. sgel = sg_next(sgel);
  2819. sgl++;
  2820. lsp_just_set = false;
  2821. } else {
  2822. sgl->word2 = cpu_to_le32(sgl->word2);
  2823. sgl->sge_len = cpu_to_le32(
  2824. phba->cfg_sg_dma_buf_size);
  2825. sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
  2826. i = i - 1;
  2827. lsp_just_set = true;
  2828. }
  2829. j++;
  2830. }
  2831. /* PBDE support for first data SGE only.
  2832. * For FCoE, we key off Performance Hints.
  2833. * For FC, we key off lpfc_enable_pbde.
  2834. */
  2835. if (nseg == 1 &&
  2836. ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
  2837. phba->cfg_enable_pbde)) {
  2838. /* Words 13-15 */
  2839. bde = (struct ulp_bde64 *)
  2840. &wqe->words[13];
  2841. bde->addrLow = first_data_sgl->addr_lo;
  2842. bde->addrHigh = first_data_sgl->addr_hi;
  2843. bde->tus.f.bdeSize =
  2844. le32_to_cpu(first_data_sgl->sge_len);
  2845. bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  2846. bde->tus.w = cpu_to_le32(bde->tus.w);
  2847. /* Word 11 - set PBDE bit */
  2848. bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
  2849. } else {
  2850. memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
  2851. /* Word 11 - PBDE bit disabled by default template */
  2852. }
  2853. } else {
  2854. sgl += 1;
  2855. /* set the last flag in the fcp_rsp map entry */
  2856. sgl->word2 = le32_to_cpu(sgl->word2);
  2857. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2858. sgl->word2 = cpu_to_le32(sgl->word2);
  2859. if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
  2860. phba->cfg_enable_pbde) {
  2861. bde = (struct ulp_bde64 *)
  2862. &wqe->words[13];
  2863. memset(bde, 0, (sizeof(uint32_t) * 3));
  2864. }
  2865. }
  2866. /*
  2867. * Finish initializing those IOCB fields that are dependent on the
  2868. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  2869. * explicitly reinitialized.
  2870. * all iocb memory resources are reused.
  2871. */
  2872. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  2873. /* Set first-burst provided it was successfully negotiated */
  2874. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  2875. vport->cfg_first_burst_size &&
  2876. scsi_cmnd->sc_data_direction == DMA_TO_DEVICE) {
  2877. u32 init_len, total_len;
  2878. total_len = be32_to_cpu(fcp_cmnd->fcpDl);
  2879. init_len = min(total_len, vport->cfg_first_burst_size);
  2880. /* Word 4 & 5 */
  2881. wqe->fcp_iwrite.initial_xfer_len = init_len;
  2882. wqe->fcp_iwrite.total_xfer_len = total_len;
  2883. } else {
  2884. /* Word 4 */
  2885. wqe->fcp_iwrite.total_xfer_len =
  2886. be32_to_cpu(fcp_cmnd->fcpDl);
  2887. }
  2888. /*
  2889. * If the OAS driver feature is enabled and the lun is enabled for
  2890. * OAS, set the oas iocb related flags.
  2891. */
  2892. if ((phba->cfg_fof) && ((struct lpfc_device_data *)
  2893. scsi_cmnd->device->hostdata)->oas_enabled) {
  2894. lpfc_cmd->cur_iocbq.cmd_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
  2895. lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
  2896. scsi_cmnd->device->hostdata)->priority;
  2897. /* Word 10 */
  2898. bf_set(wqe_oas, &wqe->generic.wqe_com, 1);
  2899. bf_set(wqe_ccpe, &wqe->generic.wqe_com, 1);
  2900. if (lpfc_cmd->cur_iocbq.priority)
  2901. bf_set(wqe_ccp, &wqe->generic.wqe_com,
  2902. (lpfc_cmd->cur_iocbq.priority << 1));
  2903. else
  2904. bf_set(wqe_ccp, &wqe->generic.wqe_com,
  2905. (phba->cfg_XLanePriority << 1));
  2906. }
  2907. return 0;
  2908. }
  2909. /**
  2910. * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
  2911. * @phba: The Hba for which this call is being executed.
  2912. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  2913. *
  2914. * This is the protection/DIF aware version of
  2915. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  2916. * two functions eventually, but for now, it's here
  2917. * Return codes:
  2918. * 2 - Error - Do not retry
  2919. * 1 - Error - Retry
  2920. * 0 - Success
  2921. **/
  2922. static int
  2923. lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
  2924. struct lpfc_io_buf *lpfc_cmd)
  2925. {
  2926. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2927. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2928. struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
  2929. struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
  2930. union lpfc_wqe128 *wqe = &pwqeq->wqe;
  2931. uint32_t num_sge = 0;
  2932. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  2933. int prot_group_type = 0;
  2934. int fcpdl;
  2935. int ret = 1;
  2936. struct lpfc_vport *vport = phba->pport;
  2937. /*
  2938. * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
  2939. * fcp_rsp regions to the first data sge entry
  2940. */
  2941. if (scsi_sg_count(scsi_cmnd)) {
  2942. /*
  2943. * The driver stores the segment count returned from dma_map_sg
  2944. * because this a count of dma-mappings used to map the use_sg
  2945. * pages. They are not guaranteed to be the same for those
  2946. * architectures that implement an IOMMU.
  2947. */
  2948. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  2949. scsi_sglist(scsi_cmnd),
  2950. scsi_sg_count(scsi_cmnd), datadir);
  2951. if (unlikely(!datasegcnt))
  2952. return 1;
  2953. sgl += 1;
  2954. /* clear the last flag in the fcp_rsp map entry */
  2955. sgl->word2 = le32_to_cpu(sgl->word2);
  2956. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2957. sgl->word2 = cpu_to_le32(sgl->word2);
  2958. sgl += 1;
  2959. lpfc_cmd->seg_cnt = datasegcnt;
  2960. /* First check if data segment count from SCSI Layer is good */
  2961. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
  2962. !phba->cfg_xpsgl) {
  2963. WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
  2964. ret = 2;
  2965. goto err;
  2966. }
  2967. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  2968. switch (prot_group_type) {
  2969. case LPFC_PG_TYPE_NO_DIF:
  2970. /* Here we need to add a DISEED to the count */
  2971. if (((lpfc_cmd->seg_cnt + 1) >
  2972. phba->cfg_total_seg_cnt) &&
  2973. !phba->cfg_xpsgl) {
  2974. ret = 2;
  2975. goto err;
  2976. }
  2977. num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
  2978. datasegcnt, lpfc_cmd);
  2979. /* we should have 2 or more entries in buffer list */
  2980. if (num_sge < 2) {
  2981. ret = 2;
  2982. goto err;
  2983. }
  2984. break;
  2985. case LPFC_PG_TYPE_DIF_BUF:
  2986. /*
  2987. * This type indicates that protection buffers are
  2988. * passed to the driver, so that needs to be prepared
  2989. * for DMA
  2990. */
  2991. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  2992. scsi_prot_sglist(scsi_cmnd),
  2993. scsi_prot_sg_count(scsi_cmnd), datadir);
  2994. if (unlikely(!protsegcnt)) {
  2995. scsi_dma_unmap(scsi_cmnd);
  2996. return 1;
  2997. }
  2998. lpfc_cmd->prot_seg_cnt = protsegcnt;
  2999. /*
  3000. * There is a minimun of 3 SGEs used for every
  3001. * protection data segment.
  3002. */
  3003. if (((lpfc_cmd->prot_seg_cnt * 3) >
  3004. (phba->cfg_total_seg_cnt - 2)) &&
  3005. !phba->cfg_xpsgl) {
  3006. ret = 2;
  3007. goto err;
  3008. }
  3009. num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
  3010. datasegcnt, protsegcnt, lpfc_cmd);
  3011. /* we should have 3 or more entries in buffer list */
  3012. if (num_sge < 3 ||
  3013. (num_sge > phba->cfg_total_seg_cnt &&
  3014. !phba->cfg_xpsgl)) {
  3015. ret = 2;
  3016. goto err;
  3017. }
  3018. break;
  3019. case LPFC_PG_TYPE_INVALID:
  3020. default:
  3021. scsi_dma_unmap(scsi_cmnd);
  3022. lpfc_cmd->seg_cnt = 0;
  3023. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  3024. "9083 Unexpected protection group %i\n",
  3025. prot_group_type);
  3026. return 2;
  3027. }
  3028. }
  3029. switch (scsi_get_prot_op(scsi_cmnd)) {
  3030. case SCSI_PROT_WRITE_STRIP:
  3031. case SCSI_PROT_READ_STRIP:
  3032. lpfc_cmd->cur_iocbq.cmd_flag |= LPFC_IO_DIF_STRIP;
  3033. break;
  3034. case SCSI_PROT_WRITE_INSERT:
  3035. case SCSI_PROT_READ_INSERT:
  3036. lpfc_cmd->cur_iocbq.cmd_flag |= LPFC_IO_DIF_INSERT;
  3037. break;
  3038. case SCSI_PROT_WRITE_PASS:
  3039. case SCSI_PROT_READ_PASS:
  3040. lpfc_cmd->cur_iocbq.cmd_flag |= LPFC_IO_DIF_PASS;
  3041. break;
  3042. }
  3043. fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
  3044. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  3045. /* Set first-burst provided it was successfully negotiated */
  3046. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  3047. vport->cfg_first_burst_size &&
  3048. scsi_cmnd->sc_data_direction == DMA_TO_DEVICE) {
  3049. u32 init_len, total_len;
  3050. total_len = be32_to_cpu(fcp_cmnd->fcpDl);
  3051. init_len = min(total_len, vport->cfg_first_burst_size);
  3052. /* Word 4 & 5 */
  3053. wqe->fcp_iwrite.initial_xfer_len = init_len;
  3054. wqe->fcp_iwrite.total_xfer_len = total_len;
  3055. } else {
  3056. /* Word 4 */
  3057. wqe->fcp_iwrite.total_xfer_len =
  3058. be32_to_cpu(fcp_cmnd->fcpDl);
  3059. }
  3060. /*
  3061. * If the OAS driver feature is enabled and the lun is enabled for
  3062. * OAS, set the oas iocb related flags.
  3063. */
  3064. if ((phba->cfg_fof) && ((struct lpfc_device_data *)
  3065. scsi_cmnd->device->hostdata)->oas_enabled) {
  3066. lpfc_cmd->cur_iocbq.cmd_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
  3067. /* Word 10 */
  3068. bf_set(wqe_oas, &wqe->generic.wqe_com, 1);
  3069. bf_set(wqe_ccpe, &wqe->generic.wqe_com, 1);
  3070. bf_set(wqe_ccp, &wqe->generic.wqe_com,
  3071. (phba->cfg_XLanePriority << 1));
  3072. }
  3073. /* Word 7. DIF Flags */
  3074. if (lpfc_cmd->cur_iocbq.cmd_flag & LPFC_IO_DIF_PASS)
  3075. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
  3076. else if (lpfc_cmd->cur_iocbq.cmd_flag & LPFC_IO_DIF_STRIP)
  3077. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
  3078. else if (lpfc_cmd->cur_iocbq.cmd_flag & LPFC_IO_DIF_INSERT)
  3079. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
  3080. lpfc_cmd->cur_iocbq.cmd_flag &= ~(LPFC_IO_DIF_PASS |
  3081. LPFC_IO_DIF_STRIP | LPFC_IO_DIF_INSERT);
  3082. return 0;
  3083. err:
  3084. if (lpfc_cmd->seg_cnt)
  3085. scsi_dma_unmap(scsi_cmnd);
  3086. if (lpfc_cmd->prot_seg_cnt)
  3087. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
  3088. scsi_prot_sg_count(scsi_cmnd),
  3089. scsi_cmnd->sc_data_direction);
  3090. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  3091. "9084 Cannot setup S/G List for HBA "
  3092. "IO segs %d/%d SGL %d SCSI %d: %d %d %d\n",
  3093. lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
  3094. phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
  3095. prot_group_type, num_sge, ret);
  3096. lpfc_cmd->seg_cnt = 0;
  3097. lpfc_cmd->prot_seg_cnt = 0;
  3098. return ret;
  3099. }
  3100. /**
  3101. * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
  3102. * @phba: The Hba for which this call is being executed.
  3103. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  3104. *
  3105. * This routine wraps the actual DMA mapping function pointer from the
  3106. * lpfc_hba struct.
  3107. *
  3108. * Return codes:
  3109. * 1 - Error
  3110. * 0 - Success
  3111. **/
  3112. static inline int
  3113. lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
  3114. {
  3115. return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  3116. }
  3117. /**
  3118. * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
  3119. * using BlockGuard.
  3120. * @phba: The Hba for which this call is being executed.
  3121. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  3122. *
  3123. * This routine wraps the actual DMA mapping function pointer from the
  3124. * lpfc_hba struct.
  3125. *
  3126. * Return codes:
  3127. * 1 - Error
  3128. * 0 - Success
  3129. **/
  3130. static inline int
  3131. lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
  3132. {
  3133. return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  3134. }
  3135. /**
  3136. * lpfc_scsi_prep_cmnd_buf - Wrapper function for IOCB/WQE mapping of scsi
  3137. * buffer
  3138. * @vport: Pointer to vport object.
  3139. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  3140. * @tmo: Timeout value for IO
  3141. *
  3142. * This routine initializes IOCB/WQE data structure from scsi command
  3143. *
  3144. * Return codes:
  3145. * 1 - Error
  3146. * 0 - Success
  3147. **/
  3148. static inline int
  3149. lpfc_scsi_prep_cmnd_buf(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
  3150. uint8_t tmo)
  3151. {
  3152. return vport->phba->lpfc_scsi_prep_cmnd_buf(vport, lpfc_cmd, tmo);
  3153. }
  3154. /**
  3155. * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
  3156. * @phba: Pointer to hba context object.
  3157. * @vport: Pointer to vport object.
  3158. * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
  3159. * @fcpi_parm: FCP Initiator parameter.
  3160. *
  3161. * This function posts an event when there is a SCSI command reporting
  3162. * error from the scsi device.
  3163. **/
  3164. static void
  3165. lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
  3166. struct lpfc_io_buf *lpfc_cmd, uint32_t fcpi_parm) {
  3167. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  3168. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  3169. uint32_t resp_info = fcprsp->rspStatus2;
  3170. uint32_t scsi_status = fcprsp->rspStatus3;
  3171. struct lpfc_fast_path_event *fast_path_evt = NULL;
  3172. struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
  3173. unsigned long flags;
  3174. if (!pnode)
  3175. return;
  3176. /* If there is queuefull or busy condition send a scsi event */
  3177. if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
  3178. (cmnd->result == SAM_STAT_BUSY)) {
  3179. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3180. if (!fast_path_evt)
  3181. return;
  3182. fast_path_evt->un.scsi_evt.event_type =
  3183. FC_REG_SCSI_EVENT;
  3184. fast_path_evt->un.scsi_evt.subcategory =
  3185. (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
  3186. LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
  3187. fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
  3188. memcpy(&fast_path_evt->un.scsi_evt.wwpn,
  3189. &pnode->nlp_portname, sizeof(struct lpfc_name));
  3190. memcpy(&fast_path_evt->un.scsi_evt.wwnn,
  3191. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3192. } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
  3193. ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
  3194. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3195. if (!fast_path_evt)
  3196. return;
  3197. fast_path_evt->un.check_cond_evt.scsi_event.event_type =
  3198. FC_REG_SCSI_EVENT;
  3199. fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
  3200. LPFC_EVENT_CHECK_COND;
  3201. fast_path_evt->un.check_cond_evt.scsi_event.lun =
  3202. cmnd->device->lun;
  3203. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
  3204. &pnode->nlp_portname, sizeof(struct lpfc_name));
  3205. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
  3206. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3207. fast_path_evt->un.check_cond_evt.sense_key =
  3208. cmnd->sense_buffer[2] & 0xf;
  3209. fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
  3210. fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
  3211. } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  3212. fcpi_parm &&
  3213. ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
  3214. ((scsi_status == SAM_STAT_GOOD) &&
  3215. !(resp_info & (RESID_UNDER | RESID_OVER))))) {
  3216. /*
  3217. * If status is good or resid does not match with fcp_param and
  3218. * there is valid fcpi_parm, then there is a read_check error
  3219. */
  3220. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3221. if (!fast_path_evt)
  3222. return;
  3223. fast_path_evt->un.read_check_error.header.event_type =
  3224. FC_REG_FABRIC_EVENT;
  3225. fast_path_evt->un.read_check_error.header.subcategory =
  3226. LPFC_EVENT_FCPRDCHKERR;
  3227. memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
  3228. &pnode->nlp_portname, sizeof(struct lpfc_name));
  3229. memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
  3230. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3231. fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
  3232. fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
  3233. fast_path_evt->un.read_check_error.fcpiparam =
  3234. fcpi_parm;
  3235. } else
  3236. return;
  3237. fast_path_evt->vport = vport;
  3238. spin_lock_irqsave(&phba->hbalock, flags);
  3239. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  3240. spin_unlock_irqrestore(&phba->hbalock, flags);
  3241. lpfc_worker_wake_up(phba);
  3242. return;
  3243. }
  3244. /**
  3245. * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
  3246. * @phba: The HBA for which this call is being executed.
  3247. * @psb: The scsi buffer which is going to be un-mapped.
  3248. *
  3249. * This routine does DMA un-mapping of scatter gather list of scsi command
  3250. * field of @lpfc_cmd for device with SLI-3 interface spec.
  3251. **/
  3252. static void
  3253. lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
  3254. {
  3255. /*
  3256. * There are only two special cases to consider. (1) the scsi command
  3257. * requested scatter-gather usage or (2) the scsi command allocated
  3258. * a request buffer, but did not request use_sg. There is a third
  3259. * case, but it does not require resource deallocation.
  3260. */
  3261. if (psb->seg_cnt > 0)
  3262. scsi_dma_unmap(psb->pCmd);
  3263. if (psb->prot_seg_cnt > 0)
  3264. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
  3265. scsi_prot_sg_count(psb->pCmd),
  3266. psb->pCmd->sc_data_direction);
  3267. }
  3268. /**
  3269. * lpfc_unblock_requests - allow further commands to be queued.
  3270. * @phba: pointer to phba object
  3271. *
  3272. * For single vport, just call scsi_unblock_requests on physical port.
  3273. * For multiple vports, send scsi_unblock_requests for all the vports.
  3274. */
  3275. void
  3276. lpfc_unblock_requests(struct lpfc_hba *phba)
  3277. {
  3278. struct lpfc_vport **vports;
  3279. struct Scsi_Host *shost;
  3280. int i;
  3281. if (phba->sli_rev == LPFC_SLI_REV4 &&
  3282. !phba->sli4_hba.max_cfg_param.vpi_used) {
  3283. shost = lpfc_shost_from_vport(phba->pport);
  3284. scsi_unblock_requests(shost);
  3285. return;
  3286. }
  3287. vports = lpfc_create_vport_work_array(phba);
  3288. if (vports != NULL)
  3289. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3290. shost = lpfc_shost_from_vport(vports[i]);
  3291. scsi_unblock_requests(shost);
  3292. }
  3293. lpfc_destroy_vport_work_array(phba, vports);
  3294. }
  3295. /**
  3296. * lpfc_block_requests - prevent further commands from being queued.
  3297. * @phba: pointer to phba object
  3298. *
  3299. * For single vport, just call scsi_block_requests on physical port.
  3300. * For multiple vports, send scsi_block_requests for all the vports.
  3301. */
  3302. void
  3303. lpfc_block_requests(struct lpfc_hba *phba)
  3304. {
  3305. struct lpfc_vport **vports;
  3306. struct Scsi_Host *shost;
  3307. int i;
  3308. if (atomic_read(&phba->cmf_stop_io))
  3309. return;
  3310. if (phba->sli_rev == LPFC_SLI_REV4 &&
  3311. !phba->sli4_hba.max_cfg_param.vpi_used) {
  3312. shost = lpfc_shost_from_vport(phba->pport);
  3313. scsi_block_requests(shost);
  3314. return;
  3315. }
  3316. vports = lpfc_create_vport_work_array(phba);
  3317. if (vports != NULL)
  3318. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3319. shost = lpfc_shost_from_vport(vports[i]);
  3320. scsi_block_requests(shost);
  3321. }
  3322. lpfc_destroy_vport_work_array(phba, vports);
  3323. }
  3324. /**
  3325. * lpfc_update_cmf_cmpl - Adjust CMF counters for IO completion
  3326. * @phba: The HBA for which this call is being executed.
  3327. * @time: The latency of the IO that completed (in ns)
  3328. * @size: The size of the IO that completed
  3329. * @shost: SCSI host the IO completed on (NULL for a NVME IO)
  3330. *
  3331. * The routine adjusts the various Burst and Bandwidth counters used in
  3332. * Congestion management and E2E. If time is set to LPFC_CGN_NOT_SENT,
  3333. * that means the IO was never issued to the HBA, so this routine is
  3334. * just being called to cleanup the counter from a previous
  3335. * lpfc_update_cmf_cmd call.
  3336. */
  3337. int
  3338. lpfc_update_cmf_cmpl(struct lpfc_hba *phba,
  3339. uint64_t time, uint32_t size, struct Scsi_Host *shost)
  3340. {
  3341. struct lpfc_cgn_stat *cgs;
  3342. if (time != LPFC_CGN_NOT_SENT) {
  3343. /* lat is ns coming in, save latency in us */
  3344. if (time < 1000)
  3345. time = 1;
  3346. else
  3347. time = div_u64(time + 500, 1000); /* round it */
  3348. cgs = per_cpu_ptr(phba->cmf_stat, raw_smp_processor_id());
  3349. atomic64_add(size, &cgs->rcv_bytes);
  3350. atomic64_add(time, &cgs->rx_latency);
  3351. atomic_inc(&cgs->rx_io_cnt);
  3352. }
  3353. return 0;
  3354. }
  3355. /**
  3356. * lpfc_update_cmf_cmd - Adjust CMF counters for IO submission
  3357. * @phba: The HBA for which this call is being executed.
  3358. * @size: The size of the IO that will be issued
  3359. *
  3360. * The routine adjusts the various Burst and Bandwidth counters used in
  3361. * Congestion management and E2E.
  3362. */
  3363. int
  3364. lpfc_update_cmf_cmd(struct lpfc_hba *phba, uint32_t size)
  3365. {
  3366. uint64_t total;
  3367. struct lpfc_cgn_stat *cgs;
  3368. int cpu;
  3369. /* At this point we are either LPFC_CFG_MANAGED or LPFC_CFG_MONITOR */
  3370. if (phba->cmf_active_mode == LPFC_CFG_MANAGED &&
  3371. phba->cmf_max_bytes_per_interval) {
  3372. total = 0;
  3373. for_each_present_cpu(cpu) {
  3374. cgs = per_cpu_ptr(phba->cmf_stat, cpu);
  3375. total += atomic64_read(&cgs->total_bytes);
  3376. }
  3377. if (total >= phba->cmf_max_bytes_per_interval) {
  3378. if (!atomic_xchg(&phba->cmf_bw_wait, 1)) {
  3379. lpfc_block_requests(phba);
  3380. phba->cmf_last_ts =
  3381. lpfc_calc_cmf_latency(phba);
  3382. }
  3383. atomic_inc(&phba->cmf_busy);
  3384. return -EBUSY;
  3385. }
  3386. if (size > atomic_read(&phba->rx_max_read_cnt))
  3387. atomic_set(&phba->rx_max_read_cnt, size);
  3388. }
  3389. cgs = per_cpu_ptr(phba->cmf_stat, raw_smp_processor_id());
  3390. atomic64_add(size, &cgs->total_bytes);
  3391. return 0;
  3392. }
  3393. /**
  3394. * lpfc_handle_fcp_err - FCP response handler
  3395. * @vport: The virtual port for which this call is being executed.
  3396. * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
  3397. * @fcpi_parm: FCP Initiator parameter.
  3398. *
  3399. * This routine is called to process response IOCB with status field
  3400. * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
  3401. * based upon SCSI and FCP error.
  3402. **/
  3403. static void
  3404. lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
  3405. uint32_t fcpi_parm)
  3406. {
  3407. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  3408. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  3409. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  3410. uint32_t resp_info = fcprsp->rspStatus2;
  3411. uint32_t scsi_status = fcprsp->rspStatus3;
  3412. uint32_t *lp;
  3413. uint32_t host_status = DID_OK;
  3414. uint32_t rsplen = 0;
  3415. uint32_t fcpDl;
  3416. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  3417. /*
  3418. * If this is a task management command, there is no
  3419. * scsi packet associated with this lpfc_cmd. The driver
  3420. * consumes it.
  3421. */
  3422. if (fcpcmd->fcpCntl2) {
  3423. scsi_status = 0;
  3424. goto out;
  3425. }
  3426. if (resp_info & RSP_LEN_VALID) {
  3427. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  3428. if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
  3429. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  3430. "2719 Invalid response length: "
  3431. "tgt x%x lun x%llx cmnd x%x rsplen "
  3432. "x%x\n", cmnd->device->id,
  3433. cmnd->device->lun, cmnd->cmnd[0],
  3434. rsplen);
  3435. host_status = DID_ERROR;
  3436. goto out;
  3437. }
  3438. if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
  3439. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  3440. "2757 Protocol failure detected during "
  3441. "processing of FCP I/O op: "
  3442. "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
  3443. cmnd->device->id,
  3444. cmnd->device->lun, cmnd->cmnd[0],
  3445. fcprsp->rspInfo3);
  3446. host_status = DID_ERROR;
  3447. goto out;
  3448. }
  3449. }
  3450. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  3451. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  3452. if (snslen > SCSI_SENSE_BUFFERSIZE)
  3453. snslen = SCSI_SENSE_BUFFERSIZE;
  3454. if (resp_info & RSP_LEN_VALID)
  3455. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  3456. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  3457. }
  3458. lp = (uint32_t *)cmnd->sense_buffer;
  3459. /* special handling for under run conditions */
  3460. if (!scsi_status && (resp_info & RESID_UNDER)) {
  3461. /* don't log under runs if fcp set... */
  3462. if (vport->cfg_log_verbose & LOG_FCP)
  3463. logit = LOG_FCP_ERROR;
  3464. /* unless operator says so */
  3465. if (vport->cfg_log_verbose & LOG_FCP_UNDER)
  3466. logit = LOG_FCP_UNDER;
  3467. }
  3468. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  3469. "9024 FCP command x%x failed: x%x SNS x%x x%x "
  3470. "Data: x%x x%x x%x x%x x%x\n",
  3471. cmnd->cmnd[0], scsi_status,
  3472. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  3473. be32_to_cpu(fcprsp->rspResId),
  3474. be32_to_cpu(fcprsp->rspSnsLen),
  3475. be32_to_cpu(fcprsp->rspRspLen),
  3476. fcprsp->rspInfo3);
  3477. scsi_set_resid(cmnd, 0);
  3478. fcpDl = be32_to_cpu(fcpcmd->fcpDl);
  3479. if (resp_info & RESID_UNDER) {
  3480. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  3481. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
  3482. "9025 FCP Underrun, expected %d, "
  3483. "residual %d Data: x%x x%x x%x\n",
  3484. fcpDl,
  3485. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  3486. cmnd->underflow);
  3487. /*
  3488. * If there is an under run, check if under run reported by
  3489. * storage array is same as the under run reported by HBA.
  3490. * If this is not same, there is a dropped frame.
  3491. */
  3492. if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
  3493. lpfc_printf_vlog(vport, KERN_WARNING,
  3494. LOG_FCP | LOG_FCP_ERROR,
  3495. "9026 FCP Read Check Error "
  3496. "and Underrun Data: x%x x%x x%x x%x\n",
  3497. fcpDl,
  3498. scsi_get_resid(cmnd), fcpi_parm,
  3499. cmnd->cmnd[0]);
  3500. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  3501. host_status = DID_ERROR;
  3502. }
  3503. /*
  3504. * The cmnd->underflow is the minimum number of bytes that must
  3505. * be transferred for this command. Provided a sense condition
  3506. * is not present, make sure the actual amount transferred is at
  3507. * least the underflow value or fail.
  3508. */
  3509. if (!(resp_info & SNS_LEN_VALID) &&
  3510. (scsi_status == SAM_STAT_GOOD) &&
  3511. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  3512. < cmnd->underflow)) {
  3513. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  3514. "9027 FCP command x%x residual "
  3515. "underrun converted to error "
  3516. "Data: x%x x%x x%x\n",
  3517. cmnd->cmnd[0], scsi_bufflen(cmnd),
  3518. scsi_get_resid(cmnd), cmnd->underflow);
  3519. host_status = DID_ERROR;
  3520. }
  3521. } else if (resp_info & RESID_OVER) {
  3522. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3523. "9028 FCP command x%x residual overrun error. "
  3524. "Data: x%x x%x\n", cmnd->cmnd[0],
  3525. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  3526. host_status = DID_ERROR;
  3527. /*
  3528. * Check SLI validation that all the transfer was actually done
  3529. * (fcpi_parm should be zero). Apply check only to reads.
  3530. */
  3531. } else if (fcpi_parm) {
  3532. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  3533. "9029 FCP %s Check Error Data: "
  3534. "x%x x%x x%x x%x x%x\n",
  3535. ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
  3536. "Read" : "Write"),
  3537. fcpDl, be32_to_cpu(fcprsp->rspResId),
  3538. fcpi_parm, cmnd->cmnd[0], scsi_status);
  3539. /* There is some issue with the LPe12000 that causes it
  3540. * to miscalculate the fcpi_parm and falsely trip this
  3541. * recovery logic. Detect this case and don't error when true.
  3542. */
  3543. if (fcpi_parm > fcpDl)
  3544. goto out;
  3545. switch (scsi_status) {
  3546. case SAM_STAT_GOOD:
  3547. case SAM_STAT_CHECK_CONDITION:
  3548. /* Fabric dropped a data frame. Fail any successful
  3549. * command in which we detected dropped frames.
  3550. * A status of good or some check conditions could
  3551. * be considered a successful command.
  3552. */
  3553. host_status = DID_ERROR;
  3554. break;
  3555. }
  3556. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  3557. }
  3558. out:
  3559. cmnd->result = host_status << 16 | scsi_status;
  3560. lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, fcpi_parm);
  3561. }
  3562. /**
  3563. * lpfc_fcp_io_cmd_wqe_cmpl - Complete a FCP IO
  3564. * @phba: The hba for which this call is being executed.
  3565. * @pwqeIn: The command WQE for the scsi cmnd.
  3566. * @pwqeOut: Pointer to driver response WQE object.
  3567. *
  3568. * This routine assigns scsi command result by looking into response WQE
  3569. * status field appropriately. This routine handles QUEUE FULL condition as
  3570. * well by ramping down device queue depth.
  3571. **/
  3572. static void
  3573. lpfc_fcp_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
  3574. struct lpfc_iocbq *pwqeOut)
  3575. {
  3576. struct lpfc_io_buf *lpfc_cmd = pwqeIn->io_buf;
  3577. struct lpfc_wcqe_complete *wcqe = &pwqeOut->wcqe_cmpl;
  3578. struct lpfc_vport *vport = pwqeIn->vport;
  3579. struct lpfc_rport_data *rdata;
  3580. struct lpfc_nodelist *ndlp;
  3581. struct scsi_cmnd *cmd;
  3582. unsigned long flags;
  3583. struct lpfc_fast_path_event *fast_path_evt;
  3584. struct Scsi_Host *shost;
  3585. u32 logit = LOG_FCP;
  3586. u32 status, idx;
  3587. u32 lat;
  3588. u8 wait_xb_clr = 0;
  3589. /* Sanity check on return of outstanding command */
  3590. if (!lpfc_cmd) {
  3591. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  3592. "9032 Null lpfc_cmd pointer. No "
  3593. "release, skip completion\n");
  3594. return;
  3595. }
  3596. rdata = lpfc_cmd->rdata;
  3597. ndlp = rdata->pnode;
  3598. /* Sanity check on return of outstanding command */
  3599. cmd = lpfc_cmd->pCmd;
  3600. if (!cmd) {
  3601. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  3602. "9042 I/O completion: Not an active IO\n");
  3603. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3604. return;
  3605. }
  3606. /* Guard against abort handler being called at same time */
  3607. spin_lock(&lpfc_cmd->buf_lock);
  3608. idx = lpfc_cmd->cur_iocbq.hba_wqidx;
  3609. if (phba->sli4_hba.hdwq)
  3610. phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
  3611. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  3612. if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
  3613. this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
  3614. #endif
  3615. shost = cmd->device->host;
  3616. status = bf_get(lpfc_wcqe_c_status, wcqe);
  3617. lpfc_cmd->status = (status & LPFC_IOCB_STATUS_MASK);
  3618. lpfc_cmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
  3619. lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
  3620. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  3621. lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
  3622. if (phba->cfg_fcp_wait_abts_rsp)
  3623. wait_xb_clr = 1;
  3624. }
  3625. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  3626. if (lpfc_cmd->prot_data_type) {
  3627. struct scsi_dif_tuple *src = NULL;
  3628. src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
  3629. /*
  3630. * Used to restore any changes to protection
  3631. * data for error injection.
  3632. */
  3633. switch (lpfc_cmd->prot_data_type) {
  3634. case LPFC_INJERR_REFTAG:
  3635. src->ref_tag =
  3636. lpfc_cmd->prot_data;
  3637. break;
  3638. case LPFC_INJERR_APPTAG:
  3639. src->app_tag =
  3640. (uint16_t)lpfc_cmd->prot_data;
  3641. break;
  3642. case LPFC_INJERR_GUARD:
  3643. src->guard_tag =
  3644. (uint16_t)lpfc_cmd->prot_data;
  3645. break;
  3646. default:
  3647. break;
  3648. }
  3649. lpfc_cmd->prot_data = 0;
  3650. lpfc_cmd->prot_data_type = 0;
  3651. lpfc_cmd->prot_data_segment = NULL;
  3652. }
  3653. #endif
  3654. if (unlikely(lpfc_cmd->status)) {
  3655. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  3656. (lpfc_cmd->result & IOERR_DRVR_MASK))
  3657. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  3658. else if (lpfc_cmd->status >= IOSTAT_CNT)
  3659. lpfc_cmd->status = IOSTAT_DEFAULT;
  3660. if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
  3661. !lpfc_cmd->fcp_rsp->rspStatus3 &&
  3662. (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
  3663. !(vport->cfg_log_verbose & LOG_FCP_UNDER))
  3664. logit = 0;
  3665. else
  3666. logit = LOG_FCP | LOG_FCP_UNDER;
  3667. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  3668. "9034 FCP cmd x%x failed <%d/%lld> "
  3669. "status: x%x result: x%x "
  3670. "sid: x%x did: x%x oxid: x%x "
  3671. "Data: x%x x%x x%x\n",
  3672. cmd->cmnd[0],
  3673. cmd->device ? cmd->device->id : 0xffff,
  3674. cmd->device ? cmd->device->lun : 0xffff,
  3675. lpfc_cmd->status, lpfc_cmd->result,
  3676. vport->fc_myDID,
  3677. (ndlp) ? ndlp->nlp_DID : 0,
  3678. lpfc_cmd->cur_iocbq.sli4_xritag,
  3679. wcqe->parameter, wcqe->total_data_placed,
  3680. lpfc_cmd->cur_iocbq.iotag);
  3681. }
  3682. switch (lpfc_cmd->status) {
  3683. case IOSTAT_SUCCESS:
  3684. cmd->result = DID_OK << 16;
  3685. break;
  3686. case IOSTAT_FCP_RSP_ERROR:
  3687. lpfc_handle_fcp_err(vport, lpfc_cmd,
  3688. pwqeIn->wqe.fcp_iread.total_xfer_len -
  3689. wcqe->total_data_placed);
  3690. break;
  3691. case IOSTAT_NPORT_BSY:
  3692. case IOSTAT_FABRIC_BSY:
  3693. cmd->result = DID_TRANSPORT_DISRUPTED << 16;
  3694. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3695. if (!fast_path_evt)
  3696. break;
  3697. fast_path_evt->un.fabric_evt.event_type =
  3698. FC_REG_FABRIC_EVENT;
  3699. fast_path_evt->un.fabric_evt.subcategory =
  3700. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  3701. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  3702. if (ndlp) {
  3703. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  3704. &ndlp->nlp_portname,
  3705. sizeof(struct lpfc_name));
  3706. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  3707. &ndlp->nlp_nodename,
  3708. sizeof(struct lpfc_name));
  3709. }
  3710. fast_path_evt->vport = vport;
  3711. fast_path_evt->work_evt.evt =
  3712. LPFC_EVT_FASTPATH_MGMT_EVT;
  3713. spin_lock_irqsave(&phba->hbalock, flags);
  3714. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  3715. &phba->work_list);
  3716. spin_unlock_irqrestore(&phba->hbalock, flags);
  3717. lpfc_worker_wake_up(phba);
  3718. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  3719. "9035 Fabric/Node busy FCP cmd x%x failed"
  3720. " <%d/%lld> "
  3721. "status: x%x result: x%x "
  3722. "sid: x%x did: x%x oxid: x%x "
  3723. "Data: x%x x%x x%x\n",
  3724. cmd->cmnd[0],
  3725. cmd->device ? cmd->device->id : 0xffff,
  3726. cmd->device ? cmd->device->lun : 0xffff,
  3727. lpfc_cmd->status, lpfc_cmd->result,
  3728. vport->fc_myDID,
  3729. (ndlp) ? ndlp->nlp_DID : 0,
  3730. lpfc_cmd->cur_iocbq.sli4_xritag,
  3731. wcqe->parameter,
  3732. wcqe->total_data_placed,
  3733. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  3734. break;
  3735. case IOSTAT_REMOTE_STOP:
  3736. if (ndlp) {
  3737. /* This I/O was aborted by the target, we don't
  3738. * know the rxid and because we did not send the
  3739. * ABTS we cannot generate and RRQ.
  3740. */
  3741. lpfc_set_rrq_active(phba, ndlp,
  3742. lpfc_cmd->cur_iocbq.sli4_lxritag,
  3743. 0, 0);
  3744. }
  3745. fallthrough;
  3746. case IOSTAT_LOCAL_REJECT:
  3747. if (lpfc_cmd->result & IOERR_DRVR_MASK)
  3748. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  3749. if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
  3750. lpfc_cmd->result ==
  3751. IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
  3752. lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
  3753. lpfc_cmd->result ==
  3754. IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
  3755. cmd->result = DID_NO_CONNECT << 16;
  3756. break;
  3757. }
  3758. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  3759. lpfc_cmd->result == IOERR_LINK_DOWN ||
  3760. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  3761. lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
  3762. lpfc_cmd->result == IOERR_RPI_SUSPENDED ||
  3763. lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
  3764. cmd->result = DID_TRANSPORT_DISRUPTED << 16;
  3765. break;
  3766. }
  3767. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  3768. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  3769. status == CQE_STATUS_DI_ERROR) {
  3770. if (scsi_get_prot_op(cmd) !=
  3771. SCSI_PROT_NORMAL) {
  3772. /*
  3773. * This is a response for a BG enabled
  3774. * cmd. Parse BG error
  3775. */
  3776. lpfc_parse_bg_err(phba, lpfc_cmd, pwqeOut);
  3777. break;
  3778. } else {
  3779. lpfc_printf_vlog(vport, KERN_WARNING,
  3780. LOG_BG,
  3781. "9040 non-zero BGSTAT "
  3782. "on unprotected cmd\n");
  3783. }
  3784. }
  3785. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  3786. "9036 Local Reject FCP cmd x%x failed"
  3787. " <%d/%lld> "
  3788. "status: x%x result: x%x "
  3789. "sid: x%x did: x%x oxid: x%x "
  3790. "Data: x%x x%x x%x\n",
  3791. cmd->cmnd[0],
  3792. cmd->device ? cmd->device->id : 0xffff,
  3793. cmd->device ? cmd->device->lun : 0xffff,
  3794. lpfc_cmd->status, lpfc_cmd->result,
  3795. vport->fc_myDID,
  3796. (ndlp) ? ndlp->nlp_DID : 0,
  3797. lpfc_cmd->cur_iocbq.sli4_xritag,
  3798. wcqe->parameter,
  3799. wcqe->total_data_placed,
  3800. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  3801. fallthrough;
  3802. default:
  3803. if (lpfc_cmd->status >= IOSTAT_CNT)
  3804. lpfc_cmd->status = IOSTAT_DEFAULT;
  3805. cmd->result = DID_ERROR << 16;
  3806. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  3807. "9037 FCP Completion Error: xri %x "
  3808. "status x%x result x%x [x%x] "
  3809. "placed x%x\n",
  3810. lpfc_cmd->cur_iocbq.sli4_xritag,
  3811. lpfc_cmd->status, lpfc_cmd->result,
  3812. wcqe->parameter,
  3813. wcqe->total_data_placed);
  3814. }
  3815. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  3816. u32 *lp = (u32 *)cmd->sense_buffer;
  3817. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  3818. "9039 Iodone <%d/%llu> cmd x%px, error "
  3819. "x%x SNS x%x x%x LBA x%llx Data: x%x x%x\n",
  3820. cmd->device->id, cmd->device->lun, cmd,
  3821. cmd->result, *lp, *(lp + 3),
  3822. (u64)scsi_get_lba(cmd),
  3823. cmd->retries, scsi_get_resid(cmd));
  3824. }
  3825. if (vport->cfg_max_scsicmpl_time &&
  3826. time_after(jiffies, lpfc_cmd->start_time +
  3827. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  3828. spin_lock_irqsave(shost->host_lock, flags);
  3829. if (ndlp) {
  3830. if (ndlp->cmd_qdepth >
  3831. atomic_read(&ndlp->cmd_pending) &&
  3832. (atomic_read(&ndlp->cmd_pending) >
  3833. LPFC_MIN_TGT_QDEPTH) &&
  3834. (cmd->cmnd[0] == READ_10 ||
  3835. cmd->cmnd[0] == WRITE_10))
  3836. ndlp->cmd_qdepth =
  3837. atomic_read(&ndlp->cmd_pending);
  3838. ndlp->last_change_time = jiffies;
  3839. }
  3840. spin_unlock_irqrestore(shost->host_lock, flags);
  3841. }
  3842. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  3843. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  3844. if (lpfc_cmd->ts_cmd_start) {
  3845. lpfc_cmd->ts_isr_cmpl = lpfc_cmd->cur_iocbq.isr_timestamp;
  3846. lpfc_cmd->ts_data_io = ktime_get_ns();
  3847. phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
  3848. lpfc_io_ktime(phba, lpfc_cmd);
  3849. }
  3850. #endif
  3851. if (likely(!wait_xb_clr))
  3852. lpfc_cmd->pCmd = NULL;
  3853. spin_unlock(&lpfc_cmd->buf_lock);
  3854. /* Check if IO qualified for CMF */
  3855. if (phba->cmf_active_mode != LPFC_CFG_OFF &&
  3856. cmd->sc_data_direction == DMA_FROM_DEVICE &&
  3857. (scsi_sg_count(cmd))) {
  3858. /* Used when calculating average latency */
  3859. lat = ktime_get_ns() - lpfc_cmd->rx_cmd_start;
  3860. lpfc_update_cmf_cmpl(phba, lat, scsi_bufflen(cmd), shost);
  3861. }
  3862. if (wait_xb_clr)
  3863. goto out;
  3864. /* The sdev is not guaranteed to be valid post scsi_done upcall. */
  3865. scsi_done(cmd);
  3866. /*
  3867. * If there is an abort thread waiting for command completion
  3868. * wake up the thread.
  3869. */
  3870. spin_lock(&lpfc_cmd->buf_lock);
  3871. lpfc_cmd->cur_iocbq.cmd_flag &= ~LPFC_DRIVER_ABORTED;
  3872. if (lpfc_cmd->waitq)
  3873. wake_up(lpfc_cmd->waitq);
  3874. spin_unlock(&lpfc_cmd->buf_lock);
  3875. out:
  3876. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3877. }
  3878. /**
  3879. * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
  3880. * @phba: The Hba for which this call is being executed.
  3881. * @pIocbIn: The command IOCBQ for the scsi cmnd.
  3882. * @pIocbOut: The response IOCBQ for the scsi cmnd.
  3883. *
  3884. * This routine assigns scsi command result by looking into response IOCB
  3885. * status field appropriately. This routine handles QUEUE FULL condition as
  3886. * well by ramping down device queue depth.
  3887. **/
  3888. static void
  3889. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  3890. struct lpfc_iocbq *pIocbOut)
  3891. {
  3892. struct lpfc_io_buf *lpfc_cmd =
  3893. (struct lpfc_io_buf *) pIocbIn->io_buf;
  3894. struct lpfc_vport *vport = pIocbIn->vport;
  3895. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  3896. struct lpfc_nodelist *pnode = rdata->pnode;
  3897. struct scsi_cmnd *cmd;
  3898. unsigned long flags;
  3899. struct lpfc_fast_path_event *fast_path_evt;
  3900. struct Scsi_Host *shost;
  3901. int idx;
  3902. uint32_t logit = LOG_FCP;
  3903. /* Guard against abort handler being called at same time */
  3904. spin_lock(&lpfc_cmd->buf_lock);
  3905. /* Sanity check on return of outstanding command */
  3906. cmd = lpfc_cmd->pCmd;
  3907. if (!cmd || !phba) {
  3908. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  3909. "2621 IO completion: Not an active IO\n");
  3910. spin_unlock(&lpfc_cmd->buf_lock);
  3911. return;
  3912. }
  3913. idx = lpfc_cmd->cur_iocbq.hba_wqidx;
  3914. if (phba->sli4_hba.hdwq)
  3915. phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
  3916. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  3917. if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
  3918. this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
  3919. #endif
  3920. shost = cmd->device->host;
  3921. lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
  3922. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  3923. /* pick up SLI4 exchange busy status from HBA */
  3924. lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
  3925. if (pIocbOut->cmd_flag & LPFC_EXCHANGE_BUSY)
  3926. lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
  3927. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  3928. if (lpfc_cmd->prot_data_type) {
  3929. struct scsi_dif_tuple *src = NULL;
  3930. src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
  3931. /*
  3932. * Used to restore any changes to protection
  3933. * data for error injection.
  3934. */
  3935. switch (lpfc_cmd->prot_data_type) {
  3936. case LPFC_INJERR_REFTAG:
  3937. src->ref_tag =
  3938. lpfc_cmd->prot_data;
  3939. break;
  3940. case LPFC_INJERR_APPTAG:
  3941. src->app_tag =
  3942. (uint16_t)lpfc_cmd->prot_data;
  3943. break;
  3944. case LPFC_INJERR_GUARD:
  3945. src->guard_tag =
  3946. (uint16_t)lpfc_cmd->prot_data;
  3947. break;
  3948. default:
  3949. break;
  3950. }
  3951. lpfc_cmd->prot_data = 0;
  3952. lpfc_cmd->prot_data_type = 0;
  3953. lpfc_cmd->prot_data_segment = NULL;
  3954. }
  3955. #endif
  3956. if (unlikely(lpfc_cmd->status)) {
  3957. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  3958. (lpfc_cmd->result & IOERR_DRVR_MASK))
  3959. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  3960. else if (lpfc_cmd->status >= IOSTAT_CNT)
  3961. lpfc_cmd->status = IOSTAT_DEFAULT;
  3962. if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
  3963. !lpfc_cmd->fcp_rsp->rspStatus3 &&
  3964. (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
  3965. !(vport->cfg_log_verbose & LOG_FCP_UNDER))
  3966. logit = 0;
  3967. else
  3968. logit = LOG_FCP | LOG_FCP_UNDER;
  3969. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  3970. "9030 FCP cmd x%x failed <%d/%lld> "
  3971. "status: x%x result: x%x "
  3972. "sid: x%x did: x%x oxid: x%x "
  3973. "Data: x%x x%x\n",
  3974. cmd->cmnd[0],
  3975. cmd->device ? cmd->device->id : 0xffff,
  3976. cmd->device ? cmd->device->lun : 0xffff,
  3977. lpfc_cmd->status, lpfc_cmd->result,
  3978. vport->fc_myDID,
  3979. (pnode) ? pnode->nlp_DID : 0,
  3980. phba->sli_rev == LPFC_SLI_REV4 ?
  3981. lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
  3982. pIocbOut->iocb.ulpContext,
  3983. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  3984. switch (lpfc_cmd->status) {
  3985. case IOSTAT_FCP_RSP_ERROR:
  3986. /* Call FCP RSP handler to determine result */
  3987. lpfc_handle_fcp_err(vport, lpfc_cmd,
  3988. pIocbOut->iocb.un.fcpi.fcpi_parm);
  3989. break;
  3990. case IOSTAT_NPORT_BSY:
  3991. case IOSTAT_FABRIC_BSY:
  3992. cmd->result = DID_TRANSPORT_DISRUPTED << 16;
  3993. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3994. if (!fast_path_evt)
  3995. break;
  3996. fast_path_evt->un.fabric_evt.event_type =
  3997. FC_REG_FABRIC_EVENT;
  3998. fast_path_evt->un.fabric_evt.subcategory =
  3999. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  4000. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  4001. if (pnode) {
  4002. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  4003. &pnode->nlp_portname,
  4004. sizeof(struct lpfc_name));
  4005. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  4006. &pnode->nlp_nodename,
  4007. sizeof(struct lpfc_name));
  4008. }
  4009. fast_path_evt->vport = vport;
  4010. fast_path_evt->work_evt.evt =
  4011. LPFC_EVT_FASTPATH_MGMT_EVT;
  4012. spin_lock_irqsave(&phba->hbalock, flags);
  4013. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  4014. &phba->work_list);
  4015. spin_unlock_irqrestore(&phba->hbalock, flags);
  4016. lpfc_worker_wake_up(phba);
  4017. break;
  4018. case IOSTAT_LOCAL_REJECT:
  4019. case IOSTAT_REMOTE_STOP:
  4020. if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
  4021. lpfc_cmd->result ==
  4022. IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
  4023. lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
  4024. lpfc_cmd->result ==
  4025. IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
  4026. cmd->result = DID_NO_CONNECT << 16;
  4027. break;
  4028. }
  4029. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  4030. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  4031. lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
  4032. lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
  4033. cmd->result = DID_TRANSPORT_DISRUPTED << 16;
  4034. break;
  4035. }
  4036. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  4037. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  4038. pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
  4039. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  4040. /*
  4041. * This is a response for a BG enabled
  4042. * cmd. Parse BG error
  4043. */
  4044. lpfc_parse_bg_err(phba, lpfc_cmd,
  4045. pIocbOut);
  4046. break;
  4047. } else {
  4048. lpfc_printf_vlog(vport, KERN_WARNING,
  4049. LOG_BG,
  4050. "9031 non-zero BGSTAT "
  4051. "on unprotected cmd\n");
  4052. }
  4053. }
  4054. if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
  4055. && (phba->sli_rev == LPFC_SLI_REV4)
  4056. && pnode) {
  4057. /* This IO was aborted by the target, we don't
  4058. * know the rxid and because we did not send the
  4059. * ABTS we cannot generate and RRQ.
  4060. */
  4061. lpfc_set_rrq_active(phba, pnode,
  4062. lpfc_cmd->cur_iocbq.sli4_lxritag,
  4063. 0, 0);
  4064. }
  4065. fallthrough;
  4066. default:
  4067. cmd->result = DID_ERROR << 16;
  4068. break;
  4069. }
  4070. if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  4071. cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
  4072. SAM_STAT_BUSY;
  4073. } else
  4074. cmd->result = DID_OK << 16;
  4075. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  4076. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  4077. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4078. "0710 Iodone <%d/%llu> cmd x%px, error "
  4079. "x%x SNS x%x x%x Data: x%x x%x\n",
  4080. cmd->device->id, cmd->device->lun, cmd,
  4081. cmd->result, *lp, *(lp + 3), cmd->retries,
  4082. scsi_get_resid(cmd));
  4083. }
  4084. if (vport->cfg_max_scsicmpl_time &&
  4085. time_after(jiffies, lpfc_cmd->start_time +
  4086. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  4087. spin_lock_irqsave(shost->host_lock, flags);
  4088. if (pnode) {
  4089. if (pnode->cmd_qdepth >
  4090. atomic_read(&pnode->cmd_pending) &&
  4091. (atomic_read(&pnode->cmd_pending) >
  4092. LPFC_MIN_TGT_QDEPTH) &&
  4093. ((cmd->cmnd[0] == READ_10) ||
  4094. (cmd->cmnd[0] == WRITE_10)))
  4095. pnode->cmd_qdepth =
  4096. atomic_read(&pnode->cmd_pending);
  4097. pnode->last_change_time = jiffies;
  4098. }
  4099. spin_unlock_irqrestore(shost->host_lock, flags);
  4100. }
  4101. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  4102. lpfc_cmd->pCmd = NULL;
  4103. spin_unlock(&lpfc_cmd->buf_lock);
  4104. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  4105. if (lpfc_cmd->ts_cmd_start) {
  4106. lpfc_cmd->ts_isr_cmpl = pIocbIn->isr_timestamp;
  4107. lpfc_cmd->ts_data_io = ktime_get_ns();
  4108. phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
  4109. lpfc_io_ktime(phba, lpfc_cmd);
  4110. }
  4111. #endif
  4112. /* The sdev is not guaranteed to be valid post scsi_done upcall. */
  4113. scsi_done(cmd);
  4114. /*
  4115. * If there is an abort thread waiting for command completion
  4116. * wake up the thread.
  4117. */
  4118. spin_lock(&lpfc_cmd->buf_lock);
  4119. lpfc_cmd->cur_iocbq.cmd_flag &= ~LPFC_DRIVER_ABORTED;
  4120. if (lpfc_cmd->waitq)
  4121. wake_up(lpfc_cmd->waitq);
  4122. spin_unlock(&lpfc_cmd->buf_lock);
  4123. lpfc_release_scsi_buf(phba, lpfc_cmd);
  4124. }
  4125. /**
  4126. * lpfc_scsi_prep_cmnd_buf_s3 - SLI-3 IOCB init for the IO
  4127. * @vport: Pointer to vport object.
  4128. * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
  4129. * @tmo: timeout value for the IO
  4130. *
  4131. * Based on the data-direction of the command, initialize IOCB
  4132. * in the I/O buffer. Fill in the IOCB fields which are independent
  4133. * of the scsi buffer
  4134. *
  4135. * RETURNS 0 - SUCCESS,
  4136. **/
  4137. static int lpfc_scsi_prep_cmnd_buf_s3(struct lpfc_vport *vport,
  4138. struct lpfc_io_buf *lpfc_cmd,
  4139. uint8_t tmo)
  4140. {
  4141. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  4142. struct lpfc_iocbq *piocbq = &lpfc_cmd->cur_iocbq;
  4143. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  4144. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  4145. struct lpfc_nodelist *pnode = lpfc_cmd->ndlp;
  4146. int datadir = scsi_cmnd->sc_data_direction;
  4147. u32 fcpdl;
  4148. piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
  4149. /*
  4150. * There are three possibilities here - use scatter-gather segment, use
  4151. * the single mapping, or neither. Start the lpfc command prep by
  4152. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  4153. * data bde entry.
  4154. */
  4155. if (scsi_sg_count(scsi_cmnd)) {
  4156. if (datadir == DMA_TO_DEVICE) {
  4157. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  4158. iocb_cmd->ulpPU = PARM_READ_CHECK;
  4159. if (vport->cfg_first_burst_size &&
  4160. (pnode->nlp_flag & NLP_FIRSTBURST)) {
  4161. u32 xrdy_len;
  4162. fcpdl = scsi_bufflen(scsi_cmnd);
  4163. xrdy_len = min(fcpdl,
  4164. vport->cfg_first_burst_size);
  4165. piocbq->iocb.un.fcpi.fcpi_XRdy = xrdy_len;
  4166. }
  4167. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  4168. } else {
  4169. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  4170. iocb_cmd->ulpPU = PARM_READ_CHECK;
  4171. fcp_cmnd->fcpCntl3 = READ_DATA;
  4172. }
  4173. } else {
  4174. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  4175. iocb_cmd->un.fcpi.fcpi_parm = 0;
  4176. iocb_cmd->ulpPU = 0;
  4177. fcp_cmnd->fcpCntl3 = 0;
  4178. }
  4179. /*
  4180. * Finish initializing those IOCB fields that are independent
  4181. * of the scsi_cmnd request_buffer
  4182. */
  4183. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  4184. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  4185. piocbq->iocb.ulpFCP2Rcvy = 1;
  4186. else
  4187. piocbq->iocb.ulpFCP2Rcvy = 0;
  4188. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  4189. piocbq->io_buf = lpfc_cmd;
  4190. if (!piocbq->cmd_cmpl)
  4191. piocbq->cmd_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  4192. piocbq->iocb.ulpTimeout = tmo;
  4193. piocbq->vport = vport;
  4194. return 0;
  4195. }
  4196. /**
  4197. * lpfc_scsi_prep_cmnd_buf_s4 - SLI-4 WQE init for the IO
  4198. * @vport: Pointer to vport object.
  4199. * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
  4200. * @tmo: timeout value for the IO
  4201. *
  4202. * Based on the data-direction of the command copy WQE template
  4203. * to I/O buffer WQE. Fill in the WQE fields which are independent
  4204. * of the scsi buffer
  4205. *
  4206. * RETURNS 0 - SUCCESS,
  4207. **/
  4208. static int lpfc_scsi_prep_cmnd_buf_s4(struct lpfc_vport *vport,
  4209. struct lpfc_io_buf *lpfc_cmd,
  4210. uint8_t tmo)
  4211. {
  4212. struct lpfc_hba *phba = vport->phba;
  4213. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  4214. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  4215. struct lpfc_sli4_hdw_queue *hdwq = NULL;
  4216. struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
  4217. struct lpfc_nodelist *pnode = lpfc_cmd->ndlp;
  4218. union lpfc_wqe128 *wqe = &pwqeq->wqe;
  4219. u16 idx = lpfc_cmd->hdwq_no;
  4220. int datadir = scsi_cmnd->sc_data_direction;
  4221. hdwq = &phba->sli4_hba.hdwq[idx];
  4222. /* Initialize 64 bytes only */
  4223. memset(wqe, 0, sizeof(union lpfc_wqe128));
  4224. /*
  4225. * There are three possibilities here - use scatter-gather segment, use
  4226. * the single mapping, or neither.
  4227. */
  4228. if (scsi_sg_count(scsi_cmnd)) {
  4229. if (datadir == DMA_TO_DEVICE) {
  4230. /* From the iwrite template, initialize words 7 - 11 */
  4231. memcpy(&wqe->words[7],
  4232. &lpfc_iwrite_cmd_template.words[7],
  4233. sizeof(uint32_t) * 5);
  4234. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  4235. if (hdwq)
  4236. hdwq->scsi_cstat.output_requests++;
  4237. } else {
  4238. /* From the iread template, initialize words 7 - 11 */
  4239. memcpy(&wqe->words[7],
  4240. &lpfc_iread_cmd_template.words[7],
  4241. sizeof(uint32_t) * 5);
  4242. /* Word 7 */
  4243. bf_set(wqe_tmo, &wqe->fcp_iread.wqe_com, tmo);
  4244. fcp_cmnd->fcpCntl3 = READ_DATA;
  4245. if (hdwq)
  4246. hdwq->scsi_cstat.input_requests++;
  4247. /* For a CMF Managed port, iod must be zero'ed */
  4248. if (phba->cmf_active_mode == LPFC_CFG_MANAGED)
  4249. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
  4250. LPFC_WQE_IOD_NONE);
  4251. }
  4252. } else {
  4253. /* From the icmnd template, initialize words 4 - 11 */
  4254. memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
  4255. sizeof(uint32_t) * 8);
  4256. /* Word 7 */
  4257. bf_set(wqe_tmo, &wqe->fcp_icmd.wqe_com, tmo);
  4258. fcp_cmnd->fcpCntl3 = 0;
  4259. if (hdwq)
  4260. hdwq->scsi_cstat.control_requests++;
  4261. }
  4262. /*
  4263. * Finish initializing those WQE fields that are independent
  4264. * of the request_buffer
  4265. */
  4266. /* Word 3 */
  4267. bf_set(payload_offset_len, &wqe->fcp_icmd,
  4268. sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  4269. /* Word 6 */
  4270. bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
  4271. phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
  4272. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
  4273. /* Word 7*/
  4274. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  4275. bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
  4276. bf_set(wqe_class, &wqe->generic.wqe_com,
  4277. (pnode->nlp_fcp_info & 0x0f));
  4278. /* Word 8 */
  4279. wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
  4280. /* Word 9 */
  4281. bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
  4282. pwqeq->vport = vport;
  4283. pwqeq->io_buf = lpfc_cmd;
  4284. pwqeq->hba_wqidx = lpfc_cmd->hdwq_no;
  4285. pwqeq->cmd_cmpl = lpfc_fcp_io_cmd_wqe_cmpl;
  4286. return 0;
  4287. }
  4288. /**
  4289. * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
  4290. * @vport: The virtual port for which this call is being executed.
  4291. * @lpfc_cmd: The scsi command which needs to send.
  4292. * @pnode: Pointer to lpfc_nodelist.
  4293. *
  4294. * This routine initializes fcp_cmnd and iocb data structure from scsi command
  4295. * to transfer for device with SLI3 interface spec.
  4296. **/
  4297. static int
  4298. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
  4299. struct lpfc_nodelist *pnode)
  4300. {
  4301. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  4302. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  4303. u8 *ptr;
  4304. if (!pnode)
  4305. return 0;
  4306. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  4307. /* clear task management bits */
  4308. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  4309. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  4310. &lpfc_cmd->fcp_cmnd->fcp_lun);
  4311. ptr = &fcp_cmnd->fcpCdb[0];
  4312. memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
  4313. if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
  4314. ptr += scsi_cmnd->cmd_len;
  4315. memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
  4316. }
  4317. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  4318. lpfc_scsi_prep_cmnd_buf(vport, lpfc_cmd, lpfc_cmd->timeout);
  4319. return 0;
  4320. }
  4321. /**
  4322. * lpfc_scsi_prep_task_mgmt_cmd_s3 - Convert SLI3 scsi TM cmd to FCP info unit
  4323. * @vport: The virtual port for which this call is being executed.
  4324. * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
  4325. * @lun: Logical unit number.
  4326. * @task_mgmt_cmd: SCSI task management command.
  4327. *
  4328. * This routine creates FCP information unit corresponding to @task_mgmt_cmd
  4329. * for device with SLI-3 interface spec.
  4330. *
  4331. * Return codes:
  4332. * 0 - Error
  4333. * 1 - Success
  4334. **/
  4335. static int
  4336. lpfc_scsi_prep_task_mgmt_cmd_s3(struct lpfc_vport *vport,
  4337. struct lpfc_io_buf *lpfc_cmd,
  4338. u64 lun, u8 task_mgmt_cmd)
  4339. {
  4340. struct lpfc_iocbq *piocbq;
  4341. IOCB_t *piocb;
  4342. struct fcp_cmnd *fcp_cmnd;
  4343. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  4344. struct lpfc_nodelist *ndlp = rdata->pnode;
  4345. if (!ndlp || ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  4346. return 0;
  4347. piocbq = &(lpfc_cmd->cur_iocbq);
  4348. piocbq->vport = vport;
  4349. piocb = &piocbq->iocb;
  4350. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  4351. /* Clear out any old data in the FCP command area */
  4352. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  4353. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  4354. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  4355. if (!(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  4356. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  4357. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  4358. piocb->ulpContext = ndlp->nlp_rpi;
  4359. piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
  4360. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  4361. piocb->ulpPU = 0;
  4362. piocb->un.fcpi.fcpi_parm = 0;
  4363. /* ulpTimeout is only one byte */
  4364. if (lpfc_cmd->timeout > 0xff) {
  4365. /*
  4366. * Do not timeout the command at the firmware level.
  4367. * The driver will provide the timeout mechanism.
  4368. */
  4369. piocb->ulpTimeout = 0;
  4370. } else
  4371. piocb->ulpTimeout = lpfc_cmd->timeout;
  4372. return 1;
  4373. }
  4374. /**
  4375. * lpfc_scsi_prep_task_mgmt_cmd_s4 - Convert SLI4 scsi TM cmd to FCP info unit
  4376. * @vport: The virtual port for which this call is being executed.
  4377. * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
  4378. * @lun: Logical unit number.
  4379. * @task_mgmt_cmd: SCSI task management command.
  4380. *
  4381. * This routine creates FCP information unit corresponding to @task_mgmt_cmd
  4382. * for device with SLI-4 interface spec.
  4383. *
  4384. * Return codes:
  4385. * 0 - Error
  4386. * 1 - Success
  4387. **/
  4388. static int
  4389. lpfc_scsi_prep_task_mgmt_cmd_s4(struct lpfc_vport *vport,
  4390. struct lpfc_io_buf *lpfc_cmd,
  4391. u64 lun, u8 task_mgmt_cmd)
  4392. {
  4393. struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
  4394. union lpfc_wqe128 *wqe = &pwqeq->wqe;
  4395. struct fcp_cmnd *fcp_cmnd;
  4396. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  4397. struct lpfc_nodelist *ndlp = rdata->pnode;
  4398. if (!ndlp || ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  4399. return 0;
  4400. pwqeq->vport = vport;
  4401. /* Initialize 64 bytes only */
  4402. memset(wqe, 0, sizeof(union lpfc_wqe128));
  4403. /* From the icmnd template, initialize words 4 - 11 */
  4404. memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
  4405. sizeof(uint32_t) * 8);
  4406. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  4407. /* Clear out any old data in the FCP command area */
  4408. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  4409. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  4410. fcp_cmnd->fcpCntl3 = 0;
  4411. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  4412. bf_set(payload_offset_len, &wqe->fcp_icmd,
  4413. sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  4414. bf_set(cmd_buff_len, &wqe->fcp_icmd, 0);
  4415. bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, /* ulpContext */
  4416. vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  4417. bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
  4418. ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0));
  4419. bf_set(wqe_class, &wqe->fcp_icmd.wqe_com,
  4420. (ndlp->nlp_fcp_info & 0x0f));
  4421. /* ulpTimeout is only one byte */
  4422. if (lpfc_cmd->timeout > 0xff) {
  4423. /*
  4424. * Do not timeout the command at the firmware level.
  4425. * The driver will provide the timeout mechanism.
  4426. */
  4427. bf_set(wqe_tmo, &wqe->fcp_icmd.wqe_com, 0);
  4428. } else {
  4429. bf_set(wqe_tmo, &wqe->fcp_icmd.wqe_com, lpfc_cmd->timeout);
  4430. }
  4431. lpfc_prep_embed_io(vport->phba, lpfc_cmd);
  4432. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
  4433. wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
  4434. bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
  4435. lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
  4436. return 1;
  4437. }
  4438. /**
  4439. * lpfc_scsi_api_table_setup - Set up scsi api function jump table
  4440. * @phba: The hba struct for which this call is being executed.
  4441. * @dev_grp: The HBA PCI-Device group number.
  4442. *
  4443. * This routine sets up the SCSI interface API function jump table in @phba
  4444. * struct.
  4445. * Returns: 0 - success, -ENODEV - failure.
  4446. **/
  4447. int
  4448. lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  4449. {
  4450. phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
  4451. switch (dev_grp) {
  4452. case LPFC_PCI_DEV_LP:
  4453. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
  4454. phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
  4455. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
  4456. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
  4457. phba->lpfc_scsi_prep_cmnd_buf = lpfc_scsi_prep_cmnd_buf_s3;
  4458. phba->lpfc_scsi_prep_task_mgmt_cmd =
  4459. lpfc_scsi_prep_task_mgmt_cmd_s3;
  4460. break;
  4461. case LPFC_PCI_DEV_OC:
  4462. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
  4463. phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
  4464. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
  4465. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
  4466. phba->lpfc_scsi_prep_cmnd_buf = lpfc_scsi_prep_cmnd_buf_s4;
  4467. phba->lpfc_scsi_prep_task_mgmt_cmd =
  4468. lpfc_scsi_prep_task_mgmt_cmd_s4;
  4469. break;
  4470. default:
  4471. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4472. "1418 Invalid HBA PCI-device group: 0x%x\n",
  4473. dev_grp);
  4474. return -ENODEV;
  4475. }
  4476. phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
  4477. phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  4478. return 0;
  4479. }
  4480. /**
  4481. * lpfc_tskmgmt_def_cmpl - IOCB completion routine for task management command
  4482. * @phba: The Hba for which this call is being executed.
  4483. * @cmdiocbq: Pointer to lpfc_iocbq data structure.
  4484. * @rspiocbq: Pointer to lpfc_iocbq data structure.
  4485. *
  4486. * This routine is IOCB completion routine for device reset and target reset
  4487. * routine. This routine release scsi buffer associated with lpfc_cmd.
  4488. **/
  4489. static void
  4490. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  4491. struct lpfc_iocbq *cmdiocbq,
  4492. struct lpfc_iocbq *rspiocbq)
  4493. {
  4494. struct lpfc_io_buf *lpfc_cmd = cmdiocbq->io_buf;
  4495. if (lpfc_cmd)
  4496. lpfc_release_scsi_buf(phba, lpfc_cmd);
  4497. return;
  4498. }
  4499. /**
  4500. * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
  4501. * if issuing a pci_bus_reset is possibly unsafe
  4502. * @phba: lpfc_hba pointer.
  4503. *
  4504. * Description:
  4505. * Walks the bus_list to ensure only PCI devices with Emulex
  4506. * vendor id, device ids that support hot reset, and only one occurrence
  4507. * of function 0.
  4508. *
  4509. * Returns:
  4510. * -EBADSLT, detected invalid device
  4511. * 0, successful
  4512. */
  4513. int
  4514. lpfc_check_pci_resettable(struct lpfc_hba *phba)
  4515. {
  4516. const struct pci_dev *pdev = phba->pcidev;
  4517. struct pci_dev *ptr = NULL;
  4518. u8 counter = 0;
  4519. /* Walk the list of devices on the pci_dev's bus */
  4520. list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
  4521. /* Check for Emulex Vendor ID */
  4522. if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
  4523. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4524. "8346 Non-Emulex vendor found: "
  4525. "0x%04x\n", ptr->vendor);
  4526. return -EBADSLT;
  4527. }
  4528. /* Check for valid Emulex Device ID */
  4529. if (phba->sli_rev != LPFC_SLI_REV4 ||
  4530. phba->hba_flag & HBA_FCOE_MODE) {
  4531. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4532. "8347 Incapable PCI reset device: "
  4533. "0x%04x\n", ptr->device);
  4534. return -EBADSLT;
  4535. }
  4536. /* Check for only one function 0 ID to ensure only one HBA on
  4537. * secondary bus
  4538. */
  4539. if (ptr->devfn == 0) {
  4540. if (++counter > 1) {
  4541. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4542. "8348 More than one device on "
  4543. "secondary bus found\n");
  4544. return -EBADSLT;
  4545. }
  4546. }
  4547. }
  4548. return 0;
  4549. }
  4550. /**
  4551. * lpfc_info - Info entry point of scsi_host_template data structure
  4552. * @host: The scsi host for which this call is being executed.
  4553. *
  4554. * This routine provides module information about hba.
  4555. *
  4556. * Reutrn code:
  4557. * Pointer to char - Success.
  4558. **/
  4559. const char *
  4560. lpfc_info(struct Scsi_Host *host)
  4561. {
  4562. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  4563. struct lpfc_hba *phba = vport->phba;
  4564. int link_speed = 0;
  4565. static char lpfcinfobuf[384];
  4566. char tmp[384] = {0};
  4567. memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
  4568. if (phba && phba->pcidev){
  4569. /* Model Description */
  4570. scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
  4571. if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
  4572. sizeof(lpfcinfobuf))
  4573. goto buffer_done;
  4574. /* PCI Info */
  4575. scnprintf(tmp, sizeof(tmp),
  4576. " on PCI bus %02x device %02x irq %d",
  4577. phba->pcidev->bus->number, phba->pcidev->devfn,
  4578. phba->pcidev->irq);
  4579. if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
  4580. sizeof(lpfcinfobuf))
  4581. goto buffer_done;
  4582. /* Port Number */
  4583. if (phba->Port[0]) {
  4584. scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
  4585. if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
  4586. sizeof(lpfcinfobuf))
  4587. goto buffer_done;
  4588. }
  4589. /* Link Speed */
  4590. link_speed = lpfc_sli_port_speed_get(phba);
  4591. if (link_speed != 0) {
  4592. scnprintf(tmp, sizeof(tmp),
  4593. " Logical Link Speed: %d Mbps", link_speed);
  4594. if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
  4595. sizeof(lpfcinfobuf))
  4596. goto buffer_done;
  4597. }
  4598. /* PCI resettable */
  4599. if (!lpfc_check_pci_resettable(phba)) {
  4600. scnprintf(tmp, sizeof(tmp), " PCI resettable");
  4601. strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
  4602. }
  4603. }
  4604. buffer_done:
  4605. return lpfcinfobuf;
  4606. }
  4607. /**
  4608. * lpfc_poll_rearm_timer - Routine to modify fcp_poll timer of hba
  4609. * @phba: The Hba for which this call is being executed.
  4610. *
  4611. * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
  4612. * The default value of cfg_poll_tmo is 10 milliseconds.
  4613. **/
  4614. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  4615. {
  4616. unsigned long poll_tmo_expires =
  4617. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  4618. if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
  4619. mod_timer(&phba->fcp_poll_timer,
  4620. poll_tmo_expires);
  4621. }
  4622. /**
  4623. * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
  4624. * @phba: The Hba for which this call is being executed.
  4625. *
  4626. * This routine starts the fcp_poll_timer of @phba.
  4627. **/
  4628. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  4629. {
  4630. lpfc_poll_rearm_timer(phba);
  4631. }
  4632. /**
  4633. * lpfc_poll_timeout - Restart polling timer
  4634. * @t: Timer construct where lpfc_hba data structure pointer is obtained.
  4635. *
  4636. * This routine restarts fcp_poll timer, when FCP ring polling is enable
  4637. * and FCP Ring interrupt is disable.
  4638. **/
  4639. void lpfc_poll_timeout(struct timer_list *t)
  4640. {
  4641. struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
  4642. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  4643. lpfc_sli_handle_fast_ring_event(phba,
  4644. &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
  4645. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  4646. lpfc_poll_rearm_timer(phba);
  4647. }
  4648. }
  4649. /*
  4650. * lpfc_is_command_vm_io - get the UUID from blk cgroup
  4651. * @cmd: Pointer to scsi_cmnd data structure
  4652. * Returns UUID if present, otherwise NULL
  4653. */
  4654. static char *lpfc_is_command_vm_io(struct scsi_cmnd *cmd)
  4655. {
  4656. struct bio *bio = scsi_cmd_to_rq(cmd)->bio;
  4657. if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !bio)
  4658. return NULL;
  4659. return blkcg_get_fc_appid(bio);
  4660. }
  4661. /**
  4662. * lpfc_queuecommand - scsi_host_template queuecommand entry point
  4663. * @shost: kernel scsi host pointer.
  4664. * @cmnd: Pointer to scsi_cmnd data structure.
  4665. *
  4666. * Driver registers this routine to scsi midlayer to submit a @cmd to process.
  4667. * This routine prepares an IOCB from scsi command and provides to firmware.
  4668. * The @done callback is invoked after driver finished processing the command.
  4669. *
  4670. * Return value :
  4671. * 0 - Success
  4672. * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
  4673. **/
  4674. static int
  4675. lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
  4676. {
  4677. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4678. struct lpfc_hba *phba = vport->phba;
  4679. struct lpfc_iocbq *cur_iocbq = NULL;
  4680. struct lpfc_rport_data *rdata;
  4681. struct lpfc_nodelist *ndlp;
  4682. struct lpfc_io_buf *lpfc_cmd;
  4683. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  4684. int err, idx;
  4685. u8 *uuid = NULL;
  4686. uint64_t start;
  4687. start = ktime_get_ns();
  4688. rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
  4689. /* sanity check on references */
  4690. if (unlikely(!rdata) || unlikely(!rport))
  4691. goto out_fail_command;
  4692. err = fc_remote_port_chkready(rport);
  4693. if (err) {
  4694. cmnd->result = err;
  4695. goto out_fail_command;
  4696. }
  4697. ndlp = rdata->pnode;
  4698. if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
  4699. (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
  4700. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  4701. "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
  4702. " op:%02x str=%s without registering for"
  4703. " BlockGuard - Rejecting command\n",
  4704. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  4705. dif_op_str[scsi_get_prot_op(cmnd)]);
  4706. goto out_fail_command;
  4707. }
  4708. /*
  4709. * Catch race where our node has transitioned, but the
  4710. * transport is still transitioning.
  4711. */
  4712. if (!ndlp)
  4713. goto out_tgt_busy1;
  4714. /* Check if IO qualifies for CMF */
  4715. if (phba->cmf_active_mode != LPFC_CFG_OFF &&
  4716. cmnd->sc_data_direction == DMA_FROM_DEVICE &&
  4717. (scsi_sg_count(cmnd))) {
  4718. /* Latency start time saved in rx_cmd_start later in routine */
  4719. err = lpfc_update_cmf_cmd(phba, scsi_bufflen(cmnd));
  4720. if (err)
  4721. goto out_tgt_busy1;
  4722. }
  4723. if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
  4724. if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
  4725. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
  4726. "3377 Target Queue Full, scsi Id:%d "
  4727. "Qdepth:%d Pending command:%d"
  4728. " WWNN:%02x:%02x:%02x:%02x:"
  4729. "%02x:%02x:%02x:%02x, "
  4730. " WWPN:%02x:%02x:%02x:%02x:"
  4731. "%02x:%02x:%02x:%02x",
  4732. ndlp->nlp_sid, ndlp->cmd_qdepth,
  4733. atomic_read(&ndlp->cmd_pending),
  4734. ndlp->nlp_nodename.u.wwn[0],
  4735. ndlp->nlp_nodename.u.wwn[1],
  4736. ndlp->nlp_nodename.u.wwn[2],
  4737. ndlp->nlp_nodename.u.wwn[3],
  4738. ndlp->nlp_nodename.u.wwn[4],
  4739. ndlp->nlp_nodename.u.wwn[5],
  4740. ndlp->nlp_nodename.u.wwn[6],
  4741. ndlp->nlp_nodename.u.wwn[7],
  4742. ndlp->nlp_portname.u.wwn[0],
  4743. ndlp->nlp_portname.u.wwn[1],
  4744. ndlp->nlp_portname.u.wwn[2],
  4745. ndlp->nlp_portname.u.wwn[3],
  4746. ndlp->nlp_portname.u.wwn[4],
  4747. ndlp->nlp_portname.u.wwn[5],
  4748. ndlp->nlp_portname.u.wwn[6],
  4749. ndlp->nlp_portname.u.wwn[7]);
  4750. goto out_tgt_busy2;
  4751. }
  4752. }
  4753. lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
  4754. if (lpfc_cmd == NULL) {
  4755. lpfc_rampdown_queue_depth(phba);
  4756. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
  4757. "0707 driver's buffer pool is empty, "
  4758. "IO busied\n");
  4759. goto out_host_busy;
  4760. }
  4761. lpfc_cmd->rx_cmd_start = start;
  4762. cur_iocbq = &lpfc_cmd->cur_iocbq;
  4763. /*
  4764. * Store the midlayer's command structure for the completion phase
  4765. * and complete the command initialization.
  4766. */
  4767. lpfc_cmd->pCmd = cmnd;
  4768. lpfc_cmd->rdata = rdata;
  4769. lpfc_cmd->ndlp = ndlp;
  4770. cur_iocbq->cmd_cmpl = NULL;
  4771. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  4772. err = lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  4773. if (err)
  4774. goto out_host_busy_release_buf;
  4775. if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  4776. if (vport->phba->cfg_enable_bg) {
  4777. lpfc_printf_vlog(vport,
  4778. KERN_INFO, LOG_SCSI_CMD,
  4779. "9033 BLKGRD: rcvd %s cmd:x%x "
  4780. "reftag x%x cnt %u pt %x\n",
  4781. dif_op_str[scsi_get_prot_op(cmnd)],
  4782. cmnd->cmnd[0],
  4783. scsi_prot_ref_tag(cmnd),
  4784. scsi_logical_block_count(cmnd),
  4785. (cmnd->cmnd[1]>>5));
  4786. }
  4787. err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  4788. } else {
  4789. if (vport->phba->cfg_enable_bg) {
  4790. lpfc_printf_vlog(vport,
  4791. KERN_INFO, LOG_SCSI_CMD,
  4792. "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
  4793. "x%x reftag x%x cnt %u pt %x\n",
  4794. cmnd->cmnd[0],
  4795. scsi_prot_ref_tag(cmnd),
  4796. scsi_logical_block_count(cmnd),
  4797. (cmnd->cmnd[1]>>5));
  4798. }
  4799. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  4800. }
  4801. if (unlikely(err)) {
  4802. if (err == 2) {
  4803. cmnd->result = DID_ERROR << 16;
  4804. goto out_fail_command_release_buf;
  4805. }
  4806. goto out_host_busy_free_buf;
  4807. }
  4808. /* check the necessary and sufficient condition to support VMID */
  4809. if (lpfc_is_vmid_enabled(phba) &&
  4810. (ndlp->vmid_support ||
  4811. phba->pport->vmid_priority_tagging ==
  4812. LPFC_VMID_PRIO_TAG_ALL_TARGETS)) {
  4813. /* is the I/O generated by a VM, get the associated virtual */
  4814. /* entity id */
  4815. uuid = lpfc_is_command_vm_io(cmnd);
  4816. if (uuid) {
  4817. err = lpfc_vmid_get_appid(vport, uuid,
  4818. cmnd->sc_data_direction,
  4819. (union lpfc_vmid_io_tag *)
  4820. &cur_iocbq->vmid_tag);
  4821. if (!err)
  4822. cur_iocbq->cmd_flag |= LPFC_IO_VMID;
  4823. }
  4824. }
  4825. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  4826. if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
  4827. this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
  4828. #endif
  4829. /* Issue I/O to adapter */
  4830. err = lpfc_sli_issue_fcp_io(phba, LPFC_FCP_RING, cur_iocbq,
  4831. SLI_IOCB_RET_IOCB);
  4832. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  4833. if (start) {
  4834. lpfc_cmd->ts_cmd_start = start;
  4835. lpfc_cmd->ts_last_cmd = phba->ktime_last_cmd;
  4836. lpfc_cmd->ts_cmd_wqput = ktime_get_ns();
  4837. } else {
  4838. lpfc_cmd->ts_cmd_start = 0;
  4839. }
  4840. #endif
  4841. if (err) {
  4842. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4843. "3376 FCP could not issue iocb err %x "
  4844. "FCP cmd x%x <%d/%llu> "
  4845. "sid: x%x did: x%x oxid: x%x "
  4846. "Data: x%x x%x x%x x%x\n",
  4847. err, cmnd->cmnd[0],
  4848. cmnd->device ? cmnd->device->id : 0xffff,
  4849. cmnd->device ? cmnd->device->lun : (u64)-1,
  4850. vport->fc_myDID, ndlp->nlp_DID,
  4851. phba->sli_rev == LPFC_SLI_REV4 ?
  4852. cur_iocbq->sli4_xritag : 0xffff,
  4853. phba->sli_rev == LPFC_SLI_REV4 ?
  4854. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi] :
  4855. cur_iocbq->iocb.ulpContext,
  4856. cur_iocbq->iotag,
  4857. phba->sli_rev == LPFC_SLI_REV4 ?
  4858. bf_get(wqe_tmo,
  4859. &cur_iocbq->wqe.generic.wqe_com) :
  4860. cur_iocbq->iocb.ulpTimeout,
  4861. (uint32_t)(scsi_cmd_to_rq(cmnd)->timeout / 1000));
  4862. goto out_host_busy_free_buf;
  4863. }
  4864. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  4865. lpfc_sli_handle_fast_ring_event(phba,
  4866. &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
  4867. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  4868. lpfc_poll_rearm_timer(phba);
  4869. }
  4870. if (phba->cfg_xri_rebalancing)
  4871. lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
  4872. return 0;
  4873. out_host_busy_free_buf:
  4874. idx = lpfc_cmd->hdwq_no;
  4875. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  4876. if (phba->sli4_hba.hdwq) {
  4877. switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
  4878. case WRITE_DATA:
  4879. phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
  4880. break;
  4881. case READ_DATA:
  4882. phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
  4883. break;
  4884. default:
  4885. phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
  4886. }
  4887. }
  4888. out_host_busy_release_buf:
  4889. lpfc_release_scsi_buf(phba, lpfc_cmd);
  4890. out_host_busy:
  4891. lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
  4892. shost);
  4893. return SCSI_MLQUEUE_HOST_BUSY;
  4894. out_tgt_busy2:
  4895. lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
  4896. shost);
  4897. out_tgt_busy1:
  4898. return SCSI_MLQUEUE_TARGET_BUSY;
  4899. out_fail_command_release_buf:
  4900. lpfc_release_scsi_buf(phba, lpfc_cmd);
  4901. lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
  4902. shost);
  4903. out_fail_command:
  4904. scsi_done(cmnd);
  4905. return 0;
  4906. }
  4907. /*
  4908. * lpfc_vmid_vport_cleanup - cleans up the resources associated with a vport
  4909. * @vport: The virtual port for which this call is being executed.
  4910. */
  4911. void lpfc_vmid_vport_cleanup(struct lpfc_vport *vport)
  4912. {
  4913. u32 bucket;
  4914. struct lpfc_vmid *cur;
  4915. if (vport->port_type == LPFC_PHYSICAL_PORT)
  4916. del_timer_sync(&vport->phba->inactive_vmid_poll);
  4917. kfree(vport->qfpa_res);
  4918. kfree(vport->vmid_priority.vmid_range);
  4919. kfree(vport->vmid);
  4920. if (!hash_empty(vport->hash_table))
  4921. hash_for_each(vport->hash_table, bucket, cur, hnode)
  4922. hash_del(&cur->hnode);
  4923. vport->qfpa_res = NULL;
  4924. vport->vmid_priority.vmid_range = NULL;
  4925. vport->vmid = NULL;
  4926. vport->cur_vmid_cnt = 0;
  4927. }
  4928. /**
  4929. * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
  4930. * @cmnd: Pointer to scsi_cmnd data structure.
  4931. *
  4932. * This routine aborts @cmnd pending in base driver.
  4933. *
  4934. * Return code :
  4935. * 0x2003 - Error
  4936. * 0x2002 - Success
  4937. **/
  4938. static int
  4939. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  4940. {
  4941. struct Scsi_Host *shost = cmnd->device->host;
  4942. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  4943. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4944. struct lpfc_hba *phba = vport->phba;
  4945. struct lpfc_iocbq *iocb;
  4946. struct lpfc_io_buf *lpfc_cmd;
  4947. int ret = SUCCESS, status = 0;
  4948. struct lpfc_sli_ring *pring_s4 = NULL;
  4949. struct lpfc_sli_ring *pring = NULL;
  4950. int ret_val;
  4951. unsigned long flags;
  4952. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  4953. status = fc_block_rport(rport);
  4954. if (status != 0 && status != SUCCESS)
  4955. return status;
  4956. lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
  4957. if (!lpfc_cmd)
  4958. return ret;
  4959. /* Guard against IO completion being called at same time */
  4960. spin_lock_irqsave(&lpfc_cmd->buf_lock, flags);
  4961. spin_lock(&phba->hbalock);
  4962. /* driver queued commands are in process of being flushed */
  4963. if (phba->hba_flag & HBA_IOQ_FLUSH) {
  4964. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4965. "3168 SCSI Layer abort requested I/O has been "
  4966. "flushed by LLD.\n");
  4967. ret = FAILED;
  4968. goto out_unlock_hba;
  4969. }
  4970. if (!lpfc_cmd->pCmd) {
  4971. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4972. "2873 SCSI Layer I/O Abort Request IO CMPL Status "
  4973. "x%x ID %d LUN %llu\n",
  4974. SUCCESS, cmnd->device->id, cmnd->device->lun);
  4975. goto out_unlock_hba;
  4976. }
  4977. iocb = &lpfc_cmd->cur_iocbq;
  4978. if (phba->sli_rev == LPFC_SLI_REV4) {
  4979. pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
  4980. if (!pring_s4) {
  4981. ret = FAILED;
  4982. goto out_unlock_hba;
  4983. }
  4984. spin_lock(&pring_s4->ring_lock);
  4985. }
  4986. /* the command is in process of being cancelled */
  4987. if (!(iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
  4988. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4989. "3169 SCSI Layer abort requested I/O has been "
  4990. "cancelled by LLD.\n");
  4991. ret = FAILED;
  4992. goto out_unlock_ring;
  4993. }
  4994. /*
  4995. * If pCmd field of the corresponding lpfc_io_buf structure
  4996. * points to a different SCSI command, then the driver has
  4997. * already completed this command, but the midlayer did not
  4998. * see the completion before the eh fired. Just return SUCCESS.
  4999. */
  5000. if (lpfc_cmd->pCmd != cmnd) {
  5001. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  5002. "3170 SCSI Layer abort requested I/O has been "
  5003. "completed by LLD.\n");
  5004. goto out_unlock_ring;
  5005. }
  5006. WARN_ON(iocb->io_buf != lpfc_cmd);
  5007. /* abort issued in recovery is still in progress */
  5008. if (iocb->cmd_flag & LPFC_DRIVER_ABORTED) {
  5009. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  5010. "3389 SCSI Layer I/O Abort Request is pending\n");
  5011. if (phba->sli_rev == LPFC_SLI_REV4)
  5012. spin_unlock(&pring_s4->ring_lock);
  5013. spin_unlock(&phba->hbalock);
  5014. spin_unlock_irqrestore(&lpfc_cmd->buf_lock, flags);
  5015. goto wait_for_cmpl;
  5016. }
  5017. lpfc_cmd->waitq = &waitq;
  5018. if (phba->sli_rev == LPFC_SLI_REV4) {
  5019. spin_unlock(&pring_s4->ring_lock);
  5020. ret_val = lpfc_sli4_issue_abort_iotag(phba, iocb,
  5021. lpfc_sli_abort_fcp_cmpl);
  5022. } else {
  5023. pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
  5024. ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocb,
  5025. lpfc_sli_abort_fcp_cmpl);
  5026. }
  5027. /* Make sure HBA is alive */
  5028. lpfc_issue_hb_tmo(phba);
  5029. if (ret_val != IOCB_SUCCESS) {
  5030. /* Indicate the IO is not being aborted by the driver. */
  5031. lpfc_cmd->waitq = NULL;
  5032. ret = FAILED;
  5033. goto out_unlock_hba;
  5034. }
  5035. /* no longer need the lock after this point */
  5036. spin_unlock(&phba->hbalock);
  5037. spin_unlock_irqrestore(&lpfc_cmd->buf_lock, flags);
  5038. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  5039. lpfc_sli_handle_fast_ring_event(phba,
  5040. &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
  5041. wait_for_cmpl:
  5042. /*
  5043. * cmd_flag is set to LPFC_DRIVER_ABORTED before we wait
  5044. * for abort to complete.
  5045. */
  5046. wait_event_timeout(waitq,
  5047. (lpfc_cmd->pCmd != cmnd),
  5048. msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
  5049. spin_lock(&lpfc_cmd->buf_lock);
  5050. if (lpfc_cmd->pCmd == cmnd) {
  5051. ret = FAILED;
  5052. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5053. "0748 abort handler timed out waiting "
  5054. "for aborting I/O (xri:x%x) to complete: "
  5055. "ret %#x, ID %d, LUN %llu\n",
  5056. iocb->sli4_xritag, ret,
  5057. cmnd->device->id, cmnd->device->lun);
  5058. }
  5059. lpfc_cmd->waitq = NULL;
  5060. spin_unlock(&lpfc_cmd->buf_lock);
  5061. goto out;
  5062. out_unlock_ring:
  5063. if (phba->sli_rev == LPFC_SLI_REV4)
  5064. spin_unlock(&pring_s4->ring_lock);
  5065. out_unlock_hba:
  5066. spin_unlock(&phba->hbalock);
  5067. spin_unlock_irqrestore(&lpfc_cmd->buf_lock, flags);
  5068. out:
  5069. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  5070. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  5071. "LUN %llu\n", ret, cmnd->device->id,
  5072. cmnd->device->lun);
  5073. return ret;
  5074. }
  5075. static char *
  5076. lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
  5077. {
  5078. switch (task_mgmt_cmd) {
  5079. case FCP_ABORT_TASK_SET:
  5080. return "ABORT_TASK_SET";
  5081. case FCP_CLEAR_TASK_SET:
  5082. return "FCP_CLEAR_TASK_SET";
  5083. case FCP_BUS_RESET:
  5084. return "FCP_BUS_RESET";
  5085. case FCP_LUN_RESET:
  5086. return "FCP_LUN_RESET";
  5087. case FCP_TARGET_RESET:
  5088. return "FCP_TARGET_RESET";
  5089. case FCP_CLEAR_ACA:
  5090. return "FCP_CLEAR_ACA";
  5091. case FCP_TERMINATE_TASK:
  5092. return "FCP_TERMINATE_TASK";
  5093. default:
  5094. return "unknown";
  5095. }
  5096. }
  5097. /**
  5098. * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
  5099. * @vport: The virtual port for which this call is being executed.
  5100. * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
  5101. *
  5102. * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
  5103. *
  5104. * Return code :
  5105. * 0x2003 - Error
  5106. * 0x2002 - Success
  5107. **/
  5108. static int
  5109. lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
  5110. {
  5111. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  5112. uint32_t rsp_info;
  5113. uint32_t rsp_len;
  5114. uint8_t rsp_info_code;
  5115. int ret = FAILED;
  5116. if (fcprsp == NULL)
  5117. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  5118. "0703 fcp_rsp is missing\n");
  5119. else {
  5120. rsp_info = fcprsp->rspStatus2;
  5121. rsp_len = be32_to_cpu(fcprsp->rspRspLen);
  5122. rsp_info_code = fcprsp->rspInfo3;
  5123. lpfc_printf_vlog(vport, KERN_INFO,
  5124. LOG_FCP,
  5125. "0706 fcp_rsp valid 0x%x,"
  5126. " rsp len=%d code 0x%x\n",
  5127. rsp_info,
  5128. rsp_len, rsp_info_code);
  5129. /* If FCP_RSP_LEN_VALID bit is one, then the FCP_RSP_LEN
  5130. * field specifies the number of valid bytes of FCP_RSP_INFO.
  5131. * The FCP_RSP_LEN field shall be set to 0x04 or 0x08
  5132. */
  5133. if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
  5134. ((rsp_len == 8) || (rsp_len == 4))) {
  5135. switch (rsp_info_code) {
  5136. case RSP_NO_FAILURE:
  5137. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  5138. "0715 Task Mgmt No Failure\n");
  5139. ret = SUCCESS;
  5140. break;
  5141. case RSP_TM_NOT_SUPPORTED: /* TM rejected */
  5142. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  5143. "0716 Task Mgmt Target "
  5144. "reject\n");
  5145. break;
  5146. case RSP_TM_NOT_COMPLETED: /* TM failed */
  5147. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  5148. "0717 Task Mgmt Target "
  5149. "failed TM\n");
  5150. break;
  5151. case RSP_TM_INVALID_LU: /* TM to invalid LU! */
  5152. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  5153. "0718 Task Mgmt to invalid "
  5154. "LUN\n");
  5155. break;
  5156. }
  5157. }
  5158. }
  5159. return ret;
  5160. }
  5161. /**
  5162. * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
  5163. * @vport: The virtual port for which this call is being executed.
  5164. * @rport: Pointer to remote port
  5165. * @tgt_id: Target ID of remote device.
  5166. * @lun_id: Lun number for the TMF
  5167. * @task_mgmt_cmd: type of TMF to send
  5168. *
  5169. * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
  5170. * a remote port.
  5171. *
  5172. * Return Code:
  5173. * 0x2003 - Error
  5174. * 0x2002 - Success.
  5175. **/
  5176. static int
  5177. lpfc_send_taskmgmt(struct lpfc_vport *vport, struct fc_rport *rport,
  5178. unsigned int tgt_id, uint64_t lun_id,
  5179. uint8_t task_mgmt_cmd)
  5180. {
  5181. struct lpfc_hba *phba = vport->phba;
  5182. struct lpfc_io_buf *lpfc_cmd;
  5183. struct lpfc_iocbq *iocbq;
  5184. struct lpfc_iocbq *iocbqrsp;
  5185. struct lpfc_rport_data *rdata;
  5186. struct lpfc_nodelist *pnode;
  5187. int ret;
  5188. int status;
  5189. rdata = rport->dd_data;
  5190. if (!rdata || !rdata->pnode)
  5191. return FAILED;
  5192. pnode = rdata->pnode;
  5193. lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode, NULL);
  5194. if (lpfc_cmd == NULL)
  5195. return FAILED;
  5196. lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
  5197. lpfc_cmd->rdata = rdata;
  5198. lpfc_cmd->pCmd = NULL;
  5199. lpfc_cmd->ndlp = pnode;
  5200. status = phba->lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
  5201. task_mgmt_cmd);
  5202. if (!status) {
  5203. lpfc_release_scsi_buf(phba, lpfc_cmd);
  5204. return FAILED;
  5205. }
  5206. iocbq = &lpfc_cmd->cur_iocbq;
  5207. iocbqrsp = lpfc_sli_get_iocbq(phba);
  5208. if (iocbqrsp == NULL) {
  5209. lpfc_release_scsi_buf(phba, lpfc_cmd);
  5210. return FAILED;
  5211. }
  5212. iocbq->cmd_cmpl = lpfc_tskmgmt_def_cmpl;
  5213. iocbq->vport = vport;
  5214. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  5215. "0702 Issue %s to TGT %d LUN %llu "
  5216. "rpi x%x nlp_flag x%x Data: x%x x%x\n",
  5217. lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
  5218. pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
  5219. iocbq->cmd_flag);
  5220. status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
  5221. iocbq, iocbqrsp, lpfc_cmd->timeout);
  5222. if ((status != IOCB_SUCCESS) ||
  5223. (get_job_ulpstatus(phba, iocbqrsp) != IOSTAT_SUCCESS)) {
  5224. if (status != IOCB_SUCCESS ||
  5225. get_job_ulpstatus(phba, iocbqrsp) != IOSTAT_FCP_RSP_ERROR)
  5226. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5227. "0727 TMF %s to TGT %d LUN %llu "
  5228. "failed (%d, %d) cmd_flag x%x\n",
  5229. lpfc_taskmgmt_name(task_mgmt_cmd),
  5230. tgt_id, lun_id,
  5231. get_job_ulpstatus(phba, iocbqrsp),
  5232. get_job_word4(phba, iocbqrsp),
  5233. iocbq->cmd_flag);
  5234. /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
  5235. if (status == IOCB_SUCCESS) {
  5236. if (get_job_ulpstatus(phba, iocbqrsp) ==
  5237. IOSTAT_FCP_RSP_ERROR)
  5238. /* Something in the FCP_RSP was invalid.
  5239. * Check conditions */
  5240. ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
  5241. else
  5242. ret = FAILED;
  5243. } else if ((status == IOCB_TIMEDOUT) ||
  5244. (status == IOCB_ABORTED)) {
  5245. ret = TIMEOUT_ERROR;
  5246. } else {
  5247. ret = FAILED;
  5248. }
  5249. } else
  5250. ret = SUCCESS;
  5251. lpfc_sli_release_iocbq(phba, iocbqrsp);
  5252. if (status != IOCB_TIMEDOUT)
  5253. lpfc_release_scsi_buf(phba, lpfc_cmd);
  5254. return ret;
  5255. }
  5256. /**
  5257. * lpfc_chk_tgt_mapped -
  5258. * @vport: The virtual port to check on
  5259. * @rport: Pointer to fc_rport data structure.
  5260. *
  5261. * This routine delays until the scsi target (aka rport) for the
  5262. * command exists (is present and logged in) or we declare it non-existent.
  5263. *
  5264. * Return code :
  5265. * 0x2003 - Error
  5266. * 0x2002 - Success
  5267. **/
  5268. static int
  5269. lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct fc_rport *rport)
  5270. {
  5271. struct lpfc_rport_data *rdata;
  5272. struct lpfc_nodelist *pnode = NULL;
  5273. unsigned long later;
  5274. rdata = rport->dd_data;
  5275. if (!rdata) {
  5276. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  5277. "0797 Tgt Map rport failure: rdata x%px\n", rdata);
  5278. return FAILED;
  5279. }
  5280. pnode = rdata->pnode;
  5281. /*
  5282. * If target is not in a MAPPED state, delay until
  5283. * target is rediscovered or devloss timeout expires.
  5284. */
  5285. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  5286. while (time_after(later, jiffies)) {
  5287. if (!pnode)
  5288. return FAILED;
  5289. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  5290. return SUCCESS;
  5291. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  5292. rdata = rport->dd_data;
  5293. if (!rdata)
  5294. return FAILED;
  5295. pnode = rdata->pnode;
  5296. }
  5297. if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  5298. return FAILED;
  5299. return SUCCESS;
  5300. }
  5301. /**
  5302. * lpfc_reset_flush_io_context -
  5303. * @vport: The virtual port (scsi_host) for the flush context
  5304. * @tgt_id: If aborting by Target contect - specifies the target id
  5305. * @lun_id: If aborting by Lun context - specifies the lun id
  5306. * @context: specifies the context level to flush at.
  5307. *
  5308. * After a reset condition via TMF, we need to flush orphaned i/o
  5309. * contexts from the adapter. This routine aborts any contexts
  5310. * outstanding, then waits for their completions. The wait is
  5311. * bounded by devloss_tmo though.
  5312. *
  5313. * Return code :
  5314. * 0x2003 - Error
  5315. * 0x2002 - Success
  5316. **/
  5317. static int
  5318. lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
  5319. uint64_t lun_id, lpfc_ctx_cmd context)
  5320. {
  5321. struct lpfc_hba *phba = vport->phba;
  5322. unsigned long later;
  5323. int cnt;
  5324. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  5325. if (cnt)
  5326. lpfc_sli_abort_taskmgmt(vport,
  5327. &phba->sli.sli3_ring[LPFC_FCP_RING],
  5328. tgt_id, lun_id, context);
  5329. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  5330. while (time_after(later, jiffies) && cnt) {
  5331. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  5332. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  5333. }
  5334. if (cnt) {
  5335. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5336. "0724 I/O flush failure for context %s : cnt x%x\n",
  5337. ((context == LPFC_CTX_LUN) ? "LUN" :
  5338. ((context == LPFC_CTX_TGT) ? "TGT" :
  5339. ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
  5340. cnt);
  5341. return FAILED;
  5342. }
  5343. return SUCCESS;
  5344. }
  5345. /**
  5346. * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
  5347. * @cmnd: Pointer to scsi_cmnd data structure.
  5348. *
  5349. * This routine does a device reset by sending a LUN_RESET task management
  5350. * command.
  5351. *
  5352. * Return code :
  5353. * 0x2003 - Error
  5354. * 0x2002 - Success
  5355. **/
  5356. static int
  5357. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  5358. {
  5359. struct Scsi_Host *shost = cmnd->device->host;
  5360. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  5361. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  5362. struct lpfc_rport_data *rdata;
  5363. struct lpfc_nodelist *pnode;
  5364. unsigned tgt_id = cmnd->device->id;
  5365. uint64_t lun_id = cmnd->device->lun;
  5366. struct lpfc_scsi_event_header scsi_event;
  5367. int status;
  5368. u32 logit = LOG_FCP;
  5369. if (!rport)
  5370. return FAILED;
  5371. rdata = rport->dd_data;
  5372. if (!rdata || !rdata->pnode) {
  5373. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5374. "0798 Device Reset rdata failure: rdata x%px\n",
  5375. rdata);
  5376. return FAILED;
  5377. }
  5378. pnode = rdata->pnode;
  5379. status = fc_block_rport(rport);
  5380. if (status != 0 && status != SUCCESS)
  5381. return status;
  5382. status = lpfc_chk_tgt_mapped(vport, rport);
  5383. if (status == FAILED) {
  5384. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5385. "0721 Device Reset rport failure: rdata x%px\n", rdata);
  5386. return FAILED;
  5387. }
  5388. scsi_event.event_type = FC_REG_SCSI_EVENT;
  5389. scsi_event.subcategory = LPFC_EVENT_LUNRESET;
  5390. scsi_event.lun = lun_id;
  5391. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  5392. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  5393. fc_host_post_vendor_event(shost, fc_get_event_number(),
  5394. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  5395. status = lpfc_send_taskmgmt(vport, rport, tgt_id, lun_id,
  5396. FCP_LUN_RESET);
  5397. if (status != SUCCESS)
  5398. logit = LOG_TRACE_EVENT;
  5399. lpfc_printf_vlog(vport, KERN_ERR, logit,
  5400. "0713 SCSI layer issued Device Reset (%d, %llu) "
  5401. "return x%x\n", tgt_id, lun_id, status);
  5402. /*
  5403. * We have to clean up i/o as : they may be orphaned by the TMF;
  5404. * or if the TMF failed, they may be in an indeterminate state.
  5405. * So, continue on.
  5406. * We will report success if all the i/o aborts successfully.
  5407. */
  5408. if (status == SUCCESS)
  5409. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  5410. LPFC_CTX_LUN);
  5411. return status;
  5412. }
  5413. /**
  5414. * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
  5415. * @cmnd: Pointer to scsi_cmnd data structure.
  5416. *
  5417. * This routine does a target reset by sending a TARGET_RESET task management
  5418. * command.
  5419. *
  5420. * Return code :
  5421. * 0x2003 - Error
  5422. * 0x2002 - Success
  5423. **/
  5424. static int
  5425. lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
  5426. {
  5427. struct Scsi_Host *shost = cmnd->device->host;
  5428. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  5429. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  5430. struct lpfc_rport_data *rdata;
  5431. struct lpfc_nodelist *pnode;
  5432. unsigned tgt_id = cmnd->device->id;
  5433. uint64_t lun_id = cmnd->device->lun;
  5434. struct lpfc_scsi_event_header scsi_event;
  5435. int status;
  5436. u32 logit = LOG_FCP;
  5437. u32 dev_loss_tmo = vport->cfg_devloss_tmo;
  5438. unsigned long flags;
  5439. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  5440. if (!rport)
  5441. return FAILED;
  5442. rdata = rport->dd_data;
  5443. if (!rdata || !rdata->pnode) {
  5444. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5445. "0799 Target Reset rdata failure: rdata x%px\n",
  5446. rdata);
  5447. return FAILED;
  5448. }
  5449. pnode = rdata->pnode;
  5450. status = fc_block_rport(rport);
  5451. if (status != 0 && status != SUCCESS)
  5452. return status;
  5453. status = lpfc_chk_tgt_mapped(vport, rport);
  5454. if (status == FAILED) {
  5455. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5456. "0722 Target Reset rport failure: rdata x%px\n", rdata);
  5457. if (pnode) {
  5458. spin_lock_irqsave(&pnode->lock, flags);
  5459. pnode->nlp_flag &= ~NLP_NPR_ADISC;
  5460. pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  5461. spin_unlock_irqrestore(&pnode->lock, flags);
  5462. }
  5463. lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  5464. LPFC_CTX_TGT);
  5465. return FAST_IO_FAIL;
  5466. }
  5467. scsi_event.event_type = FC_REG_SCSI_EVENT;
  5468. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  5469. scsi_event.lun = 0;
  5470. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  5471. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  5472. fc_host_post_vendor_event(shost, fc_get_event_number(),
  5473. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  5474. status = lpfc_send_taskmgmt(vport, rport, tgt_id, lun_id,
  5475. FCP_TARGET_RESET);
  5476. if (status != SUCCESS) {
  5477. logit = LOG_TRACE_EVENT;
  5478. /* Issue LOGO, if no LOGO is outstanding */
  5479. spin_lock_irqsave(&pnode->lock, flags);
  5480. if (!(pnode->save_flags & NLP_WAIT_FOR_LOGO) &&
  5481. !pnode->logo_waitq) {
  5482. pnode->logo_waitq = &waitq;
  5483. pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  5484. pnode->nlp_flag |= NLP_ISSUE_LOGO;
  5485. pnode->save_flags |= NLP_WAIT_FOR_LOGO;
  5486. spin_unlock_irqrestore(&pnode->lock, flags);
  5487. lpfc_unreg_rpi(vport, pnode);
  5488. wait_event_timeout(waitq,
  5489. (!(pnode->save_flags &
  5490. NLP_WAIT_FOR_LOGO)),
  5491. msecs_to_jiffies(dev_loss_tmo *
  5492. 1000));
  5493. if (pnode->save_flags & NLP_WAIT_FOR_LOGO) {
  5494. lpfc_printf_vlog(vport, KERN_ERR, logit,
  5495. "0725 SCSI layer TGTRST "
  5496. "failed & LOGO TMO (%d, %llu) "
  5497. "return x%x\n",
  5498. tgt_id, lun_id, status);
  5499. spin_lock_irqsave(&pnode->lock, flags);
  5500. pnode->save_flags &= ~NLP_WAIT_FOR_LOGO;
  5501. } else {
  5502. spin_lock_irqsave(&pnode->lock, flags);
  5503. }
  5504. pnode->logo_waitq = NULL;
  5505. spin_unlock_irqrestore(&pnode->lock, flags);
  5506. status = SUCCESS;
  5507. } else {
  5508. spin_unlock_irqrestore(&pnode->lock, flags);
  5509. status = FAILED;
  5510. }
  5511. }
  5512. lpfc_printf_vlog(vport, KERN_ERR, logit,
  5513. "0723 SCSI layer issued Target Reset (%d, %llu) "
  5514. "return x%x\n", tgt_id, lun_id, status);
  5515. /*
  5516. * We have to clean up i/o as : they may be orphaned by the TMF;
  5517. * or if the TMF failed, they may be in an indeterminate state.
  5518. * So, continue on.
  5519. * We will report success if all the i/o aborts successfully.
  5520. */
  5521. if (status == SUCCESS)
  5522. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  5523. LPFC_CTX_TGT);
  5524. return status;
  5525. }
  5526. /**
  5527. * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
  5528. * @cmnd: Pointer to scsi_cmnd data structure.
  5529. *
  5530. * This routine does host reset to the adaptor port. It brings the HBA
  5531. * offline, performs a board restart, and then brings the board back online.
  5532. * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
  5533. * reject all outstanding SCSI commands to the host and error returned
  5534. * back to SCSI mid-level. As this will be SCSI mid-level's last resort
  5535. * of error handling, it will only return error if resetting of the adapter
  5536. * is not successful; in all other cases, will return success.
  5537. *
  5538. * Return code :
  5539. * 0x2003 - Error
  5540. * 0x2002 - Success
  5541. **/
  5542. static int
  5543. lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
  5544. {
  5545. struct Scsi_Host *shost = cmnd->device->host;
  5546. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  5547. struct lpfc_hba *phba = vport->phba;
  5548. int rc, ret = SUCCESS;
  5549. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  5550. "3172 SCSI layer issued Host Reset Data:\n");
  5551. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  5552. lpfc_offline(phba);
  5553. rc = lpfc_sli_brdrestart(phba);
  5554. if (rc)
  5555. goto error;
  5556. /* Wait for successful restart of adapter */
  5557. if (phba->sli_rev < LPFC_SLI_REV4) {
  5558. rc = lpfc_sli_chipset_init(phba);
  5559. if (rc)
  5560. goto error;
  5561. }
  5562. rc = lpfc_online(phba);
  5563. if (rc)
  5564. goto error;
  5565. lpfc_unblock_mgmt_io(phba);
  5566. return ret;
  5567. error:
  5568. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5569. "3323 Failed host reset\n");
  5570. lpfc_unblock_mgmt_io(phba);
  5571. return FAILED;
  5572. }
  5573. /**
  5574. * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
  5575. * @sdev: Pointer to scsi_device.
  5576. *
  5577. * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
  5578. * globally available list of scsi buffers. This routine also makes sure scsi
  5579. * buffer is not allocated more than HBA limit conveyed to midlayer. This list
  5580. * of scsi buffer exists for the lifetime of the driver.
  5581. *
  5582. * Return codes:
  5583. * non-0 - Error
  5584. * 0 - Success
  5585. **/
  5586. static int
  5587. lpfc_slave_alloc(struct scsi_device *sdev)
  5588. {
  5589. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  5590. struct lpfc_hba *phba = vport->phba;
  5591. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  5592. uint32_t total = 0;
  5593. uint32_t num_to_alloc = 0;
  5594. int num_allocated = 0;
  5595. uint32_t sdev_cnt;
  5596. struct lpfc_device_data *device_data;
  5597. unsigned long flags;
  5598. struct lpfc_name target_wwpn;
  5599. if (!rport || fc_remote_port_chkready(rport))
  5600. return -ENXIO;
  5601. if (phba->cfg_fof) {
  5602. /*
  5603. * Check to see if the device data structure for the lun
  5604. * exists. If not, create one.
  5605. */
  5606. u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
  5607. spin_lock_irqsave(&phba->devicelock, flags);
  5608. device_data = __lpfc_get_device_data(phba,
  5609. &phba->luns,
  5610. &vport->fc_portname,
  5611. &target_wwpn,
  5612. sdev->lun);
  5613. if (!device_data) {
  5614. spin_unlock_irqrestore(&phba->devicelock, flags);
  5615. device_data = lpfc_create_device_data(phba,
  5616. &vport->fc_portname,
  5617. &target_wwpn,
  5618. sdev->lun,
  5619. phba->cfg_XLanePriority,
  5620. true);
  5621. if (!device_data)
  5622. return -ENOMEM;
  5623. spin_lock_irqsave(&phba->devicelock, flags);
  5624. list_add_tail(&device_data->listentry, &phba->luns);
  5625. }
  5626. device_data->rport_data = rport->dd_data;
  5627. device_data->available = true;
  5628. spin_unlock_irqrestore(&phba->devicelock, flags);
  5629. sdev->hostdata = device_data;
  5630. } else {
  5631. sdev->hostdata = rport->dd_data;
  5632. }
  5633. sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
  5634. /* For SLI4, all IO buffers are pre-allocated */
  5635. if (phba->sli_rev == LPFC_SLI_REV4)
  5636. return 0;
  5637. /* This code path is now ONLY for SLI3 adapters */
  5638. /*
  5639. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  5640. * available list of scsi buffers. Don't allocate more than the
  5641. * HBA limit conveyed to the midlayer via the host structure. The
  5642. * formula accounts for the lun_queue_depth + error handlers + 1
  5643. * extra. This list of scsi bufs exists for the lifetime of the driver.
  5644. */
  5645. total = phba->total_scsi_bufs;
  5646. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  5647. /* If allocated buffers are enough do nothing */
  5648. if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
  5649. return 0;
  5650. /* Allow some exchanges to be available always to complete discovery */
  5651. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  5652. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  5653. "0704 At limitation of %d preallocated "
  5654. "command buffers\n", total);
  5655. return 0;
  5656. /* Allow some exchanges to be available always to complete discovery */
  5657. } else if (total + num_to_alloc >
  5658. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  5659. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  5660. "0705 Allocation request of %d "
  5661. "command buffers will exceed max of %d. "
  5662. "Reducing allocation request to %d.\n",
  5663. num_to_alloc, phba->cfg_hba_queue_depth,
  5664. (phba->cfg_hba_queue_depth - total));
  5665. num_to_alloc = phba->cfg_hba_queue_depth - total;
  5666. }
  5667. num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
  5668. if (num_to_alloc != num_allocated) {
  5669. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  5670. "0708 Allocation request of %d "
  5671. "command buffers did not succeed. "
  5672. "Allocated %d buffers.\n",
  5673. num_to_alloc, num_allocated);
  5674. }
  5675. if (num_allocated > 0)
  5676. phba->total_scsi_bufs += num_allocated;
  5677. return 0;
  5678. }
  5679. /**
  5680. * lpfc_slave_configure - scsi_host_template slave_configure entry point
  5681. * @sdev: Pointer to scsi_device.
  5682. *
  5683. * This routine configures following items
  5684. * - Tag command queuing support for @sdev if supported.
  5685. * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
  5686. *
  5687. * Return codes:
  5688. * 0 - Success
  5689. **/
  5690. static int
  5691. lpfc_slave_configure(struct scsi_device *sdev)
  5692. {
  5693. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  5694. struct lpfc_hba *phba = vport->phba;
  5695. scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
  5696. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  5697. lpfc_sli_handle_fast_ring_event(phba,
  5698. &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
  5699. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  5700. lpfc_poll_rearm_timer(phba);
  5701. }
  5702. return 0;
  5703. }
  5704. /**
  5705. * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
  5706. * @sdev: Pointer to scsi_device.
  5707. *
  5708. * This routine sets @sdev hostatdata filed to null.
  5709. **/
  5710. static void
  5711. lpfc_slave_destroy(struct scsi_device *sdev)
  5712. {
  5713. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  5714. struct lpfc_hba *phba = vport->phba;
  5715. unsigned long flags;
  5716. struct lpfc_device_data *device_data = sdev->hostdata;
  5717. atomic_dec(&phba->sdev_cnt);
  5718. if ((phba->cfg_fof) && (device_data)) {
  5719. spin_lock_irqsave(&phba->devicelock, flags);
  5720. device_data->available = false;
  5721. if (!device_data->oas_enabled)
  5722. lpfc_delete_device_data(phba, device_data);
  5723. spin_unlock_irqrestore(&phba->devicelock, flags);
  5724. }
  5725. sdev->hostdata = NULL;
  5726. return;
  5727. }
  5728. /**
  5729. * lpfc_create_device_data - creates and initializes device data structure for OAS
  5730. * @phba: Pointer to host bus adapter structure.
  5731. * @vport_wwpn: Pointer to vport's wwpn information
  5732. * @target_wwpn: Pointer to target's wwpn information
  5733. * @lun: Lun on target
  5734. * @pri: Priority
  5735. * @atomic_create: Flag to indicate if memory should be allocated using the
  5736. * GFP_ATOMIC flag or not.
  5737. *
  5738. * This routine creates a device data structure which will contain identifying
  5739. * information for the device (host wwpn, target wwpn, lun), state of OAS,
  5740. * whether or not the corresponding lun is available by the system,
  5741. * and pointer to the rport data.
  5742. *
  5743. * Return codes:
  5744. * NULL - Error
  5745. * Pointer to lpfc_device_data - Success
  5746. **/
  5747. struct lpfc_device_data*
  5748. lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
  5749. struct lpfc_name *target_wwpn, uint64_t lun,
  5750. uint32_t pri, bool atomic_create)
  5751. {
  5752. struct lpfc_device_data *lun_info;
  5753. int memory_flags;
  5754. if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
  5755. !(phba->cfg_fof))
  5756. return NULL;
  5757. /* Attempt to create the device data to contain lun info */
  5758. if (atomic_create)
  5759. memory_flags = GFP_ATOMIC;
  5760. else
  5761. memory_flags = GFP_KERNEL;
  5762. lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
  5763. if (!lun_info)
  5764. return NULL;
  5765. INIT_LIST_HEAD(&lun_info->listentry);
  5766. lun_info->rport_data = NULL;
  5767. memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
  5768. sizeof(struct lpfc_name));
  5769. memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
  5770. sizeof(struct lpfc_name));
  5771. lun_info->device_id.lun = lun;
  5772. lun_info->oas_enabled = false;
  5773. lun_info->priority = pri;
  5774. lun_info->available = false;
  5775. return lun_info;
  5776. }
  5777. /**
  5778. * lpfc_delete_device_data - frees a device data structure for OAS
  5779. * @phba: Pointer to host bus adapter structure.
  5780. * @lun_info: Pointer to device data structure to free.
  5781. *
  5782. * This routine frees the previously allocated device data structure passed.
  5783. *
  5784. **/
  5785. void
  5786. lpfc_delete_device_data(struct lpfc_hba *phba,
  5787. struct lpfc_device_data *lun_info)
  5788. {
  5789. if (unlikely(!phba) || !lun_info ||
  5790. !(phba->cfg_fof))
  5791. return;
  5792. if (!list_empty(&lun_info->listentry))
  5793. list_del(&lun_info->listentry);
  5794. mempool_free(lun_info, phba->device_data_mem_pool);
  5795. return;
  5796. }
  5797. /**
  5798. * __lpfc_get_device_data - returns the device data for the specified lun
  5799. * @phba: Pointer to host bus adapter structure.
  5800. * @list: Point to list to search.
  5801. * @vport_wwpn: Pointer to vport's wwpn information
  5802. * @target_wwpn: Pointer to target's wwpn information
  5803. * @lun: Lun on target
  5804. *
  5805. * This routine searches the list passed for the specified lun's device data.
  5806. * This function does not hold locks, it is the responsibility of the caller
  5807. * to ensure the proper lock is held before calling the function.
  5808. *
  5809. * Return codes:
  5810. * NULL - Error
  5811. * Pointer to lpfc_device_data - Success
  5812. **/
  5813. struct lpfc_device_data*
  5814. __lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
  5815. struct lpfc_name *vport_wwpn,
  5816. struct lpfc_name *target_wwpn, uint64_t lun)
  5817. {
  5818. struct lpfc_device_data *lun_info;
  5819. if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
  5820. !phba->cfg_fof)
  5821. return NULL;
  5822. /* Check to see if the lun is already enabled for OAS. */
  5823. list_for_each_entry(lun_info, list, listentry) {
  5824. if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
  5825. sizeof(struct lpfc_name)) == 0) &&
  5826. (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
  5827. sizeof(struct lpfc_name)) == 0) &&
  5828. (lun_info->device_id.lun == lun))
  5829. return lun_info;
  5830. }
  5831. return NULL;
  5832. }
  5833. /**
  5834. * lpfc_find_next_oas_lun - searches for the next oas lun
  5835. * @phba: Pointer to host bus adapter structure.
  5836. * @vport_wwpn: Pointer to vport's wwpn information
  5837. * @target_wwpn: Pointer to target's wwpn information
  5838. * @starting_lun: Pointer to the lun to start searching for
  5839. * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
  5840. * @found_target_wwpn: Pointer to the found lun's target wwpn information
  5841. * @found_lun: Pointer to the found lun.
  5842. * @found_lun_status: Pointer to status of the found lun.
  5843. * @found_lun_pri: Pointer to priority of the found lun.
  5844. *
  5845. * This routine searches the luns list for the specified lun
  5846. * or the first lun for the vport/target. If the vport wwpn contains
  5847. * a zero value then a specific vport is not specified. In this case
  5848. * any vport which contains the lun will be considered a match. If the
  5849. * target wwpn contains a zero value then a specific target is not specified.
  5850. * In this case any target which contains the lun will be considered a
  5851. * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
  5852. * are returned. The function will also return the next lun if available.
  5853. * If the next lun is not found, starting_lun parameter will be set to
  5854. * NO_MORE_OAS_LUN.
  5855. *
  5856. * Return codes:
  5857. * non-0 - Error
  5858. * 0 - Success
  5859. **/
  5860. bool
  5861. lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
  5862. struct lpfc_name *target_wwpn, uint64_t *starting_lun,
  5863. struct lpfc_name *found_vport_wwpn,
  5864. struct lpfc_name *found_target_wwpn,
  5865. uint64_t *found_lun,
  5866. uint32_t *found_lun_status,
  5867. uint32_t *found_lun_pri)
  5868. {
  5869. unsigned long flags;
  5870. struct lpfc_device_data *lun_info;
  5871. struct lpfc_device_id *device_id;
  5872. uint64_t lun;
  5873. bool found = false;
  5874. if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
  5875. !starting_lun || !found_vport_wwpn ||
  5876. !found_target_wwpn || !found_lun || !found_lun_status ||
  5877. (*starting_lun == NO_MORE_OAS_LUN) ||
  5878. !phba->cfg_fof)
  5879. return false;
  5880. lun = *starting_lun;
  5881. *found_lun = NO_MORE_OAS_LUN;
  5882. *starting_lun = NO_MORE_OAS_LUN;
  5883. /* Search for lun or the lun closet in value */
  5884. spin_lock_irqsave(&phba->devicelock, flags);
  5885. list_for_each_entry(lun_info, &phba->luns, listentry) {
  5886. if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
  5887. (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
  5888. sizeof(struct lpfc_name)) == 0)) &&
  5889. ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
  5890. (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
  5891. sizeof(struct lpfc_name)) == 0)) &&
  5892. (lun_info->oas_enabled)) {
  5893. device_id = &lun_info->device_id;
  5894. if ((!found) &&
  5895. ((lun == FIND_FIRST_OAS_LUN) ||
  5896. (device_id->lun == lun))) {
  5897. *found_lun = device_id->lun;
  5898. memcpy(found_vport_wwpn,
  5899. &device_id->vport_wwpn,
  5900. sizeof(struct lpfc_name));
  5901. memcpy(found_target_wwpn,
  5902. &device_id->target_wwpn,
  5903. sizeof(struct lpfc_name));
  5904. if (lun_info->available)
  5905. *found_lun_status =
  5906. OAS_LUN_STATUS_EXISTS;
  5907. else
  5908. *found_lun_status = 0;
  5909. *found_lun_pri = lun_info->priority;
  5910. if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
  5911. memset(vport_wwpn, 0x0,
  5912. sizeof(struct lpfc_name));
  5913. if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
  5914. memset(target_wwpn, 0x0,
  5915. sizeof(struct lpfc_name));
  5916. found = true;
  5917. } else if (found) {
  5918. *starting_lun = device_id->lun;
  5919. memcpy(vport_wwpn, &device_id->vport_wwpn,
  5920. sizeof(struct lpfc_name));
  5921. memcpy(target_wwpn, &device_id->target_wwpn,
  5922. sizeof(struct lpfc_name));
  5923. break;
  5924. }
  5925. }
  5926. }
  5927. spin_unlock_irqrestore(&phba->devicelock, flags);
  5928. return found;
  5929. }
  5930. /**
  5931. * lpfc_enable_oas_lun - enables a lun for OAS operations
  5932. * @phba: Pointer to host bus adapter structure.
  5933. * @vport_wwpn: Pointer to vport's wwpn information
  5934. * @target_wwpn: Pointer to target's wwpn information
  5935. * @lun: Lun
  5936. * @pri: Priority
  5937. *
  5938. * This routine enables a lun for oas operations. The routines does so by
  5939. * doing the following :
  5940. *
  5941. * 1) Checks to see if the device data for the lun has been created.
  5942. * 2) If found, sets the OAS enabled flag if not set and returns.
  5943. * 3) Otherwise, creates a device data structure.
  5944. * 4) If successfully created, indicates the device data is for an OAS lun,
  5945. * indicates the lun is not available and add to the list of luns.
  5946. *
  5947. * Return codes:
  5948. * false - Error
  5949. * true - Success
  5950. **/
  5951. bool
  5952. lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
  5953. struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
  5954. {
  5955. struct lpfc_device_data *lun_info;
  5956. unsigned long flags;
  5957. if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
  5958. !phba->cfg_fof)
  5959. return false;
  5960. spin_lock_irqsave(&phba->devicelock, flags);
  5961. /* Check to see if the device data for the lun has been created */
  5962. lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
  5963. target_wwpn, lun);
  5964. if (lun_info) {
  5965. if (!lun_info->oas_enabled)
  5966. lun_info->oas_enabled = true;
  5967. lun_info->priority = pri;
  5968. spin_unlock_irqrestore(&phba->devicelock, flags);
  5969. return true;
  5970. }
  5971. /* Create an lun info structure and add to list of luns */
  5972. lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
  5973. pri, true);
  5974. if (lun_info) {
  5975. lun_info->oas_enabled = true;
  5976. lun_info->priority = pri;
  5977. lun_info->available = false;
  5978. list_add_tail(&lun_info->listentry, &phba->luns);
  5979. spin_unlock_irqrestore(&phba->devicelock, flags);
  5980. return true;
  5981. }
  5982. spin_unlock_irqrestore(&phba->devicelock, flags);
  5983. return false;
  5984. }
  5985. /**
  5986. * lpfc_disable_oas_lun - disables a lun for OAS operations
  5987. * @phba: Pointer to host bus adapter structure.
  5988. * @vport_wwpn: Pointer to vport's wwpn information
  5989. * @target_wwpn: Pointer to target's wwpn information
  5990. * @lun: Lun
  5991. * @pri: Priority
  5992. *
  5993. * This routine disables a lun for oas operations. The routines does so by
  5994. * doing the following :
  5995. *
  5996. * 1) Checks to see if the device data for the lun is created.
  5997. * 2) If present, clears the flag indicating this lun is for OAS.
  5998. * 3) If the lun is not available by the system, the device data is
  5999. * freed.
  6000. *
  6001. * Return codes:
  6002. * false - Error
  6003. * true - Success
  6004. **/
  6005. bool
  6006. lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
  6007. struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
  6008. {
  6009. struct lpfc_device_data *lun_info;
  6010. unsigned long flags;
  6011. if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
  6012. !phba->cfg_fof)
  6013. return false;
  6014. spin_lock_irqsave(&phba->devicelock, flags);
  6015. /* Check to see if the lun is available. */
  6016. lun_info = __lpfc_get_device_data(phba,
  6017. &phba->luns, vport_wwpn,
  6018. target_wwpn, lun);
  6019. if (lun_info) {
  6020. lun_info->oas_enabled = false;
  6021. lun_info->priority = pri;
  6022. if (!lun_info->available)
  6023. lpfc_delete_device_data(phba, lun_info);
  6024. spin_unlock_irqrestore(&phba->devicelock, flags);
  6025. return true;
  6026. }
  6027. spin_unlock_irqrestore(&phba->devicelock, flags);
  6028. return false;
  6029. }
  6030. static int
  6031. lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
  6032. {
  6033. return SCSI_MLQUEUE_HOST_BUSY;
  6034. }
  6035. static int
  6036. lpfc_no_slave(struct scsi_device *sdev)
  6037. {
  6038. return -ENODEV;
  6039. }
  6040. struct scsi_host_template lpfc_template_nvme = {
  6041. .module = THIS_MODULE,
  6042. .name = LPFC_DRIVER_NAME,
  6043. .proc_name = LPFC_DRIVER_NAME,
  6044. .info = lpfc_info,
  6045. .queuecommand = lpfc_no_command,
  6046. .slave_alloc = lpfc_no_slave,
  6047. .slave_configure = lpfc_no_slave,
  6048. .scan_finished = lpfc_scan_finished,
  6049. .this_id = -1,
  6050. .sg_tablesize = 1,
  6051. .cmd_per_lun = 1,
  6052. .shost_groups = lpfc_hba_groups,
  6053. .max_sectors = 0xFFFFFFFF,
  6054. .vendor_id = LPFC_NL_VENDOR_ID,
  6055. .track_queue_depth = 0,
  6056. };
  6057. struct scsi_host_template lpfc_template = {
  6058. .module = THIS_MODULE,
  6059. .name = LPFC_DRIVER_NAME,
  6060. .proc_name = LPFC_DRIVER_NAME,
  6061. .info = lpfc_info,
  6062. .queuecommand = lpfc_queuecommand,
  6063. .eh_timed_out = fc_eh_timed_out,
  6064. .eh_should_retry_cmd = fc_eh_should_retry_cmd,
  6065. .eh_abort_handler = lpfc_abort_handler,
  6066. .eh_device_reset_handler = lpfc_device_reset_handler,
  6067. .eh_target_reset_handler = lpfc_target_reset_handler,
  6068. .eh_host_reset_handler = lpfc_host_reset_handler,
  6069. .slave_alloc = lpfc_slave_alloc,
  6070. .slave_configure = lpfc_slave_configure,
  6071. .slave_destroy = lpfc_slave_destroy,
  6072. .scan_finished = lpfc_scan_finished,
  6073. .this_id = -1,
  6074. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  6075. .cmd_per_lun = LPFC_CMD_PER_LUN,
  6076. .shost_groups = lpfc_hba_groups,
  6077. .max_sectors = 0xFFFFFFFF,
  6078. .vendor_id = LPFC_NL_VENDOR_ID,
  6079. .change_queue_depth = scsi_change_queue_depth,
  6080. .track_queue_depth = 1,
  6081. };
  6082. struct scsi_host_template lpfc_vport_template = {
  6083. .module = THIS_MODULE,
  6084. .name = LPFC_DRIVER_NAME,
  6085. .proc_name = LPFC_DRIVER_NAME,
  6086. .info = lpfc_info,
  6087. .queuecommand = lpfc_queuecommand,
  6088. .eh_timed_out = fc_eh_timed_out,
  6089. .eh_should_retry_cmd = fc_eh_should_retry_cmd,
  6090. .eh_abort_handler = lpfc_abort_handler,
  6091. .eh_device_reset_handler = lpfc_device_reset_handler,
  6092. .eh_target_reset_handler = lpfc_target_reset_handler,
  6093. .eh_bus_reset_handler = NULL,
  6094. .eh_host_reset_handler = NULL,
  6095. .slave_alloc = lpfc_slave_alloc,
  6096. .slave_configure = lpfc_slave_configure,
  6097. .slave_destroy = lpfc_slave_destroy,
  6098. .scan_finished = lpfc_scan_finished,
  6099. .this_id = -1,
  6100. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  6101. .cmd_per_lun = LPFC_CMD_PER_LUN,
  6102. .shost_groups = lpfc_vport_groups,
  6103. .max_sectors = 0xFFFFFFFF,
  6104. .vendor_id = 0,
  6105. .change_queue_depth = scsi_change_queue_depth,
  6106. .track_queue_depth = 1,
  6107. };