lpfc_scsi.c 176 KB

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