qla_target.c 190 KB

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