lpfc_scsi.c 181 KB

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