lpfc_scsi.c 197 KB

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