qla_target.c 187 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987
  1. /*
  2. * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
  3. *
  4. * based on qla2x00t.c code:
  5. *
  6. * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
  7. * Copyright (C) 2004 - 2005 Leonid Stoljar
  8. * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
  9. * Copyright (C) 2006 - 2010 ID7 Ltd.
  10. *
  11. * Forward port and refactoring to modern qla2xxx and target/configfs
  12. *
  13. * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation, version 2
  18. * of the License.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/pci.h>
  31. #include <linux/delay.h>
  32. #include <linux/list.h>
  33. #include <linux/workqueue.h>
  34. #include <asm/unaligned.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_host.h>
  37. #include <scsi/scsi_tcq.h>
  38. #include <target/target_core_base.h>
  39. #include <target/target_core_fabric.h>
  40. #include "qla_def.h"
  41. #include "qla_target.h"
  42. static int ql2xtgt_tape_enable;
  43. module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
  44. MODULE_PARM_DESC(ql2xtgt_tape_enable,
  45. "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
  46. static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
  47. module_param(qlini_mode, charp, S_IRUGO);
  48. MODULE_PARM_DESC(qlini_mode,
  49. "Determines when initiator mode will be enabled. Possible values: "
  50. "\"exclusive\" - initiator mode will be enabled on load, "
  51. "disabled on enabling target mode and then on disabling target mode "
  52. "enabled back; "
  53. "\"disabled\" - initiator mode will never be enabled; "
  54. "\"enabled\" (default) - initiator mode will always stay enabled.");
  55. int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
  56. static int temp_sam_status = SAM_STAT_BUSY;
  57. /*
  58. * From scsi/fc/fc_fcp.h
  59. */
  60. enum fcp_resp_rsp_codes {
  61. FCP_TMF_CMPL = 0,
  62. FCP_DATA_LEN_INVALID = 1,
  63. FCP_CMND_FIELDS_INVALID = 2,
  64. FCP_DATA_PARAM_MISMATCH = 3,
  65. FCP_TMF_REJECTED = 4,
  66. FCP_TMF_FAILED = 5,
  67. FCP_TMF_INVALID_LUN = 9,
  68. };
  69. /*
  70. * fc_pri_ta from scsi/fc/fc_fcp.h
  71. */
  72. #define FCP_PTA_SIMPLE 0 /* simple task attribute */
  73. #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
  74. #define FCP_PTA_ORDERED 2 /* ordered task attribute */
  75. #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
  76. #define FCP_PTA_MASK 7 /* mask for task attribute field */
  77. #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
  78. #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
  79. /*
  80. * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
  81. * must be called under HW lock and could unlock/lock it inside.
  82. * It isn't an issue, since in the current implementation on the time when
  83. * those functions are called:
  84. *
  85. * - Either context is IRQ and only IRQ handler can modify HW data,
  86. * including rings related fields,
  87. *
  88. * - Or access to target mode variables from struct qla_tgt doesn't
  89. * cross those functions boundaries, except tgt_stop, which
  90. * additionally protected by irq_cmd_count.
  91. */
  92. /* Predefs for callbacks handed to qla2xxx LLD */
  93. static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
  94. struct atio_from_isp *pkt, uint8_t);
  95. static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
  96. static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
  97. int fn, void *iocb, int flags);
  98. static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
  99. *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
  100. static void qlt_reject_free_srr_imm(struct scsi_qla_host *ha,
  101. struct qla_tgt_srr_imm *imm, int ha_lock);
  102. static void qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha,
  103. struct qla_tgt_cmd *cmd);
  104. static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
  105. struct atio_from_isp *atio, uint16_t status, int qfull);
  106. static void qlt_disable_vha(struct scsi_qla_host *vha);
  107. static void qlt_clear_tgt_db(struct qla_tgt *tgt);
  108. static void qlt_send_notify_ack(struct scsi_qla_host *vha,
  109. struct imm_ntfy_from_isp *ntfy,
  110. uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
  111. uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
  112. static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
  113. struct imm_ntfy_from_isp *imm, int ha_locked);
  114. /*
  115. * Global Variables
  116. */
  117. static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
  118. static struct kmem_cache *qla_tgt_plogi_cachep;
  119. static mempool_t *qla_tgt_mgmt_cmd_mempool;
  120. static struct workqueue_struct *qla_tgt_wq;
  121. static DEFINE_MUTEX(qla_tgt_mutex);
  122. static LIST_HEAD(qla_tgt_glist);
  123. /* This API intentionally takes dest as a parameter, rather than returning
  124. * int value to avoid caller forgetting to issue wmb() after the store */
  125. void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
  126. {
  127. scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
  128. *dest = atomic_inc_return(&base_vha->generation_tick);
  129. /* memory barrier */
  130. wmb();
  131. }
  132. /* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */
  133. static struct qla_tgt_sess *qlt_find_sess_by_port_name(
  134. struct qla_tgt *tgt,
  135. const uint8_t *port_name)
  136. {
  137. struct qla_tgt_sess *sess;
  138. list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
  139. if (!memcmp(sess->port_name, port_name, WWN_SIZE))
  140. return sess;
  141. }
  142. return NULL;
  143. }
  144. /* Might release hw lock, then reaquire!! */
  145. static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
  146. {
  147. /* Send marker if required */
  148. if (unlikely(vha->marker_needed != 0)) {
  149. int rc = qla2x00_issue_marker(vha, vha_locked);
  150. if (rc != QLA_SUCCESS) {
  151. ql_dbg(ql_dbg_tgt, vha, 0xe03d,
  152. "qla_target(%d): issue_marker() failed\n",
  153. vha->vp_idx);
  154. }
  155. return rc;
  156. }
  157. return QLA_SUCCESS;
  158. }
  159. static inline
  160. struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
  161. uint8_t *d_id)
  162. {
  163. struct qla_hw_data *ha = vha->hw;
  164. uint8_t vp_idx;
  165. if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
  166. return NULL;
  167. if (vha->d_id.b.al_pa == d_id[2])
  168. return vha;
  169. BUG_ON(ha->tgt.tgt_vp_map == NULL);
  170. vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
  171. if (likely(test_bit(vp_idx, ha->vp_idx_map)))
  172. return ha->tgt.tgt_vp_map[vp_idx].vha;
  173. return NULL;
  174. }
  175. static inline
  176. struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
  177. uint16_t vp_idx)
  178. {
  179. struct qla_hw_data *ha = vha->hw;
  180. if (vha->vp_idx == vp_idx)
  181. return vha;
  182. BUG_ON(ha->tgt.tgt_vp_map == NULL);
  183. if (likely(test_bit(vp_idx, ha->vp_idx_map)))
  184. return ha->tgt.tgt_vp_map[vp_idx].vha;
  185. return NULL;
  186. }
  187. static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
  188. {
  189. unsigned long flags;
  190. spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
  191. vha->hw->tgt.num_pend_cmds++;
  192. if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
  193. vha->qla_stats.stat_max_pend_cmds =
  194. vha->hw->tgt.num_pend_cmds;
  195. spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
  196. }
  197. static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
  198. {
  199. unsigned long flags;
  200. spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
  201. vha->hw->tgt.num_pend_cmds--;
  202. spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
  203. }
  204. static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
  205. struct atio_from_isp *atio, uint8_t ha_locked)
  206. {
  207. ql_dbg(ql_dbg_tgt, vha, 0xe072,
  208. "%s: qla_target(%d): type %x ox_id %04x\n",
  209. __func__, vha->vp_idx, atio->u.raw.entry_type,
  210. be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
  211. switch (atio->u.raw.entry_type) {
  212. case ATIO_TYPE7:
  213. {
  214. struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
  215. atio->u.isp24.fcp_hdr.d_id);
  216. if (unlikely(NULL == host)) {
  217. ql_dbg(ql_dbg_tgt, vha, 0xe03e,
  218. "qla_target(%d): Received ATIO_TYPE7 "
  219. "with unknown d_id %x:%x:%x\n", vha->vp_idx,
  220. atio->u.isp24.fcp_hdr.d_id[0],
  221. atio->u.isp24.fcp_hdr.d_id[1],
  222. atio->u.isp24.fcp_hdr.d_id[2]);
  223. break;
  224. }
  225. qlt_24xx_atio_pkt(host, atio, ha_locked);
  226. break;
  227. }
  228. case IMMED_NOTIFY_TYPE:
  229. {
  230. struct scsi_qla_host *host = vha;
  231. struct imm_ntfy_from_isp *entry =
  232. (struct imm_ntfy_from_isp *)atio;
  233. if ((entry->u.isp24.vp_index != 0xFF) &&
  234. (entry->u.isp24.nport_handle != 0xFFFF)) {
  235. host = qlt_find_host_by_vp_idx(vha,
  236. entry->u.isp24.vp_index);
  237. if (unlikely(!host)) {
  238. ql_dbg(ql_dbg_tgt, vha, 0xe03f,
  239. "qla_target(%d): Received "
  240. "ATIO (IMMED_NOTIFY_TYPE) "
  241. "with unknown vp_index %d\n",
  242. vha->vp_idx, entry->u.isp24.vp_index);
  243. break;
  244. }
  245. }
  246. qlt_24xx_atio_pkt(host, atio, ha_locked);
  247. break;
  248. }
  249. default:
  250. ql_dbg(ql_dbg_tgt, vha, 0xe040,
  251. "qla_target(%d): Received unknown ATIO atio "
  252. "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
  253. break;
  254. }
  255. return false;
  256. }
  257. void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
  258. {
  259. switch (pkt->entry_type) {
  260. case CTIO_CRC2:
  261. ql_dbg(ql_dbg_tgt, vha, 0xe073,
  262. "qla_target(%d):%s: CRC2 Response pkt\n",
  263. vha->vp_idx, __func__);
  264. case CTIO_TYPE7:
  265. {
  266. struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
  267. struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
  268. entry->vp_index);
  269. if (unlikely(!host)) {
  270. ql_dbg(ql_dbg_tgt, vha, 0xe041,
  271. "qla_target(%d): Response pkt (CTIO_TYPE7) "
  272. "received, with unknown vp_index %d\n",
  273. vha->vp_idx, entry->vp_index);
  274. break;
  275. }
  276. qlt_response_pkt(host, pkt);
  277. break;
  278. }
  279. case IMMED_NOTIFY_TYPE:
  280. {
  281. struct scsi_qla_host *host = vha;
  282. struct imm_ntfy_from_isp *entry =
  283. (struct imm_ntfy_from_isp *)pkt;
  284. host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
  285. if (unlikely(!host)) {
  286. ql_dbg(ql_dbg_tgt, vha, 0xe042,
  287. "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
  288. "received, with unknown vp_index %d\n",
  289. vha->vp_idx, entry->u.isp24.vp_index);
  290. break;
  291. }
  292. qlt_response_pkt(host, pkt);
  293. break;
  294. }
  295. case NOTIFY_ACK_TYPE:
  296. {
  297. struct scsi_qla_host *host = vha;
  298. struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
  299. if (0xFF != entry->u.isp24.vp_index) {
  300. host = qlt_find_host_by_vp_idx(vha,
  301. entry->u.isp24.vp_index);
  302. if (unlikely(!host)) {
  303. ql_dbg(ql_dbg_tgt, vha, 0xe043,
  304. "qla_target(%d): Response "
  305. "pkt (NOTIFY_ACK_TYPE) "
  306. "received, with unknown "
  307. "vp_index %d\n", vha->vp_idx,
  308. entry->u.isp24.vp_index);
  309. break;
  310. }
  311. }
  312. qlt_response_pkt(host, pkt);
  313. break;
  314. }
  315. case ABTS_RECV_24XX:
  316. {
  317. struct abts_recv_from_24xx *entry =
  318. (struct abts_recv_from_24xx *)pkt;
  319. struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
  320. entry->vp_index);
  321. if (unlikely(!host)) {
  322. ql_dbg(ql_dbg_tgt, vha, 0xe044,
  323. "qla_target(%d): Response pkt "
  324. "(ABTS_RECV_24XX) received, with unknown "
  325. "vp_index %d\n", vha->vp_idx, entry->vp_index);
  326. break;
  327. }
  328. qlt_response_pkt(host, pkt);
  329. break;
  330. }
  331. case ABTS_RESP_24XX:
  332. {
  333. struct abts_resp_to_24xx *entry =
  334. (struct abts_resp_to_24xx *)pkt;
  335. struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
  336. entry->vp_index);
  337. if (unlikely(!host)) {
  338. ql_dbg(ql_dbg_tgt, vha, 0xe045,
  339. "qla_target(%d): Response pkt "
  340. "(ABTS_RECV_24XX) received, with unknown "
  341. "vp_index %d\n", vha->vp_idx, entry->vp_index);
  342. break;
  343. }
  344. qlt_response_pkt(host, pkt);
  345. break;
  346. }
  347. default:
  348. qlt_response_pkt(vha, pkt);
  349. break;
  350. }
  351. }
  352. /*
  353. * All qlt_plogi_ack_t operations are protected by hardware_lock
  354. */
  355. /*
  356. * This is a zero-base ref-counting solution, since hardware_lock
  357. * guarantees that ref_count is not modified concurrently.
  358. * Upon successful return content of iocb is undefined
  359. */
  360. static qlt_plogi_ack_t *
  361. qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
  362. struct imm_ntfy_from_isp *iocb)
  363. {
  364. qlt_plogi_ack_t *pla;
  365. list_for_each_entry(pla, &vha->plogi_ack_list, list) {
  366. if (pla->id.b24 == id->b24) {
  367. qlt_send_term_imm_notif(vha, &pla->iocb, 1);
  368. pla->iocb = *iocb;
  369. return pla;
  370. }
  371. }
  372. pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
  373. if (!pla) {
  374. ql_dbg(ql_dbg_async, vha, 0x5088,
  375. "qla_target(%d): Allocation of plogi_ack failed\n",
  376. vha->vp_idx);
  377. return NULL;
  378. }
  379. pla->iocb = *iocb;
  380. pla->id = *id;
  381. list_add_tail(&pla->list, &vha->plogi_ack_list);
  382. return pla;
  383. }
  384. static void qlt_plogi_ack_unref(struct scsi_qla_host *vha, qlt_plogi_ack_t *pla)
  385. {
  386. BUG_ON(!pla->ref_count);
  387. pla->ref_count--;
  388. if (pla->ref_count)
  389. return;
  390. ql_dbg(ql_dbg_async, vha, 0x5089,
  391. "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
  392. " exch %#x ox_id %#x\n", pla->iocb.u.isp24.port_name,
  393. pla->iocb.u.isp24.port_id[2], pla->iocb.u.isp24.port_id[1],
  394. pla->iocb.u.isp24.port_id[0],
  395. le16_to_cpu(pla->iocb.u.isp24.nport_handle),
  396. pla->iocb.u.isp24.exchange_address, pla->iocb.ox_id);
  397. qlt_send_notify_ack(vha, &pla->iocb, 0, 0, 0, 0, 0, 0);
  398. list_del(&pla->list);
  399. kmem_cache_free(qla_tgt_plogi_cachep, pla);
  400. }
  401. static void
  402. qlt_plogi_ack_link(struct scsi_qla_host *vha, qlt_plogi_ack_t *pla,
  403. struct qla_tgt_sess *sess, qlt_plogi_link_t link)
  404. {
  405. /* Inc ref_count first because link might already be pointing at pla */
  406. pla->ref_count++;
  407. if (sess->plogi_link[link])
  408. qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
  409. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
  410. "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
  411. " s_id %02x:%02x:%02x, ref=%d\n", sess, link, sess->port_name,
  412. pla->iocb.u.isp24.port_name, pla->iocb.u.isp24.port_id[2],
  413. pla->iocb.u.isp24.port_id[1], pla->iocb.u.isp24.port_id[0],
  414. pla->ref_count);
  415. sess->plogi_link[link] = pla;
  416. }
  417. typedef struct {
  418. /* These fields must be initialized by the caller */
  419. port_id_t id;
  420. /*
  421. * number of cmds dropped while we were waiting for
  422. * initiator to ack LOGO initialize to 1 if LOGO is
  423. * triggered by a command, otherwise, to 0
  424. */
  425. int cmd_count;
  426. /* These fields are used by callee */
  427. struct list_head list;
  428. } qlt_port_logo_t;
  429. static void
  430. qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
  431. {
  432. qlt_port_logo_t *tmp;
  433. int res;
  434. mutex_lock(&vha->vha_tgt.tgt_mutex);
  435. list_for_each_entry(tmp, &vha->logo_list, list) {
  436. if (tmp->id.b24 == logo->id.b24) {
  437. tmp->cmd_count += logo->cmd_count;
  438. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  439. return;
  440. }
  441. }
  442. list_add_tail(&logo->list, &vha->logo_list);
  443. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  444. res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
  445. mutex_lock(&vha->vha_tgt.tgt_mutex);
  446. list_del(&logo->list);
  447. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  448. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
  449. "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
  450. logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
  451. logo->cmd_count, res);
  452. }
  453. static void qlt_free_session_done(struct work_struct *work)
  454. {
  455. struct qla_tgt_sess *sess = container_of(work, struct qla_tgt_sess,
  456. free_work);
  457. struct qla_tgt *tgt = sess->tgt;
  458. struct scsi_qla_host *vha = sess->vha;
  459. struct qla_hw_data *ha = vha->hw;
  460. unsigned long flags;
  461. bool logout_started = false;
  462. fc_port_t fcport;
  463. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
  464. "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
  465. " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
  466. __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
  467. sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
  468. sess->logout_on_delete, sess->keep_nport_handle,
  469. sess->send_els_logo);
  470. BUG_ON(!tgt);
  471. if (sess->send_els_logo) {
  472. qlt_port_logo_t logo;
  473. logo.id = sess->s_id;
  474. logo.cmd_count = 0;
  475. qlt_send_first_logo(vha, &logo);
  476. }
  477. if (sess->logout_on_delete) {
  478. int rc;
  479. memset(&fcport, 0, sizeof(fcport));
  480. fcport.loop_id = sess->loop_id;
  481. fcport.d_id = sess->s_id;
  482. memcpy(fcport.port_name, sess->port_name, WWN_SIZE);
  483. fcport.vha = vha;
  484. fcport.tgt_session = sess;
  485. rc = qla2x00_post_async_logout_work(vha, &fcport, NULL);
  486. if (rc != QLA_SUCCESS)
  487. ql_log(ql_log_warn, vha, 0xf085,
  488. "Schedule logo failed sess %p rc %d\n",
  489. sess, rc);
  490. else
  491. logout_started = true;
  492. }
  493. /*
  494. * Release the target session for FC Nexus from fabric module code.
  495. */
  496. if (sess->se_sess != NULL)
  497. ha->tgt.tgt_ops->free_session(sess);
  498. if (logout_started) {
  499. bool traced = false;
  500. while (!ACCESS_ONCE(sess->logout_completed)) {
  501. if (!traced) {
  502. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
  503. "%s: waiting for sess %p logout\n",
  504. __func__, sess);
  505. traced = true;
  506. }
  507. msleep(100);
  508. }
  509. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf087,
  510. "%s: sess %p logout completed\n",
  511. __func__, sess);
  512. }
  513. spin_lock_irqsave(&ha->hardware_lock, flags);
  514. {
  515. qlt_plogi_ack_t *own =
  516. sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
  517. qlt_plogi_ack_t *con =
  518. sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
  519. if (con) {
  520. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
  521. "se_sess %p / sess %p port %8phC is gone,"
  522. " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
  523. sess->se_sess, sess, sess->port_name,
  524. own ? "releasing own PLOGI" :
  525. "no own PLOGI pending",
  526. own ? own->ref_count : -1,
  527. con->iocb.u.isp24.port_name, con->ref_count);
  528. qlt_plogi_ack_unref(vha, con);
  529. } else {
  530. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
  531. "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
  532. sess->se_sess, sess, sess->port_name,
  533. own ? "releasing own PLOGI" :
  534. "no own PLOGI pending",
  535. own ? own->ref_count : -1);
  536. }
  537. if (own)
  538. qlt_plogi_ack_unref(vha, own);
  539. }
  540. list_del(&sess->sess_list_entry);
  541. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  542. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
  543. "Unregistration of sess %p finished\n", sess);
  544. kfree(sess);
  545. /*
  546. * We need to protect against race, when tgt is freed before or
  547. * inside wake_up()
  548. */
  549. tgt->sess_count--;
  550. if (tgt->sess_count == 0)
  551. wake_up_all(&tgt->waitQ);
  552. }
  553. /* ha->tgt.sess_lock supposed to be held on entry */
  554. static void qlt_release_session(struct kref *kref)
  555. {
  556. struct qla_tgt_sess *sess =
  557. container_of(kref, struct qla_tgt_sess, sess_kref);
  558. struct scsi_qla_host *vha = sess->vha;
  559. if (sess->se_sess)
  560. vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
  561. if (!list_empty(&sess->del_list_entry))
  562. list_del_init(&sess->del_list_entry);
  563. sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
  564. INIT_WORK(&sess->free_work, qlt_free_session_done);
  565. schedule_work(&sess->free_work);
  566. }
  567. void qlt_put_sess(struct qla_tgt_sess *sess)
  568. {
  569. if (!sess)
  570. return;
  571. assert_spin_locked(&sess->vha->hw->tgt.sess_lock);
  572. kref_put(&sess->sess_kref, qlt_release_session);
  573. }
  574. EXPORT_SYMBOL(qlt_put_sess);
  575. static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
  576. {
  577. struct qla_hw_data *ha = vha->hw;
  578. struct qla_tgt_sess *sess = NULL;
  579. uint16_t loop_id;
  580. int res = 0;
  581. struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
  582. unsigned long flags;
  583. loop_id = le16_to_cpu(n->u.isp24.nport_handle);
  584. if (loop_id == 0xFFFF) {
  585. /* Global event */
  586. atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
  587. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  588. qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
  589. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  590. #if 0 /* FIXME: do we need to choose a session here? */
  591. if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
  592. sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
  593. typeof(*sess), sess_list_entry);
  594. switch (mcmd) {
  595. case QLA_TGT_NEXUS_LOSS_SESS:
  596. mcmd = QLA_TGT_NEXUS_LOSS;
  597. break;
  598. case QLA_TGT_ABORT_ALL_SESS:
  599. mcmd = QLA_TGT_ABORT_ALL;
  600. break;
  601. case QLA_TGT_NEXUS_LOSS:
  602. case QLA_TGT_ABORT_ALL:
  603. break;
  604. default:
  605. ql_dbg(ql_dbg_tgt, vha, 0xe046,
  606. "qla_target(%d): Not allowed "
  607. "command %x in %s", vha->vp_idx,
  608. mcmd, __func__);
  609. sess = NULL;
  610. break;
  611. }
  612. } else
  613. sess = NULL;
  614. #endif
  615. } else {
  616. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  617. sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
  618. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  619. }
  620. ql_dbg(ql_dbg_tgt, vha, 0xe000,
  621. "Using sess for qla_tgt_reset: %p\n", sess);
  622. if (!sess) {
  623. res = -ESRCH;
  624. return res;
  625. }
  626. ql_dbg(ql_dbg_tgt, vha, 0xe047,
  627. "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
  628. "loop_id %d)\n", vha->host_no, sess, sess->port_name,
  629. mcmd, loop_id);
  630. return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
  631. }
  632. /* ha->tgt.sess_lock supposed to be held on entry */
  633. static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
  634. bool immediate)
  635. {
  636. struct qla_tgt *tgt = sess->tgt;
  637. uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
  638. if (sess->deleted) {
  639. /* Upgrade to unconditional deletion in case it was temporary */
  640. if (immediate && sess->deleted == QLA_SESS_DELETION_PENDING)
  641. list_del(&sess->del_list_entry);
  642. else
  643. return;
  644. }
  645. ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
  646. "Scheduling sess %p for deletion\n", sess);
  647. if (immediate) {
  648. dev_loss_tmo = 0;
  649. sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
  650. list_add(&sess->del_list_entry, &tgt->del_sess_list);
  651. } else {
  652. sess->deleted = QLA_SESS_DELETION_PENDING;
  653. list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
  654. }
  655. sess->expires = jiffies + dev_loss_tmo * HZ;
  656. ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
  657. "qla_target(%d): session for port %8phC (loop ID %d s_id %02x:%02x:%02x)"
  658. " scheduled for deletion in %u secs (expires: %lu) immed: %d, logout: %d, gen: %#x\n",
  659. sess->vha->vp_idx, sess->port_name, sess->loop_id,
  660. sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
  661. dev_loss_tmo, sess->expires, immediate, sess->logout_on_delete,
  662. sess->generation);
  663. if (immediate)
  664. mod_delayed_work(system_wq, &tgt->sess_del_work, 0);
  665. else
  666. schedule_delayed_work(&tgt->sess_del_work,
  667. sess->expires - jiffies);
  668. }
  669. /* ha->tgt.sess_lock supposed to be held on entry */
  670. static void qlt_clear_tgt_db(struct qla_tgt *tgt)
  671. {
  672. struct qla_tgt_sess *sess;
  673. list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
  674. qlt_schedule_sess_for_deletion(sess, true);
  675. /* At this point tgt could be already dead */
  676. }
  677. static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
  678. uint16_t *loop_id)
  679. {
  680. struct qla_hw_data *ha = vha->hw;
  681. dma_addr_t gid_list_dma;
  682. struct gid_list_info *gid_list;
  683. char *id_iter;
  684. int res, rc, i;
  685. uint16_t entries;
  686. gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
  687. &gid_list_dma, GFP_KERNEL);
  688. if (!gid_list) {
  689. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
  690. "qla_target(%d): DMA Alloc failed of %u\n",
  691. vha->vp_idx, qla2x00_gid_list_size(ha));
  692. return -ENOMEM;
  693. }
  694. /* Get list of logged in devices */
  695. rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
  696. if (rc != QLA_SUCCESS) {
  697. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
  698. "qla_target(%d): get_id_list() failed: %x\n",
  699. vha->vp_idx, rc);
  700. res = -EBUSY;
  701. goto out_free_id_list;
  702. }
  703. id_iter = (char *)gid_list;
  704. res = -ENOENT;
  705. for (i = 0; i < entries; i++) {
  706. struct gid_list_info *gid = (struct gid_list_info *)id_iter;
  707. if ((gid->al_pa == s_id[2]) &&
  708. (gid->area == s_id[1]) &&
  709. (gid->domain == s_id[0])) {
  710. *loop_id = le16_to_cpu(gid->loop_id);
  711. res = 0;
  712. break;
  713. }
  714. id_iter += ha->gid_list_info_size;
  715. }
  716. out_free_id_list:
  717. dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
  718. gid_list, gid_list_dma);
  719. return res;
  720. }
  721. /* ha->tgt.sess_lock supposed to be held on entry */
  722. static void qlt_undelete_sess(struct qla_tgt_sess *sess)
  723. {
  724. BUG_ON(sess->deleted != QLA_SESS_DELETION_PENDING);
  725. list_del_init(&sess->del_list_entry);
  726. sess->deleted = 0;
  727. }
  728. static void qlt_del_sess_work_fn(struct delayed_work *work)
  729. {
  730. struct qla_tgt *tgt = container_of(work, struct qla_tgt,
  731. sess_del_work);
  732. struct scsi_qla_host *vha = tgt->vha;
  733. struct qla_hw_data *ha = vha->hw;
  734. struct qla_tgt_sess *sess;
  735. unsigned long flags, elapsed;
  736. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  737. while (!list_empty(&tgt->del_sess_list)) {
  738. sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
  739. del_list_entry);
  740. elapsed = jiffies;
  741. if (time_after_eq(elapsed, sess->expires)) {
  742. /* No turning back */
  743. list_del_init(&sess->del_list_entry);
  744. sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
  745. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
  746. "Timeout: sess %p about to be deleted\n",
  747. sess);
  748. if (sess->se_sess)
  749. ha->tgt.tgt_ops->shutdown_sess(sess);
  750. qlt_put_sess(sess);
  751. } else {
  752. schedule_delayed_work(&tgt->sess_del_work,
  753. sess->expires - elapsed);
  754. break;
  755. }
  756. }
  757. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  758. }
  759. /*
  760. * Adds an extra ref to allow to drop hw lock after adding sess to the list.
  761. * Caller must put it.
  762. */
  763. static struct qla_tgt_sess *qlt_create_sess(
  764. struct scsi_qla_host *vha,
  765. fc_port_t *fcport,
  766. bool local)
  767. {
  768. struct qla_hw_data *ha = vha->hw;
  769. struct qla_tgt_sess *sess;
  770. unsigned long flags;
  771. /* Check to avoid double sessions */
  772. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  773. list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list,
  774. sess_list_entry) {
  775. if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
  776. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
  777. "Double sess %p found (s_id %x:%x:%x, "
  778. "loop_id %d), updating to d_id %x:%x:%x, "
  779. "loop_id %d", sess, sess->s_id.b.domain,
  780. sess->s_id.b.al_pa, sess->s_id.b.area,
  781. sess->loop_id, fcport->d_id.b.domain,
  782. fcport->d_id.b.al_pa, fcport->d_id.b.area,
  783. fcport->loop_id);
  784. /* Cannot undelete at this point */
  785. if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
  786. spin_unlock_irqrestore(&ha->tgt.sess_lock,
  787. flags);
  788. return NULL;
  789. }
  790. if (sess->deleted)
  791. qlt_undelete_sess(sess);
  792. if (!sess->se_sess) {
  793. if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
  794. &sess->port_name[0], sess) < 0) {
  795. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  796. return NULL;
  797. }
  798. }
  799. kref_get(&sess->sess_kref);
  800. ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
  801. (fcport->flags & FCF_CONF_COMP_SUPPORTED));
  802. if (sess->local && !local)
  803. sess->local = 0;
  804. qlt_do_generation_tick(vha, &sess->generation);
  805. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  806. return sess;
  807. }
  808. }
  809. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  810. sess = kzalloc(sizeof(*sess), GFP_KERNEL);
  811. if (!sess) {
  812. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
  813. "qla_target(%u): session allocation failed, all commands "
  814. "from port %8phC will be refused", vha->vp_idx,
  815. fcport->port_name);
  816. return NULL;
  817. }
  818. sess->tgt = vha->vha_tgt.qla_tgt;
  819. sess->vha = vha;
  820. sess->s_id = fcport->d_id;
  821. sess->loop_id = fcport->loop_id;
  822. sess->local = local;
  823. kref_init(&sess->sess_kref);
  824. INIT_LIST_HEAD(&sess->del_list_entry);
  825. /* Under normal circumstances we want to logout from firmware when
  826. * session eventually ends and release corresponding nport handle.
  827. * In the exception cases (e.g. when new PLOGI is waiting) corresponding
  828. * code will adjust these flags as necessary. */
  829. sess->logout_on_delete = 1;
  830. sess->keep_nport_handle = 0;
  831. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
  832. "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
  833. sess, vha->vha_tgt.qla_tgt);
  834. sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
  835. BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
  836. memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
  837. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  838. list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list);
  839. vha->vha_tgt.qla_tgt->sess_count++;
  840. qlt_do_generation_tick(vha, &sess->generation);
  841. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  842. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
  843. "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
  844. "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
  845. vha->vp_idx, local ? "local " : "", fcport->port_name,
  846. fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
  847. sess->s_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
  848. /*
  849. * Determine if this fc_port->port_name is allowed to access
  850. * target mode using explict NodeACLs+MappedLUNs, or using
  851. * TPG demo mode. If this is successful a target mode FC nexus
  852. * is created.
  853. */
  854. if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
  855. &fcport->port_name[0], sess) < 0) {
  856. return NULL;
  857. } else {
  858. /*
  859. * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
  860. * access across ->tgt.sess_lock reaquire.
  861. */
  862. kref_get(&sess->sess_kref);
  863. }
  864. return sess;
  865. }
  866. /*
  867. * Called from qla2x00_reg_remote_port()
  868. */
  869. void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
  870. {
  871. struct qla_hw_data *ha = vha->hw;
  872. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  873. struct qla_tgt_sess *sess;
  874. unsigned long flags;
  875. if (!vha->hw->tgt.tgt_ops)
  876. return;
  877. if (!tgt || (fcport->port_type != FCT_INITIATOR))
  878. return;
  879. if (qla_ini_mode_enabled(vha))
  880. return;
  881. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  882. if (tgt->tgt_stop) {
  883. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  884. return;
  885. }
  886. sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
  887. if (!sess) {
  888. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  889. mutex_lock(&vha->vha_tgt.tgt_mutex);
  890. sess = qlt_create_sess(vha, fcport, false);
  891. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  892. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  893. } else if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
  894. /* Point of no return */
  895. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  896. return;
  897. } else {
  898. kref_get(&sess->sess_kref);
  899. if (sess->deleted) {
  900. qlt_undelete_sess(sess);
  901. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
  902. "qla_target(%u): %ssession for port %8phC "
  903. "(loop ID %d) reappeared\n", vha->vp_idx,
  904. sess->local ? "local " : "", sess->port_name,
  905. sess->loop_id);
  906. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
  907. "Reappeared sess %p\n", sess);
  908. }
  909. ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
  910. (fcport->flags & FCF_CONF_COMP_SUPPORTED));
  911. }
  912. if (sess && sess->local) {
  913. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
  914. "qla_target(%u): local session for "
  915. "port %8phC (loop ID %d) became global\n", vha->vp_idx,
  916. fcport->port_name, sess->loop_id);
  917. sess->local = 0;
  918. }
  919. qlt_put_sess(sess);
  920. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  921. }
  922. /*
  923. * max_gen - specifies maximum session generation
  924. * at which this deletion requestion is still valid
  925. */
  926. void
  927. qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
  928. {
  929. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  930. struct qla_tgt_sess *sess;
  931. unsigned long flags;
  932. if (!vha->hw->tgt.tgt_ops)
  933. return;
  934. if (!tgt)
  935. return;
  936. spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
  937. if (tgt->tgt_stop) {
  938. spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
  939. return;
  940. }
  941. sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
  942. if (!sess) {
  943. spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
  944. return;
  945. }
  946. if (max_gen - sess->generation < 0) {
  947. spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
  948. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
  949. "Ignoring stale deletion request for se_sess %p / sess %p"
  950. " for port %8phC, req_gen %d, sess_gen %d\n",
  951. sess->se_sess, sess, sess->port_name, max_gen,
  952. sess->generation);
  953. return;
  954. }
  955. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
  956. sess->local = 1;
  957. qlt_schedule_sess_for_deletion(sess, false);
  958. spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
  959. }
  960. static inline int test_tgt_sess_count(struct qla_tgt *tgt)
  961. {
  962. struct qla_hw_data *ha = tgt->ha;
  963. unsigned long flags;
  964. int res;
  965. /*
  966. * We need to protect against race, when tgt is freed before or
  967. * inside wake_up()
  968. */
  969. spin_lock_irqsave(&ha->hardware_lock, flags);
  970. ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
  971. "tgt %p, empty(sess_list)=%d sess_count=%d\n",
  972. tgt, list_empty(&tgt->sess_list), tgt->sess_count);
  973. res = (tgt->sess_count == 0);
  974. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  975. return res;
  976. }
  977. /* Called by tcm_qla2xxx configfs code */
  978. int qlt_stop_phase1(struct qla_tgt *tgt)
  979. {
  980. struct scsi_qla_host *vha = tgt->vha;
  981. struct qla_hw_data *ha = tgt->ha;
  982. unsigned long flags;
  983. mutex_lock(&qla_tgt_mutex);
  984. if (!vha->fc_vport) {
  985. struct Scsi_Host *sh = vha->host;
  986. struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
  987. bool npiv_vports;
  988. spin_lock_irqsave(sh->host_lock, flags);
  989. npiv_vports = (fc_host->npiv_vports_inuse);
  990. spin_unlock_irqrestore(sh->host_lock, flags);
  991. if (npiv_vports) {
  992. mutex_unlock(&qla_tgt_mutex);
  993. return -EPERM;
  994. }
  995. }
  996. if (tgt->tgt_stop || tgt->tgt_stopped) {
  997. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
  998. "Already in tgt->tgt_stop or tgt_stopped state\n");
  999. mutex_unlock(&qla_tgt_mutex);
  1000. return -EPERM;
  1001. }
  1002. ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
  1003. vha->host_no, vha);
  1004. /*
  1005. * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
  1006. * Lock is needed, because we still can get an incoming packet.
  1007. */
  1008. mutex_lock(&vha->vha_tgt.tgt_mutex);
  1009. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  1010. tgt->tgt_stop = 1;
  1011. qlt_clear_tgt_db(tgt);
  1012. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  1013. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  1014. mutex_unlock(&qla_tgt_mutex);
  1015. flush_delayed_work(&tgt->sess_del_work);
  1016. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
  1017. "Waiting for sess works (tgt %p)", tgt);
  1018. spin_lock_irqsave(&tgt->sess_work_lock, flags);
  1019. while (!list_empty(&tgt->sess_works_list)) {
  1020. spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
  1021. flush_scheduled_work();
  1022. spin_lock_irqsave(&tgt->sess_work_lock, flags);
  1023. }
  1024. spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
  1025. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
  1026. "Waiting for tgt %p: list_empty(sess_list)=%d "
  1027. "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
  1028. tgt->sess_count);
  1029. wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
  1030. /* Big hammer */
  1031. if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
  1032. qlt_disable_vha(vha);
  1033. /* Wait for sessions to clear out (just in case) */
  1034. wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL(qlt_stop_phase1);
  1038. /* Called by tcm_qla2xxx configfs code */
  1039. void qlt_stop_phase2(struct qla_tgt *tgt)
  1040. {
  1041. struct qla_hw_data *ha = tgt->ha;
  1042. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  1043. unsigned long flags;
  1044. if (tgt->tgt_stopped) {
  1045. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
  1046. "Already in tgt->tgt_stopped state\n");
  1047. dump_stack();
  1048. return;
  1049. }
  1050. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
  1051. "Waiting for %d IRQ commands to complete (tgt %p)",
  1052. tgt->irq_cmd_count, tgt);
  1053. mutex_lock(&vha->vha_tgt.tgt_mutex);
  1054. spin_lock_irqsave(&ha->hardware_lock, flags);
  1055. while ((tgt->irq_cmd_count != 0) || (tgt->atio_irq_cmd_count != 0)) {
  1056. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1057. udelay(2);
  1058. spin_lock_irqsave(&ha->hardware_lock, flags);
  1059. }
  1060. tgt->tgt_stop = 0;
  1061. tgt->tgt_stopped = 1;
  1062. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1063. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  1064. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
  1065. tgt);
  1066. }
  1067. EXPORT_SYMBOL(qlt_stop_phase2);
  1068. /* Called from qlt_remove_target() -> qla2x00_remove_one() */
  1069. static void qlt_release(struct qla_tgt *tgt)
  1070. {
  1071. scsi_qla_host_t *vha = tgt->vha;
  1072. if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
  1073. qlt_stop_phase2(tgt);
  1074. vha->vha_tgt.qla_tgt = NULL;
  1075. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
  1076. "Release of tgt %p finished\n", tgt);
  1077. kfree(tgt);
  1078. }
  1079. /* ha->hardware_lock supposed to be held on entry */
  1080. static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
  1081. const void *param, unsigned int param_size)
  1082. {
  1083. struct qla_tgt_sess_work_param *prm;
  1084. unsigned long flags;
  1085. prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
  1086. if (!prm) {
  1087. ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
  1088. "qla_target(%d): Unable to create session "
  1089. "work, command will be refused", 0);
  1090. return -ENOMEM;
  1091. }
  1092. ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
  1093. "Scheduling work (type %d, prm %p)"
  1094. " to find session for param %p (size %d, tgt %p)\n",
  1095. type, prm, param, param_size, tgt);
  1096. prm->type = type;
  1097. memcpy(&prm->tm_iocb, param, param_size);
  1098. spin_lock_irqsave(&tgt->sess_work_lock, flags);
  1099. list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
  1100. spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
  1101. schedule_work(&tgt->sess_work);
  1102. return 0;
  1103. }
  1104. /*
  1105. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  1106. */
  1107. static void qlt_send_notify_ack(struct scsi_qla_host *vha,
  1108. struct imm_ntfy_from_isp *ntfy,
  1109. uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
  1110. uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
  1111. {
  1112. struct qla_hw_data *ha = vha->hw;
  1113. request_t *pkt;
  1114. struct nack_to_isp *nack;
  1115. ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
  1116. /* Send marker if required */
  1117. if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
  1118. return;
  1119. pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
  1120. if (!pkt) {
  1121. ql_dbg(ql_dbg_tgt, vha, 0xe049,
  1122. "qla_target(%d): %s failed: unable to allocate "
  1123. "request packet\n", vha->vp_idx, __func__);
  1124. return;
  1125. }
  1126. if (vha->vha_tgt.qla_tgt != NULL)
  1127. vha->vha_tgt.qla_tgt->notify_ack_expected++;
  1128. pkt->entry_type = NOTIFY_ACK_TYPE;
  1129. pkt->entry_count = 1;
  1130. nack = (struct nack_to_isp *)pkt;
  1131. nack->ox_id = ntfy->ox_id;
  1132. nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
  1133. if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
  1134. nack->u.isp24.flags = ntfy->u.isp24.flags &
  1135. cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
  1136. }
  1137. nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
  1138. nack->u.isp24.status = ntfy->u.isp24.status;
  1139. nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
  1140. nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
  1141. nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
  1142. nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
  1143. nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
  1144. nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
  1145. nack->u.isp24.srr_reject_code = srr_reject_code;
  1146. nack->u.isp24.srr_reject_code_expl = srr_explan;
  1147. nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
  1148. ql_dbg(ql_dbg_tgt, vha, 0xe005,
  1149. "qla_target(%d): Sending 24xx Notify Ack %d\n",
  1150. vha->vp_idx, nack->u.isp24.status);
  1151. /* Memory Barrier */
  1152. wmb();
  1153. qla2x00_start_iocbs(vha, vha->req);
  1154. }
  1155. /*
  1156. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  1157. */
  1158. static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
  1159. struct abts_recv_from_24xx *abts, uint32_t status,
  1160. bool ids_reversed)
  1161. {
  1162. struct qla_hw_data *ha = vha->hw;
  1163. struct abts_resp_to_24xx *resp;
  1164. uint32_t f_ctl;
  1165. uint8_t *p;
  1166. ql_dbg(ql_dbg_tgt, vha, 0xe006,
  1167. "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
  1168. ha, abts, status);
  1169. /* Send marker if required */
  1170. if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
  1171. return;
  1172. resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
  1173. if (!resp) {
  1174. ql_dbg(ql_dbg_tgt, vha, 0xe04a,
  1175. "qla_target(%d): %s failed: unable to allocate "
  1176. "request packet", vha->vp_idx, __func__);
  1177. return;
  1178. }
  1179. resp->entry_type = ABTS_RESP_24XX;
  1180. resp->entry_count = 1;
  1181. resp->nport_handle = abts->nport_handle;
  1182. resp->vp_index = vha->vp_idx;
  1183. resp->sof_type = abts->sof_type;
  1184. resp->exchange_address = abts->exchange_address;
  1185. resp->fcp_hdr_le = abts->fcp_hdr_le;
  1186. f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
  1187. F_CTL_LAST_SEQ | F_CTL_END_SEQ |
  1188. F_CTL_SEQ_INITIATIVE);
  1189. p = (uint8_t *)&f_ctl;
  1190. resp->fcp_hdr_le.f_ctl[0] = *p++;
  1191. resp->fcp_hdr_le.f_ctl[1] = *p++;
  1192. resp->fcp_hdr_le.f_ctl[2] = *p;
  1193. if (ids_reversed) {
  1194. resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
  1195. resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
  1196. resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
  1197. resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
  1198. resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
  1199. resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
  1200. } else {
  1201. resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
  1202. resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
  1203. resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
  1204. resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
  1205. resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
  1206. resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
  1207. }
  1208. resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
  1209. if (status == FCP_TMF_CMPL) {
  1210. resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
  1211. resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
  1212. resp->payload.ba_acct.low_seq_cnt = 0x0000;
  1213. resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
  1214. resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
  1215. resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
  1216. } else {
  1217. resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
  1218. resp->payload.ba_rjt.reason_code =
  1219. BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
  1220. /* Other bytes are zero */
  1221. }
  1222. vha->vha_tgt.qla_tgt->abts_resp_expected++;
  1223. /* Memory Barrier */
  1224. wmb();
  1225. qla2x00_start_iocbs(vha, vha->req);
  1226. }
  1227. /*
  1228. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  1229. */
  1230. static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
  1231. struct abts_resp_from_24xx_fw *entry)
  1232. {
  1233. struct ctio7_to_24xx *ctio;
  1234. ql_dbg(ql_dbg_tgt, vha, 0xe007,
  1235. "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
  1236. /* Send marker if required */
  1237. if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
  1238. return;
  1239. ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
  1240. if (ctio == NULL) {
  1241. ql_dbg(ql_dbg_tgt, vha, 0xe04b,
  1242. "qla_target(%d): %s failed: unable to allocate "
  1243. "request packet\n", vha->vp_idx, __func__);
  1244. return;
  1245. }
  1246. /*
  1247. * We've got on entrance firmware's response on by us generated
  1248. * ABTS response. So, in it ID fields are reversed.
  1249. */
  1250. ctio->entry_type = CTIO_TYPE7;
  1251. ctio->entry_count = 1;
  1252. ctio->nport_handle = entry->nport_handle;
  1253. ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
  1254. ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
  1255. ctio->vp_index = vha->vp_idx;
  1256. ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
  1257. ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
  1258. ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
  1259. ctio->exchange_addr = entry->exchange_addr_to_abort;
  1260. ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
  1261. CTIO7_FLAGS_TERMINATE);
  1262. ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
  1263. /* Memory Barrier */
  1264. wmb();
  1265. qla2x00_start_iocbs(vha, vha->req);
  1266. qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
  1267. FCP_TMF_CMPL, true);
  1268. }
  1269. static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
  1270. {
  1271. struct qla_tgt_sess_op *op;
  1272. struct qla_tgt_cmd *cmd;
  1273. spin_lock(&vha->cmd_list_lock);
  1274. list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
  1275. if (tag == op->atio.u.isp24.exchange_addr) {
  1276. op->aborted = true;
  1277. spin_unlock(&vha->cmd_list_lock);
  1278. return 1;
  1279. }
  1280. }
  1281. list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
  1282. if (tag == cmd->atio.u.isp24.exchange_addr) {
  1283. cmd->aborted = 1;
  1284. spin_unlock(&vha->cmd_list_lock);
  1285. return 1;
  1286. }
  1287. }
  1288. spin_unlock(&vha->cmd_list_lock);
  1289. return 0;
  1290. }
  1291. /* drop cmds for the given lun
  1292. * XXX only looks for cmds on the port through which lun reset was recieved
  1293. * XXX does not go through the list of other port (which may have cmds
  1294. * for the same lun)
  1295. */
  1296. static void abort_cmds_for_lun(struct scsi_qla_host *vha,
  1297. uint32_t lun, uint8_t *s_id)
  1298. {
  1299. struct qla_tgt_sess_op *op;
  1300. struct qla_tgt_cmd *cmd;
  1301. uint32_t key;
  1302. key = sid_to_key(s_id);
  1303. spin_lock(&vha->cmd_list_lock);
  1304. list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
  1305. uint32_t op_key;
  1306. uint32_t op_lun;
  1307. op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
  1308. op_lun = scsilun_to_int(
  1309. (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
  1310. if (op_key == key && op_lun == lun)
  1311. op->aborted = true;
  1312. }
  1313. list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
  1314. uint32_t cmd_key;
  1315. uint32_t cmd_lun;
  1316. cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
  1317. cmd_lun = scsilun_to_int(
  1318. (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
  1319. if (cmd_key == key && cmd_lun == lun)
  1320. cmd->aborted = 1;
  1321. }
  1322. spin_unlock(&vha->cmd_list_lock);
  1323. }
  1324. /* ha->hardware_lock supposed to be held on entry */
  1325. static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
  1326. struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
  1327. {
  1328. struct qla_hw_data *ha = vha->hw;
  1329. struct se_session *se_sess = sess->se_sess;
  1330. struct qla_tgt_mgmt_cmd *mcmd;
  1331. struct se_cmd *se_cmd;
  1332. u32 lun = 0;
  1333. int rc;
  1334. bool found_lun = false;
  1335. spin_lock(&se_sess->sess_cmd_lock);
  1336. list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
  1337. struct qla_tgt_cmd *cmd =
  1338. container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
  1339. if (se_cmd->tag == abts->exchange_addr_to_abort) {
  1340. lun = cmd->unpacked_lun;
  1341. found_lun = true;
  1342. break;
  1343. }
  1344. }
  1345. spin_unlock(&se_sess->sess_cmd_lock);
  1346. /* cmd not in LIO lists, look in qla list */
  1347. if (!found_lun) {
  1348. if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
  1349. /* send TASK_ABORT response immediately */
  1350. qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
  1351. return 0;
  1352. } else {
  1353. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
  1354. "unable to find cmd in driver or LIO for tag 0x%x\n",
  1355. abts->exchange_addr_to_abort);
  1356. return -ENOENT;
  1357. }
  1358. }
  1359. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
  1360. "qla_target(%d): task abort (tag=%d)\n",
  1361. vha->vp_idx, abts->exchange_addr_to_abort);
  1362. mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
  1363. if (mcmd == NULL) {
  1364. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
  1365. "qla_target(%d): %s: Allocation of ABORT cmd failed",
  1366. vha->vp_idx, __func__);
  1367. return -ENOMEM;
  1368. }
  1369. memset(mcmd, 0, sizeof(*mcmd));
  1370. mcmd->sess = sess;
  1371. memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
  1372. mcmd->reset_count = vha->hw->chip_reset;
  1373. rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
  1374. abts->exchange_addr_to_abort);
  1375. if (rc != 0) {
  1376. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
  1377. "qla_target(%d): tgt_ops->handle_tmr()"
  1378. " failed: %d", vha->vp_idx, rc);
  1379. mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
  1380. return -EFAULT;
  1381. }
  1382. return 0;
  1383. }
  1384. /*
  1385. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  1386. */
  1387. static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
  1388. struct abts_recv_from_24xx *abts)
  1389. {
  1390. struct qla_hw_data *ha = vha->hw;
  1391. struct qla_tgt_sess *sess;
  1392. uint32_t tag = abts->exchange_addr_to_abort;
  1393. uint8_t s_id[3];
  1394. int rc;
  1395. unsigned long flags;
  1396. if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
  1397. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
  1398. "qla_target(%d): ABTS: Abort Sequence not "
  1399. "supported\n", vha->vp_idx);
  1400. qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
  1401. return;
  1402. }
  1403. if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
  1404. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
  1405. "qla_target(%d): ABTS: Unknown Exchange "
  1406. "Address received\n", vha->vp_idx);
  1407. qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
  1408. return;
  1409. }
  1410. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
  1411. "qla_target(%d): task abort (s_id=%x:%x:%x, "
  1412. "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
  1413. abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
  1414. le32_to_cpu(abts->fcp_hdr_le.parameter));
  1415. s_id[0] = abts->fcp_hdr_le.s_id[2];
  1416. s_id[1] = abts->fcp_hdr_le.s_id[1];
  1417. s_id[2] = abts->fcp_hdr_le.s_id[0];
  1418. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  1419. sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
  1420. if (!sess) {
  1421. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
  1422. "qla_target(%d): task abort for non-existant session\n",
  1423. vha->vp_idx);
  1424. rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
  1425. QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
  1426. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  1427. if (rc != 0) {
  1428. qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
  1429. false);
  1430. }
  1431. return;
  1432. }
  1433. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  1434. if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
  1435. qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
  1436. return;
  1437. }
  1438. rc = __qlt_24xx_handle_abts(vha, abts, sess);
  1439. if (rc != 0) {
  1440. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
  1441. "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
  1442. vha->vp_idx, rc);
  1443. qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
  1444. return;
  1445. }
  1446. }
  1447. /*
  1448. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  1449. */
  1450. static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
  1451. struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
  1452. {
  1453. struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
  1454. struct ctio7_to_24xx *ctio;
  1455. uint16_t temp;
  1456. ql_dbg(ql_dbg_tgt, ha, 0xe008,
  1457. "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
  1458. ha, atio, resp_code);
  1459. /* Send marker if required */
  1460. if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
  1461. return;
  1462. ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
  1463. if (ctio == NULL) {
  1464. ql_dbg(ql_dbg_tgt, ha, 0xe04c,
  1465. "qla_target(%d): %s failed: unable to allocate "
  1466. "request packet\n", ha->vp_idx, __func__);
  1467. return;
  1468. }
  1469. ctio->entry_type = CTIO_TYPE7;
  1470. ctio->entry_count = 1;
  1471. ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
  1472. ctio->nport_handle = mcmd->sess->loop_id;
  1473. ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
  1474. ctio->vp_index = ha->vp_idx;
  1475. ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
  1476. ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
  1477. ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
  1478. ctio->exchange_addr = atio->u.isp24.exchange_addr;
  1479. ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
  1480. cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
  1481. temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
  1482. ctio->u.status1.ox_id = cpu_to_le16(temp);
  1483. ctio->u.status1.scsi_status =
  1484. cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
  1485. ctio->u.status1.response_len = cpu_to_le16(8);
  1486. ctio->u.status1.sense_data[0] = resp_code;
  1487. /* Memory Barrier */
  1488. wmb();
  1489. qla2x00_start_iocbs(ha, ha->req);
  1490. }
  1491. void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
  1492. {
  1493. mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
  1494. }
  1495. EXPORT_SYMBOL(qlt_free_mcmd);
  1496. /* callback from target fabric module code */
  1497. void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
  1498. {
  1499. struct scsi_qla_host *vha = mcmd->sess->vha;
  1500. struct qla_hw_data *ha = vha->hw;
  1501. unsigned long flags;
  1502. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
  1503. "TM response mcmd (%p) status %#x state %#x",
  1504. mcmd, mcmd->fc_tm_rsp, mcmd->flags);
  1505. spin_lock_irqsave(&ha->hardware_lock, flags);
  1506. if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
  1507. /*
  1508. * Either the port is not online or this request was from
  1509. * previous life, just abort the processing.
  1510. */
  1511. ql_dbg(ql_dbg_async, vha, 0xe100,
  1512. "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
  1513. vha->flags.online, qla2x00_reset_active(vha),
  1514. mcmd->reset_count, ha->chip_reset);
  1515. ha->tgt.tgt_ops->free_mcmd(mcmd);
  1516. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1517. return;
  1518. }
  1519. if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
  1520. qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
  1521. 0, 0, 0, 0, 0, 0);
  1522. else {
  1523. if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
  1524. qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
  1525. mcmd->fc_tm_rsp, false);
  1526. else
  1527. qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
  1528. mcmd->fc_tm_rsp);
  1529. }
  1530. /*
  1531. * Make the callback for ->free_mcmd() to queue_work() and invoke
  1532. * target_put_sess_cmd() to drop cmd_kref to 1. The final
  1533. * target_put_sess_cmd() call will be made from TFO->check_stop_free()
  1534. * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
  1535. * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
  1536. * qlt_xmit_tm_rsp() returns here..
  1537. */
  1538. ha->tgt.tgt_ops->free_mcmd(mcmd);
  1539. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1540. }
  1541. EXPORT_SYMBOL(qlt_xmit_tm_rsp);
  1542. /* No locks */
  1543. static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
  1544. {
  1545. struct qla_tgt_cmd *cmd = prm->cmd;
  1546. BUG_ON(cmd->sg_cnt == 0);
  1547. prm->sg = (struct scatterlist *)cmd->sg;
  1548. prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
  1549. cmd->sg_cnt, cmd->dma_data_direction);
  1550. if (unlikely(prm->seg_cnt == 0))
  1551. goto out_err;
  1552. prm->cmd->sg_mapped = 1;
  1553. if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
  1554. /*
  1555. * If greater than four sg entries then we need to allocate
  1556. * the continuation entries
  1557. */
  1558. if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
  1559. prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
  1560. prm->tgt->datasegs_per_cmd,
  1561. prm->tgt->datasegs_per_cont);
  1562. } else {
  1563. /* DIF */
  1564. if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
  1565. (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
  1566. prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
  1567. prm->tot_dsds = prm->seg_cnt;
  1568. } else
  1569. prm->tot_dsds = prm->seg_cnt;
  1570. if (cmd->prot_sg_cnt) {
  1571. prm->prot_sg = cmd->prot_sg;
  1572. prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
  1573. cmd->prot_sg, cmd->prot_sg_cnt,
  1574. cmd->dma_data_direction);
  1575. if (unlikely(prm->prot_seg_cnt == 0))
  1576. goto out_err;
  1577. if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
  1578. (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
  1579. /* Dif Bundling not support here */
  1580. prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
  1581. cmd->blk_sz);
  1582. prm->tot_dsds += prm->prot_seg_cnt;
  1583. } else
  1584. prm->tot_dsds += prm->prot_seg_cnt;
  1585. }
  1586. }
  1587. return 0;
  1588. out_err:
  1589. ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
  1590. "qla_target(%d): PCI mapping failed: sg_cnt=%d",
  1591. 0, prm->cmd->sg_cnt);
  1592. return -1;
  1593. }
  1594. static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
  1595. {
  1596. struct qla_hw_data *ha = vha->hw;
  1597. if (!cmd->sg_mapped)
  1598. return;
  1599. pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
  1600. cmd->sg_mapped = 0;
  1601. if (cmd->prot_sg_cnt)
  1602. pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
  1603. cmd->dma_data_direction);
  1604. if (cmd->ctx_dsd_alloced)
  1605. qla2x00_clean_dsd_pool(ha, NULL, cmd);
  1606. if (cmd->ctx)
  1607. dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
  1608. }
  1609. static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
  1610. uint32_t req_cnt)
  1611. {
  1612. uint32_t cnt, cnt_in;
  1613. if (vha->req->cnt < (req_cnt + 2)) {
  1614. cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
  1615. cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
  1616. if (vha->req->ring_index < cnt)
  1617. vha->req->cnt = cnt - vha->req->ring_index;
  1618. else
  1619. vha->req->cnt = vha->req->length -
  1620. (vha->req->ring_index - cnt);
  1621. if (unlikely(vha->req->cnt < (req_cnt + 2))) {
  1622. ql_dbg(ql_dbg_io, vha, 0x305a,
  1623. "qla_target(%d): There is no room in the request ring: vha->req->ring_index=%d, vha->req->cnt=%d, req_cnt=%d Req-out=%d Req-in=%d Req-Length=%d\n",
  1624. vha->vp_idx, vha->req->ring_index,
  1625. vha->req->cnt, req_cnt, cnt, cnt_in,
  1626. vha->req->length);
  1627. return -EAGAIN;
  1628. }
  1629. }
  1630. vha->req->cnt -= req_cnt;
  1631. return 0;
  1632. }
  1633. /*
  1634. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  1635. */
  1636. static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
  1637. {
  1638. /* Adjust ring index. */
  1639. vha->req->ring_index++;
  1640. if (vha->req->ring_index == vha->req->length) {
  1641. vha->req->ring_index = 0;
  1642. vha->req->ring_ptr = vha->req->ring;
  1643. } else {
  1644. vha->req->ring_ptr++;
  1645. }
  1646. return (cont_entry_t *)vha->req->ring_ptr;
  1647. }
  1648. /* ha->hardware_lock supposed to be held on entry */
  1649. static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
  1650. {
  1651. struct qla_hw_data *ha = vha->hw;
  1652. uint32_t h;
  1653. h = ha->tgt.current_handle;
  1654. /* always increment cmd handle */
  1655. do {
  1656. ++h;
  1657. if (h > DEFAULT_OUTSTANDING_COMMANDS)
  1658. h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
  1659. if (h == ha->tgt.current_handle) {
  1660. ql_dbg(ql_dbg_io, vha, 0x305b,
  1661. "qla_target(%d): Ran out of "
  1662. "empty cmd slots in ha %p\n", vha->vp_idx, ha);
  1663. h = QLA_TGT_NULL_HANDLE;
  1664. break;
  1665. }
  1666. } while ((h == QLA_TGT_NULL_HANDLE) ||
  1667. (h == QLA_TGT_SKIP_HANDLE) ||
  1668. (ha->tgt.cmds[h-1] != NULL));
  1669. if (h != QLA_TGT_NULL_HANDLE)
  1670. ha->tgt.current_handle = h;
  1671. return h;
  1672. }
  1673. /* ha->hardware_lock supposed to be held on entry */
  1674. static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
  1675. struct scsi_qla_host *vha)
  1676. {
  1677. uint32_t h;
  1678. struct ctio7_to_24xx *pkt;
  1679. struct qla_hw_data *ha = vha->hw;
  1680. struct atio_from_isp *atio = &prm->cmd->atio;
  1681. uint16_t temp;
  1682. pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
  1683. prm->pkt = pkt;
  1684. memset(pkt, 0, sizeof(*pkt));
  1685. pkt->entry_type = CTIO_TYPE7;
  1686. pkt->entry_count = (uint8_t)prm->req_cnt;
  1687. pkt->vp_index = vha->vp_idx;
  1688. h = qlt_make_handle(vha);
  1689. if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
  1690. /*
  1691. * CTIO type 7 from the firmware doesn't provide a way to
  1692. * know the initiator's LOOP ID, hence we can't find
  1693. * the session and, so, the command.
  1694. */
  1695. return -EAGAIN;
  1696. } else
  1697. ha->tgt.cmds[h-1] = prm->cmd;
  1698. pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
  1699. pkt->nport_handle = prm->cmd->loop_id;
  1700. pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
  1701. pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
  1702. pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
  1703. pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
  1704. pkt->exchange_addr = atio->u.isp24.exchange_addr;
  1705. pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
  1706. temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
  1707. pkt->u.status0.ox_id = cpu_to_le16(temp);
  1708. pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
  1709. return 0;
  1710. }
  1711. /*
  1712. * ha->hardware_lock supposed to be held on entry. We have already made sure
  1713. * that there is sufficient amount of request entries to not drop it.
  1714. */
  1715. static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
  1716. struct scsi_qla_host *vha)
  1717. {
  1718. int cnt;
  1719. uint32_t *dword_ptr;
  1720. int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
  1721. /* Build continuation packets */
  1722. while (prm->seg_cnt > 0) {
  1723. cont_a64_entry_t *cont_pkt64 =
  1724. (cont_a64_entry_t *)qlt_get_req_pkt(vha);
  1725. /*
  1726. * Make sure that from cont_pkt64 none of
  1727. * 64-bit specific fields used for 32-bit
  1728. * addressing. Cast to (cont_entry_t *) for
  1729. * that.
  1730. */
  1731. memset(cont_pkt64, 0, sizeof(*cont_pkt64));
  1732. cont_pkt64->entry_count = 1;
  1733. cont_pkt64->sys_define = 0;
  1734. if (enable_64bit_addressing) {
  1735. cont_pkt64->entry_type = CONTINUE_A64_TYPE;
  1736. dword_ptr =
  1737. (uint32_t *)&cont_pkt64->dseg_0_address;
  1738. } else {
  1739. cont_pkt64->entry_type = CONTINUE_TYPE;
  1740. dword_ptr =
  1741. (uint32_t *)&((cont_entry_t *)
  1742. cont_pkt64)->dseg_0_address;
  1743. }
  1744. /* Load continuation entry data segments */
  1745. for (cnt = 0;
  1746. cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
  1747. cnt++, prm->seg_cnt--) {
  1748. *dword_ptr++ =
  1749. cpu_to_le32(pci_dma_lo32
  1750. (sg_dma_address(prm->sg)));
  1751. if (enable_64bit_addressing) {
  1752. *dword_ptr++ =
  1753. cpu_to_le32(pci_dma_hi32
  1754. (sg_dma_address
  1755. (prm->sg)));
  1756. }
  1757. *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
  1758. prm->sg = sg_next(prm->sg);
  1759. }
  1760. }
  1761. }
  1762. /*
  1763. * ha->hardware_lock supposed to be held on entry. We have already made sure
  1764. * that there is sufficient amount of request entries to not drop it.
  1765. */
  1766. static void qlt_load_data_segments(struct qla_tgt_prm *prm,
  1767. struct scsi_qla_host *vha)
  1768. {
  1769. int cnt;
  1770. uint32_t *dword_ptr;
  1771. int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
  1772. struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
  1773. pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
  1774. /* Setup packet address segment pointer */
  1775. dword_ptr = pkt24->u.status0.dseg_0_address;
  1776. /* Set total data segment count */
  1777. if (prm->seg_cnt)
  1778. pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
  1779. if (prm->seg_cnt == 0) {
  1780. /* No data transfer */
  1781. *dword_ptr++ = 0;
  1782. *dword_ptr = 0;
  1783. return;
  1784. }
  1785. /* If scatter gather */
  1786. /* Load command entry data segments */
  1787. for (cnt = 0;
  1788. (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
  1789. cnt++, prm->seg_cnt--) {
  1790. *dword_ptr++ =
  1791. cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
  1792. if (enable_64bit_addressing) {
  1793. *dword_ptr++ =
  1794. cpu_to_le32(pci_dma_hi32(
  1795. sg_dma_address(prm->sg)));
  1796. }
  1797. *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
  1798. prm->sg = sg_next(prm->sg);
  1799. }
  1800. qlt_load_cont_data_segments(prm, vha);
  1801. }
  1802. static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
  1803. {
  1804. return cmd->bufflen > 0;
  1805. }
  1806. /*
  1807. * Called without ha->hardware_lock held
  1808. */
  1809. static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
  1810. struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
  1811. uint32_t *full_req_cnt)
  1812. {
  1813. struct qla_tgt *tgt = cmd->tgt;
  1814. struct scsi_qla_host *vha = tgt->vha;
  1815. struct qla_hw_data *ha = vha->hw;
  1816. struct se_cmd *se_cmd = &cmd->se_cmd;
  1817. prm->cmd = cmd;
  1818. prm->tgt = tgt;
  1819. prm->rq_result = scsi_status;
  1820. prm->sense_buffer = &cmd->sense_buffer[0];
  1821. prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
  1822. prm->sg = NULL;
  1823. prm->seg_cnt = -1;
  1824. prm->req_cnt = 1;
  1825. prm->add_status_pkt = 0;
  1826. /* Send marker if required */
  1827. if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
  1828. return -EFAULT;
  1829. if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
  1830. if (qlt_pci_map_calc_cnt(prm) != 0)
  1831. return -EAGAIN;
  1832. }
  1833. *full_req_cnt = prm->req_cnt;
  1834. if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  1835. prm->residual = se_cmd->residual_count;
  1836. ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
  1837. "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
  1838. prm->residual, se_cmd->tag,
  1839. se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
  1840. cmd->bufflen, prm->rq_result);
  1841. prm->rq_result |= SS_RESIDUAL_UNDER;
  1842. } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  1843. prm->residual = se_cmd->residual_count;
  1844. ql_dbg(ql_dbg_io, vha, 0x305d,
  1845. "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
  1846. prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
  1847. se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
  1848. prm->rq_result |= SS_RESIDUAL_OVER;
  1849. }
  1850. if (xmit_type & QLA_TGT_XMIT_STATUS) {
  1851. /*
  1852. * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
  1853. * ignored in *xmit_response() below
  1854. */
  1855. if (qlt_has_data(cmd)) {
  1856. if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
  1857. (IS_FWI2_CAPABLE(ha) &&
  1858. (prm->rq_result != 0))) {
  1859. prm->add_status_pkt = 1;
  1860. (*full_req_cnt)++;
  1861. }
  1862. }
  1863. }
  1864. return 0;
  1865. }
  1866. static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
  1867. struct qla_tgt_cmd *cmd, int sending_sense)
  1868. {
  1869. if (ha->tgt.enable_class_2)
  1870. return 0;
  1871. if (sending_sense)
  1872. return cmd->conf_compl_supported;
  1873. else
  1874. return ha->tgt.enable_explicit_conf &&
  1875. cmd->conf_compl_supported;
  1876. }
  1877. #ifdef CONFIG_QLA_TGT_DEBUG_SRR
  1878. /*
  1879. * Original taken from the XFS code
  1880. */
  1881. static unsigned long qlt_srr_random(void)
  1882. {
  1883. static int Inited;
  1884. static unsigned long RandomValue;
  1885. static DEFINE_SPINLOCK(lock);
  1886. /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
  1887. register long rv;
  1888. register long lo;
  1889. register long hi;
  1890. unsigned long flags;
  1891. spin_lock_irqsave(&lock, flags);
  1892. if (!Inited) {
  1893. RandomValue = jiffies;
  1894. Inited = 1;
  1895. }
  1896. rv = RandomValue;
  1897. hi = rv / 127773;
  1898. lo = rv % 127773;
  1899. rv = 16807 * lo - 2836 * hi;
  1900. if (rv <= 0)
  1901. rv += 2147483647;
  1902. RandomValue = rv;
  1903. spin_unlock_irqrestore(&lock, flags);
  1904. return rv;
  1905. }
  1906. static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
  1907. {
  1908. #if 0 /* This is not a real status packets lost, so it won't lead to SRR */
  1909. if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
  1910. == 50) {
  1911. *xmit_type &= ~QLA_TGT_XMIT_STATUS;
  1912. ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
  1913. "Dropping cmd %p (tag %d) status", cmd, se_cmd->tag);
  1914. }
  1915. #endif
  1916. /*
  1917. * It's currently not possible to simulate SRRs for FCP_WRITE without
  1918. * a physical link layer failure, so don't even try here..
  1919. */
  1920. if (cmd->dma_data_direction != DMA_FROM_DEVICE)
  1921. return;
  1922. if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
  1923. ((qlt_srr_random() % 100) == 20)) {
  1924. int i, leave = 0;
  1925. unsigned int tot_len = 0;
  1926. while (leave == 0)
  1927. leave = qlt_srr_random() % cmd->sg_cnt;
  1928. for (i = 0; i < leave; i++)
  1929. tot_len += cmd->sg[i].length;
  1930. ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
  1931. "Cutting cmd %p (tag %d) buffer"
  1932. " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
  1933. " cmd->sg_cnt %d)", cmd, se_cmd->tag, tot_len, leave,
  1934. cmd->bufflen, cmd->sg_cnt);
  1935. cmd->bufflen = tot_len;
  1936. cmd->sg_cnt = leave;
  1937. }
  1938. if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
  1939. unsigned int offset = qlt_srr_random() % cmd->bufflen;
  1940. ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
  1941. "Cutting cmd %p (tag %d) buffer head "
  1942. "to offset %d (cmd->bufflen %d)", cmd, se_cmd->tag, offset,
  1943. cmd->bufflen);
  1944. if (offset == 0)
  1945. *xmit_type &= ~QLA_TGT_XMIT_DATA;
  1946. else if (qlt_set_data_offset(cmd, offset)) {
  1947. ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
  1948. "qlt_set_data_offset() failed (tag %d)", se_cmd->tag);
  1949. }
  1950. }
  1951. }
  1952. #else
  1953. static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
  1954. {}
  1955. #endif
  1956. static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
  1957. struct qla_tgt_prm *prm)
  1958. {
  1959. prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
  1960. (uint32_t)sizeof(ctio->u.status1.sense_data));
  1961. ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
  1962. if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
  1963. ctio->u.status0.flags |= cpu_to_le16(
  1964. CTIO7_FLAGS_EXPLICIT_CONFORM |
  1965. CTIO7_FLAGS_CONFORM_REQ);
  1966. }
  1967. ctio->u.status0.residual = cpu_to_le32(prm->residual);
  1968. ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
  1969. if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
  1970. int i;
  1971. if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
  1972. if (prm->cmd->se_cmd.scsi_status != 0) {
  1973. ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
  1974. "Skipping EXPLICIT_CONFORM and "
  1975. "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
  1976. "non GOOD status\n");
  1977. goto skip_explict_conf;
  1978. }
  1979. ctio->u.status1.flags |= cpu_to_le16(
  1980. CTIO7_FLAGS_EXPLICIT_CONFORM |
  1981. CTIO7_FLAGS_CONFORM_REQ);
  1982. }
  1983. skip_explict_conf:
  1984. ctio->u.status1.flags &=
  1985. ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
  1986. ctio->u.status1.flags |=
  1987. cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
  1988. ctio->u.status1.scsi_status |=
  1989. cpu_to_le16(SS_SENSE_LEN_VALID);
  1990. ctio->u.status1.sense_length =
  1991. cpu_to_le16(prm->sense_buffer_len);
  1992. for (i = 0; i < prm->sense_buffer_len/4; i++)
  1993. ((uint32_t *)ctio->u.status1.sense_data)[i] =
  1994. cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
  1995. #if 0
  1996. if (unlikely((prm->sense_buffer_len % 4) != 0)) {
  1997. static int q;
  1998. if (q < 10) {
  1999. ql_dbg(ql_dbg_tgt, vha, 0xe04f,
  2000. "qla_target(%d): %d bytes of sense "
  2001. "lost", prm->tgt->ha->vp_idx,
  2002. prm->sense_buffer_len % 4);
  2003. q++;
  2004. }
  2005. }
  2006. #endif
  2007. } else {
  2008. ctio->u.status1.flags &=
  2009. ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
  2010. ctio->u.status1.flags |=
  2011. cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
  2012. ctio->u.status1.sense_length = 0;
  2013. memset(ctio->u.status1.sense_data, 0,
  2014. sizeof(ctio->u.status1.sense_data));
  2015. }
  2016. /* Sense with len > 24, is it possible ??? */
  2017. }
  2018. /* diff */
  2019. static inline int
  2020. qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
  2021. {
  2022. /*
  2023. * Uncomment when corresponding SCSI changes are done.
  2024. *
  2025. if (!sp->cmd->prot_chk)
  2026. return 0;
  2027. *
  2028. */
  2029. switch (se_cmd->prot_op) {
  2030. case TARGET_PROT_DOUT_INSERT:
  2031. case TARGET_PROT_DIN_STRIP:
  2032. if (ql2xenablehba_err_chk >= 1)
  2033. return 1;
  2034. break;
  2035. case TARGET_PROT_DOUT_PASS:
  2036. case TARGET_PROT_DIN_PASS:
  2037. if (ql2xenablehba_err_chk >= 2)
  2038. return 1;
  2039. break;
  2040. case TARGET_PROT_DIN_INSERT:
  2041. case TARGET_PROT_DOUT_STRIP:
  2042. return 1;
  2043. default:
  2044. break;
  2045. }
  2046. return 0;
  2047. }
  2048. /*
  2049. * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
  2050. *
  2051. */
  2052. static inline void
  2053. qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx)
  2054. {
  2055. uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
  2056. /* wait til Mode Sense/Select cmd, modepage Ah, subpage 2
  2057. * have been immplemented by TCM, before AppTag is avail.
  2058. * Look for modesense_handlers[]
  2059. */
  2060. ctx->app_tag = 0;
  2061. ctx->app_tag_mask[0] = 0x0;
  2062. ctx->app_tag_mask[1] = 0x0;
  2063. switch (se_cmd->prot_type) {
  2064. case TARGET_DIF_TYPE0_PROT:
  2065. /*
  2066. * No check for ql2xenablehba_err_chk, as it would be an
  2067. * I/O error if hba tag generation is not done.
  2068. */
  2069. ctx->ref_tag = cpu_to_le32(lba);
  2070. if (!qlt_hba_err_chk_enabled(se_cmd))
  2071. break;
  2072. /* enable ALL bytes of the ref tag */
  2073. ctx->ref_tag_mask[0] = 0xff;
  2074. ctx->ref_tag_mask[1] = 0xff;
  2075. ctx->ref_tag_mask[2] = 0xff;
  2076. ctx->ref_tag_mask[3] = 0xff;
  2077. break;
  2078. /*
  2079. * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
  2080. * 16 bit app tag.
  2081. */
  2082. case TARGET_DIF_TYPE1_PROT:
  2083. ctx->ref_tag = cpu_to_le32(lba);
  2084. if (!qlt_hba_err_chk_enabled(se_cmd))
  2085. break;
  2086. /* enable ALL bytes of the ref tag */
  2087. ctx->ref_tag_mask[0] = 0xff;
  2088. ctx->ref_tag_mask[1] = 0xff;
  2089. ctx->ref_tag_mask[2] = 0xff;
  2090. ctx->ref_tag_mask[3] = 0xff;
  2091. break;
  2092. /*
  2093. * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
  2094. * match LBA in CDB + N
  2095. */
  2096. case TARGET_DIF_TYPE2_PROT:
  2097. ctx->ref_tag = cpu_to_le32(lba);
  2098. if (!qlt_hba_err_chk_enabled(se_cmd))
  2099. break;
  2100. /* enable ALL bytes of the ref tag */
  2101. ctx->ref_tag_mask[0] = 0xff;
  2102. ctx->ref_tag_mask[1] = 0xff;
  2103. ctx->ref_tag_mask[2] = 0xff;
  2104. ctx->ref_tag_mask[3] = 0xff;
  2105. break;
  2106. /* For Type 3 protection: 16 bit GUARD only */
  2107. case TARGET_DIF_TYPE3_PROT:
  2108. ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
  2109. ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
  2110. break;
  2111. }
  2112. }
  2113. static inline int
  2114. qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
  2115. {
  2116. uint32_t *cur_dsd;
  2117. uint32_t transfer_length = 0;
  2118. uint32_t data_bytes;
  2119. uint32_t dif_bytes;
  2120. uint8_t bundling = 1;
  2121. uint8_t *clr_ptr;
  2122. struct crc_context *crc_ctx_pkt = NULL;
  2123. struct qla_hw_data *ha;
  2124. struct ctio_crc2_to_fw *pkt;
  2125. dma_addr_t crc_ctx_dma;
  2126. uint16_t fw_prot_opts = 0;
  2127. struct qla_tgt_cmd *cmd = prm->cmd;
  2128. struct se_cmd *se_cmd = &cmd->se_cmd;
  2129. uint32_t h;
  2130. struct atio_from_isp *atio = &prm->cmd->atio;
  2131. uint16_t t16;
  2132. ha = vha->hw;
  2133. pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
  2134. prm->pkt = pkt;
  2135. memset(pkt, 0, sizeof(*pkt));
  2136. ql_dbg(ql_dbg_tgt, vha, 0xe071,
  2137. "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
  2138. vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
  2139. prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
  2140. if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
  2141. (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
  2142. bundling = 0;
  2143. /* Compute dif len and adjust data len to incude protection */
  2144. data_bytes = cmd->bufflen;
  2145. dif_bytes = (data_bytes / cmd->blk_sz) * 8;
  2146. switch (se_cmd->prot_op) {
  2147. case TARGET_PROT_DIN_INSERT:
  2148. case TARGET_PROT_DOUT_STRIP:
  2149. transfer_length = data_bytes;
  2150. data_bytes += dif_bytes;
  2151. break;
  2152. case TARGET_PROT_DIN_STRIP:
  2153. case TARGET_PROT_DOUT_INSERT:
  2154. case TARGET_PROT_DIN_PASS:
  2155. case TARGET_PROT_DOUT_PASS:
  2156. transfer_length = data_bytes + dif_bytes;
  2157. break;
  2158. default:
  2159. BUG();
  2160. break;
  2161. }
  2162. if (!qlt_hba_err_chk_enabled(se_cmd))
  2163. fw_prot_opts |= 0x10; /* Disable Guard tag checking */
  2164. /* HBA error checking enabled */
  2165. else if (IS_PI_UNINIT_CAPABLE(ha)) {
  2166. if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
  2167. (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
  2168. fw_prot_opts |= PO_DIS_VALD_APP_ESC;
  2169. else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
  2170. fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
  2171. }
  2172. switch (se_cmd->prot_op) {
  2173. case TARGET_PROT_DIN_INSERT:
  2174. case TARGET_PROT_DOUT_INSERT:
  2175. fw_prot_opts |= PO_MODE_DIF_INSERT;
  2176. break;
  2177. case TARGET_PROT_DIN_STRIP:
  2178. case TARGET_PROT_DOUT_STRIP:
  2179. fw_prot_opts |= PO_MODE_DIF_REMOVE;
  2180. break;
  2181. case TARGET_PROT_DIN_PASS:
  2182. case TARGET_PROT_DOUT_PASS:
  2183. fw_prot_opts |= PO_MODE_DIF_PASS;
  2184. /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
  2185. break;
  2186. default:/* Normal Request */
  2187. fw_prot_opts |= PO_MODE_DIF_PASS;
  2188. break;
  2189. }
  2190. /* ---- PKT ---- */
  2191. /* Update entry type to indicate Command Type CRC_2 IOCB */
  2192. pkt->entry_type = CTIO_CRC2;
  2193. pkt->entry_count = 1;
  2194. pkt->vp_index = vha->vp_idx;
  2195. h = qlt_make_handle(vha);
  2196. if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
  2197. /*
  2198. * CTIO type 7 from the firmware doesn't provide a way to
  2199. * know the initiator's LOOP ID, hence we can't find
  2200. * the session and, so, the command.
  2201. */
  2202. return -EAGAIN;
  2203. } else
  2204. ha->tgt.cmds[h-1] = prm->cmd;
  2205. pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
  2206. pkt->nport_handle = prm->cmd->loop_id;
  2207. pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
  2208. pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
  2209. pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
  2210. pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
  2211. pkt->exchange_addr = atio->u.isp24.exchange_addr;
  2212. /* silence compile warning */
  2213. t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
  2214. pkt->ox_id = cpu_to_le16(t16);
  2215. t16 = (atio->u.isp24.attr << 9);
  2216. pkt->flags |= cpu_to_le16(t16);
  2217. pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
  2218. /* Set transfer direction */
  2219. if (cmd->dma_data_direction == DMA_TO_DEVICE)
  2220. pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
  2221. else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
  2222. pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
  2223. pkt->dseg_count = prm->tot_dsds;
  2224. /* Fibre channel byte count */
  2225. pkt->transfer_length = cpu_to_le32(transfer_length);
  2226. /* ----- CRC context -------- */
  2227. /* Allocate CRC context from global pool */
  2228. crc_ctx_pkt = cmd->ctx =
  2229. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
  2230. if (!crc_ctx_pkt)
  2231. goto crc_queuing_error;
  2232. /* Zero out CTX area. */
  2233. clr_ptr = (uint8_t *)crc_ctx_pkt;
  2234. memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
  2235. crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
  2236. INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
  2237. /* Set handle */
  2238. crc_ctx_pkt->handle = pkt->handle;
  2239. qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt);
  2240. pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
  2241. pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
  2242. pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
  2243. if (!bundling) {
  2244. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
  2245. } else {
  2246. /*
  2247. * Configure Bundling if we need to fetch interlaving
  2248. * protection PCI accesses
  2249. */
  2250. fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  2251. crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  2252. crc_ctx_pkt->u.bundling.dseg_count =
  2253. cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
  2254. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
  2255. }
  2256. /* Finish the common fields of CRC pkt */
  2257. crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
  2258. crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
  2259. crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
  2260. crc_ctx_pkt->guard_seed = cpu_to_le16(0);
  2261. /* Walks data segments */
  2262. pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
  2263. if (!bundling && prm->prot_seg_cnt) {
  2264. if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
  2265. prm->tot_dsds, cmd))
  2266. goto crc_queuing_error;
  2267. } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
  2268. (prm->tot_dsds - prm->prot_seg_cnt), cmd))
  2269. goto crc_queuing_error;
  2270. if (bundling && prm->prot_seg_cnt) {
  2271. /* Walks dif segments */
  2272. pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
  2273. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
  2274. if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
  2275. prm->prot_seg_cnt, cmd))
  2276. goto crc_queuing_error;
  2277. }
  2278. return QLA_SUCCESS;
  2279. crc_queuing_error:
  2280. /* Cleanup will be performed by the caller */
  2281. return QLA_FUNCTION_FAILED;
  2282. }
  2283. /*
  2284. * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
  2285. * QLA_TGT_XMIT_STATUS for >= 24xx silicon
  2286. */
  2287. int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
  2288. uint8_t scsi_status)
  2289. {
  2290. struct scsi_qla_host *vha = cmd->vha;
  2291. struct qla_hw_data *ha = vha->hw;
  2292. struct ctio7_to_24xx *pkt;
  2293. struct qla_tgt_prm prm;
  2294. uint32_t full_req_cnt = 0;
  2295. unsigned long flags = 0;
  2296. int res;
  2297. spin_lock_irqsave(&ha->hardware_lock, flags);
  2298. if (cmd->sess && cmd->sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
  2299. cmd->state = QLA_TGT_STATE_PROCESSED;
  2300. if (cmd->sess->logout_completed)
  2301. /* no need to terminate. FW already freed exchange. */
  2302. qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
  2303. else
  2304. qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
  2305. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2306. return 0;
  2307. }
  2308. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2309. memset(&prm, 0, sizeof(prm));
  2310. qlt_check_srr_debug(cmd, &xmit_type);
  2311. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
  2312. "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
  2313. (xmit_type & QLA_TGT_XMIT_STATUS) ?
  2314. 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
  2315. &cmd->se_cmd);
  2316. res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
  2317. &full_req_cnt);
  2318. if (unlikely(res != 0)) {
  2319. return res;
  2320. }
  2321. spin_lock_irqsave(&ha->hardware_lock, flags);
  2322. if (xmit_type == QLA_TGT_XMIT_STATUS)
  2323. vha->tgt_counters.core_qla_snd_status++;
  2324. else
  2325. vha->tgt_counters.core_qla_que_buf++;
  2326. if (!vha->flags.online || cmd->reset_count != ha->chip_reset) {
  2327. /*
  2328. * Either the port is not online or this request was from
  2329. * previous life, just abort the processing.
  2330. */
  2331. cmd->state = QLA_TGT_STATE_PROCESSED;
  2332. qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
  2333. ql_dbg(ql_dbg_async, vha, 0xe101,
  2334. "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
  2335. vha->flags.online, qla2x00_reset_active(vha),
  2336. cmd->reset_count, ha->chip_reset);
  2337. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2338. return 0;
  2339. }
  2340. /* Does F/W have an IOCBs for this request */
  2341. res = qlt_check_reserve_free_req(vha, full_req_cnt);
  2342. if (unlikely(res))
  2343. goto out_unmap_unlock;
  2344. if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
  2345. res = qlt_build_ctio_crc2_pkt(&prm, vha);
  2346. else
  2347. res = qlt_24xx_build_ctio_pkt(&prm, vha);
  2348. if (unlikely(res != 0)) {
  2349. vha->req->cnt += full_req_cnt;
  2350. goto out_unmap_unlock;
  2351. }
  2352. pkt = (struct ctio7_to_24xx *)prm.pkt;
  2353. if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
  2354. pkt->u.status0.flags |=
  2355. cpu_to_le16(CTIO7_FLAGS_DATA_IN |
  2356. CTIO7_FLAGS_STATUS_MODE_0);
  2357. if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
  2358. qlt_load_data_segments(&prm, vha);
  2359. if (prm.add_status_pkt == 0) {
  2360. if (xmit_type & QLA_TGT_XMIT_STATUS) {
  2361. pkt->u.status0.scsi_status =
  2362. cpu_to_le16(prm.rq_result);
  2363. pkt->u.status0.residual =
  2364. cpu_to_le32(prm.residual);
  2365. pkt->u.status0.flags |= cpu_to_le16(
  2366. CTIO7_FLAGS_SEND_STATUS);
  2367. if (qlt_need_explicit_conf(ha, cmd, 0)) {
  2368. pkt->u.status0.flags |=
  2369. cpu_to_le16(
  2370. CTIO7_FLAGS_EXPLICIT_CONFORM |
  2371. CTIO7_FLAGS_CONFORM_REQ);
  2372. }
  2373. }
  2374. } else {
  2375. /*
  2376. * We have already made sure that there is sufficient
  2377. * amount of request entries to not drop HW lock in
  2378. * req_pkt().
  2379. */
  2380. struct ctio7_to_24xx *ctio =
  2381. (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
  2382. ql_dbg(ql_dbg_io, vha, 0x305e,
  2383. "Building additional status packet 0x%p.\n",
  2384. ctio);
  2385. /*
  2386. * T10Dif: ctio_crc2_to_fw overlay ontop of
  2387. * ctio7_to_24xx
  2388. */
  2389. memcpy(ctio, pkt, sizeof(*ctio));
  2390. /* reset back to CTIO7 */
  2391. ctio->entry_count = 1;
  2392. ctio->entry_type = CTIO_TYPE7;
  2393. ctio->dseg_count = 0;
  2394. ctio->u.status1.flags &= ~cpu_to_le16(
  2395. CTIO7_FLAGS_DATA_IN);
  2396. /* Real finish is ctio_m1's finish */
  2397. pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
  2398. pkt->u.status0.flags |= cpu_to_le16(
  2399. CTIO7_FLAGS_DONT_RET_CTIO);
  2400. /* qlt_24xx_init_ctio_to_isp will correct
  2401. * all neccessary fields that's part of CTIO7.
  2402. * There should be no residual of CTIO-CRC2 data.
  2403. */
  2404. qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
  2405. &prm);
  2406. pr_debug("Status CTIO7: %p\n", ctio);
  2407. }
  2408. } else
  2409. qlt_24xx_init_ctio_to_isp(pkt, &prm);
  2410. cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
  2411. cmd->cmd_sent_to_fw = 1;
  2412. /* Memory Barrier */
  2413. wmb();
  2414. qla2x00_start_iocbs(vha, vha->req);
  2415. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2416. return 0;
  2417. out_unmap_unlock:
  2418. qlt_unmap_sg(vha, cmd);
  2419. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2420. return res;
  2421. }
  2422. EXPORT_SYMBOL(qlt_xmit_response);
  2423. int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
  2424. {
  2425. struct ctio7_to_24xx *pkt;
  2426. struct scsi_qla_host *vha = cmd->vha;
  2427. struct qla_hw_data *ha = vha->hw;
  2428. struct qla_tgt *tgt = cmd->tgt;
  2429. struct qla_tgt_prm prm;
  2430. unsigned long flags;
  2431. int res = 0;
  2432. memset(&prm, 0, sizeof(prm));
  2433. prm.cmd = cmd;
  2434. prm.tgt = tgt;
  2435. prm.sg = NULL;
  2436. prm.req_cnt = 1;
  2437. /* Send marker if required */
  2438. if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
  2439. return -EIO;
  2440. /* Calculate number of entries and segments required */
  2441. if (qlt_pci_map_calc_cnt(&prm) != 0)
  2442. return -EAGAIN;
  2443. spin_lock_irqsave(&ha->hardware_lock, flags);
  2444. if (!vha->flags.online || (cmd->reset_count != ha->chip_reset) ||
  2445. (cmd->sess && cmd->sess->deleted == QLA_SESS_DELETION_IN_PROGRESS)) {
  2446. /*
  2447. * Either the port is not online or this request was from
  2448. * previous life, just abort the processing.
  2449. */
  2450. cmd->state = QLA_TGT_STATE_NEED_DATA;
  2451. qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
  2452. ql_dbg(ql_dbg_async, vha, 0xe102,
  2453. "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
  2454. vha->flags.online, qla2x00_reset_active(vha),
  2455. cmd->reset_count, ha->chip_reset);
  2456. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2457. return 0;
  2458. }
  2459. /* Does F/W have an IOCBs for this request */
  2460. res = qlt_check_reserve_free_req(vha, prm.req_cnt);
  2461. if (res != 0)
  2462. goto out_unlock_free_unmap;
  2463. if (cmd->se_cmd.prot_op)
  2464. res = qlt_build_ctio_crc2_pkt(&prm, vha);
  2465. else
  2466. res = qlt_24xx_build_ctio_pkt(&prm, vha);
  2467. if (unlikely(res != 0)) {
  2468. vha->req->cnt += prm.req_cnt;
  2469. goto out_unlock_free_unmap;
  2470. }
  2471. pkt = (struct ctio7_to_24xx *)prm.pkt;
  2472. pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
  2473. CTIO7_FLAGS_STATUS_MODE_0);
  2474. if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
  2475. qlt_load_data_segments(&prm, vha);
  2476. cmd->state = QLA_TGT_STATE_NEED_DATA;
  2477. cmd->cmd_sent_to_fw = 1;
  2478. /* Memory Barrier */
  2479. wmb();
  2480. qla2x00_start_iocbs(vha, vha->req);
  2481. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2482. return res;
  2483. out_unlock_free_unmap:
  2484. qlt_unmap_sg(vha, cmd);
  2485. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2486. return res;
  2487. }
  2488. EXPORT_SYMBOL(qlt_rdy_to_xfer);
  2489. /*
  2490. * Checks the guard or meta-data for the type of error
  2491. * detected by the HBA.
  2492. */
  2493. static inline int
  2494. qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
  2495. struct ctio_crc_from_fw *sts)
  2496. {
  2497. uint8_t *ap = &sts->actual_dif[0];
  2498. uint8_t *ep = &sts->expected_dif[0];
  2499. uint32_t e_ref_tag, a_ref_tag;
  2500. uint16_t e_app_tag, a_app_tag;
  2501. uint16_t e_guard, a_guard;
  2502. uint64_t lba = cmd->se_cmd.t_task_lba;
  2503. a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
  2504. a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
  2505. a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
  2506. e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
  2507. e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
  2508. e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
  2509. ql_dbg(ql_dbg_tgt, vha, 0xe075,
  2510. "iocb(s) %p Returned STATUS.\n", sts);
  2511. ql_dbg(ql_dbg_tgt, vha, 0xf075,
  2512. "dif check TGT cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x]\n",
  2513. cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
  2514. a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard);
  2515. /*
  2516. * Ignore sector if:
  2517. * For type 3: ref & app tag is all 'f's
  2518. * For type 0,1,2: app tag is all 'f's
  2519. */
  2520. if ((a_app_tag == 0xffff) &&
  2521. ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) ||
  2522. (a_ref_tag == 0xffffffff))) {
  2523. uint32_t blocks_done;
  2524. /* 2TB boundary case covered automatically with this */
  2525. blocks_done = e_ref_tag - (uint32_t)lba + 1;
  2526. cmd->se_cmd.bad_sector = e_ref_tag;
  2527. cmd->se_cmd.pi_err = 0;
  2528. ql_dbg(ql_dbg_tgt, vha, 0xf074,
  2529. "need to return scsi good\n");
  2530. /* Update protection tag */
  2531. if (cmd->prot_sg_cnt) {
  2532. uint32_t i, k = 0, num_ent;
  2533. struct scatterlist *sg, *sgl;
  2534. sgl = cmd->prot_sg;
  2535. /* Patch the corresponding protection tags */
  2536. for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) {
  2537. num_ent = sg_dma_len(sg) / 8;
  2538. if (k + num_ent < blocks_done) {
  2539. k += num_ent;
  2540. continue;
  2541. }
  2542. k = blocks_done;
  2543. break;
  2544. }
  2545. if (k != blocks_done) {
  2546. ql_log(ql_log_warn, vha, 0xf076,
  2547. "unexpected tag values tag:lba=%u:%llu)\n",
  2548. e_ref_tag, (unsigned long long)lba);
  2549. goto out;
  2550. }
  2551. #if 0
  2552. struct sd_dif_tuple *spt;
  2553. /* TODO:
  2554. * This section came from initiator. Is it valid here?
  2555. * should ulp be override with actual val???
  2556. */
  2557. spt = page_address(sg_page(sg)) + sg->offset;
  2558. spt += j;
  2559. spt->app_tag = 0xffff;
  2560. if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3)
  2561. spt->ref_tag = 0xffffffff;
  2562. #endif
  2563. }
  2564. return 0;
  2565. }
  2566. /* check guard */
  2567. if (e_guard != a_guard) {
  2568. cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
  2569. cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
  2570. ql_log(ql_log_warn, vha, 0xe076,
  2571. "Guard ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
  2572. cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
  2573. a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
  2574. a_guard, e_guard, cmd);
  2575. goto out;
  2576. }
  2577. /* check ref tag */
  2578. if (e_ref_tag != a_ref_tag) {
  2579. cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
  2580. cmd->se_cmd.bad_sector = e_ref_tag;
  2581. ql_log(ql_log_warn, vha, 0xe077,
  2582. "Ref Tag ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
  2583. cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
  2584. a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
  2585. a_guard, e_guard, cmd);
  2586. goto out;
  2587. }
  2588. /* check appl tag */
  2589. if (e_app_tag != a_app_tag) {
  2590. cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
  2591. cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
  2592. ql_log(ql_log_warn, vha, 0xe078,
  2593. "App Tag ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
  2594. cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
  2595. a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
  2596. a_guard, e_guard, cmd);
  2597. goto out;
  2598. }
  2599. out:
  2600. return 1;
  2601. }
  2602. /* If hardware_lock held on entry, might drop it, then reaquire */
  2603. /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
  2604. static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
  2605. struct imm_ntfy_from_isp *ntfy)
  2606. {
  2607. struct nack_to_isp *nack;
  2608. struct qla_hw_data *ha = vha->hw;
  2609. request_t *pkt;
  2610. int ret = 0;
  2611. ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
  2612. "Sending TERM ELS CTIO (ha=%p)\n", ha);
  2613. pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
  2614. if (pkt == NULL) {
  2615. ql_dbg(ql_dbg_tgt, vha, 0xe080,
  2616. "qla_target(%d): %s failed: unable to allocate "
  2617. "request packet\n", vha->vp_idx, __func__);
  2618. return -ENOMEM;
  2619. }
  2620. pkt->entry_type = NOTIFY_ACK_TYPE;
  2621. pkt->entry_count = 1;
  2622. pkt->handle = QLA_TGT_SKIP_HANDLE;
  2623. nack = (struct nack_to_isp *)pkt;
  2624. nack->ox_id = ntfy->ox_id;
  2625. nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
  2626. if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
  2627. nack->u.isp24.flags = ntfy->u.isp24.flags &
  2628. __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
  2629. }
  2630. /* terminate */
  2631. nack->u.isp24.flags |=
  2632. __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
  2633. nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
  2634. nack->u.isp24.status = ntfy->u.isp24.status;
  2635. nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
  2636. nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
  2637. nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
  2638. nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
  2639. nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
  2640. nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
  2641. qla2x00_start_iocbs(vha, vha->req);
  2642. return ret;
  2643. }
  2644. static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
  2645. struct imm_ntfy_from_isp *imm, int ha_locked)
  2646. {
  2647. unsigned long flags = 0;
  2648. int rc;
  2649. if (qlt_issue_marker(vha, ha_locked) < 0)
  2650. return;
  2651. if (ha_locked) {
  2652. rc = __qlt_send_term_imm_notif(vha, imm);
  2653. #if 0 /* Todo */
  2654. if (rc == -ENOMEM)
  2655. qlt_alloc_qfull_cmd(vha, imm, 0, 0);
  2656. #endif
  2657. goto done;
  2658. }
  2659. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  2660. rc = __qlt_send_term_imm_notif(vha, imm);
  2661. #if 0 /* Todo */
  2662. if (rc == -ENOMEM)
  2663. qlt_alloc_qfull_cmd(vha, imm, 0, 0);
  2664. #endif
  2665. done:
  2666. if (!ha_locked)
  2667. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  2668. }
  2669. /* If hardware_lock held on entry, might drop it, then reaquire */
  2670. /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
  2671. static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
  2672. struct qla_tgt_cmd *cmd,
  2673. struct atio_from_isp *atio)
  2674. {
  2675. struct ctio7_to_24xx *ctio24;
  2676. struct qla_hw_data *ha = vha->hw;
  2677. request_t *pkt;
  2678. int ret = 0;
  2679. uint16_t temp;
  2680. ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
  2681. pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
  2682. if (pkt == NULL) {
  2683. ql_dbg(ql_dbg_tgt, vha, 0xe050,
  2684. "qla_target(%d): %s failed: unable to allocate "
  2685. "request packet\n", vha->vp_idx, __func__);
  2686. return -ENOMEM;
  2687. }
  2688. if (cmd != NULL) {
  2689. if (cmd->state < QLA_TGT_STATE_PROCESSED) {
  2690. ql_dbg(ql_dbg_tgt, vha, 0xe051,
  2691. "qla_target(%d): Terminating cmd %p with "
  2692. "incorrect state %d\n", vha->vp_idx, cmd,
  2693. cmd->state);
  2694. } else
  2695. ret = 1;
  2696. }
  2697. vha->tgt_counters.num_term_xchg_sent++;
  2698. pkt->entry_count = 1;
  2699. pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
  2700. ctio24 = (struct ctio7_to_24xx *)pkt;
  2701. ctio24->entry_type = CTIO_TYPE7;
  2702. ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
  2703. ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
  2704. ctio24->vp_index = vha->vp_idx;
  2705. ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
  2706. ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
  2707. ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
  2708. ctio24->exchange_addr = atio->u.isp24.exchange_addr;
  2709. ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
  2710. cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
  2711. CTIO7_FLAGS_TERMINATE);
  2712. temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
  2713. ctio24->u.status1.ox_id = cpu_to_le16(temp);
  2714. /* Most likely, it isn't needed */
  2715. ctio24->u.status1.residual = get_unaligned((uint32_t *)
  2716. &atio->u.isp24.fcp_cmnd.add_cdb[
  2717. atio->u.isp24.fcp_cmnd.add_cdb_len]);
  2718. if (ctio24->u.status1.residual != 0)
  2719. ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
  2720. /* Memory Barrier */
  2721. wmb();
  2722. qla2x00_start_iocbs(vha, vha->req);
  2723. return ret;
  2724. }
  2725. static void qlt_send_term_exchange(struct scsi_qla_host *vha,
  2726. struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
  2727. int ul_abort)
  2728. {
  2729. unsigned long flags = 0;
  2730. int rc;
  2731. if (qlt_issue_marker(vha, ha_locked) < 0)
  2732. return;
  2733. if (ha_locked) {
  2734. rc = __qlt_send_term_exchange(vha, cmd, atio);
  2735. if (rc == -ENOMEM)
  2736. qlt_alloc_qfull_cmd(vha, atio, 0, 0);
  2737. goto done;
  2738. }
  2739. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  2740. rc = __qlt_send_term_exchange(vha, cmd, atio);
  2741. if (rc == -ENOMEM)
  2742. qlt_alloc_qfull_cmd(vha, atio, 0, 0);
  2743. done:
  2744. if (cmd && !ul_abort && !cmd->aborted) {
  2745. if (cmd->sg_mapped)
  2746. qlt_unmap_sg(vha, cmd);
  2747. vha->hw->tgt.tgt_ops->free_cmd(cmd);
  2748. }
  2749. if (!ha_locked)
  2750. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  2751. return;
  2752. }
  2753. static void qlt_init_term_exchange(struct scsi_qla_host *vha)
  2754. {
  2755. struct list_head free_list;
  2756. struct qla_tgt_cmd *cmd, *tcmd;
  2757. vha->hw->tgt.leak_exchg_thresh_hold =
  2758. (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
  2759. cmd = tcmd = NULL;
  2760. if (!list_empty(&vha->hw->tgt.q_full_list)) {
  2761. INIT_LIST_HEAD(&free_list);
  2762. list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
  2763. list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
  2764. list_del(&cmd->cmd_list);
  2765. /* This cmd was never sent to TCM. There is no need
  2766. * to schedule free or call free_cmd
  2767. */
  2768. qlt_free_cmd(cmd);
  2769. vha->hw->tgt.num_qfull_cmds_alloc--;
  2770. }
  2771. }
  2772. vha->hw->tgt.num_qfull_cmds_dropped = 0;
  2773. }
  2774. static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
  2775. {
  2776. uint32_t total_leaked;
  2777. total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
  2778. if (vha->hw->tgt.leak_exchg_thresh_hold &&
  2779. (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
  2780. ql_dbg(ql_dbg_tgt, vha, 0xe079,
  2781. "Chip reset due to exchange starvation: %d/%d.\n",
  2782. total_leaked, vha->hw->cur_fw_xcb_count);
  2783. if (IS_P3P_TYPE(vha->hw))
  2784. set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
  2785. else
  2786. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  2787. qla2xxx_wake_dpc(vha);
  2788. }
  2789. }
  2790. int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
  2791. {
  2792. struct qla_tgt *tgt = cmd->tgt;
  2793. struct scsi_qla_host *vha = tgt->vha;
  2794. struct se_cmd *se_cmd = &cmd->se_cmd;
  2795. unsigned long flags;
  2796. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
  2797. "qla_target(%d): terminating exchange for aborted cmd=%p "
  2798. "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
  2799. se_cmd->tag);
  2800. spin_lock_irqsave(&cmd->cmd_lock, flags);
  2801. if (cmd->aborted) {
  2802. spin_unlock_irqrestore(&cmd->cmd_lock, flags);
  2803. /*
  2804. * It's normal to see 2 calls in this path:
  2805. * 1) XFER Rdy completion + CMD_T_ABORT
  2806. * 2) TCM TMR - drain_state_list
  2807. */
  2808. ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
  2809. "multiple abort. %p transport_state %x, t_state %x,"
  2810. " se_cmd_flags %x \n", cmd, cmd->se_cmd.transport_state,
  2811. cmd->se_cmd.t_state,cmd->se_cmd.se_cmd_flags);
  2812. return EIO;
  2813. }
  2814. cmd->aborted = 1;
  2815. cmd->cmd_flags |= BIT_6;
  2816. spin_unlock_irqrestore(&cmd->cmd_lock, flags);
  2817. qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
  2818. return 0;
  2819. }
  2820. EXPORT_SYMBOL(qlt_abort_cmd);
  2821. void qlt_free_cmd(struct qla_tgt_cmd *cmd)
  2822. {
  2823. struct qla_tgt_sess *sess = cmd->sess;
  2824. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
  2825. "%s: se_cmd[%p] ox_id %04x\n",
  2826. __func__, &cmd->se_cmd,
  2827. be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
  2828. BUG_ON(cmd->cmd_in_wq);
  2829. if (cmd->sg_mapped)
  2830. qlt_unmap_sg(cmd->vha, cmd);
  2831. if (!cmd->q_full)
  2832. qlt_decr_num_pend_cmds(cmd->vha);
  2833. BUG_ON(cmd->sg_mapped);
  2834. cmd->jiffies_at_free = get_jiffies_64();
  2835. if (unlikely(cmd->free_sg))
  2836. kfree(cmd->sg);
  2837. if (!sess || !sess->se_sess) {
  2838. WARN_ON(1);
  2839. return;
  2840. }
  2841. cmd->jiffies_at_free = get_jiffies_64();
  2842. percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
  2843. }
  2844. EXPORT_SYMBOL(qlt_free_cmd);
  2845. /* ha->hardware_lock supposed to be held on entry */
  2846. static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
  2847. struct qla_tgt_cmd *cmd, void *ctio)
  2848. {
  2849. struct qla_tgt_srr_ctio *sc;
  2850. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  2851. struct qla_tgt_srr_imm *imm;
  2852. tgt->ctio_srr_id++;
  2853. cmd->cmd_flags |= BIT_15;
  2854. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
  2855. "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
  2856. if (!ctio) {
  2857. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
  2858. "qla_target(%d): SRR CTIO, but ctio is NULL\n",
  2859. vha->vp_idx);
  2860. return -EINVAL;
  2861. }
  2862. sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
  2863. if (sc != NULL) {
  2864. sc->cmd = cmd;
  2865. /* IRQ is already OFF */
  2866. spin_lock(&tgt->srr_lock);
  2867. sc->srr_id = tgt->ctio_srr_id;
  2868. list_add_tail(&sc->srr_list_entry,
  2869. &tgt->srr_ctio_list);
  2870. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
  2871. "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
  2872. if (tgt->imm_srr_id == tgt->ctio_srr_id) {
  2873. int found = 0;
  2874. list_for_each_entry(imm, &tgt->srr_imm_list,
  2875. srr_list_entry) {
  2876. if (imm->srr_id == sc->srr_id) {
  2877. found = 1;
  2878. break;
  2879. }
  2880. }
  2881. if (found) {
  2882. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
  2883. "Scheduling srr work\n");
  2884. schedule_work(&tgt->srr_work);
  2885. } else {
  2886. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
  2887. "qla_target(%d): imm_srr_id "
  2888. "== ctio_srr_id (%d), but there is no "
  2889. "corresponding SRR IMM, deleting CTIO "
  2890. "SRR %p\n", vha->vp_idx,
  2891. tgt->ctio_srr_id, sc);
  2892. list_del(&sc->srr_list_entry);
  2893. spin_unlock(&tgt->srr_lock);
  2894. kfree(sc);
  2895. return -EINVAL;
  2896. }
  2897. }
  2898. spin_unlock(&tgt->srr_lock);
  2899. } else {
  2900. struct qla_tgt_srr_imm *ti;
  2901. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
  2902. "qla_target(%d): Unable to allocate SRR CTIO entry\n",
  2903. vha->vp_idx);
  2904. spin_lock(&tgt->srr_lock);
  2905. list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
  2906. srr_list_entry) {
  2907. if (imm->srr_id == tgt->ctio_srr_id) {
  2908. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
  2909. "IMM SRR %p deleted (id %d)\n",
  2910. imm, imm->srr_id);
  2911. list_del(&imm->srr_list_entry);
  2912. qlt_reject_free_srr_imm(vha, imm, 1);
  2913. }
  2914. }
  2915. spin_unlock(&tgt->srr_lock);
  2916. return -ENOMEM;
  2917. }
  2918. return 0;
  2919. }
  2920. /*
  2921. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  2922. */
  2923. static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
  2924. struct qla_tgt_cmd *cmd, uint32_t status)
  2925. {
  2926. int term = 0;
  2927. if (ctio != NULL) {
  2928. struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
  2929. term = !(c->flags &
  2930. cpu_to_le16(OF_TERM_EXCH));
  2931. } else
  2932. term = 1;
  2933. if (term)
  2934. qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
  2935. return term;
  2936. }
  2937. /* ha->hardware_lock supposed to be held on entry */
  2938. static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
  2939. uint32_t handle)
  2940. {
  2941. struct qla_hw_data *ha = vha->hw;
  2942. handle--;
  2943. if (ha->tgt.cmds[handle] != NULL) {
  2944. struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
  2945. ha->tgt.cmds[handle] = NULL;
  2946. return cmd;
  2947. } else
  2948. return NULL;
  2949. }
  2950. /* ha->hardware_lock supposed to be held on entry */
  2951. static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
  2952. uint32_t handle, void *ctio)
  2953. {
  2954. struct qla_tgt_cmd *cmd = NULL;
  2955. /* Clear out internal marks */
  2956. handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
  2957. CTIO_INTERMEDIATE_HANDLE_MARK);
  2958. if (handle != QLA_TGT_NULL_HANDLE) {
  2959. if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
  2960. return NULL;
  2961. /* handle-1 is actually used */
  2962. if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
  2963. ql_dbg(ql_dbg_tgt, vha, 0xe052,
  2964. "qla_target(%d): Wrong handle %x received\n",
  2965. vha->vp_idx, handle);
  2966. return NULL;
  2967. }
  2968. cmd = qlt_get_cmd(vha, handle);
  2969. if (unlikely(cmd == NULL)) {
  2970. ql_dbg(ql_dbg_tgt, vha, 0xe053,
  2971. "qla_target(%d): Suspicious: unable to "
  2972. "find the command with handle %x\n", vha->vp_idx,
  2973. handle);
  2974. return NULL;
  2975. }
  2976. } else if (ctio != NULL) {
  2977. /* We can't get loop ID from CTIO7 */
  2978. ql_dbg(ql_dbg_tgt, vha, 0xe054,
  2979. "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
  2980. "support NULL handles\n", vha->vp_idx);
  2981. return NULL;
  2982. }
  2983. return cmd;
  2984. }
  2985. /* hardware_lock should be held by caller. */
  2986. static void
  2987. qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
  2988. {
  2989. struct qla_hw_data *ha = vha->hw;
  2990. uint32_t handle;
  2991. if (cmd->sg_mapped)
  2992. qlt_unmap_sg(vha, cmd);
  2993. handle = qlt_make_handle(vha);
  2994. /* TODO: fix debug message type and ids. */
  2995. if (cmd->state == QLA_TGT_STATE_PROCESSED) {
  2996. ql_dbg(ql_dbg_io, vha, 0xff00,
  2997. "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
  2998. } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
  2999. cmd->write_data_transferred = 0;
  3000. cmd->state = QLA_TGT_STATE_DATA_IN;
  3001. ql_dbg(ql_dbg_io, vha, 0xff01,
  3002. "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
  3003. ha->tgt.tgt_ops->handle_data(cmd);
  3004. return;
  3005. } else {
  3006. ql_dbg(ql_dbg_io, vha, 0xff03,
  3007. "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
  3008. cmd->state);
  3009. dump_stack();
  3010. }
  3011. cmd->cmd_flags |= BIT_17;
  3012. ha->tgt.tgt_ops->free_cmd(cmd);
  3013. }
  3014. void
  3015. qlt_host_reset_handler(struct qla_hw_data *ha)
  3016. {
  3017. struct qla_tgt_cmd *cmd;
  3018. unsigned long flags;
  3019. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  3020. scsi_qla_host_t *vha = NULL;
  3021. struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
  3022. uint32_t i;
  3023. if (!base_vha->hw->tgt.tgt_ops)
  3024. return;
  3025. if (!tgt || qla_ini_mode_enabled(base_vha)) {
  3026. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
  3027. "Target mode disabled\n");
  3028. return;
  3029. }
  3030. ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
  3031. "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
  3032. base_vha->dpc_flags);
  3033. spin_lock_irqsave(&ha->hardware_lock, flags);
  3034. for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
  3035. cmd = qlt_get_cmd(base_vha, i);
  3036. if (!cmd)
  3037. continue;
  3038. /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
  3039. vha = cmd->vha;
  3040. qlt_abort_cmd_on_host_reset(vha, cmd);
  3041. }
  3042. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3043. }
  3044. /*
  3045. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  3046. */
  3047. static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
  3048. uint32_t status, void *ctio)
  3049. {
  3050. struct qla_hw_data *ha = vha->hw;
  3051. struct se_cmd *se_cmd;
  3052. struct qla_tgt_cmd *cmd;
  3053. if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
  3054. /* That could happen only in case of an error/reset/abort */
  3055. if (status != CTIO_SUCCESS) {
  3056. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
  3057. "Intermediate CTIO received"
  3058. " (status %x)\n", status);
  3059. }
  3060. return;
  3061. }
  3062. cmd = qlt_ctio_to_cmd(vha, handle, ctio);
  3063. if (cmd == NULL)
  3064. return;
  3065. se_cmd = &cmd->se_cmd;
  3066. cmd->cmd_sent_to_fw = 0;
  3067. qlt_unmap_sg(vha, cmd);
  3068. if (unlikely(status != CTIO_SUCCESS)) {
  3069. switch (status & 0xFFFF) {
  3070. case CTIO_LIP_RESET:
  3071. case CTIO_TARGET_RESET:
  3072. case CTIO_ABORTED:
  3073. /* driver request abort via Terminate exchange */
  3074. case CTIO_TIMEOUT:
  3075. case CTIO_INVALID_RX_ID:
  3076. /* They are OK */
  3077. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
  3078. "qla_target(%d): CTIO with "
  3079. "status %#x received, state %x, se_cmd %p, "
  3080. "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
  3081. "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
  3082. status, cmd->state, se_cmd);
  3083. break;
  3084. case CTIO_PORT_LOGGED_OUT:
  3085. case CTIO_PORT_UNAVAILABLE:
  3086. {
  3087. int logged_out =
  3088. (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
  3089. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
  3090. "qla_target(%d): CTIO with %s status %x "
  3091. "received (state %x, se_cmd %p)\n", vha->vp_idx,
  3092. logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
  3093. status, cmd->state, se_cmd);
  3094. if (logged_out && cmd->sess) {
  3095. /*
  3096. * Session is already logged out, but we need
  3097. * to notify initiator, who's not aware of this
  3098. */
  3099. cmd->sess->logout_on_delete = 0;
  3100. cmd->sess->send_els_logo = 1;
  3101. qlt_schedule_sess_for_deletion(cmd->sess, true);
  3102. }
  3103. break;
  3104. }
  3105. case CTIO_SRR_RECEIVED:
  3106. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
  3107. "qla_target(%d): CTIO with SRR_RECEIVED"
  3108. " status %x received (state %x, se_cmd %p)\n",
  3109. vha->vp_idx, status, cmd->state, se_cmd);
  3110. if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
  3111. break;
  3112. else
  3113. return;
  3114. case CTIO_DIF_ERROR: {
  3115. struct ctio_crc_from_fw *crc =
  3116. (struct ctio_crc_from_fw *)ctio;
  3117. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
  3118. "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n",
  3119. vha->vp_idx, status, cmd->state, se_cmd,
  3120. *((u64 *)&crc->actual_dif[0]),
  3121. *((u64 *)&crc->expected_dif[0]));
  3122. if (qlt_handle_dif_error(vha, cmd, ctio)) {
  3123. if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
  3124. /* scsi Write/xfer rdy complete */
  3125. goto skip_term;
  3126. } else {
  3127. /* scsi read/xmit respond complete
  3128. * call handle dif to send scsi status
  3129. * rather than terminate exchange.
  3130. */
  3131. cmd->state = QLA_TGT_STATE_PROCESSED;
  3132. ha->tgt.tgt_ops->handle_dif_err(cmd);
  3133. return;
  3134. }
  3135. } else {
  3136. /* Need to generate a SCSI good completion.
  3137. * because FW did not send scsi status.
  3138. */
  3139. status = 0;
  3140. goto skip_term;
  3141. }
  3142. break;
  3143. }
  3144. default:
  3145. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
  3146. "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
  3147. vha->vp_idx, status, cmd->state, se_cmd);
  3148. break;
  3149. }
  3150. /* "cmd->aborted" means
  3151. * cmd is already aborted/terminated, we don't
  3152. * need to terminate again. The exchange is already
  3153. * cleaned up/freed at FW level. Just cleanup at driver
  3154. * level.
  3155. */
  3156. if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
  3157. (!cmd->aborted)) {
  3158. cmd->cmd_flags |= BIT_13;
  3159. if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
  3160. return;
  3161. }
  3162. }
  3163. skip_term:
  3164. if (cmd->state == QLA_TGT_STATE_PROCESSED) {
  3165. cmd->cmd_flags |= BIT_12;
  3166. } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
  3167. cmd->state = QLA_TGT_STATE_DATA_IN;
  3168. if (status == CTIO_SUCCESS)
  3169. cmd->write_data_transferred = 1;
  3170. ha->tgt.tgt_ops->handle_data(cmd);
  3171. return;
  3172. } else if (cmd->aborted) {
  3173. cmd->cmd_flags |= BIT_18;
  3174. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
  3175. "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
  3176. } else {
  3177. cmd->cmd_flags |= BIT_19;
  3178. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
  3179. "qla_target(%d): A command in state (%d) should "
  3180. "not return a CTIO complete\n", vha->vp_idx, cmd->state);
  3181. }
  3182. if (unlikely(status != CTIO_SUCCESS) &&
  3183. !cmd->aborted) {
  3184. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
  3185. dump_stack();
  3186. }
  3187. ha->tgt.tgt_ops->free_cmd(cmd);
  3188. }
  3189. static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
  3190. uint8_t task_codes)
  3191. {
  3192. int fcp_task_attr;
  3193. switch (task_codes) {
  3194. case ATIO_SIMPLE_QUEUE:
  3195. fcp_task_attr = TCM_SIMPLE_TAG;
  3196. break;
  3197. case ATIO_HEAD_OF_QUEUE:
  3198. fcp_task_attr = TCM_HEAD_TAG;
  3199. break;
  3200. case ATIO_ORDERED_QUEUE:
  3201. fcp_task_attr = TCM_ORDERED_TAG;
  3202. break;
  3203. case ATIO_ACA_QUEUE:
  3204. fcp_task_attr = TCM_ACA_TAG;
  3205. break;
  3206. case ATIO_UNTAGGED:
  3207. fcp_task_attr = TCM_SIMPLE_TAG;
  3208. break;
  3209. default:
  3210. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
  3211. "qla_target: unknown task code %x, use ORDERED instead\n",
  3212. task_codes);
  3213. fcp_task_attr = TCM_ORDERED_TAG;
  3214. break;
  3215. }
  3216. return fcp_task_attr;
  3217. }
  3218. static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
  3219. uint8_t *);
  3220. /*
  3221. * Process context for I/O path into tcm_qla2xxx code
  3222. */
  3223. static void __qlt_do_work(struct qla_tgt_cmd *cmd)
  3224. {
  3225. scsi_qla_host_t *vha = cmd->vha;
  3226. struct qla_hw_data *ha = vha->hw;
  3227. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  3228. struct qla_tgt_sess *sess = cmd->sess;
  3229. struct atio_from_isp *atio = &cmd->atio;
  3230. unsigned char *cdb;
  3231. unsigned long flags;
  3232. uint32_t data_length;
  3233. int ret, fcp_task_attr, data_dir, bidi = 0;
  3234. cmd->cmd_in_wq = 0;
  3235. cmd->cmd_flags |= BIT_1;
  3236. if (tgt->tgt_stop)
  3237. goto out_term;
  3238. if (cmd->aborted) {
  3239. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
  3240. "cmd with tag %u is aborted\n",
  3241. cmd->atio.u.isp24.exchange_addr);
  3242. goto out_term;
  3243. }
  3244. spin_lock_init(&cmd->cmd_lock);
  3245. cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
  3246. cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
  3247. cmd->unpacked_lun = scsilun_to_int(
  3248. (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
  3249. if (atio->u.isp24.fcp_cmnd.rddata &&
  3250. atio->u.isp24.fcp_cmnd.wrdata) {
  3251. bidi = 1;
  3252. data_dir = DMA_TO_DEVICE;
  3253. } else if (atio->u.isp24.fcp_cmnd.rddata)
  3254. data_dir = DMA_FROM_DEVICE;
  3255. else if (atio->u.isp24.fcp_cmnd.wrdata)
  3256. data_dir = DMA_TO_DEVICE;
  3257. else
  3258. data_dir = DMA_NONE;
  3259. fcp_task_attr = qlt_get_fcp_task_attr(vha,
  3260. atio->u.isp24.fcp_cmnd.task_attr);
  3261. data_length = be32_to_cpu(get_unaligned((uint32_t *)
  3262. &atio->u.isp24.fcp_cmnd.add_cdb[
  3263. atio->u.isp24.fcp_cmnd.add_cdb_len]));
  3264. ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
  3265. fcp_task_attr, data_dir, bidi);
  3266. if (ret != 0)
  3267. goto out_term;
  3268. /*
  3269. * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
  3270. */
  3271. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  3272. qlt_put_sess(sess);
  3273. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  3274. return;
  3275. out_term:
  3276. ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
  3277. /*
  3278. * cmd has not sent to target yet, so pass NULL as the second
  3279. * argument to qlt_send_term_exchange() and free the memory here.
  3280. */
  3281. cmd->cmd_flags |= BIT_2;
  3282. spin_lock_irqsave(&ha->hardware_lock, flags);
  3283. qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
  3284. qlt_decr_num_pend_cmds(vha);
  3285. percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
  3286. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3287. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  3288. qlt_put_sess(sess);
  3289. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  3290. }
  3291. static void qlt_do_work(struct work_struct *work)
  3292. {
  3293. struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
  3294. scsi_qla_host_t *vha = cmd->vha;
  3295. unsigned long flags;
  3296. spin_lock_irqsave(&vha->cmd_list_lock, flags);
  3297. list_del(&cmd->cmd_list);
  3298. spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
  3299. __qlt_do_work(cmd);
  3300. }
  3301. static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
  3302. struct qla_tgt_sess *sess,
  3303. struct atio_from_isp *atio)
  3304. {
  3305. struct se_session *se_sess = sess->se_sess;
  3306. struct qla_tgt_cmd *cmd;
  3307. int tag;
  3308. tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
  3309. if (tag < 0)
  3310. return NULL;
  3311. cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
  3312. memset(cmd, 0, sizeof(struct qla_tgt_cmd));
  3313. memcpy(&cmd->atio, atio, sizeof(*atio));
  3314. cmd->state = QLA_TGT_STATE_NEW;
  3315. cmd->tgt = vha->vha_tgt.qla_tgt;
  3316. qlt_incr_num_pend_cmds(vha);
  3317. cmd->vha = vha;
  3318. cmd->se_cmd.map_tag = tag;
  3319. cmd->sess = sess;
  3320. cmd->loop_id = sess->loop_id;
  3321. cmd->conf_compl_supported = sess->conf_compl_supported;
  3322. cmd->cmd_flags = 0;
  3323. cmd->jiffies_at_alloc = get_jiffies_64();
  3324. cmd->reset_count = vha->hw->chip_reset;
  3325. return cmd;
  3326. }
  3327. static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
  3328. uint16_t);
  3329. static void qlt_create_sess_from_atio(struct work_struct *work)
  3330. {
  3331. struct qla_tgt_sess_op *op = container_of(work,
  3332. struct qla_tgt_sess_op, work);
  3333. scsi_qla_host_t *vha = op->vha;
  3334. struct qla_hw_data *ha = vha->hw;
  3335. struct qla_tgt_sess *sess;
  3336. struct qla_tgt_cmd *cmd;
  3337. unsigned long flags;
  3338. uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
  3339. spin_lock_irqsave(&vha->cmd_list_lock, flags);
  3340. list_del(&op->cmd_list);
  3341. spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
  3342. if (op->aborted) {
  3343. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
  3344. "sess_op with tag %u is aborted\n",
  3345. op->atio.u.isp24.exchange_addr);
  3346. goto out_term;
  3347. }
  3348. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
  3349. "qla_target(%d): Unable to find wwn login"
  3350. " (s_id %x:%x:%x), trying to create it manually\n",
  3351. vha->vp_idx, s_id[0], s_id[1], s_id[2]);
  3352. if (op->atio.u.raw.entry_count > 1) {
  3353. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
  3354. "Dropping multy entry atio %p\n", &op->atio);
  3355. goto out_term;
  3356. }
  3357. sess = qlt_make_local_sess(vha, s_id);
  3358. /* sess has an extra creation ref. */
  3359. if (!sess)
  3360. goto out_term;
  3361. /*
  3362. * Now obtain a pre-allocated session tag using the original op->atio
  3363. * packet header, and dispatch into __qlt_do_work() using the existing
  3364. * process context.
  3365. */
  3366. cmd = qlt_get_tag(vha, sess, &op->atio);
  3367. if (!cmd) {
  3368. spin_lock_irqsave(&ha->hardware_lock, flags);
  3369. qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
  3370. qlt_put_sess(sess);
  3371. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3372. kfree(op);
  3373. return;
  3374. }
  3375. /*
  3376. * __qlt_do_work() will call qlt_put_sess() to release
  3377. * the extra reference taken above by qlt_make_local_sess()
  3378. */
  3379. __qlt_do_work(cmd);
  3380. kfree(op);
  3381. return;
  3382. out_term:
  3383. spin_lock_irqsave(&ha->hardware_lock, flags);
  3384. qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
  3385. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3386. kfree(op);
  3387. }
  3388. /* ha->hardware_lock supposed to be held on entry */
  3389. static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
  3390. struct atio_from_isp *atio)
  3391. {
  3392. struct qla_hw_data *ha = vha->hw;
  3393. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  3394. struct qla_tgt_sess *sess;
  3395. struct qla_tgt_cmd *cmd;
  3396. if (unlikely(tgt->tgt_stop)) {
  3397. ql_dbg(ql_dbg_io, vha, 0x3061,
  3398. "New command while device %p is shutting down\n", tgt);
  3399. return -EFAULT;
  3400. }
  3401. sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
  3402. if (unlikely(!sess)) {
  3403. struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
  3404. GFP_ATOMIC);
  3405. if (!op)
  3406. return -ENOMEM;
  3407. memcpy(&op->atio, atio, sizeof(*atio));
  3408. op->vha = vha;
  3409. spin_lock(&vha->cmd_list_lock);
  3410. list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
  3411. spin_unlock(&vha->cmd_list_lock);
  3412. INIT_WORK(&op->work, qlt_create_sess_from_atio);
  3413. queue_work(qla_tgt_wq, &op->work);
  3414. return 0;
  3415. }
  3416. /* Another WWN used to have our s_id. Our PLOGI scheduled its
  3417. * session deletion, but it's still in sess_del_work wq */
  3418. if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
  3419. ql_dbg(ql_dbg_io, vha, 0x3061,
  3420. "New command while old session %p is being deleted\n",
  3421. sess);
  3422. return -EFAULT;
  3423. }
  3424. /*
  3425. * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
  3426. */
  3427. kref_get(&sess->sess_kref);
  3428. cmd = qlt_get_tag(vha, sess, atio);
  3429. if (!cmd) {
  3430. ql_dbg(ql_dbg_io, vha, 0x3062,
  3431. "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
  3432. qlt_put_sess(sess);
  3433. return -ENOMEM;
  3434. }
  3435. cmd->cmd_in_wq = 1;
  3436. cmd->cmd_flags |= BIT_0;
  3437. cmd->se_cmd.cpuid = ha->msix_count ?
  3438. ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
  3439. spin_lock(&vha->cmd_list_lock);
  3440. list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
  3441. spin_unlock(&vha->cmd_list_lock);
  3442. INIT_WORK(&cmd->work, qlt_do_work);
  3443. if (ha->msix_count) {
  3444. if (cmd->atio.u.isp24.fcp_cmnd.rddata)
  3445. queue_work_on(smp_processor_id(), qla_tgt_wq,
  3446. &cmd->work);
  3447. else
  3448. queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
  3449. &cmd->work);
  3450. } else {
  3451. queue_work(qla_tgt_wq, &cmd->work);
  3452. }
  3453. return 0;
  3454. }
  3455. /* ha->hardware_lock supposed to be held on entry */
  3456. static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
  3457. int fn, void *iocb, int flags)
  3458. {
  3459. struct scsi_qla_host *vha = sess->vha;
  3460. struct qla_hw_data *ha = vha->hw;
  3461. struct qla_tgt_mgmt_cmd *mcmd;
  3462. struct atio_from_isp *a = (struct atio_from_isp *)iocb;
  3463. int res;
  3464. uint8_t tmr_func;
  3465. mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
  3466. if (!mcmd) {
  3467. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
  3468. "qla_target(%d): Allocation of management "
  3469. "command failed, some commands and their data could "
  3470. "leak\n", vha->vp_idx);
  3471. return -ENOMEM;
  3472. }
  3473. memset(mcmd, 0, sizeof(*mcmd));
  3474. mcmd->sess = sess;
  3475. if (iocb) {
  3476. memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
  3477. sizeof(mcmd->orig_iocb.imm_ntfy));
  3478. }
  3479. mcmd->tmr_func = fn;
  3480. mcmd->flags = flags;
  3481. mcmd->reset_count = vha->hw->chip_reset;
  3482. switch (fn) {
  3483. case QLA_TGT_CLEAR_ACA:
  3484. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
  3485. "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
  3486. tmr_func = TMR_CLEAR_ACA;
  3487. break;
  3488. case QLA_TGT_TARGET_RESET:
  3489. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
  3490. "qla_target(%d): TARGET_RESET received\n",
  3491. sess->vha->vp_idx);
  3492. tmr_func = TMR_TARGET_WARM_RESET;
  3493. break;
  3494. case QLA_TGT_LUN_RESET:
  3495. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
  3496. "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
  3497. tmr_func = TMR_LUN_RESET;
  3498. abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
  3499. break;
  3500. case QLA_TGT_CLEAR_TS:
  3501. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
  3502. "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
  3503. tmr_func = TMR_CLEAR_TASK_SET;
  3504. break;
  3505. case QLA_TGT_ABORT_TS:
  3506. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
  3507. "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
  3508. tmr_func = TMR_ABORT_TASK_SET;
  3509. break;
  3510. #if 0
  3511. case QLA_TGT_ABORT_ALL:
  3512. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
  3513. "qla_target(%d): Doing ABORT_ALL_TASKS\n",
  3514. sess->vha->vp_idx);
  3515. tmr_func = 0;
  3516. break;
  3517. case QLA_TGT_ABORT_ALL_SESS:
  3518. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
  3519. "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
  3520. sess->vha->vp_idx);
  3521. tmr_func = 0;
  3522. break;
  3523. case QLA_TGT_NEXUS_LOSS_SESS:
  3524. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
  3525. "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
  3526. sess->vha->vp_idx);
  3527. tmr_func = 0;
  3528. break;
  3529. case QLA_TGT_NEXUS_LOSS:
  3530. ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
  3531. "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
  3532. tmr_func = 0;
  3533. break;
  3534. #endif
  3535. default:
  3536. ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
  3537. "qla_target(%d): Unknown task mgmt fn 0x%x\n",
  3538. sess->vha->vp_idx, fn);
  3539. mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
  3540. return -ENOSYS;
  3541. }
  3542. res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
  3543. if (res != 0) {
  3544. ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
  3545. "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
  3546. sess->vha->vp_idx, res);
  3547. mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
  3548. return -EFAULT;
  3549. }
  3550. return 0;
  3551. }
  3552. /* ha->hardware_lock supposed to be held on entry */
  3553. static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
  3554. {
  3555. struct atio_from_isp *a = (struct atio_from_isp *)iocb;
  3556. struct qla_hw_data *ha = vha->hw;
  3557. struct qla_tgt *tgt;
  3558. struct qla_tgt_sess *sess;
  3559. uint32_t lun, unpacked_lun;
  3560. int fn;
  3561. unsigned long flags;
  3562. tgt = vha->vha_tgt.qla_tgt;
  3563. lun = a->u.isp24.fcp_cmnd.lun;
  3564. fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
  3565. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  3566. sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
  3567. a->u.isp24.fcp_hdr.s_id);
  3568. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  3569. unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
  3570. if (!sess) {
  3571. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
  3572. "qla_target(%d): task mgmt fn 0x%x for "
  3573. "non-existant session\n", vha->vp_idx, fn);
  3574. return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
  3575. sizeof(struct atio_from_isp));
  3576. }
  3577. if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS)
  3578. return -EFAULT;
  3579. return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
  3580. }
  3581. /* ha->hardware_lock supposed to be held on entry */
  3582. static int __qlt_abort_task(struct scsi_qla_host *vha,
  3583. struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
  3584. {
  3585. struct atio_from_isp *a = (struct atio_from_isp *)iocb;
  3586. struct qla_hw_data *ha = vha->hw;
  3587. struct qla_tgt_mgmt_cmd *mcmd;
  3588. uint32_t lun, unpacked_lun;
  3589. int rc;
  3590. mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
  3591. if (mcmd == NULL) {
  3592. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
  3593. "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
  3594. vha->vp_idx, __func__);
  3595. return -ENOMEM;
  3596. }
  3597. memset(mcmd, 0, sizeof(*mcmd));
  3598. mcmd->sess = sess;
  3599. memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
  3600. sizeof(mcmd->orig_iocb.imm_ntfy));
  3601. lun = a->u.isp24.fcp_cmnd.lun;
  3602. unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
  3603. mcmd->reset_count = vha->hw->chip_reset;
  3604. rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
  3605. le16_to_cpu(iocb->u.isp2x.seq_id));
  3606. if (rc != 0) {
  3607. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
  3608. "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
  3609. vha->vp_idx, rc);
  3610. mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
  3611. return -EFAULT;
  3612. }
  3613. return 0;
  3614. }
  3615. /* ha->hardware_lock supposed to be held on entry */
  3616. static int qlt_abort_task(struct scsi_qla_host *vha,
  3617. struct imm_ntfy_from_isp *iocb)
  3618. {
  3619. struct qla_hw_data *ha = vha->hw;
  3620. struct qla_tgt_sess *sess;
  3621. int loop_id;
  3622. unsigned long flags;
  3623. loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
  3624. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  3625. sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
  3626. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  3627. if (sess == NULL) {
  3628. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
  3629. "qla_target(%d): task abort for unexisting "
  3630. "session\n", vha->vp_idx);
  3631. return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
  3632. QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
  3633. }
  3634. return __qlt_abort_task(vha, iocb, sess);
  3635. }
  3636. void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
  3637. {
  3638. if (fcport->tgt_session) {
  3639. if (rc != MBS_COMMAND_COMPLETE) {
  3640. ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
  3641. "%s: se_sess %p / sess %p from"
  3642. " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
  3643. " LOGO failed: %#x\n",
  3644. __func__,
  3645. fcport->tgt_session->se_sess,
  3646. fcport->tgt_session,
  3647. fcport->port_name, fcport->loop_id,
  3648. fcport->d_id.b.domain, fcport->d_id.b.area,
  3649. fcport->d_id.b.al_pa, rc);
  3650. }
  3651. fcport->tgt_session->logout_completed = 1;
  3652. }
  3653. }
  3654. /*
  3655. * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
  3656. *
  3657. * Schedules sessions with matching port_id/loop_id but different wwn for
  3658. * deletion. Returns existing session with matching wwn if present.
  3659. * Null otherwise.
  3660. */
  3661. static struct qla_tgt_sess *
  3662. qlt_find_sess_invalidate_other(struct qla_tgt *tgt, uint64_t wwn,
  3663. port_id_t port_id, uint16_t loop_id, struct qla_tgt_sess **conflict_sess)
  3664. {
  3665. struct qla_tgt_sess *sess = NULL, *other_sess;
  3666. uint64_t other_wwn;
  3667. *conflict_sess = NULL;
  3668. list_for_each_entry(other_sess, &tgt->sess_list, sess_list_entry) {
  3669. other_wwn = wwn_to_u64(other_sess->port_name);
  3670. if (wwn == other_wwn) {
  3671. WARN_ON(sess);
  3672. sess = other_sess;
  3673. continue;
  3674. }
  3675. /* find other sess with nport_id collision */
  3676. if (port_id.b24 == other_sess->s_id.b24) {
  3677. if (loop_id != other_sess->loop_id) {
  3678. ql_dbg(ql_dbg_tgt_tmr, tgt->vha, 0x1000c,
  3679. "Invalidating sess %p loop_id %d wwn %llx.\n",
  3680. other_sess, other_sess->loop_id, other_wwn);
  3681. /*
  3682. * logout_on_delete is set by default, but another
  3683. * session that has the same s_id/loop_id combo
  3684. * might have cleared it when requested this session
  3685. * deletion, so don't touch it
  3686. */
  3687. qlt_schedule_sess_for_deletion(other_sess, true);
  3688. } else {
  3689. /*
  3690. * Another wwn used to have our s_id/loop_id
  3691. * kill the session, but don't free the loop_id
  3692. */
  3693. other_sess->keep_nport_handle = 1;
  3694. *conflict_sess = other_sess;
  3695. qlt_schedule_sess_for_deletion(other_sess,
  3696. true);
  3697. }
  3698. continue;
  3699. }
  3700. /* find other sess with nport handle collision */
  3701. if (loop_id == other_sess->loop_id) {
  3702. ql_dbg(ql_dbg_tgt_tmr, tgt->vha, 0x1000d,
  3703. "Invalidating sess %p loop_id %d wwn %llx.\n",
  3704. other_sess, other_sess->loop_id, other_wwn);
  3705. /* Same loop_id but different s_id
  3706. * Ok to kill and logout */
  3707. qlt_schedule_sess_for_deletion(other_sess, true);
  3708. }
  3709. }
  3710. return sess;
  3711. }
  3712. /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
  3713. static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
  3714. {
  3715. struct qla_tgt_sess_op *op;
  3716. struct qla_tgt_cmd *cmd;
  3717. uint32_t key;
  3718. int count = 0;
  3719. key = (((u32)s_id->b.domain << 16) |
  3720. ((u32)s_id->b.area << 8) |
  3721. ((u32)s_id->b.al_pa));
  3722. spin_lock(&vha->cmd_list_lock);
  3723. list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
  3724. uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
  3725. if (op_key == key) {
  3726. op->aborted = true;
  3727. count++;
  3728. }
  3729. }
  3730. list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
  3731. uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
  3732. if (cmd_key == key) {
  3733. cmd->aborted = 1;
  3734. count++;
  3735. }
  3736. }
  3737. spin_unlock(&vha->cmd_list_lock);
  3738. return count;
  3739. }
  3740. /*
  3741. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  3742. */
  3743. static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
  3744. struct imm_ntfy_from_isp *iocb)
  3745. {
  3746. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  3747. struct qla_hw_data *ha = vha->hw;
  3748. struct qla_tgt_sess *sess = NULL, *conflict_sess = NULL;
  3749. uint64_t wwn;
  3750. port_id_t port_id;
  3751. uint16_t loop_id;
  3752. uint16_t wd3_lo;
  3753. int res = 0;
  3754. qlt_plogi_ack_t *pla;
  3755. unsigned long flags;
  3756. wwn = wwn_to_u64(iocb->u.isp24.port_name);
  3757. port_id.b.domain = iocb->u.isp24.port_id[2];
  3758. port_id.b.area = iocb->u.isp24.port_id[1];
  3759. port_id.b.al_pa = iocb->u.isp24.port_id[0];
  3760. port_id.b.rsvd_1 = 0;
  3761. loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
  3762. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
  3763. "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n",
  3764. vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode);
  3765. /* res = 1 means ack at the end of thread
  3766. * res = 0 means ack async/later.
  3767. */
  3768. switch (iocb->u.isp24.status_subcode) {
  3769. case ELS_PLOGI:
  3770. /* Mark all stale commands in qla_tgt_wq for deletion */
  3771. abort_cmds_for_s_id(vha, &port_id);
  3772. if (wwn) {
  3773. spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
  3774. sess = qlt_find_sess_invalidate_other(tgt, wwn,
  3775. port_id, loop_id, &conflict_sess);
  3776. spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
  3777. }
  3778. if (IS_SW_RESV_ADDR(port_id) || (!sess && !conflict_sess)) {
  3779. res = 1;
  3780. break;
  3781. }
  3782. pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
  3783. if (!pla) {
  3784. qlt_send_term_imm_notif(vha, iocb, 1);
  3785. res = 0;
  3786. break;
  3787. }
  3788. res = 0;
  3789. if (conflict_sess)
  3790. qlt_plogi_ack_link(vha, pla, conflict_sess,
  3791. QLT_PLOGI_LINK_CONFLICT);
  3792. if (!sess)
  3793. break;
  3794. qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
  3795. /*
  3796. * Under normal circumstances we want to release nport handle
  3797. * during LOGO process to avoid nport handle leaks inside FW.
  3798. * The exception is when LOGO is done while another PLOGI with
  3799. * the same nport handle is waiting as might be the case here.
  3800. * Note: there is always a possibily of a race where session
  3801. * deletion has already started for other reasons (e.g. ACL
  3802. * removal) and now PLOGI arrives:
  3803. * 1. if PLOGI arrived in FW after nport handle has been freed,
  3804. * FW must have assigned this PLOGI a new/same handle and we
  3805. * can proceed ACK'ing it as usual when session deletion
  3806. * completes.
  3807. * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
  3808. * bit reached it, the handle has now been released. We'll
  3809. * get an error when we ACK this PLOGI. Nothing will be sent
  3810. * back to initiator. Initiator should eventually retry
  3811. * PLOGI and situation will correct itself.
  3812. */
  3813. sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
  3814. (sess->s_id.b24 == port_id.b24));
  3815. qlt_schedule_sess_for_deletion(sess, true);
  3816. break;
  3817. case ELS_PRLI:
  3818. wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
  3819. if (wwn) {
  3820. spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
  3821. sess = qlt_find_sess_invalidate_other(tgt, wwn, port_id,
  3822. loop_id, &conflict_sess);
  3823. spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
  3824. }
  3825. if (conflict_sess) {
  3826. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
  3827. "PRLI with conflicting sess %p port %8phC\n",
  3828. conflict_sess, conflict_sess->port_name);
  3829. qlt_send_term_imm_notif(vha, iocb, 1);
  3830. res = 0;
  3831. break;
  3832. }
  3833. if (sess != NULL) {
  3834. if (sess->deleted) {
  3835. /*
  3836. * Impatient initiator sent PRLI before last
  3837. * PLOGI could finish. Will force him to re-try,
  3838. * while last one finishes.
  3839. */
  3840. ql_log(ql_log_warn, sess->vha, 0xf095,
  3841. "sess %p PRLI received, before plogi ack.\n",
  3842. sess);
  3843. qlt_send_term_imm_notif(vha, iocb, 1);
  3844. res = 0;
  3845. break;
  3846. }
  3847. /*
  3848. * This shouldn't happen under normal circumstances,
  3849. * since we have deleted the old session during PLOGI
  3850. */
  3851. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
  3852. "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
  3853. sess->loop_id, sess, iocb->u.isp24.nport_handle);
  3854. sess->local = 0;
  3855. sess->loop_id = loop_id;
  3856. sess->s_id = port_id;
  3857. if (wd3_lo & BIT_7)
  3858. sess->conf_compl_supported = 1;
  3859. }
  3860. res = 1; /* send notify ack */
  3861. /* Make session global (not used in fabric mode) */
  3862. if (ha->current_topology != ISP_CFG_F) {
  3863. set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
  3864. set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
  3865. qla2xxx_wake_dpc(vha);
  3866. } else {
  3867. /* todo: else - create sess here. */
  3868. res = 1; /* send notify ack */
  3869. }
  3870. break;
  3871. case ELS_LOGO:
  3872. case ELS_PRLO:
  3873. res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
  3874. break;
  3875. case ELS_PDISC:
  3876. case ELS_ADISC:
  3877. {
  3878. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  3879. if (tgt->link_reinit_iocb_pending) {
  3880. qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
  3881. 0, 0, 0, 0, 0, 0);
  3882. tgt->link_reinit_iocb_pending = 0;
  3883. }
  3884. res = 1; /* send notify ack */
  3885. break;
  3886. }
  3887. case ELS_FLOGI: /* should never happen */
  3888. default:
  3889. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
  3890. "qla_target(%d): Unsupported ELS command %x "
  3891. "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
  3892. res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
  3893. break;
  3894. }
  3895. return res;
  3896. }
  3897. static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
  3898. {
  3899. #if 1
  3900. /*
  3901. * FIXME: Reject non zero SRR relative offset until we can test
  3902. * this code properly.
  3903. */
  3904. pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
  3905. return -1;
  3906. #else
  3907. struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
  3908. size_t first_offset = 0, rem_offset = offset, tmp = 0;
  3909. int i, sg_srr_cnt, bufflen = 0;
  3910. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
  3911. "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
  3912. "cmd->sg_cnt: %u, direction: %d\n",
  3913. cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
  3914. if (!cmd->sg || !cmd->sg_cnt) {
  3915. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
  3916. "Missing cmd->sg or zero cmd->sg_cnt in"
  3917. " qla_tgt_set_data_offset\n");
  3918. return -EINVAL;
  3919. }
  3920. /*
  3921. * Walk the current cmd->sg list until we locate the new sg_srr_start
  3922. */
  3923. for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
  3924. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
  3925. "sg[%d]: %p page: %p, length: %d, offset: %d\n",
  3926. i, sg, sg_page(sg), sg->length, sg->offset);
  3927. if ((sg->length + tmp) > offset) {
  3928. first_offset = rem_offset;
  3929. sg_srr_start = sg;
  3930. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
  3931. "Found matching sg[%d], using %p as sg_srr_start, "
  3932. "and using first_offset: %zu\n", i, sg,
  3933. first_offset);
  3934. break;
  3935. }
  3936. tmp += sg->length;
  3937. rem_offset -= sg->length;
  3938. }
  3939. if (!sg_srr_start) {
  3940. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
  3941. "Unable to locate sg_srr_start for offset: %u\n", offset);
  3942. return -EINVAL;
  3943. }
  3944. sg_srr_cnt = (cmd->sg_cnt - i);
  3945. sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
  3946. if (!sg_srr) {
  3947. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
  3948. "Unable to allocate sgp\n");
  3949. return -ENOMEM;
  3950. }
  3951. sg_init_table(sg_srr, sg_srr_cnt);
  3952. sgp = &sg_srr[0];
  3953. /*
  3954. * Walk the remaining list for sg_srr_start, mapping to the newly
  3955. * allocated sg_srr taking first_offset into account.
  3956. */
  3957. for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
  3958. if (first_offset) {
  3959. sg_set_page(sgp, sg_page(sg),
  3960. (sg->length - first_offset), first_offset);
  3961. first_offset = 0;
  3962. } else {
  3963. sg_set_page(sgp, sg_page(sg), sg->length, 0);
  3964. }
  3965. bufflen += sgp->length;
  3966. sgp = sg_next(sgp);
  3967. if (!sgp)
  3968. break;
  3969. }
  3970. cmd->sg = sg_srr;
  3971. cmd->sg_cnt = sg_srr_cnt;
  3972. cmd->bufflen = bufflen;
  3973. cmd->offset += offset;
  3974. cmd->free_sg = 1;
  3975. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
  3976. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
  3977. cmd->sg_cnt);
  3978. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
  3979. cmd->bufflen);
  3980. ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
  3981. cmd->offset);
  3982. if (cmd->sg_cnt < 0)
  3983. BUG();
  3984. if (cmd->bufflen < 0)
  3985. BUG();
  3986. return 0;
  3987. #endif
  3988. }
  3989. static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
  3990. uint32_t srr_rel_offs, int *xmit_type)
  3991. {
  3992. int res = 0, rel_offs;
  3993. rel_offs = srr_rel_offs - cmd->offset;
  3994. ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
  3995. srr_rel_offs, rel_offs);
  3996. *xmit_type = QLA_TGT_XMIT_ALL;
  3997. if (rel_offs < 0) {
  3998. ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
  3999. "qla_target(%d): SRR rel_offs (%d) < 0",
  4000. cmd->vha->vp_idx, rel_offs);
  4001. res = -1;
  4002. } else if (rel_offs == cmd->bufflen)
  4003. *xmit_type = QLA_TGT_XMIT_STATUS;
  4004. else if (rel_offs > 0)
  4005. res = qlt_set_data_offset(cmd, rel_offs);
  4006. return res;
  4007. }
  4008. /* No locks, thread context */
  4009. static void qlt_handle_srr(struct scsi_qla_host *vha,
  4010. struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
  4011. {
  4012. struct imm_ntfy_from_isp *ntfy =
  4013. (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
  4014. struct qla_hw_data *ha = vha->hw;
  4015. struct qla_tgt_cmd *cmd = sctio->cmd;
  4016. struct se_cmd *se_cmd = &cmd->se_cmd;
  4017. unsigned long flags;
  4018. int xmit_type = 0, resp = 0;
  4019. uint32_t offset;
  4020. uint16_t srr_ui;
  4021. offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
  4022. srr_ui = ntfy->u.isp24.srr_ui;
  4023. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
  4024. cmd, srr_ui);
  4025. switch (srr_ui) {
  4026. case SRR_IU_STATUS:
  4027. spin_lock_irqsave(&ha->hardware_lock, flags);
  4028. qlt_send_notify_ack(vha, ntfy,
  4029. 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
  4030. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4031. xmit_type = QLA_TGT_XMIT_STATUS;
  4032. resp = 1;
  4033. break;
  4034. case SRR_IU_DATA_IN:
  4035. if (!cmd->sg || !cmd->sg_cnt) {
  4036. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
  4037. "Unable to process SRR_IU_DATA_IN due to"
  4038. " missing cmd->sg, state: %d\n", cmd->state);
  4039. dump_stack();
  4040. goto out_reject;
  4041. }
  4042. if (se_cmd->scsi_status != 0) {
  4043. ql_dbg(ql_dbg_tgt, vha, 0xe02a,
  4044. "Rejecting SRR_IU_DATA_IN with non GOOD "
  4045. "scsi_status\n");
  4046. goto out_reject;
  4047. }
  4048. cmd->bufflen = se_cmd->data_length;
  4049. if (qlt_has_data(cmd)) {
  4050. if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
  4051. goto out_reject;
  4052. spin_lock_irqsave(&ha->hardware_lock, flags);
  4053. qlt_send_notify_ack(vha, ntfy,
  4054. 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
  4055. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4056. resp = 1;
  4057. } else {
  4058. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
  4059. "qla_target(%d): SRR for in data for cmd without them (tag %lld, SCSI status %d), reject",
  4060. vha->vp_idx, se_cmd->tag,
  4061. cmd->se_cmd.scsi_status);
  4062. goto out_reject;
  4063. }
  4064. break;
  4065. case SRR_IU_DATA_OUT:
  4066. if (!cmd->sg || !cmd->sg_cnt) {
  4067. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
  4068. "Unable to process SRR_IU_DATA_OUT due to"
  4069. " missing cmd->sg\n");
  4070. dump_stack();
  4071. goto out_reject;
  4072. }
  4073. if (se_cmd->scsi_status != 0) {
  4074. ql_dbg(ql_dbg_tgt, vha, 0xe02b,
  4075. "Rejecting SRR_IU_DATA_OUT"
  4076. " with non GOOD scsi_status\n");
  4077. goto out_reject;
  4078. }
  4079. cmd->bufflen = se_cmd->data_length;
  4080. if (qlt_has_data(cmd)) {
  4081. if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
  4082. goto out_reject;
  4083. spin_lock_irqsave(&ha->hardware_lock, flags);
  4084. qlt_send_notify_ack(vha, ntfy,
  4085. 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
  4086. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4087. if (xmit_type & QLA_TGT_XMIT_DATA) {
  4088. cmd->cmd_flags |= BIT_8;
  4089. qlt_rdy_to_xfer(cmd);
  4090. }
  4091. } else {
  4092. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
  4093. "qla_target(%d): SRR for out data for cmd without them (tag %lld, SCSI status %d), reject",
  4094. vha->vp_idx, se_cmd->tag, cmd->se_cmd.scsi_status);
  4095. goto out_reject;
  4096. }
  4097. break;
  4098. default:
  4099. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
  4100. "qla_target(%d): Unknown srr_ui value %x",
  4101. vha->vp_idx, srr_ui);
  4102. goto out_reject;
  4103. }
  4104. /* Transmit response in case of status and data-in cases */
  4105. if (resp) {
  4106. cmd->cmd_flags |= BIT_7;
  4107. qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
  4108. }
  4109. return;
  4110. out_reject:
  4111. spin_lock_irqsave(&ha->hardware_lock, flags);
  4112. qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
  4113. NOTIFY_ACK_SRR_FLAGS_REJECT,
  4114. NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
  4115. NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
  4116. if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
  4117. cmd->state = QLA_TGT_STATE_DATA_IN;
  4118. dump_stack();
  4119. } else {
  4120. cmd->cmd_flags |= BIT_9;
  4121. qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
  4122. }
  4123. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4124. }
  4125. static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
  4126. struct qla_tgt_srr_imm *imm, int ha_locked)
  4127. {
  4128. struct qla_hw_data *ha = vha->hw;
  4129. unsigned long flags = 0;
  4130. #ifndef __CHECKER__
  4131. if (!ha_locked)
  4132. spin_lock_irqsave(&ha->hardware_lock, flags);
  4133. #endif
  4134. qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
  4135. NOTIFY_ACK_SRR_FLAGS_REJECT,
  4136. NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
  4137. NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
  4138. #ifndef __CHECKER__
  4139. if (!ha_locked)
  4140. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4141. #endif
  4142. kfree(imm);
  4143. }
  4144. static void qlt_handle_srr_work(struct work_struct *work)
  4145. {
  4146. struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
  4147. struct scsi_qla_host *vha = tgt->vha;
  4148. struct qla_tgt_srr_ctio *sctio;
  4149. unsigned long flags;
  4150. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
  4151. tgt);
  4152. restart:
  4153. spin_lock_irqsave(&tgt->srr_lock, flags);
  4154. list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
  4155. struct qla_tgt_srr_imm *imm, *i, *ti;
  4156. struct qla_tgt_cmd *cmd;
  4157. struct se_cmd *se_cmd;
  4158. imm = NULL;
  4159. list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
  4160. srr_list_entry) {
  4161. if (i->srr_id == sctio->srr_id) {
  4162. list_del(&i->srr_list_entry);
  4163. if (imm) {
  4164. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
  4165. "qla_target(%d): There must be "
  4166. "only one IMM SRR per CTIO SRR "
  4167. "(IMM SRR %p, id %d, CTIO %p\n",
  4168. vha->vp_idx, i, i->srr_id, sctio);
  4169. qlt_reject_free_srr_imm(tgt->vha, i, 0);
  4170. } else
  4171. imm = i;
  4172. }
  4173. }
  4174. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
  4175. "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
  4176. sctio->srr_id);
  4177. if (imm == NULL) {
  4178. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
  4179. "Not found matching IMM for SRR CTIO (id %d)\n",
  4180. sctio->srr_id);
  4181. continue;
  4182. } else
  4183. list_del(&sctio->srr_list_entry);
  4184. spin_unlock_irqrestore(&tgt->srr_lock, flags);
  4185. cmd = sctio->cmd;
  4186. /*
  4187. * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
  4188. * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
  4189. * logic..
  4190. */
  4191. cmd->offset = 0;
  4192. if (cmd->free_sg) {
  4193. kfree(cmd->sg);
  4194. cmd->sg = NULL;
  4195. cmd->free_sg = 0;
  4196. }
  4197. se_cmd = &cmd->se_cmd;
  4198. cmd->sg_cnt = se_cmd->t_data_nents;
  4199. cmd->sg = se_cmd->t_data_sg;
  4200. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
  4201. "SRR cmd %p (se_cmd %p, tag %lld, op %x), sg_cnt=%d, offset=%d",
  4202. cmd, &cmd->se_cmd, se_cmd->tag, se_cmd->t_task_cdb ?
  4203. se_cmd->t_task_cdb[0] : 0, cmd->sg_cnt, cmd->offset);
  4204. qlt_handle_srr(vha, sctio, imm);
  4205. kfree(imm);
  4206. kfree(sctio);
  4207. goto restart;
  4208. }
  4209. spin_unlock_irqrestore(&tgt->srr_lock, flags);
  4210. }
  4211. /* ha->hardware_lock supposed to be held on entry */
  4212. static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
  4213. struct imm_ntfy_from_isp *iocb)
  4214. {
  4215. struct qla_tgt_srr_imm *imm;
  4216. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  4217. struct qla_tgt_srr_ctio *sctio;
  4218. tgt->imm_srr_id++;
  4219. ql_log(ql_log_warn, vha, 0xf02d, "qla_target(%d): SRR received\n",
  4220. vha->vp_idx);
  4221. imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
  4222. if (imm != NULL) {
  4223. memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
  4224. /* IRQ is already OFF */
  4225. spin_lock(&tgt->srr_lock);
  4226. imm->srr_id = tgt->imm_srr_id;
  4227. list_add_tail(&imm->srr_list_entry,
  4228. &tgt->srr_imm_list);
  4229. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
  4230. "IMM NTFY SRR %p added (id %d, ui %x)\n",
  4231. imm, imm->srr_id, iocb->u.isp24.srr_ui);
  4232. if (tgt->imm_srr_id == tgt->ctio_srr_id) {
  4233. int found = 0;
  4234. list_for_each_entry(sctio, &tgt->srr_ctio_list,
  4235. srr_list_entry) {
  4236. if (sctio->srr_id == imm->srr_id) {
  4237. found = 1;
  4238. break;
  4239. }
  4240. }
  4241. if (found) {
  4242. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
  4243. "Scheduling srr work\n");
  4244. schedule_work(&tgt->srr_work);
  4245. } else {
  4246. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
  4247. "qla_target(%d): imm_srr_id "
  4248. "== ctio_srr_id (%d), but there is no "
  4249. "corresponding SRR CTIO, deleting IMM "
  4250. "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
  4251. imm);
  4252. list_del(&imm->srr_list_entry);
  4253. kfree(imm);
  4254. spin_unlock(&tgt->srr_lock);
  4255. goto out_reject;
  4256. }
  4257. }
  4258. spin_unlock(&tgt->srr_lock);
  4259. } else {
  4260. struct qla_tgt_srr_ctio *ts;
  4261. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
  4262. "qla_target(%d): Unable to allocate SRR IMM "
  4263. "entry, SRR request will be rejected\n", vha->vp_idx);
  4264. /* IRQ is already OFF */
  4265. spin_lock(&tgt->srr_lock);
  4266. list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
  4267. srr_list_entry) {
  4268. if (sctio->srr_id == tgt->imm_srr_id) {
  4269. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
  4270. "CTIO SRR %p deleted (id %d)\n",
  4271. sctio, sctio->srr_id);
  4272. list_del(&sctio->srr_list_entry);
  4273. qlt_send_term_exchange(vha, sctio->cmd,
  4274. &sctio->cmd->atio, 1, 0);
  4275. kfree(sctio);
  4276. }
  4277. }
  4278. spin_unlock(&tgt->srr_lock);
  4279. goto out_reject;
  4280. }
  4281. return;
  4282. out_reject:
  4283. qlt_send_notify_ack(vha, iocb, 0, 0, 0,
  4284. NOTIFY_ACK_SRR_FLAGS_REJECT,
  4285. NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
  4286. NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
  4287. }
  4288. /*
  4289. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  4290. */
  4291. static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
  4292. struct imm_ntfy_from_isp *iocb)
  4293. {
  4294. struct qla_hw_data *ha = vha->hw;
  4295. uint32_t add_flags = 0;
  4296. int send_notify_ack = 1;
  4297. uint16_t status;
  4298. status = le16_to_cpu(iocb->u.isp2x.status);
  4299. switch (status) {
  4300. case IMM_NTFY_LIP_RESET:
  4301. {
  4302. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
  4303. "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
  4304. vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
  4305. iocb->u.isp24.status_subcode);
  4306. if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
  4307. send_notify_ack = 0;
  4308. break;
  4309. }
  4310. case IMM_NTFY_LIP_LINK_REINIT:
  4311. {
  4312. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  4313. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
  4314. "qla_target(%d): LINK REINIT (loop %#x, "
  4315. "subcode %x)\n", vha->vp_idx,
  4316. le16_to_cpu(iocb->u.isp24.nport_handle),
  4317. iocb->u.isp24.status_subcode);
  4318. if (tgt->link_reinit_iocb_pending) {
  4319. qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
  4320. 0, 0, 0, 0, 0, 0);
  4321. }
  4322. memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
  4323. tgt->link_reinit_iocb_pending = 1;
  4324. /*
  4325. * QLogic requires to wait after LINK REINIT for possible
  4326. * PDISC or ADISC ELS commands
  4327. */
  4328. send_notify_ack = 0;
  4329. break;
  4330. }
  4331. case IMM_NTFY_PORT_LOGOUT:
  4332. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
  4333. "qla_target(%d): Port logout (loop "
  4334. "%#x, subcode %x)\n", vha->vp_idx,
  4335. le16_to_cpu(iocb->u.isp24.nport_handle),
  4336. iocb->u.isp24.status_subcode);
  4337. if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
  4338. send_notify_ack = 0;
  4339. /* The sessions will be cleared in the callback, if needed */
  4340. break;
  4341. case IMM_NTFY_GLBL_TPRLO:
  4342. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
  4343. "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
  4344. if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
  4345. send_notify_ack = 0;
  4346. /* The sessions will be cleared in the callback, if needed */
  4347. break;
  4348. case IMM_NTFY_PORT_CONFIG:
  4349. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
  4350. "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
  4351. status);
  4352. if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
  4353. send_notify_ack = 0;
  4354. /* The sessions will be cleared in the callback, if needed */
  4355. break;
  4356. case IMM_NTFY_GLBL_LOGO:
  4357. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
  4358. "qla_target(%d): Link failure detected\n",
  4359. vha->vp_idx);
  4360. /* I_T nexus loss */
  4361. if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
  4362. send_notify_ack = 0;
  4363. break;
  4364. case IMM_NTFY_IOCB_OVERFLOW:
  4365. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
  4366. "qla_target(%d): Cannot provide requested "
  4367. "capability (IOCB overflowed the immediate notify "
  4368. "resource count)\n", vha->vp_idx);
  4369. break;
  4370. case IMM_NTFY_ABORT_TASK:
  4371. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
  4372. "qla_target(%d): Abort Task (S %08x I %#x -> "
  4373. "L %#x)\n", vha->vp_idx,
  4374. le16_to_cpu(iocb->u.isp2x.seq_id),
  4375. GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
  4376. le16_to_cpu(iocb->u.isp2x.lun));
  4377. if (qlt_abort_task(vha, iocb) == 0)
  4378. send_notify_ack = 0;
  4379. break;
  4380. case IMM_NTFY_RESOURCE:
  4381. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
  4382. "qla_target(%d): Out of resources, host %ld\n",
  4383. vha->vp_idx, vha->host_no);
  4384. break;
  4385. case IMM_NTFY_MSG_RX:
  4386. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
  4387. "qla_target(%d): Immediate notify task %x\n",
  4388. vha->vp_idx, iocb->u.isp2x.task_flags);
  4389. if (qlt_handle_task_mgmt(vha, iocb) == 0)
  4390. send_notify_ack = 0;
  4391. break;
  4392. case IMM_NTFY_ELS:
  4393. if (qlt_24xx_handle_els(vha, iocb) == 0)
  4394. send_notify_ack = 0;
  4395. break;
  4396. case IMM_NTFY_SRR:
  4397. qlt_prepare_srr_imm(vha, iocb);
  4398. send_notify_ack = 0;
  4399. break;
  4400. default:
  4401. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
  4402. "qla_target(%d): Received unknown immediate "
  4403. "notify status %x\n", vha->vp_idx, status);
  4404. break;
  4405. }
  4406. if (send_notify_ack)
  4407. qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
  4408. }
  4409. /*
  4410. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  4411. * This function sends busy to ISP 2xxx or 24xx.
  4412. */
  4413. static int __qlt_send_busy(struct scsi_qla_host *vha,
  4414. struct atio_from_isp *atio, uint16_t status)
  4415. {
  4416. struct ctio7_to_24xx *ctio24;
  4417. struct qla_hw_data *ha = vha->hw;
  4418. request_t *pkt;
  4419. struct qla_tgt_sess *sess = NULL;
  4420. unsigned long flags;
  4421. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  4422. sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
  4423. atio->u.isp24.fcp_hdr.s_id);
  4424. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  4425. if (!sess) {
  4426. qlt_send_term_exchange(vha, NULL, atio, 1, 0);
  4427. return 0;
  4428. }
  4429. /* Sending marker isn't necessary, since we called from ISR */
  4430. pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
  4431. if (!pkt) {
  4432. ql_dbg(ql_dbg_io, vha, 0x3063,
  4433. "qla_target(%d): %s failed: unable to allocate "
  4434. "request packet", vha->vp_idx, __func__);
  4435. return -ENOMEM;
  4436. }
  4437. vha->tgt_counters.num_q_full_sent++;
  4438. pkt->entry_count = 1;
  4439. pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
  4440. ctio24 = (struct ctio7_to_24xx *)pkt;
  4441. ctio24->entry_type = CTIO_TYPE7;
  4442. ctio24->nport_handle = sess->loop_id;
  4443. ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
  4444. ctio24->vp_index = vha->vp_idx;
  4445. ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
  4446. ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
  4447. ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
  4448. ctio24->exchange_addr = atio->u.isp24.exchange_addr;
  4449. ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
  4450. cpu_to_le16(
  4451. CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
  4452. CTIO7_FLAGS_DONT_RET_CTIO);
  4453. /*
  4454. * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
  4455. * if the explicit conformation is used.
  4456. */
  4457. ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
  4458. ctio24->u.status1.scsi_status = cpu_to_le16(status);
  4459. /* Memory Barrier */
  4460. wmb();
  4461. qla2x00_start_iocbs(vha, vha->req);
  4462. return 0;
  4463. }
  4464. /*
  4465. * This routine is used to allocate a command for either a QFull condition
  4466. * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
  4467. * out previously.
  4468. */
  4469. static void
  4470. qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
  4471. struct atio_from_isp *atio, uint16_t status, int qfull)
  4472. {
  4473. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  4474. struct qla_hw_data *ha = vha->hw;
  4475. struct qla_tgt_sess *sess;
  4476. struct se_session *se_sess;
  4477. struct qla_tgt_cmd *cmd;
  4478. int tag;
  4479. if (unlikely(tgt->tgt_stop)) {
  4480. ql_dbg(ql_dbg_io, vha, 0x300a,
  4481. "New command while device %p is shutting down\n", tgt);
  4482. return;
  4483. }
  4484. if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
  4485. vha->hw->tgt.num_qfull_cmds_dropped++;
  4486. if (vha->hw->tgt.num_qfull_cmds_dropped >
  4487. vha->qla_stats.stat_max_qfull_cmds_dropped)
  4488. vha->qla_stats.stat_max_qfull_cmds_dropped =
  4489. vha->hw->tgt.num_qfull_cmds_dropped;
  4490. ql_dbg(ql_dbg_io, vha, 0x3068,
  4491. "qla_target(%d): %s: QFull CMD dropped[%d]\n",
  4492. vha->vp_idx, __func__,
  4493. vha->hw->tgt.num_qfull_cmds_dropped);
  4494. qlt_chk_exch_leak_thresh_hold(vha);
  4495. return;
  4496. }
  4497. sess = ha->tgt.tgt_ops->find_sess_by_s_id
  4498. (vha, atio->u.isp24.fcp_hdr.s_id);
  4499. if (!sess)
  4500. return;
  4501. se_sess = sess->se_sess;
  4502. tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
  4503. if (tag < 0)
  4504. return;
  4505. cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
  4506. if (!cmd) {
  4507. ql_dbg(ql_dbg_io, vha, 0x3009,
  4508. "qla_target(%d): %s: Allocation of cmd failed\n",
  4509. vha->vp_idx, __func__);
  4510. vha->hw->tgt.num_qfull_cmds_dropped++;
  4511. if (vha->hw->tgt.num_qfull_cmds_dropped >
  4512. vha->qla_stats.stat_max_qfull_cmds_dropped)
  4513. vha->qla_stats.stat_max_qfull_cmds_dropped =
  4514. vha->hw->tgt.num_qfull_cmds_dropped;
  4515. qlt_chk_exch_leak_thresh_hold(vha);
  4516. return;
  4517. }
  4518. memset(cmd, 0, sizeof(struct qla_tgt_cmd));
  4519. qlt_incr_num_pend_cmds(vha);
  4520. INIT_LIST_HEAD(&cmd->cmd_list);
  4521. memcpy(&cmd->atio, atio, sizeof(*atio));
  4522. cmd->tgt = vha->vha_tgt.qla_tgt;
  4523. cmd->vha = vha;
  4524. cmd->reset_count = vha->hw->chip_reset;
  4525. cmd->q_full = 1;
  4526. if (qfull) {
  4527. cmd->q_full = 1;
  4528. /* NOTE: borrowing the state field to carry the status */
  4529. cmd->state = status;
  4530. } else
  4531. cmd->term_exchg = 1;
  4532. list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
  4533. vha->hw->tgt.num_qfull_cmds_alloc++;
  4534. if (vha->hw->tgt.num_qfull_cmds_alloc >
  4535. vha->qla_stats.stat_max_qfull_cmds_alloc)
  4536. vha->qla_stats.stat_max_qfull_cmds_alloc =
  4537. vha->hw->tgt.num_qfull_cmds_alloc;
  4538. }
  4539. int
  4540. qlt_free_qfull_cmds(struct scsi_qla_host *vha)
  4541. {
  4542. struct qla_hw_data *ha = vha->hw;
  4543. unsigned long flags;
  4544. struct qla_tgt_cmd *cmd, *tcmd;
  4545. struct list_head free_list;
  4546. int rc = 0;
  4547. if (list_empty(&ha->tgt.q_full_list))
  4548. return 0;
  4549. INIT_LIST_HEAD(&free_list);
  4550. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  4551. if (list_empty(&ha->tgt.q_full_list)) {
  4552. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  4553. return 0;
  4554. }
  4555. list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
  4556. if (cmd->q_full)
  4557. /* cmd->state is a borrowed field to hold status */
  4558. rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
  4559. else if (cmd->term_exchg)
  4560. rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
  4561. if (rc == -ENOMEM)
  4562. break;
  4563. if (cmd->q_full)
  4564. ql_dbg(ql_dbg_io, vha, 0x3006,
  4565. "%s: busy sent for ox_id[%04x]\n", __func__,
  4566. be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
  4567. else if (cmd->term_exchg)
  4568. ql_dbg(ql_dbg_io, vha, 0x3007,
  4569. "%s: Term exchg sent for ox_id[%04x]\n", __func__,
  4570. be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
  4571. else
  4572. ql_dbg(ql_dbg_io, vha, 0x3008,
  4573. "%s: Unexpected cmd in QFull list %p\n", __func__,
  4574. cmd);
  4575. list_del(&cmd->cmd_list);
  4576. list_add_tail(&cmd->cmd_list, &free_list);
  4577. /* piggy back on hardware_lock for protection */
  4578. vha->hw->tgt.num_qfull_cmds_alloc--;
  4579. }
  4580. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  4581. cmd = NULL;
  4582. list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
  4583. list_del(&cmd->cmd_list);
  4584. /* This cmd was never sent to TCM. There is no need
  4585. * to schedule free or call free_cmd
  4586. */
  4587. qlt_free_cmd(cmd);
  4588. }
  4589. return rc;
  4590. }
  4591. static void
  4592. qlt_send_busy(struct scsi_qla_host *vha,
  4593. struct atio_from_isp *atio, uint16_t status)
  4594. {
  4595. int rc = 0;
  4596. rc = __qlt_send_busy(vha, atio, status);
  4597. if (rc == -ENOMEM)
  4598. qlt_alloc_qfull_cmd(vha, atio, status, 1);
  4599. }
  4600. static int
  4601. qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
  4602. struct atio_from_isp *atio, bool ha_locked)
  4603. {
  4604. struct qla_hw_data *ha = vha->hw;
  4605. uint16_t status;
  4606. unsigned long flags;
  4607. if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
  4608. return 0;
  4609. if (!ha_locked)
  4610. spin_lock_irqsave(&ha->hardware_lock, flags);
  4611. status = temp_sam_status;
  4612. qlt_send_busy(vha, atio, status);
  4613. if (!ha_locked)
  4614. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4615. return 1;
  4616. }
  4617. /* ha->hardware_lock supposed to be held on entry */
  4618. /* called via callback from qla2xxx */
  4619. static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
  4620. struct atio_from_isp *atio, uint8_t ha_locked)
  4621. {
  4622. struct qla_hw_data *ha = vha->hw;
  4623. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  4624. int rc;
  4625. unsigned long flags;
  4626. if (unlikely(tgt == NULL)) {
  4627. ql_dbg(ql_dbg_io, vha, 0x3064,
  4628. "ATIO pkt, but no tgt (ha %p)", ha);
  4629. return;
  4630. }
  4631. /*
  4632. * In tgt_stop mode we also should allow all requests to pass.
  4633. * Otherwise, some commands can stuck.
  4634. */
  4635. tgt->atio_irq_cmd_count++;
  4636. switch (atio->u.raw.entry_type) {
  4637. case ATIO_TYPE7:
  4638. if (unlikely(atio->u.isp24.exchange_addr ==
  4639. ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
  4640. ql_dbg(ql_dbg_io, vha, 0x3065,
  4641. "qla_target(%d): ATIO_TYPE7 "
  4642. "received with UNKNOWN exchange address, "
  4643. "sending QUEUE_FULL\n", vha->vp_idx);
  4644. if (!ha_locked)
  4645. spin_lock_irqsave(&ha->hardware_lock, flags);
  4646. qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
  4647. if (!ha_locked)
  4648. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4649. break;
  4650. }
  4651. if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
  4652. rc = qlt_chk_qfull_thresh_hold(vha, atio, ha_locked);
  4653. if (rc != 0) {
  4654. tgt->atio_irq_cmd_count--;
  4655. return;
  4656. }
  4657. rc = qlt_handle_cmd_for_atio(vha, atio);
  4658. } else {
  4659. rc = qlt_handle_task_mgmt(vha, atio);
  4660. }
  4661. if (unlikely(rc != 0)) {
  4662. if (rc == -ESRCH) {
  4663. if (!ha_locked)
  4664. spin_lock_irqsave
  4665. (&ha->hardware_lock, flags);
  4666. #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
  4667. qlt_send_busy(vha, atio, SAM_STAT_BUSY);
  4668. #else
  4669. qlt_send_term_exchange(vha, NULL, atio, 1, 0);
  4670. #endif
  4671. if (!ha_locked)
  4672. spin_unlock_irqrestore
  4673. (&ha->hardware_lock, flags);
  4674. } else {
  4675. if (tgt->tgt_stop) {
  4676. ql_dbg(ql_dbg_tgt, vha, 0xe059,
  4677. "qla_target: Unable to send "
  4678. "command to target for req, "
  4679. "ignoring.\n");
  4680. } else {
  4681. ql_dbg(ql_dbg_tgt, vha, 0xe05a,
  4682. "qla_target(%d): Unable to send "
  4683. "command to target, sending BUSY "
  4684. "status.\n", vha->vp_idx);
  4685. if (!ha_locked)
  4686. spin_lock_irqsave(
  4687. &ha->hardware_lock, flags);
  4688. qlt_send_busy(vha, atio, SAM_STAT_BUSY);
  4689. if (!ha_locked)
  4690. spin_unlock_irqrestore(
  4691. &ha->hardware_lock, flags);
  4692. }
  4693. }
  4694. }
  4695. break;
  4696. case IMMED_NOTIFY_TYPE:
  4697. {
  4698. if (unlikely(atio->u.isp2x.entry_status != 0)) {
  4699. ql_dbg(ql_dbg_tgt, vha, 0xe05b,
  4700. "qla_target(%d): Received ATIO packet %x "
  4701. "with error status %x\n", vha->vp_idx,
  4702. atio->u.raw.entry_type,
  4703. atio->u.isp2x.entry_status);
  4704. break;
  4705. }
  4706. ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
  4707. if (!ha_locked)
  4708. spin_lock_irqsave(&ha->hardware_lock, flags);
  4709. qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
  4710. if (!ha_locked)
  4711. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4712. break;
  4713. }
  4714. default:
  4715. ql_dbg(ql_dbg_tgt, vha, 0xe05c,
  4716. "qla_target(%d): Received unknown ATIO atio "
  4717. "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
  4718. break;
  4719. }
  4720. tgt->atio_irq_cmd_count--;
  4721. }
  4722. /* ha->hardware_lock supposed to be held on entry */
  4723. /* called via callback from qla2xxx */
  4724. static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
  4725. {
  4726. struct qla_hw_data *ha = vha->hw;
  4727. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  4728. if (unlikely(tgt == NULL)) {
  4729. ql_dbg(ql_dbg_tgt, vha, 0xe05d,
  4730. "qla_target(%d): Response pkt %x received, but no "
  4731. "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
  4732. return;
  4733. }
  4734. /*
  4735. * In tgt_stop mode we also should allow all requests to pass.
  4736. * Otherwise, some commands can stuck.
  4737. */
  4738. tgt->irq_cmd_count++;
  4739. switch (pkt->entry_type) {
  4740. case CTIO_CRC2:
  4741. case CTIO_TYPE7:
  4742. {
  4743. struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
  4744. qlt_do_ctio_completion(vha, entry->handle,
  4745. le16_to_cpu(entry->status)|(pkt->entry_status << 16),
  4746. entry);
  4747. break;
  4748. }
  4749. case ACCEPT_TGT_IO_TYPE:
  4750. {
  4751. struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
  4752. int rc;
  4753. if (atio->u.isp2x.status !=
  4754. cpu_to_le16(ATIO_CDB_VALID)) {
  4755. ql_dbg(ql_dbg_tgt, vha, 0xe05e,
  4756. "qla_target(%d): ATIO with error "
  4757. "status %x received\n", vha->vp_idx,
  4758. le16_to_cpu(atio->u.isp2x.status));
  4759. break;
  4760. }
  4761. rc = qlt_chk_qfull_thresh_hold(vha, atio, true);
  4762. if (rc != 0) {
  4763. tgt->irq_cmd_count--;
  4764. return;
  4765. }
  4766. rc = qlt_handle_cmd_for_atio(vha, atio);
  4767. if (unlikely(rc != 0)) {
  4768. if (rc == -ESRCH) {
  4769. #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
  4770. qlt_send_busy(vha, atio, 0);
  4771. #else
  4772. qlt_send_term_exchange(vha, NULL, atio, 1, 0);
  4773. #endif
  4774. } else {
  4775. if (tgt->tgt_stop) {
  4776. ql_dbg(ql_dbg_tgt, vha, 0xe05f,
  4777. "qla_target: Unable to send "
  4778. "command to target, sending TERM "
  4779. "EXCHANGE for rsp\n");
  4780. qlt_send_term_exchange(vha, NULL,
  4781. atio, 1, 0);
  4782. } else {
  4783. ql_dbg(ql_dbg_tgt, vha, 0xe060,
  4784. "qla_target(%d): Unable to send "
  4785. "command to target, sending BUSY "
  4786. "status\n", vha->vp_idx);
  4787. qlt_send_busy(vha, atio, 0);
  4788. }
  4789. }
  4790. }
  4791. }
  4792. break;
  4793. case CONTINUE_TGT_IO_TYPE:
  4794. {
  4795. struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
  4796. qlt_do_ctio_completion(vha, entry->handle,
  4797. le16_to_cpu(entry->status)|(pkt->entry_status << 16),
  4798. entry);
  4799. break;
  4800. }
  4801. case CTIO_A64_TYPE:
  4802. {
  4803. struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
  4804. qlt_do_ctio_completion(vha, entry->handle,
  4805. le16_to_cpu(entry->status)|(pkt->entry_status << 16),
  4806. entry);
  4807. break;
  4808. }
  4809. case IMMED_NOTIFY_TYPE:
  4810. ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
  4811. qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
  4812. break;
  4813. case NOTIFY_ACK_TYPE:
  4814. if (tgt->notify_ack_expected > 0) {
  4815. struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
  4816. ql_dbg(ql_dbg_tgt, vha, 0xe036,
  4817. "NOTIFY_ACK seq %08x status %x\n",
  4818. le16_to_cpu(entry->u.isp2x.seq_id),
  4819. le16_to_cpu(entry->u.isp2x.status));
  4820. tgt->notify_ack_expected--;
  4821. if (entry->u.isp2x.status !=
  4822. cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
  4823. ql_dbg(ql_dbg_tgt, vha, 0xe061,
  4824. "qla_target(%d): NOTIFY_ACK "
  4825. "failed %x\n", vha->vp_idx,
  4826. le16_to_cpu(entry->u.isp2x.status));
  4827. }
  4828. } else {
  4829. ql_dbg(ql_dbg_tgt, vha, 0xe062,
  4830. "qla_target(%d): Unexpected NOTIFY_ACK received\n",
  4831. vha->vp_idx);
  4832. }
  4833. break;
  4834. case ABTS_RECV_24XX:
  4835. ql_dbg(ql_dbg_tgt, vha, 0xe037,
  4836. "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
  4837. qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
  4838. break;
  4839. case ABTS_RESP_24XX:
  4840. if (tgt->abts_resp_expected > 0) {
  4841. struct abts_resp_from_24xx_fw *entry =
  4842. (struct abts_resp_from_24xx_fw *)pkt;
  4843. ql_dbg(ql_dbg_tgt, vha, 0xe038,
  4844. "ABTS_RESP_24XX: compl_status %x\n",
  4845. entry->compl_status);
  4846. tgt->abts_resp_expected--;
  4847. if (le16_to_cpu(entry->compl_status) !=
  4848. ABTS_RESP_COMPL_SUCCESS) {
  4849. if ((entry->error_subcode1 == 0x1E) &&
  4850. (entry->error_subcode2 == 0)) {
  4851. /*
  4852. * We've got a race here: aborted
  4853. * exchange not terminated, i.e.
  4854. * response for the aborted command was
  4855. * sent between the abort request was
  4856. * received and processed.
  4857. * Unfortunately, the firmware has a
  4858. * silly requirement that all aborted
  4859. * exchanges must be explicitely
  4860. * terminated, otherwise it refuses to
  4861. * send responses for the abort
  4862. * requests. So, we have to
  4863. * (re)terminate the exchange and retry
  4864. * the abort response.
  4865. */
  4866. qlt_24xx_retry_term_exchange(vha,
  4867. entry);
  4868. } else
  4869. ql_dbg(ql_dbg_tgt, vha, 0xe063,
  4870. "qla_target(%d): ABTS_RESP_24XX "
  4871. "failed %x (subcode %x:%x)",
  4872. vha->vp_idx, entry->compl_status,
  4873. entry->error_subcode1,
  4874. entry->error_subcode2);
  4875. }
  4876. } else {
  4877. ql_dbg(ql_dbg_tgt, vha, 0xe064,
  4878. "qla_target(%d): Unexpected ABTS_RESP_24XX "
  4879. "received\n", vha->vp_idx);
  4880. }
  4881. break;
  4882. default:
  4883. ql_dbg(ql_dbg_tgt, vha, 0xe065,
  4884. "qla_target(%d): Received unknown response pkt "
  4885. "type %x\n", vha->vp_idx, pkt->entry_type);
  4886. break;
  4887. }
  4888. tgt->irq_cmd_count--;
  4889. }
  4890. /*
  4891. * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
  4892. */
  4893. void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
  4894. uint16_t *mailbox)
  4895. {
  4896. struct qla_hw_data *ha = vha->hw;
  4897. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  4898. int login_code;
  4899. if (!ha->tgt.tgt_ops)
  4900. return;
  4901. if (unlikely(tgt == NULL)) {
  4902. ql_dbg(ql_dbg_tgt, vha, 0xe03a,
  4903. "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
  4904. return;
  4905. }
  4906. if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
  4907. IS_QLA2100(ha))
  4908. return;
  4909. /*
  4910. * In tgt_stop mode we also should allow all requests to pass.
  4911. * Otherwise, some commands can stuck.
  4912. */
  4913. tgt->irq_cmd_count++;
  4914. switch (code) {
  4915. case MBA_RESET: /* Reset */
  4916. case MBA_SYSTEM_ERR: /* System Error */
  4917. case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
  4918. case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
  4919. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
  4920. "qla_target(%d): System error async event %#x "
  4921. "occurred", vha->vp_idx, code);
  4922. break;
  4923. case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
  4924. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  4925. break;
  4926. case MBA_LOOP_UP:
  4927. {
  4928. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
  4929. "qla_target(%d): Async LOOP_UP occurred "
  4930. "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
  4931. le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
  4932. le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
  4933. if (tgt->link_reinit_iocb_pending) {
  4934. qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
  4935. 0, 0, 0, 0, 0, 0);
  4936. tgt->link_reinit_iocb_pending = 0;
  4937. }
  4938. break;
  4939. }
  4940. case MBA_LIP_OCCURRED:
  4941. case MBA_LOOP_DOWN:
  4942. case MBA_LIP_RESET:
  4943. case MBA_RSCN_UPDATE:
  4944. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
  4945. "qla_target(%d): Async event %#x occurred "
  4946. "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
  4947. le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
  4948. le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
  4949. break;
  4950. case MBA_PORT_UPDATE:
  4951. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
  4952. "qla_target(%d): Port update async event %#x "
  4953. "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
  4954. "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
  4955. le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
  4956. le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
  4957. login_code = le16_to_cpu(mailbox[2]);
  4958. if (login_code == 0x4)
  4959. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
  4960. "Async MB 2: Got PLOGI Complete\n");
  4961. else if (login_code == 0x7)
  4962. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
  4963. "Async MB 2: Port Logged Out\n");
  4964. break;
  4965. default:
  4966. break;
  4967. }
  4968. tgt->irq_cmd_count--;
  4969. }
  4970. static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
  4971. uint16_t loop_id)
  4972. {
  4973. fc_port_t *fcport;
  4974. int rc;
  4975. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  4976. if (!fcport) {
  4977. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
  4978. "qla_target(%d): Allocation of tmp FC port failed",
  4979. vha->vp_idx);
  4980. return NULL;
  4981. }
  4982. fcport->loop_id = loop_id;
  4983. rc = qla2x00_get_port_database(vha, fcport, 0);
  4984. if (rc != QLA_SUCCESS) {
  4985. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
  4986. "qla_target(%d): Failed to retrieve fcport "
  4987. "information -- get_port_database() returned %x "
  4988. "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
  4989. kfree(fcport);
  4990. return NULL;
  4991. }
  4992. return fcport;
  4993. }
  4994. /* Must be called under tgt_mutex */
  4995. static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
  4996. uint8_t *s_id)
  4997. {
  4998. struct qla_tgt_sess *sess = NULL;
  4999. fc_port_t *fcport = NULL;
  5000. int rc, global_resets;
  5001. uint16_t loop_id = 0;
  5002. mutex_lock(&vha->vha_tgt.tgt_mutex);
  5003. retry:
  5004. global_resets =
  5005. atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
  5006. rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
  5007. if (rc != 0) {
  5008. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  5009. if ((s_id[0] == 0xFF) &&
  5010. (s_id[1] == 0xFC)) {
  5011. /*
  5012. * This is Domain Controller, so it should be
  5013. * OK to drop SCSI commands from it.
  5014. */
  5015. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
  5016. "Unable to find initiator with S_ID %x:%x:%x",
  5017. s_id[0], s_id[1], s_id[2]);
  5018. } else
  5019. ql_log(ql_log_info, vha, 0xf071,
  5020. "qla_target(%d): Unable to find "
  5021. "initiator with S_ID %x:%x:%x",
  5022. vha->vp_idx, s_id[0], s_id[1],
  5023. s_id[2]);
  5024. if (rc == -ENOENT) {
  5025. qlt_port_logo_t logo;
  5026. sid_to_portid(s_id, &logo.id);
  5027. logo.cmd_count = 1;
  5028. qlt_send_first_logo(vha, &logo);
  5029. }
  5030. return NULL;
  5031. }
  5032. fcport = qlt_get_port_database(vha, loop_id);
  5033. if (!fcport) {
  5034. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  5035. return NULL;
  5036. }
  5037. if (global_resets !=
  5038. atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
  5039. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
  5040. "qla_target(%d): global reset during session discovery "
  5041. "(counter was %d, new %d), retrying", vha->vp_idx,
  5042. global_resets,
  5043. atomic_read(&vha->vha_tgt.
  5044. qla_tgt->tgt_global_resets_count));
  5045. goto retry;
  5046. }
  5047. sess = qlt_create_sess(vha, fcport, true);
  5048. mutex_unlock(&vha->vha_tgt.tgt_mutex);
  5049. kfree(fcport);
  5050. return sess;
  5051. }
  5052. static void qlt_abort_work(struct qla_tgt *tgt,
  5053. struct qla_tgt_sess_work_param *prm)
  5054. {
  5055. struct scsi_qla_host *vha = tgt->vha;
  5056. struct qla_hw_data *ha = vha->hw;
  5057. struct qla_tgt_sess *sess = NULL;
  5058. unsigned long flags = 0, flags2 = 0;
  5059. uint32_t be_s_id;
  5060. uint8_t s_id[3];
  5061. int rc;
  5062. spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
  5063. if (tgt->tgt_stop)
  5064. goto out_term2;
  5065. s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
  5066. s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
  5067. s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
  5068. sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
  5069. (unsigned char *)&be_s_id);
  5070. if (!sess) {
  5071. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
  5072. sess = qlt_make_local_sess(vha, s_id);
  5073. /* sess has got an extra creation ref */
  5074. spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
  5075. if (!sess)
  5076. goto out_term2;
  5077. } else {
  5078. if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
  5079. sess = NULL;
  5080. goto out_term2;
  5081. }
  5082. kref_get(&sess->sess_kref);
  5083. }
  5084. spin_lock_irqsave(&ha->hardware_lock, flags);
  5085. if (tgt->tgt_stop)
  5086. goto out_term;
  5087. rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
  5088. if (rc != 0)
  5089. goto out_term;
  5090. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5091. qlt_put_sess(sess);
  5092. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
  5093. return;
  5094. out_term2:
  5095. spin_lock_irqsave(&ha->hardware_lock, flags);
  5096. out_term:
  5097. qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
  5098. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5099. qlt_put_sess(sess);
  5100. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
  5101. }
  5102. static void qlt_tmr_work(struct qla_tgt *tgt,
  5103. struct qla_tgt_sess_work_param *prm)
  5104. {
  5105. struct atio_from_isp *a = &prm->tm_iocb2;
  5106. struct scsi_qla_host *vha = tgt->vha;
  5107. struct qla_hw_data *ha = vha->hw;
  5108. struct qla_tgt_sess *sess = NULL;
  5109. unsigned long flags;
  5110. uint8_t *s_id = NULL; /* to hide compiler warnings */
  5111. int rc;
  5112. uint32_t lun, unpacked_lun;
  5113. int fn;
  5114. void *iocb;
  5115. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  5116. if (tgt->tgt_stop)
  5117. goto out_term;
  5118. s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
  5119. sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
  5120. if (!sess) {
  5121. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  5122. sess = qlt_make_local_sess(vha, s_id);
  5123. /* sess has got an extra creation ref */
  5124. spin_lock_irqsave(&ha->tgt.sess_lock, flags);
  5125. if (!sess)
  5126. goto out_term;
  5127. } else {
  5128. if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
  5129. sess = NULL;
  5130. goto out_term;
  5131. }
  5132. kref_get(&sess->sess_kref);
  5133. }
  5134. iocb = a;
  5135. lun = a->u.isp24.fcp_cmnd.lun;
  5136. fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
  5137. unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
  5138. rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
  5139. if (rc != 0)
  5140. goto out_term;
  5141. qlt_put_sess(sess);
  5142. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  5143. return;
  5144. out_term:
  5145. qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
  5146. qlt_put_sess(sess);
  5147. spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
  5148. }
  5149. static void qlt_sess_work_fn(struct work_struct *work)
  5150. {
  5151. struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
  5152. struct scsi_qla_host *vha = tgt->vha;
  5153. unsigned long flags;
  5154. ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
  5155. spin_lock_irqsave(&tgt->sess_work_lock, flags);
  5156. while (!list_empty(&tgt->sess_works_list)) {
  5157. struct qla_tgt_sess_work_param *prm = list_entry(
  5158. tgt->sess_works_list.next, typeof(*prm),
  5159. sess_works_list_entry);
  5160. /*
  5161. * This work can be scheduled on several CPUs at time, so we
  5162. * must delete the entry to eliminate double processing
  5163. */
  5164. list_del(&prm->sess_works_list_entry);
  5165. spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
  5166. switch (prm->type) {
  5167. case QLA_TGT_SESS_WORK_ABORT:
  5168. qlt_abort_work(tgt, prm);
  5169. break;
  5170. case QLA_TGT_SESS_WORK_TM:
  5171. qlt_tmr_work(tgt, prm);
  5172. break;
  5173. default:
  5174. BUG_ON(1);
  5175. break;
  5176. }
  5177. spin_lock_irqsave(&tgt->sess_work_lock, flags);
  5178. kfree(prm);
  5179. }
  5180. spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
  5181. }
  5182. /* Must be called under tgt_host_action_mutex */
  5183. int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
  5184. {
  5185. struct qla_tgt *tgt;
  5186. if (!QLA_TGT_MODE_ENABLED())
  5187. return 0;
  5188. if (!IS_TGT_MODE_CAPABLE(ha)) {
  5189. ql_log(ql_log_warn, base_vha, 0xe070,
  5190. "This adapter does not support target mode.\n");
  5191. return 0;
  5192. }
  5193. ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
  5194. "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
  5195. BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
  5196. tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
  5197. if (!tgt) {
  5198. ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
  5199. "Unable to allocate struct qla_tgt\n");
  5200. return -ENOMEM;
  5201. }
  5202. if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
  5203. base_vha->host->hostt->supported_mode |= MODE_TARGET;
  5204. tgt->ha = ha;
  5205. tgt->vha = base_vha;
  5206. init_waitqueue_head(&tgt->waitQ);
  5207. INIT_LIST_HEAD(&tgt->sess_list);
  5208. INIT_LIST_HEAD(&tgt->del_sess_list);
  5209. INIT_DELAYED_WORK(&tgt->sess_del_work,
  5210. (void (*)(struct work_struct *))qlt_del_sess_work_fn);
  5211. spin_lock_init(&tgt->sess_work_lock);
  5212. INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
  5213. INIT_LIST_HEAD(&tgt->sess_works_list);
  5214. spin_lock_init(&tgt->srr_lock);
  5215. INIT_LIST_HEAD(&tgt->srr_ctio_list);
  5216. INIT_LIST_HEAD(&tgt->srr_imm_list);
  5217. INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
  5218. atomic_set(&tgt->tgt_global_resets_count, 0);
  5219. base_vha->vha_tgt.qla_tgt = tgt;
  5220. ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
  5221. "qla_target(%d): using 64 Bit PCI addressing",
  5222. base_vha->vp_idx);
  5223. tgt->tgt_enable_64bit_addr = 1;
  5224. /* 3 is reserved */
  5225. tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
  5226. tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
  5227. tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
  5228. if (base_vha->fc_vport)
  5229. return 0;
  5230. mutex_lock(&qla_tgt_mutex);
  5231. list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
  5232. mutex_unlock(&qla_tgt_mutex);
  5233. return 0;
  5234. }
  5235. /* Must be called under tgt_host_action_mutex */
  5236. int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
  5237. {
  5238. if (!vha->vha_tgt.qla_tgt)
  5239. return 0;
  5240. if (vha->fc_vport) {
  5241. qlt_release(vha->vha_tgt.qla_tgt);
  5242. return 0;
  5243. }
  5244. /* free left over qfull cmds */
  5245. qlt_init_term_exchange(vha);
  5246. mutex_lock(&qla_tgt_mutex);
  5247. list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
  5248. mutex_unlock(&qla_tgt_mutex);
  5249. ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
  5250. vha->host_no, ha);
  5251. qlt_release(vha->vha_tgt.qla_tgt);
  5252. return 0;
  5253. }
  5254. static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
  5255. unsigned char *b)
  5256. {
  5257. int i;
  5258. pr_debug("qla2xxx HW vha->node_name: ");
  5259. for (i = 0; i < WWN_SIZE; i++)
  5260. pr_debug("%02x ", vha->node_name[i]);
  5261. pr_debug("\n");
  5262. pr_debug("qla2xxx HW vha->port_name: ");
  5263. for (i = 0; i < WWN_SIZE; i++)
  5264. pr_debug("%02x ", vha->port_name[i]);
  5265. pr_debug("\n");
  5266. pr_debug("qla2xxx passed configfs WWPN: ");
  5267. put_unaligned_be64(wwpn, b);
  5268. for (i = 0; i < WWN_SIZE; i++)
  5269. pr_debug("%02x ", b[i]);
  5270. pr_debug("\n");
  5271. }
  5272. /**
  5273. * qla_tgt_lport_register - register lport with external module
  5274. *
  5275. * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
  5276. * @wwpn: Passwd FC target WWPN
  5277. * @callback: lport initialization callback for tcm_qla2xxx code
  5278. * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
  5279. */
  5280. int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
  5281. u64 npiv_wwpn, u64 npiv_wwnn,
  5282. int (*callback)(struct scsi_qla_host *, void *, u64, u64))
  5283. {
  5284. struct qla_tgt *tgt;
  5285. struct scsi_qla_host *vha;
  5286. struct qla_hw_data *ha;
  5287. struct Scsi_Host *host;
  5288. unsigned long flags;
  5289. int rc;
  5290. u8 b[WWN_SIZE];
  5291. mutex_lock(&qla_tgt_mutex);
  5292. list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
  5293. vha = tgt->vha;
  5294. ha = vha->hw;
  5295. host = vha->host;
  5296. if (!host)
  5297. continue;
  5298. if (!(host->hostt->supported_mode & MODE_TARGET))
  5299. continue;
  5300. spin_lock_irqsave(&ha->hardware_lock, flags);
  5301. if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
  5302. pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
  5303. host->host_no);
  5304. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5305. continue;
  5306. }
  5307. if (tgt->tgt_stop) {
  5308. pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
  5309. host->host_no);
  5310. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5311. continue;
  5312. }
  5313. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5314. if (!scsi_host_get(host)) {
  5315. ql_dbg(ql_dbg_tgt, vha, 0xe068,
  5316. "Unable to scsi_host_get() for"
  5317. " qla2xxx scsi_host\n");
  5318. continue;
  5319. }
  5320. qlt_lport_dump(vha, phys_wwpn, b);
  5321. if (memcmp(vha->port_name, b, WWN_SIZE)) {
  5322. scsi_host_put(host);
  5323. continue;
  5324. }
  5325. rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
  5326. if (rc != 0)
  5327. scsi_host_put(host);
  5328. mutex_unlock(&qla_tgt_mutex);
  5329. return rc;
  5330. }
  5331. mutex_unlock(&qla_tgt_mutex);
  5332. return -ENODEV;
  5333. }
  5334. EXPORT_SYMBOL(qlt_lport_register);
  5335. /**
  5336. * qla_tgt_lport_deregister - Degister lport
  5337. *
  5338. * @vha: Registered scsi_qla_host pointer
  5339. */
  5340. void qlt_lport_deregister(struct scsi_qla_host *vha)
  5341. {
  5342. struct qla_hw_data *ha = vha->hw;
  5343. struct Scsi_Host *sh = vha->host;
  5344. /*
  5345. * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
  5346. */
  5347. vha->vha_tgt.target_lport_ptr = NULL;
  5348. ha->tgt.tgt_ops = NULL;
  5349. /*
  5350. * Release the Scsi_Host reference for the underlying qla2xxx host
  5351. */
  5352. scsi_host_put(sh);
  5353. }
  5354. EXPORT_SYMBOL(qlt_lport_deregister);
  5355. /* Must be called under HW lock */
  5356. static void qlt_set_mode(struct scsi_qla_host *vha)
  5357. {
  5358. struct qla_hw_data *ha = vha->hw;
  5359. switch (ql2x_ini_mode) {
  5360. case QLA2XXX_INI_MODE_DISABLED:
  5361. case QLA2XXX_INI_MODE_EXCLUSIVE:
  5362. vha->host->active_mode = MODE_TARGET;
  5363. break;
  5364. case QLA2XXX_INI_MODE_ENABLED:
  5365. vha->host->active_mode |= MODE_TARGET;
  5366. break;
  5367. default:
  5368. break;
  5369. }
  5370. if (ha->tgt.ini_mode_force_reverse)
  5371. qla_reverse_ini_mode(vha);
  5372. }
  5373. /* Must be called under HW lock */
  5374. static void qlt_clear_mode(struct scsi_qla_host *vha)
  5375. {
  5376. struct qla_hw_data *ha = vha->hw;
  5377. switch (ql2x_ini_mode) {
  5378. case QLA2XXX_INI_MODE_DISABLED:
  5379. vha->host->active_mode = MODE_UNKNOWN;
  5380. break;
  5381. case QLA2XXX_INI_MODE_EXCLUSIVE:
  5382. vha->host->active_mode = MODE_INITIATOR;
  5383. break;
  5384. case QLA2XXX_INI_MODE_ENABLED:
  5385. vha->host->active_mode &= ~MODE_TARGET;
  5386. break;
  5387. default:
  5388. break;
  5389. }
  5390. if (ha->tgt.ini_mode_force_reverse)
  5391. qla_reverse_ini_mode(vha);
  5392. }
  5393. /*
  5394. * qla_tgt_enable_vha - NO LOCK HELD
  5395. *
  5396. * host_reset, bring up w/ Target Mode Enabled
  5397. */
  5398. void
  5399. qlt_enable_vha(struct scsi_qla_host *vha)
  5400. {
  5401. struct qla_hw_data *ha = vha->hw;
  5402. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  5403. unsigned long flags;
  5404. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  5405. int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
  5406. if (!tgt) {
  5407. ql_dbg(ql_dbg_tgt, vha, 0xe069,
  5408. "Unable to locate qla_tgt pointer from"
  5409. " struct qla_hw_data\n");
  5410. dump_stack();
  5411. return;
  5412. }
  5413. spin_lock_irqsave(&ha->hardware_lock, flags);
  5414. tgt->tgt_stopped = 0;
  5415. qlt_set_mode(vha);
  5416. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5417. if (vha->vp_idx) {
  5418. qla24xx_disable_vp(vha);
  5419. qla24xx_enable_vp(vha);
  5420. } else {
  5421. if (ha->msix_entries) {
  5422. ql_dbg(ql_dbg_tgt, vha, 0xffff,
  5423. "%s: host%ld : vector %d cpu %d\n",
  5424. __func__, vha->host_no,
  5425. ha->msix_entries[rspq_ent].vector,
  5426. ha->msix_entries[rspq_ent].cpuid);
  5427. ha->tgt.rspq_vector_cpuid =
  5428. ha->msix_entries[rspq_ent].cpuid;
  5429. }
  5430. set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
  5431. qla2xxx_wake_dpc(base_vha);
  5432. qla2x00_wait_for_hba_online(base_vha);
  5433. }
  5434. }
  5435. EXPORT_SYMBOL(qlt_enable_vha);
  5436. /*
  5437. * qla_tgt_disable_vha - NO LOCK HELD
  5438. *
  5439. * Disable Target Mode and reset the adapter
  5440. */
  5441. static void qlt_disable_vha(struct scsi_qla_host *vha)
  5442. {
  5443. struct qla_hw_data *ha = vha->hw;
  5444. struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
  5445. unsigned long flags;
  5446. if (!tgt) {
  5447. ql_dbg(ql_dbg_tgt, vha, 0xe06a,
  5448. "Unable to locate qla_tgt pointer from"
  5449. " struct qla_hw_data\n");
  5450. dump_stack();
  5451. return;
  5452. }
  5453. spin_lock_irqsave(&ha->hardware_lock, flags);
  5454. qlt_clear_mode(vha);
  5455. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5456. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  5457. qla2xxx_wake_dpc(vha);
  5458. qla2x00_wait_for_hba_online(vha);
  5459. }
  5460. /*
  5461. * Called from qla_init.c:qla24xx_vport_create() contex to setup
  5462. * the target mode specific struct scsi_qla_host and struct qla_hw_data
  5463. * members.
  5464. */
  5465. void
  5466. qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
  5467. {
  5468. if (!qla_tgt_mode_enabled(vha))
  5469. return;
  5470. vha->vha_tgt.qla_tgt = NULL;
  5471. mutex_init(&vha->vha_tgt.tgt_mutex);
  5472. mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
  5473. qlt_clear_mode(vha);
  5474. /*
  5475. * NOTE: Currently the value is kept the same for <24xx and
  5476. * >=24xx ISPs. If it is necessary to change it,
  5477. * the check should be added for specific ISPs,
  5478. * assigning the value appropriately.
  5479. */
  5480. ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
  5481. qlt_add_target(ha, vha);
  5482. }
  5483. void
  5484. qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
  5485. {
  5486. /*
  5487. * FC-4 Feature bit 0 indicates target functionality to the name server.
  5488. */
  5489. if (qla_tgt_mode_enabled(vha)) {
  5490. if (qla_ini_mode_enabled(vha))
  5491. ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
  5492. else
  5493. ct_req->req.rff_id.fc4_feature = BIT_0;
  5494. } else if (qla_ini_mode_enabled(vha)) {
  5495. ct_req->req.rff_id.fc4_feature = BIT_1;
  5496. }
  5497. }
  5498. /*
  5499. * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
  5500. * @ha: HA context
  5501. *
  5502. * Beginning of ATIO ring has initialization control block already built
  5503. * by nvram config routine.
  5504. *
  5505. * Returns 0 on success.
  5506. */
  5507. void
  5508. qlt_init_atio_q_entries(struct scsi_qla_host *vha)
  5509. {
  5510. struct qla_hw_data *ha = vha->hw;
  5511. uint16_t cnt;
  5512. struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
  5513. if (!qla_tgt_mode_enabled(vha))
  5514. return;
  5515. for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
  5516. pkt->u.raw.signature = ATIO_PROCESSED;
  5517. pkt++;
  5518. }
  5519. }
  5520. /*
  5521. * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
  5522. * @ha: SCSI driver HA context
  5523. */
  5524. void
  5525. qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
  5526. {
  5527. struct qla_hw_data *ha = vha->hw;
  5528. struct atio_from_isp *pkt;
  5529. int cnt, i;
  5530. if (!vha->flags.online)
  5531. return;
  5532. while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
  5533. fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
  5534. pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
  5535. cnt = pkt->u.raw.entry_count;
  5536. if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
  5537. /*
  5538. * This packet is corrupted. The header + payload
  5539. * can not be trusted. There is no point in passing
  5540. * it further up.
  5541. */
  5542. ql_log(ql_log_warn, vha, 0xffff,
  5543. "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
  5544. pkt->u.isp24.fcp_hdr.s_id,
  5545. be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
  5546. le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
  5547. adjust_corrupted_atio(pkt);
  5548. qlt_send_term_exchange(vha, NULL, pkt, ha_locked, 0);
  5549. } else {
  5550. qlt_24xx_atio_pkt_all_vps(vha,
  5551. (struct atio_from_isp *)pkt, ha_locked);
  5552. }
  5553. for (i = 0; i < cnt; i++) {
  5554. ha->tgt.atio_ring_index++;
  5555. if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
  5556. ha->tgt.atio_ring_index = 0;
  5557. ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
  5558. } else
  5559. ha->tgt.atio_ring_ptr++;
  5560. pkt->u.raw.signature = ATIO_PROCESSED;
  5561. pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
  5562. }
  5563. wmb();
  5564. }
  5565. /* Adjust ring index */
  5566. WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
  5567. RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
  5568. }
  5569. void
  5570. qlt_24xx_config_rings(struct scsi_qla_host *vha)
  5571. {
  5572. struct qla_hw_data *ha = vha->hw;
  5573. if (!QLA_TGT_MODE_ENABLED())
  5574. return;
  5575. WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
  5576. WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
  5577. RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
  5578. if (IS_ATIO_MSIX_CAPABLE(ha)) {
  5579. struct qla_msix_entry *msix = &ha->msix_entries[2];
  5580. struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
  5581. icb->msix_atio = cpu_to_le16(msix->entry);
  5582. ql_dbg(ql_dbg_init, vha, 0xf072,
  5583. "Registering ICB vector 0x%x for atio que.\n",
  5584. msix->entry);
  5585. }
  5586. }
  5587. void
  5588. qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
  5589. {
  5590. struct qla_hw_data *ha = vha->hw;
  5591. if (qla_tgt_mode_enabled(vha)) {
  5592. if (!ha->tgt.saved_set) {
  5593. /* We save only once */
  5594. ha->tgt.saved_exchange_count = nv->exchange_count;
  5595. ha->tgt.saved_firmware_options_1 =
  5596. nv->firmware_options_1;
  5597. ha->tgt.saved_firmware_options_2 =
  5598. nv->firmware_options_2;
  5599. ha->tgt.saved_firmware_options_3 =
  5600. nv->firmware_options_3;
  5601. ha->tgt.saved_set = 1;
  5602. }
  5603. nv->exchange_count = cpu_to_le16(0xFFFF);
  5604. /* Enable target mode */
  5605. nv->firmware_options_1 |= cpu_to_le32(BIT_4);
  5606. /* Disable ini mode, if requested */
  5607. if (!qla_ini_mode_enabled(vha))
  5608. nv->firmware_options_1 |= cpu_to_le32(BIT_5);
  5609. /* Disable Full Login after LIP */
  5610. nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
  5611. /* Enable initial LIP */
  5612. nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
  5613. if (ql2xtgt_tape_enable)
  5614. /* Enable FC Tape support */
  5615. nv->firmware_options_2 |= cpu_to_le32(BIT_12);
  5616. else
  5617. /* Disable FC Tape support */
  5618. nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
  5619. /* Disable Full Login after LIP */
  5620. nv->host_p &= cpu_to_le32(~BIT_10);
  5621. /* Enable target PRLI control */
  5622. nv->firmware_options_2 |= cpu_to_le32(BIT_14);
  5623. } else {
  5624. if (ha->tgt.saved_set) {
  5625. nv->exchange_count = ha->tgt.saved_exchange_count;
  5626. nv->firmware_options_1 =
  5627. ha->tgt.saved_firmware_options_1;
  5628. nv->firmware_options_2 =
  5629. ha->tgt.saved_firmware_options_2;
  5630. nv->firmware_options_3 =
  5631. ha->tgt.saved_firmware_options_3;
  5632. }
  5633. return;
  5634. }
  5635. /* out-of-order frames reassembly */
  5636. nv->firmware_options_3 |= BIT_6|BIT_9;
  5637. if (ha->tgt.enable_class_2) {
  5638. if (vha->flags.init_done)
  5639. fc_host_supported_classes(vha->host) =
  5640. FC_COS_CLASS2 | FC_COS_CLASS3;
  5641. nv->firmware_options_2 |= cpu_to_le32(BIT_8);
  5642. } else {
  5643. if (vha->flags.init_done)
  5644. fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
  5645. nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
  5646. }
  5647. }
  5648. void
  5649. qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
  5650. struct init_cb_24xx *icb)
  5651. {
  5652. struct qla_hw_data *ha = vha->hw;
  5653. if (!QLA_TGT_MODE_ENABLED())
  5654. return;
  5655. if (ha->tgt.node_name_set) {
  5656. memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
  5657. icb->firmware_options_1 |= cpu_to_le32(BIT_14);
  5658. }
  5659. /* disable ZIO at start time. */
  5660. if (!vha->flags.init_done) {
  5661. uint32_t tmp;
  5662. tmp = le32_to_cpu(icb->firmware_options_2);
  5663. tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  5664. icb->firmware_options_2 = cpu_to_le32(tmp);
  5665. }
  5666. }
  5667. void
  5668. qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
  5669. {
  5670. struct qla_hw_data *ha = vha->hw;
  5671. if (!QLA_TGT_MODE_ENABLED())
  5672. return;
  5673. if (qla_tgt_mode_enabled(vha)) {
  5674. if (!ha->tgt.saved_set) {
  5675. /* We save only once */
  5676. ha->tgt.saved_exchange_count = nv->exchange_count;
  5677. ha->tgt.saved_firmware_options_1 =
  5678. nv->firmware_options_1;
  5679. ha->tgt.saved_firmware_options_2 =
  5680. nv->firmware_options_2;
  5681. ha->tgt.saved_firmware_options_3 =
  5682. nv->firmware_options_3;
  5683. ha->tgt.saved_set = 1;
  5684. }
  5685. nv->exchange_count = cpu_to_le16(0xFFFF);
  5686. /* Enable target mode */
  5687. nv->firmware_options_1 |= cpu_to_le32(BIT_4);
  5688. /* Disable ini mode, if requested */
  5689. if (!qla_ini_mode_enabled(vha))
  5690. nv->firmware_options_1 |= cpu_to_le32(BIT_5);
  5691. /* Disable Full Login after LIP */
  5692. nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
  5693. /* Enable initial LIP */
  5694. nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
  5695. if (ql2xtgt_tape_enable)
  5696. /* Enable FC tape support */
  5697. nv->firmware_options_2 |= cpu_to_le32(BIT_12);
  5698. else
  5699. /* Disable FC tape support */
  5700. nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
  5701. /* Disable Full Login after LIP */
  5702. nv->host_p &= cpu_to_le32(~BIT_10);
  5703. /* Enable target PRLI control */
  5704. nv->firmware_options_2 |= cpu_to_le32(BIT_14);
  5705. } else {
  5706. if (ha->tgt.saved_set) {
  5707. nv->exchange_count = ha->tgt.saved_exchange_count;
  5708. nv->firmware_options_1 =
  5709. ha->tgt.saved_firmware_options_1;
  5710. nv->firmware_options_2 =
  5711. ha->tgt.saved_firmware_options_2;
  5712. nv->firmware_options_3 =
  5713. ha->tgt.saved_firmware_options_3;
  5714. }
  5715. return;
  5716. }
  5717. /* out-of-order frames reassembly */
  5718. nv->firmware_options_3 |= BIT_6|BIT_9;
  5719. if (ha->tgt.enable_class_2) {
  5720. if (vha->flags.init_done)
  5721. fc_host_supported_classes(vha->host) =
  5722. FC_COS_CLASS2 | FC_COS_CLASS3;
  5723. nv->firmware_options_2 |= cpu_to_le32(BIT_8);
  5724. } else {
  5725. if (vha->flags.init_done)
  5726. fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
  5727. nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
  5728. }
  5729. }
  5730. void
  5731. qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
  5732. struct init_cb_81xx *icb)
  5733. {
  5734. struct qla_hw_data *ha = vha->hw;
  5735. if (!QLA_TGT_MODE_ENABLED())
  5736. return;
  5737. if (ha->tgt.node_name_set) {
  5738. memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
  5739. icb->firmware_options_1 |= cpu_to_le32(BIT_14);
  5740. }
  5741. /* disable ZIO at start time. */
  5742. if (!vha->flags.init_done) {
  5743. uint32_t tmp;
  5744. tmp = le32_to_cpu(icb->firmware_options_2);
  5745. tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  5746. icb->firmware_options_2 = cpu_to_le32(tmp);
  5747. }
  5748. }
  5749. void
  5750. qlt_83xx_iospace_config(struct qla_hw_data *ha)
  5751. {
  5752. if (!QLA_TGT_MODE_ENABLED())
  5753. return;
  5754. ha->msix_count += 1; /* For ATIO Q */
  5755. }
  5756. int
  5757. qlt_24xx_process_response_error(struct scsi_qla_host *vha,
  5758. struct sts_entry_24xx *pkt)
  5759. {
  5760. switch (pkt->entry_type) {
  5761. case ABTS_RECV_24XX:
  5762. case ABTS_RESP_24XX:
  5763. case CTIO_TYPE7:
  5764. case NOTIFY_ACK_TYPE:
  5765. case CTIO_CRC2:
  5766. return 1;
  5767. default:
  5768. return 0;
  5769. }
  5770. }
  5771. void
  5772. qlt_modify_vp_config(struct scsi_qla_host *vha,
  5773. struct vp_config_entry_24xx *vpmod)
  5774. {
  5775. if (qla_tgt_mode_enabled(vha))
  5776. vpmod->options_idx1 &= ~BIT_5;
  5777. /* Disable ini mode, if requested */
  5778. if (!qla_ini_mode_enabled(vha))
  5779. vpmod->options_idx1 &= ~BIT_4;
  5780. }
  5781. void
  5782. qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
  5783. {
  5784. if (!QLA_TGT_MODE_ENABLED())
  5785. return;
  5786. if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
  5787. ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
  5788. ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
  5789. } else {
  5790. ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
  5791. ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
  5792. }
  5793. mutex_init(&base_vha->vha_tgt.tgt_mutex);
  5794. mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
  5795. qlt_clear_mode(base_vha);
  5796. }
  5797. irqreturn_t
  5798. qla83xx_msix_atio_q(int irq, void *dev_id)
  5799. {
  5800. struct rsp_que *rsp;
  5801. scsi_qla_host_t *vha;
  5802. struct qla_hw_data *ha;
  5803. unsigned long flags;
  5804. rsp = (struct rsp_que *) dev_id;
  5805. ha = rsp->hw;
  5806. vha = pci_get_drvdata(ha->pdev);
  5807. spin_lock_irqsave(&ha->tgt.atio_lock, flags);
  5808. qlt_24xx_process_atio_queue(vha, 0);
  5809. spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
  5810. return IRQ_HANDLED;
  5811. }
  5812. static void
  5813. qlt_handle_abts_recv_work(struct work_struct *work)
  5814. {
  5815. struct qla_tgt_sess_op *op = container_of(work,
  5816. struct qla_tgt_sess_op, work);
  5817. scsi_qla_host_t *vha = op->vha;
  5818. struct qla_hw_data *ha = vha->hw;
  5819. unsigned long flags;
  5820. if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
  5821. return;
  5822. spin_lock_irqsave(&ha->tgt.atio_lock, flags);
  5823. qlt_24xx_process_atio_queue(vha, 0);
  5824. spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
  5825. spin_lock_irqsave(&ha->hardware_lock, flags);
  5826. qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
  5827. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5828. kfree(op);
  5829. }
  5830. void
  5831. qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
  5832. {
  5833. struct qla_tgt_sess_op *op;
  5834. op = kzalloc(sizeof(*op), GFP_ATOMIC);
  5835. if (!op) {
  5836. /* do not reach for ATIO queue here. This is best effort err
  5837. * recovery at this point.
  5838. */
  5839. qlt_response_pkt_all_vps(vha, pkt);
  5840. return;
  5841. }
  5842. memcpy(&op->atio, pkt, sizeof(*pkt));
  5843. op->vha = vha;
  5844. op->chip_reset = vha->hw->chip_reset;
  5845. INIT_WORK(&op->work, qlt_handle_abts_recv_work);
  5846. queue_work(qla_tgt_wq, &op->work);
  5847. return;
  5848. }
  5849. int
  5850. qlt_mem_alloc(struct qla_hw_data *ha)
  5851. {
  5852. if (!QLA_TGT_MODE_ENABLED())
  5853. return 0;
  5854. ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
  5855. MAX_MULTI_ID_FABRIC, GFP_KERNEL);
  5856. if (!ha->tgt.tgt_vp_map)
  5857. return -ENOMEM;
  5858. ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
  5859. (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
  5860. &ha->tgt.atio_dma, GFP_KERNEL);
  5861. if (!ha->tgt.atio_ring) {
  5862. kfree(ha->tgt.tgt_vp_map);
  5863. return -ENOMEM;
  5864. }
  5865. return 0;
  5866. }
  5867. void
  5868. qlt_mem_free(struct qla_hw_data *ha)
  5869. {
  5870. if (!QLA_TGT_MODE_ENABLED())
  5871. return;
  5872. if (ha->tgt.atio_ring) {
  5873. dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
  5874. sizeof(struct atio_from_isp), ha->tgt.atio_ring,
  5875. ha->tgt.atio_dma);
  5876. }
  5877. kfree(ha->tgt.tgt_vp_map);
  5878. }
  5879. /* vport_slock to be held by the caller */
  5880. void
  5881. qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
  5882. {
  5883. if (!QLA_TGT_MODE_ENABLED())
  5884. return;
  5885. switch (cmd) {
  5886. case SET_VP_IDX:
  5887. vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
  5888. break;
  5889. case SET_AL_PA:
  5890. vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
  5891. break;
  5892. case RESET_VP_IDX:
  5893. vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
  5894. break;
  5895. case RESET_AL_PA:
  5896. vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
  5897. break;
  5898. }
  5899. }
  5900. static int __init qlt_parse_ini_mode(void)
  5901. {
  5902. if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
  5903. ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
  5904. else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
  5905. ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
  5906. else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
  5907. ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
  5908. else
  5909. return false;
  5910. return true;
  5911. }
  5912. int __init qlt_init(void)
  5913. {
  5914. int ret;
  5915. if (!qlt_parse_ini_mode()) {
  5916. ql_log(ql_log_fatal, NULL, 0xe06b,
  5917. "qlt_parse_ini_mode() failed\n");
  5918. return -EINVAL;
  5919. }
  5920. if (!QLA_TGT_MODE_ENABLED())
  5921. return 0;
  5922. qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
  5923. sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
  5924. qla_tgt_mgmt_cmd), 0, NULL);
  5925. if (!qla_tgt_mgmt_cmd_cachep) {
  5926. ql_log(ql_log_fatal, NULL, 0xe06d,
  5927. "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
  5928. return -ENOMEM;
  5929. }
  5930. qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
  5931. sizeof(qlt_plogi_ack_t),
  5932. __alignof__(qlt_plogi_ack_t),
  5933. 0, NULL);
  5934. if (!qla_tgt_plogi_cachep) {
  5935. ql_log(ql_log_fatal, NULL, 0xe06d,
  5936. "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
  5937. ret = -ENOMEM;
  5938. goto out_mgmt_cmd_cachep;
  5939. }
  5940. qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
  5941. mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
  5942. if (!qla_tgt_mgmt_cmd_mempool) {
  5943. ql_log(ql_log_fatal, NULL, 0xe06e,
  5944. "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
  5945. ret = -ENOMEM;
  5946. goto out_plogi_cachep;
  5947. }
  5948. qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
  5949. if (!qla_tgt_wq) {
  5950. ql_log(ql_log_fatal, NULL, 0xe06f,
  5951. "alloc_workqueue for qla_tgt_wq failed\n");
  5952. ret = -ENOMEM;
  5953. goto out_cmd_mempool;
  5954. }
  5955. /*
  5956. * Return 1 to signal that initiator-mode is being disabled
  5957. */
  5958. return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
  5959. out_cmd_mempool:
  5960. mempool_destroy(qla_tgt_mgmt_cmd_mempool);
  5961. out_plogi_cachep:
  5962. kmem_cache_destroy(qla_tgt_plogi_cachep);
  5963. out_mgmt_cmd_cachep:
  5964. kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
  5965. return ret;
  5966. }
  5967. void qlt_exit(void)
  5968. {
  5969. if (!QLA_TGT_MODE_ENABLED())
  5970. return;
  5971. destroy_workqueue(qla_tgt_wq);
  5972. mempool_destroy(qla_tgt_mgmt_cmd_mempool);
  5973. kmem_cache_destroy(qla_tgt_plogi_cachep);
  5974. kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
  5975. }