qla_target.c 197 KB

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