qla_target.c 176 KB

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