lpfc_scsi.c 178 KB

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