target_core_transport.c 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163
  1. /*******************************************************************************
  2. * Filename: target_core_transport.c
  3. *
  4. * This file contains the Generic Target Engine Core.
  5. *
  6. * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
  7. * Copyright (c) 2005, 2006, 2007 SBE, Inc.
  8. * Copyright (c) 2007-2010 Rising Tide Systems
  9. * Copyright (c) 2008-2010 Linux-iSCSI.org
  10. *
  11. * Nicholas A. Bellinger <nab@kernel.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. *
  27. ******************************************************************************/
  28. #include <linux/version.h>
  29. #include <linux/net.h>
  30. #include <linux/delay.h>
  31. #include <linux/string.h>
  32. #include <linux/timer.h>
  33. #include <linux/slab.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/kthread.h>
  37. #include <linux/in.h>
  38. #include <linux/cdrom.h>
  39. #include <asm/unaligned.h>
  40. #include <net/sock.h>
  41. #include <net/tcp.h>
  42. #include <scsi/scsi.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <scsi/libsas.h> /* For TASK_ATTR_* */
  45. #include <target/target_core_base.h>
  46. #include <target/target_core_device.h>
  47. #include <target/target_core_tmr.h>
  48. #include <target/target_core_tpg.h>
  49. #include <target/target_core_transport.h>
  50. #include <target/target_core_fabric_ops.h>
  51. #include <target/target_core_configfs.h>
  52. #include "target_core_alua.h"
  53. #include "target_core_hba.h"
  54. #include "target_core_pr.h"
  55. #include "target_core_scdb.h"
  56. #include "target_core_ua.h"
  57. /* #define DEBUG_CDB_HANDLER */
  58. #ifdef DEBUG_CDB_HANDLER
  59. #define DEBUG_CDB_H(x...) printk(KERN_INFO x)
  60. #else
  61. #define DEBUG_CDB_H(x...)
  62. #endif
  63. /* #define DEBUG_CMD_MAP */
  64. #ifdef DEBUG_CMD_MAP
  65. #define DEBUG_CMD_M(x...) printk(KERN_INFO x)
  66. #else
  67. #define DEBUG_CMD_M(x...)
  68. #endif
  69. /* #define DEBUG_MEM_ALLOC */
  70. #ifdef DEBUG_MEM_ALLOC
  71. #define DEBUG_MEM(x...) printk(KERN_INFO x)
  72. #else
  73. #define DEBUG_MEM(x...)
  74. #endif
  75. /* #define DEBUG_MEM2_ALLOC */
  76. #ifdef DEBUG_MEM2_ALLOC
  77. #define DEBUG_MEM2(x...) printk(KERN_INFO x)
  78. #else
  79. #define DEBUG_MEM2(x...)
  80. #endif
  81. /* #define DEBUG_SG_CALC */
  82. #ifdef DEBUG_SG_CALC
  83. #define DEBUG_SC(x...) printk(KERN_INFO x)
  84. #else
  85. #define DEBUG_SC(x...)
  86. #endif
  87. /* #define DEBUG_SE_OBJ */
  88. #ifdef DEBUG_SE_OBJ
  89. #define DEBUG_SO(x...) printk(KERN_INFO x)
  90. #else
  91. #define DEBUG_SO(x...)
  92. #endif
  93. /* #define DEBUG_CMD_VOL */
  94. #ifdef DEBUG_CMD_VOL
  95. #define DEBUG_VOL(x...) printk(KERN_INFO x)
  96. #else
  97. #define DEBUG_VOL(x...)
  98. #endif
  99. /* #define DEBUG_CMD_STOP */
  100. #ifdef DEBUG_CMD_STOP
  101. #define DEBUG_CS(x...) printk(KERN_INFO x)
  102. #else
  103. #define DEBUG_CS(x...)
  104. #endif
  105. /* #define DEBUG_PASSTHROUGH */
  106. #ifdef DEBUG_PASSTHROUGH
  107. #define DEBUG_PT(x...) printk(KERN_INFO x)
  108. #else
  109. #define DEBUG_PT(x...)
  110. #endif
  111. /* #define DEBUG_TASK_STOP */
  112. #ifdef DEBUG_TASK_STOP
  113. #define DEBUG_TS(x...) printk(KERN_INFO x)
  114. #else
  115. #define DEBUG_TS(x...)
  116. #endif
  117. /* #define DEBUG_TRANSPORT_STOP */
  118. #ifdef DEBUG_TRANSPORT_STOP
  119. #define DEBUG_TRANSPORT_S(x...) printk(KERN_INFO x)
  120. #else
  121. #define DEBUG_TRANSPORT_S(x...)
  122. #endif
  123. /* #define DEBUG_TASK_FAILURE */
  124. #ifdef DEBUG_TASK_FAILURE
  125. #define DEBUG_TF(x...) printk(KERN_INFO x)
  126. #else
  127. #define DEBUG_TF(x...)
  128. #endif
  129. /* #define DEBUG_DEV_OFFLINE */
  130. #ifdef DEBUG_DEV_OFFLINE
  131. #define DEBUG_DO(x...) printk(KERN_INFO x)
  132. #else
  133. #define DEBUG_DO(x...)
  134. #endif
  135. /* #define DEBUG_TASK_STATE */
  136. #ifdef DEBUG_TASK_STATE
  137. #define DEBUG_TSTATE(x...) printk(KERN_INFO x)
  138. #else
  139. #define DEBUG_TSTATE(x...)
  140. #endif
  141. /* #define DEBUG_STATUS_THR */
  142. #ifdef DEBUG_STATUS_THR
  143. #define DEBUG_ST(x...) printk(KERN_INFO x)
  144. #else
  145. #define DEBUG_ST(x...)
  146. #endif
  147. /* #define DEBUG_TASK_TIMEOUT */
  148. #ifdef DEBUG_TASK_TIMEOUT
  149. #define DEBUG_TT(x...) printk(KERN_INFO x)
  150. #else
  151. #define DEBUG_TT(x...)
  152. #endif
  153. /* #define DEBUG_GENERIC_REQUEST_FAILURE */
  154. #ifdef DEBUG_GENERIC_REQUEST_FAILURE
  155. #define DEBUG_GRF(x...) printk(KERN_INFO x)
  156. #else
  157. #define DEBUG_GRF(x...)
  158. #endif
  159. /* #define DEBUG_SAM_TASK_ATTRS */
  160. #ifdef DEBUG_SAM_TASK_ATTRS
  161. #define DEBUG_STA(x...) printk(KERN_INFO x)
  162. #else
  163. #define DEBUG_STA(x...)
  164. #endif
  165. struct se_global *se_global;
  166. static struct kmem_cache *se_cmd_cache;
  167. static struct kmem_cache *se_sess_cache;
  168. struct kmem_cache *se_tmr_req_cache;
  169. struct kmem_cache *se_ua_cache;
  170. struct kmem_cache *se_mem_cache;
  171. struct kmem_cache *t10_pr_reg_cache;
  172. struct kmem_cache *t10_alua_lu_gp_cache;
  173. struct kmem_cache *t10_alua_lu_gp_mem_cache;
  174. struct kmem_cache *t10_alua_tg_pt_gp_cache;
  175. struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
  176. /* Used for transport_dev_get_map_*() */
  177. typedef int (*map_func_t)(struct se_task *, u32);
  178. static int transport_generic_write_pending(struct se_cmd *);
  179. static int transport_processing_thread(void *);
  180. static int __transport_execute_tasks(struct se_device *dev);
  181. static void transport_complete_task_attr(struct se_cmd *cmd);
  182. static void transport_direct_request_timeout(struct se_cmd *cmd);
  183. static void transport_free_dev_tasks(struct se_cmd *cmd);
  184. static u32 transport_generic_get_cdb_count(struct se_cmd *cmd,
  185. unsigned long long starting_lba, u32 sectors,
  186. enum dma_data_direction data_direction,
  187. struct list_head *mem_list, int set_counts);
  188. static int transport_generic_get_mem(struct se_cmd *cmd, u32 length,
  189. u32 dma_size);
  190. static int transport_generic_remove(struct se_cmd *cmd,
  191. int release_to_pool, int session_reinstatement);
  192. static int transport_get_sectors(struct se_cmd *cmd);
  193. static struct list_head *transport_init_se_mem_list(void);
  194. static int transport_map_sg_to_mem(struct se_cmd *cmd,
  195. struct list_head *se_mem_list, void *in_mem,
  196. u32 *se_mem_cnt);
  197. static void transport_memcpy_se_mem_read_contig(struct se_cmd *cmd,
  198. unsigned char *dst, struct list_head *se_mem_list);
  199. static void transport_release_fe_cmd(struct se_cmd *cmd);
  200. static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
  201. struct se_queue_obj *qobj);
  202. static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
  203. static void transport_stop_all_task_timers(struct se_cmd *cmd);
  204. int transport_emulate_control_cdb(struct se_task *task);
  205. int init_se_global(void)
  206. {
  207. struct se_global *global;
  208. global = kzalloc(sizeof(struct se_global), GFP_KERNEL);
  209. if (!(global)) {
  210. printk(KERN_ERR "Unable to allocate memory for struct se_global\n");
  211. return -1;
  212. }
  213. INIT_LIST_HEAD(&global->g_lu_gps_list);
  214. INIT_LIST_HEAD(&global->g_se_tpg_list);
  215. INIT_LIST_HEAD(&global->g_hba_list);
  216. INIT_LIST_HEAD(&global->g_se_dev_list);
  217. spin_lock_init(&global->g_device_lock);
  218. spin_lock_init(&global->hba_lock);
  219. spin_lock_init(&global->se_tpg_lock);
  220. spin_lock_init(&global->lu_gps_lock);
  221. spin_lock_init(&global->plugin_class_lock);
  222. se_cmd_cache = kmem_cache_create("se_cmd_cache",
  223. sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
  224. if (!(se_cmd_cache)) {
  225. printk(KERN_ERR "kmem_cache_create for struct se_cmd failed\n");
  226. goto out;
  227. }
  228. se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
  229. sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
  230. 0, NULL);
  231. if (!(se_tmr_req_cache)) {
  232. printk(KERN_ERR "kmem_cache_create() for struct se_tmr_req"
  233. " failed\n");
  234. goto out;
  235. }
  236. se_sess_cache = kmem_cache_create("se_sess_cache",
  237. sizeof(struct se_session), __alignof__(struct se_session),
  238. 0, NULL);
  239. if (!(se_sess_cache)) {
  240. printk(KERN_ERR "kmem_cache_create() for struct se_session"
  241. " failed\n");
  242. goto out;
  243. }
  244. se_ua_cache = kmem_cache_create("se_ua_cache",
  245. sizeof(struct se_ua), __alignof__(struct se_ua),
  246. 0, NULL);
  247. if (!(se_ua_cache)) {
  248. printk(KERN_ERR "kmem_cache_create() for struct se_ua failed\n");
  249. goto out;
  250. }
  251. se_mem_cache = kmem_cache_create("se_mem_cache",
  252. sizeof(struct se_mem), __alignof__(struct se_mem), 0, NULL);
  253. if (!(se_mem_cache)) {
  254. printk(KERN_ERR "kmem_cache_create() for struct se_mem failed\n");
  255. goto out;
  256. }
  257. t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
  258. sizeof(struct t10_pr_registration),
  259. __alignof__(struct t10_pr_registration), 0, NULL);
  260. if (!(t10_pr_reg_cache)) {
  261. printk(KERN_ERR "kmem_cache_create() for struct t10_pr_registration"
  262. " failed\n");
  263. goto out;
  264. }
  265. t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
  266. sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
  267. 0, NULL);
  268. if (!(t10_alua_lu_gp_cache)) {
  269. printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_cache"
  270. " failed\n");
  271. goto out;
  272. }
  273. t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
  274. sizeof(struct t10_alua_lu_gp_member),
  275. __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
  276. if (!(t10_alua_lu_gp_mem_cache)) {
  277. printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_mem_"
  278. "cache failed\n");
  279. goto out;
  280. }
  281. t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
  282. sizeof(struct t10_alua_tg_pt_gp),
  283. __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
  284. if (!(t10_alua_tg_pt_gp_cache)) {
  285. printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
  286. "cache failed\n");
  287. goto out;
  288. }
  289. t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
  290. "t10_alua_tg_pt_gp_mem_cache",
  291. sizeof(struct t10_alua_tg_pt_gp_member),
  292. __alignof__(struct t10_alua_tg_pt_gp_member),
  293. 0, NULL);
  294. if (!(t10_alua_tg_pt_gp_mem_cache)) {
  295. printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
  296. "mem_t failed\n");
  297. goto out;
  298. }
  299. se_global = global;
  300. return 0;
  301. out:
  302. if (se_cmd_cache)
  303. kmem_cache_destroy(se_cmd_cache);
  304. if (se_tmr_req_cache)
  305. kmem_cache_destroy(se_tmr_req_cache);
  306. if (se_sess_cache)
  307. kmem_cache_destroy(se_sess_cache);
  308. if (se_ua_cache)
  309. kmem_cache_destroy(se_ua_cache);
  310. if (se_mem_cache)
  311. kmem_cache_destroy(se_mem_cache);
  312. if (t10_pr_reg_cache)
  313. kmem_cache_destroy(t10_pr_reg_cache);
  314. if (t10_alua_lu_gp_cache)
  315. kmem_cache_destroy(t10_alua_lu_gp_cache);
  316. if (t10_alua_lu_gp_mem_cache)
  317. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  318. if (t10_alua_tg_pt_gp_cache)
  319. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  320. if (t10_alua_tg_pt_gp_mem_cache)
  321. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  322. kfree(global);
  323. return -1;
  324. }
  325. void release_se_global(void)
  326. {
  327. struct se_global *global;
  328. global = se_global;
  329. if (!(global))
  330. return;
  331. kmem_cache_destroy(se_cmd_cache);
  332. kmem_cache_destroy(se_tmr_req_cache);
  333. kmem_cache_destroy(se_sess_cache);
  334. kmem_cache_destroy(se_ua_cache);
  335. kmem_cache_destroy(se_mem_cache);
  336. kmem_cache_destroy(t10_pr_reg_cache);
  337. kmem_cache_destroy(t10_alua_lu_gp_cache);
  338. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  339. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  340. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  341. kfree(global);
  342. se_global = NULL;
  343. }
  344. /* SCSI statistics table index */
  345. static struct scsi_index_table scsi_index_table;
  346. /*
  347. * Initialize the index table for allocating unique row indexes to various mib
  348. * tables.
  349. */
  350. void init_scsi_index_table(void)
  351. {
  352. memset(&scsi_index_table, 0, sizeof(struct scsi_index_table));
  353. spin_lock_init(&scsi_index_table.lock);
  354. }
  355. /*
  356. * Allocate a new row index for the entry type specified
  357. */
  358. u32 scsi_get_new_index(scsi_index_t type)
  359. {
  360. u32 new_index;
  361. if ((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)) {
  362. printk(KERN_ERR "Invalid index type %d\n", type);
  363. return -EINVAL;
  364. }
  365. spin_lock(&scsi_index_table.lock);
  366. new_index = ++scsi_index_table.scsi_mib_index[type];
  367. if (new_index == 0)
  368. new_index = ++scsi_index_table.scsi_mib_index[type];
  369. spin_unlock(&scsi_index_table.lock);
  370. return new_index;
  371. }
  372. void transport_init_queue_obj(struct se_queue_obj *qobj)
  373. {
  374. atomic_set(&qobj->queue_cnt, 0);
  375. INIT_LIST_HEAD(&qobj->qobj_list);
  376. init_waitqueue_head(&qobj->thread_wq);
  377. spin_lock_init(&qobj->cmd_queue_lock);
  378. }
  379. EXPORT_SYMBOL(transport_init_queue_obj);
  380. static int transport_subsystem_reqmods(void)
  381. {
  382. int ret;
  383. ret = request_module("target_core_iblock");
  384. if (ret != 0)
  385. printk(KERN_ERR "Unable to load target_core_iblock\n");
  386. ret = request_module("target_core_file");
  387. if (ret != 0)
  388. printk(KERN_ERR "Unable to load target_core_file\n");
  389. ret = request_module("target_core_pscsi");
  390. if (ret != 0)
  391. printk(KERN_ERR "Unable to load target_core_pscsi\n");
  392. ret = request_module("target_core_stgt");
  393. if (ret != 0)
  394. printk(KERN_ERR "Unable to load target_core_stgt\n");
  395. return 0;
  396. }
  397. int transport_subsystem_check_init(void)
  398. {
  399. if (se_global->g_sub_api_initialized)
  400. return 0;
  401. /*
  402. * Request the loading of known TCM subsystem plugins..
  403. */
  404. if (transport_subsystem_reqmods() < 0)
  405. return -1;
  406. se_global->g_sub_api_initialized = 1;
  407. return 0;
  408. }
  409. struct se_session *transport_init_session(void)
  410. {
  411. struct se_session *se_sess;
  412. se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
  413. if (!(se_sess)) {
  414. printk(KERN_ERR "Unable to allocate struct se_session from"
  415. " se_sess_cache\n");
  416. return ERR_PTR(-ENOMEM);
  417. }
  418. INIT_LIST_HEAD(&se_sess->sess_list);
  419. INIT_LIST_HEAD(&se_sess->sess_acl_list);
  420. return se_sess;
  421. }
  422. EXPORT_SYMBOL(transport_init_session);
  423. /*
  424. * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
  425. */
  426. void __transport_register_session(
  427. struct se_portal_group *se_tpg,
  428. struct se_node_acl *se_nacl,
  429. struct se_session *se_sess,
  430. void *fabric_sess_ptr)
  431. {
  432. unsigned char buf[PR_REG_ISID_LEN];
  433. se_sess->se_tpg = se_tpg;
  434. se_sess->fabric_sess_ptr = fabric_sess_ptr;
  435. /*
  436. * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
  437. *
  438. * Only set for struct se_session's that will actually be moving I/O.
  439. * eg: *NOT* discovery sessions.
  440. */
  441. if (se_nacl) {
  442. /*
  443. * If the fabric module supports an ISID based TransportID,
  444. * save this value in binary from the fabric I_T Nexus now.
  445. */
  446. if (TPG_TFO(se_tpg)->sess_get_initiator_sid != NULL) {
  447. memset(&buf[0], 0, PR_REG_ISID_LEN);
  448. TPG_TFO(se_tpg)->sess_get_initiator_sid(se_sess,
  449. &buf[0], PR_REG_ISID_LEN);
  450. se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
  451. }
  452. spin_lock_irq(&se_nacl->nacl_sess_lock);
  453. /*
  454. * The se_nacl->nacl_sess pointer will be set to the
  455. * last active I_T Nexus for each struct se_node_acl.
  456. */
  457. se_nacl->nacl_sess = se_sess;
  458. list_add_tail(&se_sess->sess_acl_list,
  459. &se_nacl->acl_sess_list);
  460. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  461. }
  462. list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
  463. printk(KERN_INFO "TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
  464. TPG_TFO(se_tpg)->get_fabric_name(), se_sess->fabric_sess_ptr);
  465. }
  466. EXPORT_SYMBOL(__transport_register_session);
  467. void transport_register_session(
  468. struct se_portal_group *se_tpg,
  469. struct se_node_acl *se_nacl,
  470. struct se_session *se_sess,
  471. void *fabric_sess_ptr)
  472. {
  473. spin_lock_bh(&se_tpg->session_lock);
  474. __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
  475. spin_unlock_bh(&se_tpg->session_lock);
  476. }
  477. EXPORT_SYMBOL(transport_register_session);
  478. void transport_deregister_session_configfs(struct se_session *se_sess)
  479. {
  480. struct se_node_acl *se_nacl;
  481. /*
  482. * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
  483. */
  484. se_nacl = se_sess->se_node_acl;
  485. if ((se_nacl)) {
  486. spin_lock_irq(&se_nacl->nacl_sess_lock);
  487. list_del(&se_sess->sess_acl_list);
  488. /*
  489. * If the session list is empty, then clear the pointer.
  490. * Otherwise, set the struct se_session pointer from the tail
  491. * element of the per struct se_node_acl active session list.
  492. */
  493. if (list_empty(&se_nacl->acl_sess_list))
  494. se_nacl->nacl_sess = NULL;
  495. else {
  496. se_nacl->nacl_sess = container_of(
  497. se_nacl->acl_sess_list.prev,
  498. struct se_session, sess_acl_list);
  499. }
  500. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  501. }
  502. }
  503. EXPORT_SYMBOL(transport_deregister_session_configfs);
  504. void transport_free_session(struct se_session *se_sess)
  505. {
  506. kmem_cache_free(se_sess_cache, se_sess);
  507. }
  508. EXPORT_SYMBOL(transport_free_session);
  509. void transport_deregister_session(struct se_session *se_sess)
  510. {
  511. struct se_portal_group *se_tpg = se_sess->se_tpg;
  512. struct se_node_acl *se_nacl;
  513. if (!(se_tpg)) {
  514. transport_free_session(se_sess);
  515. return;
  516. }
  517. spin_lock_bh(&se_tpg->session_lock);
  518. list_del(&se_sess->sess_list);
  519. se_sess->se_tpg = NULL;
  520. se_sess->fabric_sess_ptr = NULL;
  521. spin_unlock_bh(&se_tpg->session_lock);
  522. /*
  523. * Determine if we need to do extra work for this initiator node's
  524. * struct se_node_acl if it had been previously dynamically generated.
  525. */
  526. se_nacl = se_sess->se_node_acl;
  527. if ((se_nacl)) {
  528. spin_lock_bh(&se_tpg->acl_node_lock);
  529. if (se_nacl->dynamic_node_acl) {
  530. if (!(TPG_TFO(se_tpg)->tpg_check_demo_mode_cache(
  531. se_tpg))) {
  532. list_del(&se_nacl->acl_list);
  533. se_tpg->num_node_acls--;
  534. spin_unlock_bh(&se_tpg->acl_node_lock);
  535. core_tpg_wait_for_nacl_pr_ref(se_nacl);
  536. core_free_device_list_for_node(se_nacl, se_tpg);
  537. TPG_TFO(se_tpg)->tpg_release_fabric_acl(se_tpg,
  538. se_nacl);
  539. spin_lock_bh(&se_tpg->acl_node_lock);
  540. }
  541. }
  542. spin_unlock_bh(&se_tpg->acl_node_lock);
  543. }
  544. transport_free_session(se_sess);
  545. printk(KERN_INFO "TARGET_CORE[%s]: Deregistered fabric_sess\n",
  546. TPG_TFO(se_tpg)->get_fabric_name());
  547. }
  548. EXPORT_SYMBOL(transport_deregister_session);
  549. /*
  550. * Called with T_TASK(cmd)->t_state_lock held.
  551. */
  552. static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
  553. {
  554. struct se_device *dev;
  555. struct se_task *task;
  556. unsigned long flags;
  557. if (!T_TASK(cmd))
  558. return;
  559. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  560. dev = task->se_dev;
  561. if (!(dev))
  562. continue;
  563. if (atomic_read(&task->task_active))
  564. continue;
  565. if (!(atomic_read(&task->task_state_active)))
  566. continue;
  567. spin_lock_irqsave(&dev->execute_task_lock, flags);
  568. list_del(&task->t_state_list);
  569. DEBUG_TSTATE("Removed ITT: 0x%08x dev: %p task[%p]\n",
  570. CMD_TFO(cmd)->tfo_get_task_tag(cmd), dev, task);
  571. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  572. atomic_set(&task->task_state_active, 0);
  573. atomic_dec(&T_TASK(cmd)->t_task_cdbs_ex_left);
  574. }
  575. }
  576. /* transport_cmd_check_stop():
  577. *
  578. * 'transport_off = 1' determines if t_transport_active should be cleared.
  579. * 'transport_off = 2' determines if task_dev_state should be removed.
  580. *
  581. * A non-zero u8 t_state sets cmd->t_state.
  582. * Returns 1 when command is stopped, else 0.
  583. */
  584. static int transport_cmd_check_stop(
  585. struct se_cmd *cmd,
  586. int transport_off,
  587. u8 t_state)
  588. {
  589. unsigned long flags;
  590. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  591. /*
  592. * Determine if IOCTL context caller in requesting the stopping of this
  593. * command for LUN shutdown purposes.
  594. */
  595. if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
  596. DEBUG_CS("%s:%d atomic_read(&T_TASK(cmd)->transport_lun_stop)"
  597. " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
  598. CMD_TFO(cmd)->get_task_tag(cmd));
  599. cmd->deferred_t_state = cmd->t_state;
  600. cmd->t_state = TRANSPORT_DEFERRED_CMD;
  601. atomic_set(&T_TASK(cmd)->t_transport_active, 0);
  602. if (transport_off == 2)
  603. transport_all_task_dev_remove_state(cmd);
  604. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  605. complete(&T_TASK(cmd)->transport_lun_stop_comp);
  606. return 1;
  607. }
  608. /*
  609. * Determine if frontend context caller is requesting the stopping of
  610. * this command for frontend excpections.
  611. */
  612. if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
  613. DEBUG_CS("%s:%d atomic_read(&T_TASK(cmd)->t_transport_stop) =="
  614. " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
  615. CMD_TFO(cmd)->get_task_tag(cmd));
  616. cmd->deferred_t_state = cmd->t_state;
  617. cmd->t_state = TRANSPORT_DEFERRED_CMD;
  618. if (transport_off == 2)
  619. transport_all_task_dev_remove_state(cmd);
  620. /*
  621. * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
  622. * to FE.
  623. */
  624. if (transport_off == 2)
  625. cmd->se_lun = NULL;
  626. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  627. complete(&T_TASK(cmd)->t_transport_stop_comp);
  628. return 1;
  629. }
  630. if (transport_off) {
  631. atomic_set(&T_TASK(cmd)->t_transport_active, 0);
  632. if (transport_off == 2) {
  633. transport_all_task_dev_remove_state(cmd);
  634. /*
  635. * Clear struct se_cmd->se_lun before the transport_off == 2
  636. * handoff to fabric module.
  637. */
  638. cmd->se_lun = NULL;
  639. /*
  640. * Some fabric modules like tcm_loop can release
  641. * their internally allocated I/O refrence now and
  642. * struct se_cmd now.
  643. */
  644. if (CMD_TFO(cmd)->check_stop_free != NULL) {
  645. spin_unlock_irqrestore(
  646. &T_TASK(cmd)->t_state_lock, flags);
  647. CMD_TFO(cmd)->check_stop_free(cmd);
  648. return 1;
  649. }
  650. }
  651. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  652. return 0;
  653. } else if (t_state)
  654. cmd->t_state = t_state;
  655. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  656. return 0;
  657. }
  658. static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
  659. {
  660. return transport_cmd_check_stop(cmd, 2, 0);
  661. }
  662. static void transport_lun_remove_cmd(struct se_cmd *cmd)
  663. {
  664. struct se_lun *lun = SE_LUN(cmd);
  665. unsigned long flags;
  666. if (!lun)
  667. return;
  668. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  669. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  670. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  671. goto check_lun;
  672. }
  673. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  674. transport_all_task_dev_remove_state(cmd);
  675. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  676. transport_free_dev_tasks(cmd);
  677. check_lun:
  678. spin_lock_irqsave(&lun->lun_cmd_lock, flags);
  679. if (atomic_read(&T_TASK(cmd)->transport_lun_active)) {
  680. list_del(&cmd->se_lun_list);
  681. atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
  682. #if 0
  683. printk(KERN_INFO "Removed ITT: 0x%08x from LUN LIST[%d]\n"
  684. CMD_TFO(cmd)->get_task_tag(cmd), lun->unpacked_lun);
  685. #endif
  686. }
  687. spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
  688. }
  689. void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
  690. {
  691. transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
  692. transport_lun_remove_cmd(cmd);
  693. if (transport_cmd_check_stop_to_fabric(cmd))
  694. return;
  695. if (remove)
  696. transport_generic_remove(cmd, 0, 0);
  697. }
  698. void transport_cmd_finish_abort_tmr(struct se_cmd *cmd)
  699. {
  700. transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
  701. if (transport_cmd_check_stop_to_fabric(cmd))
  702. return;
  703. transport_generic_remove(cmd, 0, 0);
  704. }
  705. static int transport_add_cmd_to_queue(
  706. struct se_cmd *cmd,
  707. int t_state)
  708. {
  709. struct se_device *dev = cmd->se_dev;
  710. struct se_queue_obj *qobj = dev->dev_queue_obj;
  711. struct se_queue_req *qr;
  712. unsigned long flags;
  713. qr = kzalloc(sizeof(struct se_queue_req), GFP_ATOMIC);
  714. if (!(qr)) {
  715. printk(KERN_ERR "Unable to allocate memory for"
  716. " struct se_queue_req\n");
  717. return -1;
  718. }
  719. INIT_LIST_HEAD(&qr->qr_list);
  720. qr->cmd = (void *)cmd;
  721. qr->state = t_state;
  722. if (t_state) {
  723. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  724. cmd->t_state = t_state;
  725. atomic_set(&T_TASK(cmd)->t_transport_active, 1);
  726. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  727. }
  728. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  729. list_add_tail(&qr->qr_list, &qobj->qobj_list);
  730. atomic_inc(&T_TASK(cmd)->t_transport_queue_active);
  731. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  732. atomic_inc(&qobj->queue_cnt);
  733. wake_up_interruptible(&qobj->thread_wq);
  734. return 0;
  735. }
  736. /*
  737. * Called with struct se_queue_obj->cmd_queue_lock held.
  738. */
  739. static struct se_queue_req *
  740. __transport_get_qr_from_queue(struct se_queue_obj *qobj)
  741. {
  742. struct se_cmd *cmd;
  743. struct se_queue_req *qr = NULL;
  744. if (list_empty(&qobj->qobj_list))
  745. return NULL;
  746. list_for_each_entry(qr, &qobj->qobj_list, qr_list)
  747. break;
  748. if (qr->cmd) {
  749. cmd = (struct se_cmd *)qr->cmd;
  750. atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
  751. }
  752. list_del(&qr->qr_list);
  753. atomic_dec(&qobj->queue_cnt);
  754. return qr;
  755. }
  756. static struct se_queue_req *
  757. transport_get_qr_from_queue(struct se_queue_obj *qobj)
  758. {
  759. struct se_cmd *cmd;
  760. struct se_queue_req *qr;
  761. unsigned long flags;
  762. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  763. if (list_empty(&qobj->qobj_list)) {
  764. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  765. return NULL;
  766. }
  767. list_for_each_entry(qr, &qobj->qobj_list, qr_list)
  768. break;
  769. if (qr->cmd) {
  770. cmd = (struct se_cmd *)qr->cmd;
  771. atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
  772. }
  773. list_del(&qr->qr_list);
  774. atomic_dec(&qobj->queue_cnt);
  775. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  776. return qr;
  777. }
  778. static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
  779. struct se_queue_obj *qobj)
  780. {
  781. struct se_cmd *q_cmd;
  782. struct se_queue_req *qr = NULL, *qr_p = NULL;
  783. unsigned long flags;
  784. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  785. if (!(atomic_read(&T_TASK(cmd)->t_transport_queue_active))) {
  786. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  787. return;
  788. }
  789. list_for_each_entry_safe(qr, qr_p, &qobj->qobj_list, qr_list) {
  790. q_cmd = (struct se_cmd *)qr->cmd;
  791. if (q_cmd != cmd)
  792. continue;
  793. atomic_dec(&T_TASK(q_cmd)->t_transport_queue_active);
  794. atomic_dec(&qobj->queue_cnt);
  795. list_del(&qr->qr_list);
  796. kfree(qr);
  797. }
  798. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  799. if (atomic_read(&T_TASK(cmd)->t_transport_queue_active)) {
  800. printk(KERN_ERR "ITT: 0x%08x t_transport_queue_active: %d\n",
  801. CMD_TFO(cmd)->get_task_tag(cmd),
  802. atomic_read(&T_TASK(cmd)->t_transport_queue_active));
  803. }
  804. }
  805. /*
  806. * Completion function used by TCM subsystem plugins (such as FILEIO)
  807. * for queueing up response from struct se_subsystem_api->do_task()
  808. */
  809. void transport_complete_sync_cache(struct se_cmd *cmd, int good)
  810. {
  811. struct se_task *task = list_entry(T_TASK(cmd)->t_task_list.next,
  812. struct se_task, t_list);
  813. if (good) {
  814. cmd->scsi_status = SAM_STAT_GOOD;
  815. task->task_scsi_status = GOOD;
  816. } else {
  817. task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
  818. task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
  819. TASK_CMD(task)->transport_error_status =
  820. PYX_TRANSPORT_ILLEGAL_REQUEST;
  821. }
  822. transport_complete_task(task, good);
  823. }
  824. EXPORT_SYMBOL(transport_complete_sync_cache);
  825. /* transport_complete_task():
  826. *
  827. * Called from interrupt and non interrupt context depending
  828. * on the transport plugin.
  829. */
  830. void transport_complete_task(struct se_task *task, int success)
  831. {
  832. struct se_cmd *cmd = TASK_CMD(task);
  833. struct se_device *dev = task->se_dev;
  834. int t_state;
  835. unsigned long flags;
  836. #if 0
  837. printk(KERN_INFO "task: %p CDB: 0x%02x obj_ptr: %p\n", task,
  838. T_TASK(cmd)->t_task_cdb[0], dev);
  839. #endif
  840. if (dev) {
  841. spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
  842. atomic_inc(&dev->depth_left);
  843. atomic_inc(&SE_HBA(dev)->left_queue_depth);
  844. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  845. }
  846. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  847. atomic_set(&task->task_active, 0);
  848. /*
  849. * See if any sense data exists, if so set the TASK_SENSE flag.
  850. * Also check for any other post completion work that needs to be
  851. * done by the plugins.
  852. */
  853. if (dev && dev->transport->transport_complete) {
  854. if (dev->transport->transport_complete(task) != 0) {
  855. cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
  856. task->task_sense = 1;
  857. success = 1;
  858. }
  859. }
  860. /*
  861. * See if we are waiting for outstanding struct se_task
  862. * to complete for an exception condition
  863. */
  864. if (atomic_read(&task->task_stop)) {
  865. /*
  866. * Decrement T_TASK(cmd)->t_se_count if this task had
  867. * previously thrown its timeout exception handler.
  868. */
  869. if (atomic_read(&task->task_timeout)) {
  870. atomic_dec(&T_TASK(cmd)->t_se_count);
  871. atomic_set(&task->task_timeout, 0);
  872. }
  873. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  874. complete(&task->task_stop_comp);
  875. return;
  876. }
  877. /*
  878. * If the task's timeout handler has fired, use the t_task_cdbs_timeout
  879. * left counter to determine when the struct se_cmd is ready to be queued to
  880. * the processing thread.
  881. */
  882. if (atomic_read(&task->task_timeout)) {
  883. if (!(atomic_dec_and_test(
  884. &T_TASK(cmd)->t_task_cdbs_timeout_left))) {
  885. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  886. flags);
  887. return;
  888. }
  889. t_state = TRANSPORT_COMPLETE_TIMEOUT;
  890. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  891. transport_add_cmd_to_queue(cmd, t_state);
  892. return;
  893. }
  894. atomic_dec(&T_TASK(cmd)->t_task_cdbs_timeout_left);
  895. /*
  896. * Decrement the outstanding t_task_cdbs_left count. The last
  897. * struct se_task from struct se_cmd will complete itself into the
  898. * device queue depending upon int success.
  899. */
  900. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
  901. if (!success)
  902. T_TASK(cmd)->t_tasks_failed = 1;
  903. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  904. return;
  905. }
  906. if (!success || T_TASK(cmd)->t_tasks_failed) {
  907. t_state = TRANSPORT_COMPLETE_FAILURE;
  908. if (!task->task_error_status) {
  909. task->task_error_status =
  910. PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  911. cmd->transport_error_status =
  912. PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  913. }
  914. } else {
  915. atomic_set(&T_TASK(cmd)->t_transport_complete, 1);
  916. t_state = TRANSPORT_COMPLETE_OK;
  917. }
  918. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  919. transport_add_cmd_to_queue(cmd, t_state);
  920. }
  921. EXPORT_SYMBOL(transport_complete_task);
  922. /*
  923. * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
  924. * struct se_task list are ready to be added to the active execution list
  925. * struct se_device
  926. * Called with se_dev_t->execute_task_lock called.
  927. */
  928. static inline int transport_add_task_check_sam_attr(
  929. struct se_task *task,
  930. struct se_task *task_prev,
  931. struct se_device *dev)
  932. {
  933. /*
  934. * No SAM Task attribute emulation enabled, add to tail of
  935. * execution queue
  936. */
  937. if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
  938. list_add_tail(&task->t_execute_list, &dev->execute_task_list);
  939. return 0;
  940. }
  941. /*
  942. * HEAD_OF_QUEUE attribute for received CDB, which means
  943. * the first task that is associated with a struct se_cmd goes to
  944. * head of the struct se_device->execute_task_list, and task_prev
  945. * after that for each subsequent task
  946. */
  947. if (task->task_se_cmd->sam_task_attr == TASK_ATTR_HOQ) {
  948. list_add(&task->t_execute_list,
  949. (task_prev != NULL) ?
  950. &task_prev->t_execute_list :
  951. &dev->execute_task_list);
  952. DEBUG_STA("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
  953. " in execution queue\n",
  954. T_TASK(task->task_se_cmd)->t_task_cdb[0]);
  955. return 1;
  956. }
  957. /*
  958. * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
  959. * transitioned from Dermant -> Active state, and are added to the end
  960. * of the struct se_device->execute_task_list
  961. */
  962. list_add_tail(&task->t_execute_list, &dev->execute_task_list);
  963. return 0;
  964. }
  965. /* __transport_add_task_to_execute_queue():
  966. *
  967. * Called with se_dev_t->execute_task_lock called.
  968. */
  969. static void __transport_add_task_to_execute_queue(
  970. struct se_task *task,
  971. struct se_task *task_prev,
  972. struct se_device *dev)
  973. {
  974. int head_of_queue;
  975. head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
  976. atomic_inc(&dev->execute_tasks);
  977. if (atomic_read(&task->task_state_active))
  978. return;
  979. /*
  980. * Determine if this task needs to go to HEAD_OF_QUEUE for the
  981. * state list as well. Running with SAM Task Attribute emulation
  982. * will always return head_of_queue == 0 here
  983. */
  984. if (head_of_queue)
  985. list_add(&task->t_state_list, (task_prev) ?
  986. &task_prev->t_state_list :
  987. &dev->state_task_list);
  988. else
  989. list_add_tail(&task->t_state_list, &dev->state_task_list);
  990. atomic_set(&task->task_state_active, 1);
  991. DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
  992. CMD_TFO(task->task_se_cmd)->get_task_tag(task->task_se_cmd),
  993. task, dev);
  994. }
  995. static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
  996. {
  997. struct se_device *dev;
  998. struct se_task *task;
  999. unsigned long flags;
  1000. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1001. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  1002. dev = task->se_dev;
  1003. if (atomic_read(&task->task_state_active))
  1004. continue;
  1005. spin_lock(&dev->execute_task_lock);
  1006. list_add_tail(&task->t_state_list, &dev->state_task_list);
  1007. atomic_set(&task->task_state_active, 1);
  1008. DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
  1009. CMD_TFO(task->task_se_cmd)->get_task_tag(
  1010. task->task_se_cmd), task, dev);
  1011. spin_unlock(&dev->execute_task_lock);
  1012. }
  1013. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  1014. }
  1015. static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
  1016. {
  1017. struct se_device *dev = SE_DEV(cmd);
  1018. struct se_task *task, *task_prev = NULL;
  1019. unsigned long flags;
  1020. spin_lock_irqsave(&dev->execute_task_lock, flags);
  1021. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  1022. if (atomic_read(&task->task_execute_queue))
  1023. continue;
  1024. /*
  1025. * __transport_add_task_to_execute_queue() handles the
  1026. * SAM Task Attribute emulation if enabled
  1027. */
  1028. __transport_add_task_to_execute_queue(task, task_prev, dev);
  1029. atomic_set(&task->task_execute_queue, 1);
  1030. task_prev = task;
  1031. }
  1032. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  1033. return;
  1034. }
  1035. /* transport_get_task_from_execute_queue():
  1036. *
  1037. * Called with dev->execute_task_lock held.
  1038. */
  1039. static struct se_task *
  1040. transport_get_task_from_execute_queue(struct se_device *dev)
  1041. {
  1042. struct se_task *task;
  1043. if (list_empty(&dev->execute_task_list))
  1044. return NULL;
  1045. list_for_each_entry(task, &dev->execute_task_list, t_execute_list)
  1046. break;
  1047. list_del(&task->t_execute_list);
  1048. atomic_dec(&dev->execute_tasks);
  1049. return task;
  1050. }
  1051. /* transport_remove_task_from_execute_queue():
  1052. *
  1053. *
  1054. */
  1055. static void transport_remove_task_from_execute_queue(
  1056. struct se_task *task,
  1057. struct se_device *dev)
  1058. {
  1059. unsigned long flags;
  1060. spin_lock_irqsave(&dev->execute_task_lock, flags);
  1061. list_del(&task->t_execute_list);
  1062. atomic_dec(&dev->execute_tasks);
  1063. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  1064. }
  1065. unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
  1066. {
  1067. switch (cmd->data_direction) {
  1068. case DMA_NONE:
  1069. return "NONE";
  1070. case DMA_FROM_DEVICE:
  1071. return "READ";
  1072. case DMA_TO_DEVICE:
  1073. return "WRITE";
  1074. case DMA_BIDIRECTIONAL:
  1075. return "BIDI";
  1076. default:
  1077. break;
  1078. }
  1079. return "UNKNOWN";
  1080. }
  1081. void transport_dump_dev_state(
  1082. struct se_device *dev,
  1083. char *b,
  1084. int *bl)
  1085. {
  1086. *bl += sprintf(b + *bl, "Status: ");
  1087. switch (dev->dev_status) {
  1088. case TRANSPORT_DEVICE_ACTIVATED:
  1089. *bl += sprintf(b + *bl, "ACTIVATED");
  1090. break;
  1091. case TRANSPORT_DEVICE_DEACTIVATED:
  1092. *bl += sprintf(b + *bl, "DEACTIVATED");
  1093. break;
  1094. case TRANSPORT_DEVICE_SHUTDOWN:
  1095. *bl += sprintf(b + *bl, "SHUTDOWN");
  1096. break;
  1097. case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
  1098. case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
  1099. *bl += sprintf(b + *bl, "OFFLINE");
  1100. break;
  1101. default:
  1102. *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
  1103. break;
  1104. }
  1105. *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
  1106. atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
  1107. dev->queue_depth);
  1108. *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
  1109. DEV_ATTRIB(dev)->block_size, DEV_ATTRIB(dev)->max_sectors);
  1110. *bl += sprintf(b + *bl, " ");
  1111. }
  1112. /* transport_release_all_cmds():
  1113. *
  1114. *
  1115. */
  1116. static void transport_release_all_cmds(struct se_device *dev)
  1117. {
  1118. struct se_cmd *cmd = NULL;
  1119. struct se_queue_req *qr = NULL, *qr_p = NULL;
  1120. int bug_out = 0, t_state;
  1121. unsigned long flags;
  1122. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  1123. list_for_each_entry_safe(qr, qr_p, &dev->dev_queue_obj->qobj_list,
  1124. qr_list) {
  1125. cmd = (struct se_cmd *)qr->cmd;
  1126. t_state = qr->state;
  1127. list_del(&qr->qr_list);
  1128. kfree(qr);
  1129. spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock,
  1130. flags);
  1131. printk(KERN_ERR "Releasing ITT: 0x%08x, i_state: %u,"
  1132. " t_state: %u directly\n",
  1133. CMD_TFO(cmd)->get_task_tag(cmd),
  1134. CMD_TFO(cmd)->get_cmd_state(cmd), t_state);
  1135. transport_release_fe_cmd(cmd);
  1136. bug_out = 1;
  1137. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  1138. }
  1139. spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
  1140. #if 0
  1141. if (bug_out)
  1142. BUG();
  1143. #endif
  1144. }
  1145. void transport_dump_vpd_proto_id(
  1146. struct t10_vpd *vpd,
  1147. unsigned char *p_buf,
  1148. int p_buf_len)
  1149. {
  1150. unsigned char buf[VPD_TMP_BUF_SIZE];
  1151. int len;
  1152. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1153. len = sprintf(buf, "T10 VPD Protocol Identifier: ");
  1154. switch (vpd->protocol_identifier) {
  1155. case 0x00:
  1156. sprintf(buf+len, "Fibre Channel\n");
  1157. break;
  1158. case 0x10:
  1159. sprintf(buf+len, "Parallel SCSI\n");
  1160. break;
  1161. case 0x20:
  1162. sprintf(buf+len, "SSA\n");
  1163. break;
  1164. case 0x30:
  1165. sprintf(buf+len, "IEEE 1394\n");
  1166. break;
  1167. case 0x40:
  1168. sprintf(buf+len, "SCSI Remote Direct Memory Access"
  1169. " Protocol\n");
  1170. break;
  1171. case 0x50:
  1172. sprintf(buf+len, "Internet SCSI (iSCSI)\n");
  1173. break;
  1174. case 0x60:
  1175. sprintf(buf+len, "SAS Serial SCSI Protocol\n");
  1176. break;
  1177. case 0x70:
  1178. sprintf(buf+len, "Automation/Drive Interface Transport"
  1179. " Protocol\n");
  1180. break;
  1181. case 0x80:
  1182. sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
  1183. break;
  1184. default:
  1185. sprintf(buf+len, "Unknown 0x%02x\n",
  1186. vpd->protocol_identifier);
  1187. break;
  1188. }
  1189. if (p_buf)
  1190. strncpy(p_buf, buf, p_buf_len);
  1191. else
  1192. printk(KERN_INFO "%s", buf);
  1193. }
  1194. void
  1195. transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
  1196. {
  1197. /*
  1198. * Check if the Protocol Identifier Valid (PIV) bit is set..
  1199. *
  1200. * from spc3r23.pdf section 7.5.1
  1201. */
  1202. if (page_83[1] & 0x80) {
  1203. vpd->protocol_identifier = (page_83[0] & 0xf0);
  1204. vpd->protocol_identifier_set = 1;
  1205. transport_dump_vpd_proto_id(vpd, NULL, 0);
  1206. }
  1207. }
  1208. EXPORT_SYMBOL(transport_set_vpd_proto_id);
  1209. int transport_dump_vpd_assoc(
  1210. struct t10_vpd *vpd,
  1211. unsigned char *p_buf,
  1212. int p_buf_len)
  1213. {
  1214. unsigned char buf[VPD_TMP_BUF_SIZE];
  1215. int ret = 0, len;
  1216. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1217. len = sprintf(buf, "T10 VPD Identifier Association: ");
  1218. switch (vpd->association) {
  1219. case 0x00:
  1220. sprintf(buf+len, "addressed logical unit\n");
  1221. break;
  1222. case 0x10:
  1223. sprintf(buf+len, "target port\n");
  1224. break;
  1225. case 0x20:
  1226. sprintf(buf+len, "SCSI target device\n");
  1227. break;
  1228. default:
  1229. sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
  1230. ret = -1;
  1231. break;
  1232. }
  1233. if (p_buf)
  1234. strncpy(p_buf, buf, p_buf_len);
  1235. else
  1236. printk("%s", buf);
  1237. return ret;
  1238. }
  1239. int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
  1240. {
  1241. /*
  1242. * The VPD identification association..
  1243. *
  1244. * from spc3r23.pdf Section 7.6.3.1 Table 297
  1245. */
  1246. vpd->association = (page_83[1] & 0x30);
  1247. return transport_dump_vpd_assoc(vpd, NULL, 0);
  1248. }
  1249. EXPORT_SYMBOL(transport_set_vpd_assoc);
  1250. int transport_dump_vpd_ident_type(
  1251. struct t10_vpd *vpd,
  1252. unsigned char *p_buf,
  1253. int p_buf_len)
  1254. {
  1255. unsigned char buf[VPD_TMP_BUF_SIZE];
  1256. int ret = 0, len;
  1257. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1258. len = sprintf(buf, "T10 VPD Identifier Type: ");
  1259. switch (vpd->device_identifier_type) {
  1260. case 0x00:
  1261. sprintf(buf+len, "Vendor specific\n");
  1262. break;
  1263. case 0x01:
  1264. sprintf(buf+len, "T10 Vendor ID based\n");
  1265. break;
  1266. case 0x02:
  1267. sprintf(buf+len, "EUI-64 based\n");
  1268. break;
  1269. case 0x03:
  1270. sprintf(buf+len, "NAA\n");
  1271. break;
  1272. case 0x04:
  1273. sprintf(buf+len, "Relative target port identifier\n");
  1274. break;
  1275. case 0x08:
  1276. sprintf(buf+len, "SCSI name string\n");
  1277. break;
  1278. default:
  1279. sprintf(buf+len, "Unsupported: 0x%02x\n",
  1280. vpd->device_identifier_type);
  1281. ret = -1;
  1282. break;
  1283. }
  1284. if (p_buf)
  1285. strncpy(p_buf, buf, p_buf_len);
  1286. else
  1287. printk("%s", buf);
  1288. return ret;
  1289. }
  1290. int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
  1291. {
  1292. /*
  1293. * The VPD identifier type..
  1294. *
  1295. * from spc3r23.pdf Section 7.6.3.1 Table 298
  1296. */
  1297. vpd->device_identifier_type = (page_83[1] & 0x0f);
  1298. return transport_dump_vpd_ident_type(vpd, NULL, 0);
  1299. }
  1300. EXPORT_SYMBOL(transport_set_vpd_ident_type);
  1301. int transport_dump_vpd_ident(
  1302. struct t10_vpd *vpd,
  1303. unsigned char *p_buf,
  1304. int p_buf_len)
  1305. {
  1306. unsigned char buf[VPD_TMP_BUF_SIZE];
  1307. int ret = 0;
  1308. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1309. switch (vpd->device_identifier_code_set) {
  1310. case 0x01: /* Binary */
  1311. sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
  1312. &vpd->device_identifier[0]);
  1313. break;
  1314. case 0x02: /* ASCII */
  1315. sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
  1316. &vpd->device_identifier[0]);
  1317. break;
  1318. case 0x03: /* UTF-8 */
  1319. sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
  1320. &vpd->device_identifier[0]);
  1321. break;
  1322. default:
  1323. sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
  1324. " 0x%02x", vpd->device_identifier_code_set);
  1325. ret = -1;
  1326. break;
  1327. }
  1328. if (p_buf)
  1329. strncpy(p_buf, buf, p_buf_len);
  1330. else
  1331. printk("%s", buf);
  1332. return ret;
  1333. }
  1334. int
  1335. transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
  1336. {
  1337. static const char hex_str[] = "0123456789abcdef";
  1338. int j = 0, i = 4; /* offset to start of the identifer */
  1339. /*
  1340. * The VPD Code Set (encoding)
  1341. *
  1342. * from spc3r23.pdf Section 7.6.3.1 Table 296
  1343. */
  1344. vpd->device_identifier_code_set = (page_83[0] & 0x0f);
  1345. switch (vpd->device_identifier_code_set) {
  1346. case 0x01: /* Binary */
  1347. vpd->device_identifier[j++] =
  1348. hex_str[vpd->device_identifier_type];
  1349. while (i < (4 + page_83[3])) {
  1350. vpd->device_identifier[j++] =
  1351. hex_str[(page_83[i] & 0xf0) >> 4];
  1352. vpd->device_identifier[j++] =
  1353. hex_str[page_83[i] & 0x0f];
  1354. i++;
  1355. }
  1356. break;
  1357. case 0x02: /* ASCII */
  1358. case 0x03: /* UTF-8 */
  1359. while (i < (4 + page_83[3]))
  1360. vpd->device_identifier[j++] = page_83[i++];
  1361. break;
  1362. default:
  1363. break;
  1364. }
  1365. return transport_dump_vpd_ident(vpd, NULL, 0);
  1366. }
  1367. EXPORT_SYMBOL(transport_set_vpd_ident);
  1368. static void core_setup_task_attr_emulation(struct se_device *dev)
  1369. {
  1370. /*
  1371. * If this device is from Target_Core_Mod/pSCSI, disable the
  1372. * SAM Task Attribute emulation.
  1373. *
  1374. * This is currently not available in upsream Linux/SCSI Target
  1375. * mode code, and is assumed to be disabled while using TCM/pSCSI.
  1376. */
  1377. if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1378. dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
  1379. return;
  1380. }
  1381. dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
  1382. DEBUG_STA("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
  1383. " device\n", TRANSPORT(dev)->name,
  1384. TRANSPORT(dev)->get_device_rev(dev));
  1385. }
  1386. static void scsi_dump_inquiry(struct se_device *dev)
  1387. {
  1388. struct t10_wwn *wwn = DEV_T10_WWN(dev);
  1389. int i, device_type;
  1390. /*
  1391. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1392. */
  1393. printk(" Vendor: ");
  1394. for (i = 0; i < 8; i++)
  1395. if (wwn->vendor[i] >= 0x20)
  1396. printk("%c", wwn->vendor[i]);
  1397. else
  1398. printk(" ");
  1399. printk(" Model: ");
  1400. for (i = 0; i < 16; i++)
  1401. if (wwn->model[i] >= 0x20)
  1402. printk("%c", wwn->model[i]);
  1403. else
  1404. printk(" ");
  1405. printk(" Revision: ");
  1406. for (i = 0; i < 4; i++)
  1407. if (wwn->revision[i] >= 0x20)
  1408. printk("%c", wwn->revision[i]);
  1409. else
  1410. printk(" ");
  1411. printk("\n");
  1412. device_type = TRANSPORT(dev)->get_device_type(dev);
  1413. printk(" Type: %s ", scsi_device_type(device_type));
  1414. printk(" ANSI SCSI revision: %02x\n",
  1415. TRANSPORT(dev)->get_device_rev(dev));
  1416. }
  1417. struct se_device *transport_add_device_to_core_hba(
  1418. struct se_hba *hba,
  1419. struct se_subsystem_api *transport,
  1420. struct se_subsystem_dev *se_dev,
  1421. u32 device_flags,
  1422. void *transport_dev,
  1423. struct se_dev_limits *dev_limits,
  1424. const char *inquiry_prod,
  1425. const char *inquiry_rev)
  1426. {
  1427. int ret = 0, force_pt;
  1428. struct se_device *dev;
  1429. dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
  1430. if (!(dev)) {
  1431. printk(KERN_ERR "Unable to allocate memory for se_dev_t\n");
  1432. return NULL;
  1433. }
  1434. dev->dev_queue_obj = kzalloc(sizeof(struct se_queue_obj), GFP_KERNEL);
  1435. if (!(dev->dev_queue_obj)) {
  1436. printk(KERN_ERR "Unable to allocate memory for"
  1437. " dev->dev_queue_obj\n");
  1438. kfree(dev);
  1439. return NULL;
  1440. }
  1441. transport_init_queue_obj(dev->dev_queue_obj);
  1442. dev->dev_status_queue_obj = kzalloc(sizeof(struct se_queue_obj),
  1443. GFP_KERNEL);
  1444. if (!(dev->dev_status_queue_obj)) {
  1445. printk(KERN_ERR "Unable to allocate memory for"
  1446. " dev->dev_status_queue_obj\n");
  1447. kfree(dev->dev_queue_obj);
  1448. kfree(dev);
  1449. return NULL;
  1450. }
  1451. transport_init_queue_obj(dev->dev_status_queue_obj);
  1452. dev->dev_flags = device_flags;
  1453. dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
  1454. dev->dev_ptr = (void *) transport_dev;
  1455. dev->se_hba = hba;
  1456. dev->se_sub_dev = se_dev;
  1457. dev->transport = transport;
  1458. atomic_set(&dev->active_cmds, 0);
  1459. INIT_LIST_HEAD(&dev->dev_list);
  1460. INIT_LIST_HEAD(&dev->dev_sep_list);
  1461. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1462. INIT_LIST_HEAD(&dev->execute_task_list);
  1463. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1464. INIT_LIST_HEAD(&dev->ordered_cmd_list);
  1465. INIT_LIST_HEAD(&dev->state_task_list);
  1466. spin_lock_init(&dev->execute_task_lock);
  1467. spin_lock_init(&dev->delayed_cmd_lock);
  1468. spin_lock_init(&dev->ordered_cmd_lock);
  1469. spin_lock_init(&dev->state_task_lock);
  1470. spin_lock_init(&dev->dev_alua_lock);
  1471. spin_lock_init(&dev->dev_reservation_lock);
  1472. spin_lock_init(&dev->dev_status_lock);
  1473. spin_lock_init(&dev->dev_status_thr_lock);
  1474. spin_lock_init(&dev->se_port_lock);
  1475. spin_lock_init(&dev->se_tmr_lock);
  1476. dev->queue_depth = dev_limits->queue_depth;
  1477. atomic_set(&dev->depth_left, dev->queue_depth);
  1478. atomic_set(&dev->dev_ordered_id, 0);
  1479. se_dev_set_default_attribs(dev, dev_limits);
  1480. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1481. dev->creation_time = get_jiffies_64();
  1482. spin_lock_init(&dev->stats_lock);
  1483. spin_lock(&hba->device_lock);
  1484. list_add_tail(&dev->dev_list, &hba->hba_dev_list);
  1485. hba->dev_count++;
  1486. spin_unlock(&hba->device_lock);
  1487. /*
  1488. * Setup the SAM Task Attribute emulation for struct se_device
  1489. */
  1490. core_setup_task_attr_emulation(dev);
  1491. /*
  1492. * Force PR and ALUA passthrough emulation with internal object use.
  1493. */
  1494. force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
  1495. /*
  1496. * Setup the Reservations infrastructure for struct se_device
  1497. */
  1498. core_setup_reservations(dev, force_pt);
  1499. /*
  1500. * Setup the Asymmetric Logical Unit Assignment for struct se_device
  1501. */
  1502. if (core_setup_alua(dev, force_pt) < 0)
  1503. goto out;
  1504. /*
  1505. * Startup the struct se_device processing thread
  1506. */
  1507. dev->process_thread = kthread_run(transport_processing_thread, dev,
  1508. "LIO_%s", TRANSPORT(dev)->name);
  1509. if (IS_ERR(dev->process_thread)) {
  1510. printk(KERN_ERR "Unable to create kthread: LIO_%s\n",
  1511. TRANSPORT(dev)->name);
  1512. goto out;
  1513. }
  1514. /*
  1515. * Preload the initial INQUIRY const values if we are doing
  1516. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1517. * passthrough because this is being provided by the backend LLD.
  1518. * This is required so that transport_get_inquiry() copies these
  1519. * originals once back into DEV_T10_WWN(dev) for the virtual device
  1520. * setup.
  1521. */
  1522. if (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1523. if (!(inquiry_prod) || !(inquiry_prod)) {
  1524. printk(KERN_ERR "All non TCM/pSCSI plugins require"
  1525. " INQUIRY consts\n");
  1526. goto out;
  1527. }
  1528. strncpy(&DEV_T10_WWN(dev)->vendor[0], "LIO-ORG", 8);
  1529. strncpy(&DEV_T10_WWN(dev)->model[0], inquiry_prod, 16);
  1530. strncpy(&DEV_T10_WWN(dev)->revision[0], inquiry_rev, 4);
  1531. }
  1532. scsi_dump_inquiry(dev);
  1533. out:
  1534. if (!ret)
  1535. return dev;
  1536. kthread_stop(dev->process_thread);
  1537. spin_lock(&hba->device_lock);
  1538. list_del(&dev->dev_list);
  1539. hba->dev_count--;
  1540. spin_unlock(&hba->device_lock);
  1541. se_release_vpd_for_dev(dev);
  1542. kfree(dev->dev_status_queue_obj);
  1543. kfree(dev->dev_queue_obj);
  1544. kfree(dev);
  1545. return NULL;
  1546. }
  1547. EXPORT_SYMBOL(transport_add_device_to_core_hba);
  1548. /* transport_generic_prepare_cdb():
  1549. *
  1550. * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
  1551. * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
  1552. * The point of this is since we are mapping iSCSI LUNs to
  1553. * SCSI Target IDs having a non-zero LUN in the CDB will throw the
  1554. * devices and HBAs for a loop.
  1555. */
  1556. static inline void transport_generic_prepare_cdb(
  1557. unsigned char *cdb)
  1558. {
  1559. switch (cdb[0]) {
  1560. case READ_10: /* SBC - RDProtect */
  1561. case READ_12: /* SBC - RDProtect */
  1562. case READ_16: /* SBC - RDProtect */
  1563. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  1564. case VERIFY: /* SBC - VRProtect */
  1565. case VERIFY_16: /* SBC - VRProtect */
  1566. case WRITE_VERIFY: /* SBC - VRProtect */
  1567. case WRITE_VERIFY_12: /* SBC - VRProtect */
  1568. break;
  1569. default:
  1570. cdb[1] &= 0x1f; /* clear logical unit number */
  1571. break;
  1572. }
  1573. }
  1574. static struct se_task *
  1575. transport_generic_get_task(struct se_cmd *cmd,
  1576. enum dma_data_direction data_direction)
  1577. {
  1578. struct se_task *task;
  1579. struct se_device *dev = SE_DEV(cmd);
  1580. unsigned long flags;
  1581. task = dev->transport->alloc_task(cmd);
  1582. if (!task) {
  1583. printk(KERN_ERR "Unable to allocate struct se_task\n");
  1584. return NULL;
  1585. }
  1586. INIT_LIST_HEAD(&task->t_list);
  1587. INIT_LIST_HEAD(&task->t_execute_list);
  1588. INIT_LIST_HEAD(&task->t_state_list);
  1589. init_completion(&task->task_stop_comp);
  1590. task->task_no = T_TASK(cmd)->t_tasks_no++;
  1591. task->task_se_cmd = cmd;
  1592. task->se_dev = dev;
  1593. task->task_data_direction = data_direction;
  1594. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1595. list_add_tail(&task->t_list, &T_TASK(cmd)->t_task_list);
  1596. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  1597. return task;
  1598. }
  1599. static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
  1600. void transport_device_setup_cmd(struct se_cmd *cmd)
  1601. {
  1602. cmd->se_dev = SE_LUN(cmd)->lun_se_dev;
  1603. }
  1604. EXPORT_SYMBOL(transport_device_setup_cmd);
  1605. /*
  1606. * Used by fabric modules containing a local struct se_cmd within their
  1607. * fabric dependent per I/O descriptor.
  1608. */
  1609. void transport_init_se_cmd(
  1610. struct se_cmd *cmd,
  1611. struct target_core_fabric_ops *tfo,
  1612. struct se_session *se_sess,
  1613. u32 data_length,
  1614. int data_direction,
  1615. int task_attr,
  1616. unsigned char *sense_buffer)
  1617. {
  1618. INIT_LIST_HEAD(&cmd->se_lun_list);
  1619. INIT_LIST_HEAD(&cmd->se_delayed_list);
  1620. INIT_LIST_HEAD(&cmd->se_ordered_list);
  1621. /*
  1622. * Setup t_task pointer to t_task_backstore
  1623. */
  1624. cmd->t_task = &cmd->t_task_backstore;
  1625. INIT_LIST_HEAD(&T_TASK(cmd)->t_task_list);
  1626. init_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  1627. init_completion(&T_TASK(cmd)->transport_lun_stop_comp);
  1628. init_completion(&T_TASK(cmd)->t_transport_stop_comp);
  1629. spin_lock_init(&T_TASK(cmd)->t_state_lock);
  1630. atomic_set(&T_TASK(cmd)->transport_dev_active, 1);
  1631. cmd->se_tfo = tfo;
  1632. cmd->se_sess = se_sess;
  1633. cmd->data_length = data_length;
  1634. cmd->data_direction = data_direction;
  1635. cmd->sam_task_attr = task_attr;
  1636. cmd->sense_buffer = sense_buffer;
  1637. }
  1638. EXPORT_SYMBOL(transport_init_se_cmd);
  1639. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1640. {
  1641. /*
  1642. * Check if SAM Task Attribute emulation is enabled for this
  1643. * struct se_device storage object
  1644. */
  1645. if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1646. return 0;
  1647. if (cmd->sam_task_attr == TASK_ATTR_ACA) {
  1648. DEBUG_STA("SAM Task Attribute ACA"
  1649. " emulation is not supported\n");
  1650. return -1;
  1651. }
  1652. /*
  1653. * Used to determine when ORDERED commands should go from
  1654. * Dormant to Active status.
  1655. */
  1656. cmd->se_ordered_id = atomic_inc_return(&SE_DEV(cmd)->dev_ordered_id);
  1657. smp_mb__after_atomic_inc();
  1658. DEBUG_STA("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1659. cmd->se_ordered_id, cmd->sam_task_attr,
  1660. TRANSPORT(cmd->se_dev)->name);
  1661. return 0;
  1662. }
  1663. void transport_free_se_cmd(
  1664. struct se_cmd *se_cmd)
  1665. {
  1666. if (se_cmd->se_tmr_req)
  1667. core_tmr_release_req(se_cmd->se_tmr_req);
  1668. /*
  1669. * Check and free any extended CDB buffer that was allocated
  1670. */
  1671. if (T_TASK(se_cmd)->t_task_cdb != T_TASK(se_cmd)->__t_task_cdb)
  1672. kfree(T_TASK(se_cmd)->t_task_cdb);
  1673. }
  1674. EXPORT_SYMBOL(transport_free_se_cmd);
  1675. static void transport_generic_wait_for_tasks(struct se_cmd *, int, int);
  1676. /* transport_generic_allocate_tasks():
  1677. *
  1678. * Called from fabric RX Thread.
  1679. */
  1680. int transport_generic_allocate_tasks(
  1681. struct se_cmd *cmd,
  1682. unsigned char *cdb)
  1683. {
  1684. int ret;
  1685. transport_generic_prepare_cdb(cdb);
  1686. /*
  1687. * This is needed for early exceptions.
  1688. */
  1689. cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
  1690. transport_device_setup_cmd(cmd);
  1691. /*
  1692. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1693. * for VARIABLE_LENGTH_CMD
  1694. */
  1695. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1696. printk(KERN_ERR "Received SCSI CDB with command_size: %d that"
  1697. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1698. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1699. return -1;
  1700. }
  1701. /*
  1702. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1703. * allocate the additional extended CDB buffer now.. Otherwise
  1704. * setup the pointer from __t_task_cdb to t_task_cdb.
  1705. */
  1706. if (scsi_command_size(cdb) > sizeof(T_TASK(cmd)->__t_task_cdb)) {
  1707. T_TASK(cmd)->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1708. GFP_KERNEL);
  1709. if (!(T_TASK(cmd)->t_task_cdb)) {
  1710. printk(KERN_ERR "Unable to allocate T_TASK(cmd)->t_task_cdb"
  1711. " %u > sizeof(T_TASK(cmd)->__t_task_cdb): %lu ops\n",
  1712. scsi_command_size(cdb),
  1713. (unsigned long)sizeof(T_TASK(cmd)->__t_task_cdb));
  1714. return -1;
  1715. }
  1716. } else
  1717. T_TASK(cmd)->t_task_cdb = &T_TASK(cmd)->__t_task_cdb[0];
  1718. /*
  1719. * Copy the original CDB into T_TASK(cmd).
  1720. */
  1721. memcpy(T_TASK(cmd)->t_task_cdb, cdb, scsi_command_size(cdb));
  1722. /*
  1723. * Setup the received CDB based on SCSI defined opcodes and
  1724. * perform unit attention, persistent reservations and ALUA
  1725. * checks for virtual device backends. The T_TASK(cmd)->t_task_cdb
  1726. * pointer is expected to be setup before we reach this point.
  1727. */
  1728. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1729. if (ret < 0)
  1730. return ret;
  1731. /*
  1732. * Check for SAM Task Attribute Emulation
  1733. */
  1734. if (transport_check_alloc_task_attr(cmd) < 0) {
  1735. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1736. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1737. return -2;
  1738. }
  1739. spin_lock(&cmd->se_lun->lun_sep_lock);
  1740. if (cmd->se_lun->lun_sep)
  1741. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1742. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1743. return 0;
  1744. }
  1745. EXPORT_SYMBOL(transport_generic_allocate_tasks);
  1746. /*
  1747. * Used by fabric module frontends not defining a TFO->new_cmd_map()
  1748. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
  1749. */
  1750. int transport_generic_handle_cdb(
  1751. struct se_cmd *cmd)
  1752. {
  1753. if (!SE_LUN(cmd)) {
  1754. dump_stack();
  1755. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  1756. return -1;
  1757. }
  1758. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD);
  1759. return 0;
  1760. }
  1761. EXPORT_SYMBOL(transport_generic_handle_cdb);
  1762. /*
  1763. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1764. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1765. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1766. */
  1767. int transport_generic_handle_cdb_map(
  1768. struct se_cmd *cmd)
  1769. {
  1770. if (!SE_LUN(cmd)) {
  1771. dump_stack();
  1772. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  1773. return -1;
  1774. }
  1775. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
  1776. return 0;
  1777. }
  1778. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1779. /* transport_generic_handle_data():
  1780. *
  1781. *
  1782. */
  1783. int transport_generic_handle_data(
  1784. struct se_cmd *cmd)
  1785. {
  1786. /*
  1787. * For the software fabric case, then we assume the nexus is being
  1788. * failed/shutdown when signals are pending from the kthread context
  1789. * caller, so we return a failure. For the HW target mode case running
  1790. * in interrupt code, the signal_pending() check is skipped.
  1791. */
  1792. if (!in_interrupt() && signal_pending(current))
  1793. return -1;
  1794. /*
  1795. * If the received CDB has aleady been ABORTED by the generic
  1796. * target engine, we now call transport_check_aborted_status()
  1797. * to queue any delated TASK_ABORTED status for the received CDB to the
  1798. * fabric module as we are expecting no futher incoming DATA OUT
  1799. * sequences at this point.
  1800. */
  1801. if (transport_check_aborted_status(cmd, 1) != 0)
  1802. return 0;
  1803. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
  1804. return 0;
  1805. }
  1806. EXPORT_SYMBOL(transport_generic_handle_data);
  1807. /* transport_generic_handle_tmr():
  1808. *
  1809. *
  1810. */
  1811. int transport_generic_handle_tmr(
  1812. struct se_cmd *cmd)
  1813. {
  1814. /*
  1815. * This is needed for early exceptions.
  1816. */
  1817. cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
  1818. transport_device_setup_cmd(cmd);
  1819. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
  1820. return 0;
  1821. }
  1822. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1823. static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
  1824. {
  1825. struct se_task *task, *task_tmp;
  1826. unsigned long flags;
  1827. int ret = 0;
  1828. DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
  1829. CMD_TFO(cmd)->get_task_tag(cmd));
  1830. /*
  1831. * No tasks remain in the execution queue
  1832. */
  1833. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1834. list_for_each_entry_safe(task, task_tmp,
  1835. &T_TASK(cmd)->t_task_list, t_list) {
  1836. DEBUG_TS("task_no[%d] - Processing task %p\n",
  1837. task->task_no, task);
  1838. /*
  1839. * If the struct se_task has not been sent and is not active,
  1840. * remove the struct se_task from the execution queue.
  1841. */
  1842. if (!atomic_read(&task->task_sent) &&
  1843. !atomic_read(&task->task_active)) {
  1844. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  1845. flags);
  1846. transport_remove_task_from_execute_queue(task,
  1847. task->se_dev);
  1848. DEBUG_TS("task_no[%d] - Removed from execute queue\n",
  1849. task->task_no);
  1850. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1851. continue;
  1852. }
  1853. /*
  1854. * If the struct se_task is active, sleep until it is returned
  1855. * from the plugin.
  1856. */
  1857. if (atomic_read(&task->task_active)) {
  1858. atomic_set(&task->task_stop, 1);
  1859. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  1860. flags);
  1861. DEBUG_TS("task_no[%d] - Waiting to complete\n",
  1862. task->task_no);
  1863. wait_for_completion(&task->task_stop_comp);
  1864. DEBUG_TS("task_no[%d] - Stopped successfully\n",
  1865. task->task_no);
  1866. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1867. atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
  1868. atomic_set(&task->task_active, 0);
  1869. atomic_set(&task->task_stop, 0);
  1870. } else {
  1871. DEBUG_TS("task_no[%d] - Did nothing\n", task->task_no);
  1872. ret++;
  1873. }
  1874. __transport_stop_task_timer(task, &flags);
  1875. }
  1876. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  1877. return ret;
  1878. }
  1879. static void transport_failure_reset_queue_depth(struct se_device *dev)
  1880. {
  1881. unsigned long flags;
  1882. spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);;
  1883. atomic_inc(&dev->depth_left);
  1884. atomic_inc(&SE_HBA(dev)->left_queue_depth);
  1885. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  1886. }
  1887. /*
  1888. * Handle SAM-esque emulation for generic transport request failures.
  1889. */
  1890. static void transport_generic_request_failure(
  1891. struct se_cmd *cmd,
  1892. struct se_device *dev,
  1893. int complete,
  1894. int sc)
  1895. {
  1896. DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1897. " CDB: 0x%02x\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
  1898. T_TASK(cmd)->t_task_cdb[0]);
  1899. DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
  1900. " %d/%d transport_error_status: %d\n",
  1901. CMD_TFO(cmd)->get_cmd_state(cmd),
  1902. cmd->t_state, cmd->deferred_t_state,
  1903. cmd->transport_error_status);
  1904. DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
  1905. " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
  1906. " t_transport_active: %d t_transport_stop: %d"
  1907. " t_transport_sent: %d\n", T_TASK(cmd)->t_task_cdbs,
  1908. atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
  1909. atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
  1910. atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left),
  1911. atomic_read(&T_TASK(cmd)->t_transport_active),
  1912. atomic_read(&T_TASK(cmd)->t_transport_stop),
  1913. atomic_read(&T_TASK(cmd)->t_transport_sent));
  1914. transport_stop_all_task_timers(cmd);
  1915. if (dev)
  1916. transport_failure_reset_queue_depth(dev);
  1917. /*
  1918. * For SAM Task Attribute emulation for failed struct se_cmd
  1919. */
  1920. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1921. transport_complete_task_attr(cmd);
  1922. if (complete) {
  1923. transport_direct_request_timeout(cmd);
  1924. cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
  1925. }
  1926. switch (cmd->transport_error_status) {
  1927. case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
  1928. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1929. break;
  1930. case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
  1931. cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
  1932. break;
  1933. case PYX_TRANSPORT_INVALID_CDB_FIELD:
  1934. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1935. break;
  1936. case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
  1937. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1938. break;
  1939. case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
  1940. if (!sc)
  1941. transport_new_cmd_failure(cmd);
  1942. /*
  1943. * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
  1944. * we force this session to fall back to session
  1945. * recovery.
  1946. */
  1947. CMD_TFO(cmd)->fall_back_to_erl0(cmd->se_sess);
  1948. CMD_TFO(cmd)->stop_session(cmd->se_sess, 0, 0);
  1949. goto check_stop;
  1950. case PYX_TRANSPORT_LU_COMM_FAILURE:
  1951. case PYX_TRANSPORT_ILLEGAL_REQUEST:
  1952. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1953. break;
  1954. case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
  1955. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  1956. break;
  1957. case PYX_TRANSPORT_WRITE_PROTECTED:
  1958. cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
  1959. break;
  1960. case PYX_TRANSPORT_RESERVATION_CONFLICT:
  1961. /*
  1962. * No SENSE Data payload for this case, set SCSI Status
  1963. * and queue the response to $FABRIC_MOD.
  1964. *
  1965. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1966. */
  1967. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1968. /*
  1969. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1970. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1971. * CONFLICT STATUS.
  1972. *
  1973. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1974. */
  1975. if (SE_SESS(cmd) &&
  1976. DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
  1977. core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
  1978. cmd->orig_fe_lun, 0x2C,
  1979. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1980. CMD_TFO(cmd)->queue_status(cmd);
  1981. goto check_stop;
  1982. case PYX_TRANSPORT_USE_SENSE_REASON:
  1983. /*
  1984. * struct se_cmd->scsi_sense_reason already set
  1985. */
  1986. break;
  1987. default:
  1988. printk(KERN_ERR "Unknown transport error for CDB 0x%02x: %d\n",
  1989. T_TASK(cmd)->t_task_cdb[0],
  1990. cmd->transport_error_status);
  1991. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1992. break;
  1993. }
  1994. if (!sc)
  1995. transport_new_cmd_failure(cmd);
  1996. else
  1997. transport_send_check_condition_and_sense(cmd,
  1998. cmd->scsi_sense_reason, 0);
  1999. check_stop:
  2000. transport_lun_remove_cmd(cmd);
  2001. if (!(transport_cmd_check_stop_to_fabric(cmd)))
  2002. ;
  2003. }
  2004. static void transport_direct_request_timeout(struct se_cmd *cmd)
  2005. {
  2006. unsigned long flags;
  2007. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2008. if (!(atomic_read(&T_TASK(cmd)->t_transport_timeout))) {
  2009. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2010. return;
  2011. }
  2012. if (atomic_read(&T_TASK(cmd)->t_task_cdbs_timeout_left)) {
  2013. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2014. return;
  2015. }
  2016. atomic_sub(atomic_read(&T_TASK(cmd)->t_transport_timeout),
  2017. &T_TASK(cmd)->t_se_count);
  2018. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2019. }
  2020. static void transport_generic_request_timeout(struct se_cmd *cmd)
  2021. {
  2022. unsigned long flags;
  2023. /*
  2024. * Reset T_TASK(cmd)->t_se_count to allow transport_generic_remove()
  2025. * to allow last call to free memory resources.
  2026. */
  2027. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2028. if (atomic_read(&T_TASK(cmd)->t_transport_timeout) > 1) {
  2029. int tmp = (atomic_read(&T_TASK(cmd)->t_transport_timeout) - 1);
  2030. atomic_sub(tmp, &T_TASK(cmd)->t_se_count);
  2031. }
  2032. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2033. transport_generic_remove(cmd, 0, 0);
  2034. }
  2035. static int
  2036. transport_generic_allocate_buf(struct se_cmd *cmd, u32 data_length)
  2037. {
  2038. unsigned char *buf;
  2039. buf = kzalloc(data_length, GFP_KERNEL);
  2040. if (!(buf)) {
  2041. printk(KERN_ERR "Unable to allocate memory for buffer\n");
  2042. return -1;
  2043. }
  2044. T_TASK(cmd)->t_tasks_se_num = 0;
  2045. T_TASK(cmd)->t_task_buf = buf;
  2046. return 0;
  2047. }
  2048. static inline u32 transport_lba_21(unsigned char *cdb)
  2049. {
  2050. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  2051. }
  2052. static inline u32 transport_lba_32(unsigned char *cdb)
  2053. {
  2054. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  2055. }
  2056. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  2057. {
  2058. unsigned int __v1, __v2;
  2059. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  2060. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2061. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  2062. }
  2063. /*
  2064. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  2065. */
  2066. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  2067. {
  2068. unsigned int __v1, __v2;
  2069. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  2070. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  2071. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  2072. }
  2073. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  2074. {
  2075. unsigned long flags;
  2076. spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
  2077. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  2078. spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
  2079. }
  2080. /*
  2081. * Called from interrupt context.
  2082. */
  2083. static void transport_task_timeout_handler(unsigned long data)
  2084. {
  2085. struct se_task *task = (struct se_task *)data;
  2086. struct se_cmd *cmd = TASK_CMD(task);
  2087. unsigned long flags;
  2088. DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
  2089. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2090. if (task->task_flags & TF_STOP) {
  2091. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2092. return;
  2093. }
  2094. task->task_flags &= ~TF_RUNNING;
  2095. /*
  2096. * Determine if transport_complete_task() has already been called.
  2097. */
  2098. if (!(atomic_read(&task->task_active))) {
  2099. DEBUG_TT("transport task: %p cmd: %p timeout task_active"
  2100. " == 0\n", task, cmd);
  2101. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2102. return;
  2103. }
  2104. atomic_inc(&T_TASK(cmd)->t_se_count);
  2105. atomic_inc(&T_TASK(cmd)->t_transport_timeout);
  2106. T_TASK(cmd)->t_tasks_failed = 1;
  2107. atomic_set(&task->task_timeout, 1);
  2108. task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
  2109. task->task_scsi_status = 1;
  2110. if (atomic_read(&task->task_stop)) {
  2111. DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
  2112. " == 1\n", task, cmd);
  2113. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2114. complete(&task->task_stop_comp);
  2115. return;
  2116. }
  2117. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
  2118. DEBUG_TT("transport task: %p cmd: %p timeout non zero"
  2119. " t_task_cdbs_left\n", task, cmd);
  2120. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2121. return;
  2122. }
  2123. DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
  2124. task, cmd);
  2125. cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
  2126. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2127. transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
  2128. }
  2129. /*
  2130. * Called with T_TASK(cmd)->t_state_lock held.
  2131. */
  2132. static void transport_start_task_timer(struct se_task *task)
  2133. {
  2134. struct se_device *dev = task->se_dev;
  2135. int timeout;
  2136. if (task->task_flags & TF_RUNNING)
  2137. return;
  2138. /*
  2139. * If the task_timeout is disabled, exit now.
  2140. */
  2141. timeout = DEV_ATTRIB(dev)->task_timeout;
  2142. if (!(timeout))
  2143. return;
  2144. init_timer(&task->task_timer);
  2145. task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
  2146. task->task_timer.data = (unsigned long) task;
  2147. task->task_timer.function = transport_task_timeout_handler;
  2148. task->task_flags |= TF_RUNNING;
  2149. add_timer(&task->task_timer);
  2150. #if 0
  2151. printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
  2152. " %d\n", task->task_se_cmd, task, timeout);
  2153. #endif
  2154. }
  2155. /*
  2156. * Called with spin_lock_irq(&T_TASK(cmd)->t_state_lock) held.
  2157. */
  2158. void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
  2159. {
  2160. struct se_cmd *cmd = TASK_CMD(task);
  2161. if (!(task->task_flags & TF_RUNNING))
  2162. return;
  2163. task->task_flags |= TF_STOP;
  2164. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, *flags);
  2165. del_timer_sync(&task->task_timer);
  2166. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, *flags);
  2167. task->task_flags &= ~TF_RUNNING;
  2168. task->task_flags &= ~TF_STOP;
  2169. }
  2170. static void transport_stop_all_task_timers(struct se_cmd *cmd)
  2171. {
  2172. struct se_task *task = NULL, *task_tmp;
  2173. unsigned long flags;
  2174. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2175. list_for_each_entry_safe(task, task_tmp,
  2176. &T_TASK(cmd)->t_task_list, t_list)
  2177. __transport_stop_task_timer(task, &flags);
  2178. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2179. }
  2180. static inline int transport_tcq_window_closed(struct se_device *dev)
  2181. {
  2182. if (dev->dev_tcq_window_closed++ <
  2183. PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
  2184. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
  2185. } else
  2186. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
  2187. wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
  2188. return 0;
  2189. }
  2190. /*
  2191. * Called from Fabric Module context from transport_execute_tasks()
  2192. *
  2193. * The return of this function determins if the tasks from struct se_cmd
  2194. * get added to the execution queue in transport_execute_tasks(),
  2195. * or are added to the delayed or ordered lists here.
  2196. */
  2197. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  2198. {
  2199. if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  2200. return 1;
  2201. /*
  2202. * Check for the existance of HEAD_OF_QUEUE, and if true return 1
  2203. * to allow the passed struct se_cmd list of tasks to the front of the list.
  2204. */
  2205. if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
  2206. atomic_inc(&SE_DEV(cmd)->dev_hoq_count);
  2207. smp_mb__after_atomic_inc();
  2208. DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
  2209. " 0x%02x, se_ordered_id: %u\n",
  2210. T_TASK(cmd)->t_task_cdb[0],
  2211. cmd->se_ordered_id);
  2212. return 1;
  2213. } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
  2214. spin_lock(&SE_DEV(cmd)->ordered_cmd_lock);
  2215. list_add_tail(&cmd->se_ordered_list,
  2216. &SE_DEV(cmd)->ordered_cmd_list);
  2217. spin_unlock(&SE_DEV(cmd)->ordered_cmd_lock);
  2218. atomic_inc(&SE_DEV(cmd)->dev_ordered_sync);
  2219. smp_mb__after_atomic_inc();
  2220. DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
  2221. " list, se_ordered_id: %u\n",
  2222. T_TASK(cmd)->t_task_cdb[0],
  2223. cmd->se_ordered_id);
  2224. /*
  2225. * Add ORDERED command to tail of execution queue if
  2226. * no other older commands exist that need to be
  2227. * completed first.
  2228. */
  2229. if (!(atomic_read(&SE_DEV(cmd)->simple_cmds)))
  2230. return 1;
  2231. } else {
  2232. /*
  2233. * For SIMPLE and UNTAGGED Task Attribute commands
  2234. */
  2235. atomic_inc(&SE_DEV(cmd)->simple_cmds);
  2236. smp_mb__after_atomic_inc();
  2237. }
  2238. /*
  2239. * Otherwise if one or more outstanding ORDERED task attribute exist,
  2240. * add the dormant task(s) built for the passed struct se_cmd to the
  2241. * execution queue and become in Active state for this struct se_device.
  2242. */
  2243. if (atomic_read(&SE_DEV(cmd)->dev_ordered_sync) != 0) {
  2244. /*
  2245. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  2246. * will be drained upon competion of HEAD_OF_QUEUE task.
  2247. */
  2248. spin_lock(&SE_DEV(cmd)->delayed_cmd_lock);
  2249. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  2250. list_add_tail(&cmd->se_delayed_list,
  2251. &SE_DEV(cmd)->delayed_cmd_list);
  2252. spin_unlock(&SE_DEV(cmd)->delayed_cmd_lock);
  2253. DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
  2254. " delayed CMD list, se_ordered_id: %u\n",
  2255. T_TASK(cmd)->t_task_cdb[0], cmd->sam_task_attr,
  2256. cmd->se_ordered_id);
  2257. /*
  2258. * Return zero to let transport_execute_tasks() know
  2259. * not to add the delayed tasks to the execution list.
  2260. */
  2261. return 0;
  2262. }
  2263. /*
  2264. * Otherwise, no ORDERED task attributes exist..
  2265. */
  2266. return 1;
  2267. }
  2268. /*
  2269. * Called from fabric module context in transport_generic_new_cmd() and
  2270. * transport_generic_process_write()
  2271. */
  2272. static int transport_execute_tasks(struct se_cmd *cmd)
  2273. {
  2274. int add_tasks;
  2275. if (!(cmd->se_cmd_flags & SCF_SE_DISABLE_ONLINE_CHECK)) {
  2276. if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
  2277. cmd->transport_error_status =
  2278. PYX_TRANSPORT_LU_COMM_FAILURE;
  2279. transport_generic_request_failure(cmd, NULL, 0, 1);
  2280. return 0;
  2281. }
  2282. }
  2283. /*
  2284. * Call transport_cmd_check_stop() to see if a fabric exception
  2285. * has occured that prevents execution.
  2286. */
  2287. if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
  2288. /*
  2289. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  2290. * attribute for the tasks of the received struct se_cmd CDB
  2291. */
  2292. add_tasks = transport_execute_task_attr(cmd);
  2293. if (add_tasks == 0)
  2294. goto execute_tasks;
  2295. /*
  2296. * This calls transport_add_tasks_from_cmd() to handle
  2297. * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
  2298. * (if enabled) in __transport_add_task_to_execute_queue() and
  2299. * transport_add_task_check_sam_attr().
  2300. */
  2301. transport_add_tasks_from_cmd(cmd);
  2302. }
  2303. /*
  2304. * Kick the execution queue for the cmd associated struct se_device
  2305. * storage object.
  2306. */
  2307. execute_tasks:
  2308. __transport_execute_tasks(SE_DEV(cmd));
  2309. return 0;
  2310. }
  2311. /*
  2312. * Called to check struct se_device tcq depth window, and once open pull struct se_task
  2313. * from struct se_device->execute_task_list and
  2314. *
  2315. * Called from transport_processing_thread()
  2316. */
  2317. static int __transport_execute_tasks(struct se_device *dev)
  2318. {
  2319. int error;
  2320. struct se_cmd *cmd = NULL;
  2321. struct se_task *task;
  2322. unsigned long flags;
  2323. /*
  2324. * Check if there is enough room in the device and HBA queue to send
  2325. * struct se_transport_task's to the selected transport.
  2326. */
  2327. check_depth:
  2328. spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
  2329. if (!(atomic_read(&dev->depth_left)) ||
  2330. !(atomic_read(&SE_HBA(dev)->left_queue_depth))) {
  2331. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2332. return transport_tcq_window_closed(dev);
  2333. }
  2334. dev->dev_tcq_window_closed = 0;
  2335. spin_lock(&dev->execute_task_lock);
  2336. task = transport_get_task_from_execute_queue(dev);
  2337. spin_unlock(&dev->execute_task_lock);
  2338. if (!task) {
  2339. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2340. return 0;
  2341. }
  2342. atomic_dec(&dev->depth_left);
  2343. atomic_dec(&SE_HBA(dev)->left_queue_depth);
  2344. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2345. cmd = TASK_CMD(task);
  2346. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2347. atomic_set(&task->task_active, 1);
  2348. atomic_set(&task->task_sent, 1);
  2349. atomic_inc(&T_TASK(cmd)->t_task_cdbs_sent);
  2350. if (atomic_read(&T_TASK(cmd)->t_task_cdbs_sent) ==
  2351. T_TASK(cmd)->t_task_cdbs)
  2352. atomic_set(&cmd->transport_sent, 1);
  2353. transport_start_task_timer(task);
  2354. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2355. /*
  2356. * The struct se_cmd->transport_emulate_cdb() function pointer is used
  2357. * to grab REPORT_LUNS CDBs before they hit the
  2358. * struct se_subsystem_api->do_task() caller below.
  2359. */
  2360. if (cmd->transport_emulate_cdb) {
  2361. error = cmd->transport_emulate_cdb(cmd);
  2362. if (error != 0) {
  2363. cmd->transport_error_status = error;
  2364. atomic_set(&task->task_active, 0);
  2365. atomic_set(&cmd->transport_sent, 0);
  2366. transport_stop_tasks_for_cmd(cmd);
  2367. transport_generic_request_failure(cmd, dev, 0, 1);
  2368. goto check_depth;
  2369. }
  2370. /*
  2371. * Handle the successful completion for transport_emulate_cdb()
  2372. * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
  2373. * Otherwise the caller is expected to complete the task with
  2374. * proper status.
  2375. */
  2376. if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
  2377. cmd->scsi_status = SAM_STAT_GOOD;
  2378. task->task_scsi_status = GOOD;
  2379. transport_complete_task(task, 1);
  2380. }
  2381. } else {
  2382. /*
  2383. * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
  2384. * RAMDISK we use the internal transport_emulate_control_cdb() logic
  2385. * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
  2386. * LUN emulation code.
  2387. *
  2388. * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
  2389. * call ->do_task() directly and let the underlying TCM subsystem plugin
  2390. * code handle the CDB emulation.
  2391. */
  2392. if ((TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
  2393. (!(TASK_CMD(task)->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2394. error = transport_emulate_control_cdb(task);
  2395. else
  2396. error = TRANSPORT(dev)->do_task(task);
  2397. if (error != 0) {
  2398. cmd->transport_error_status = error;
  2399. atomic_set(&task->task_active, 0);
  2400. atomic_set(&cmd->transport_sent, 0);
  2401. transport_stop_tasks_for_cmd(cmd);
  2402. transport_generic_request_failure(cmd, dev, 0, 1);
  2403. }
  2404. }
  2405. goto check_depth;
  2406. return 0;
  2407. }
  2408. void transport_new_cmd_failure(struct se_cmd *se_cmd)
  2409. {
  2410. unsigned long flags;
  2411. /*
  2412. * Any unsolicited data will get dumped for failed command inside of
  2413. * the fabric plugin
  2414. */
  2415. spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
  2416. se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
  2417. se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2418. spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
  2419. CMD_TFO(se_cmd)->new_cmd_failure(se_cmd);
  2420. }
  2421. static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
  2422. static inline u32 transport_get_sectors_6(
  2423. unsigned char *cdb,
  2424. struct se_cmd *cmd,
  2425. int *ret)
  2426. {
  2427. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2428. /*
  2429. * Assume TYPE_DISK for non struct se_device objects.
  2430. * Use 8-bit sector value.
  2431. */
  2432. if (!dev)
  2433. goto type_disk;
  2434. /*
  2435. * Use 24-bit allocation length for TYPE_TAPE.
  2436. */
  2437. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
  2438. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  2439. /*
  2440. * Everything else assume TYPE_DISK Sector CDB location.
  2441. * Use 8-bit sector value.
  2442. */
  2443. type_disk:
  2444. return (u32)cdb[4];
  2445. }
  2446. static inline u32 transport_get_sectors_10(
  2447. unsigned char *cdb,
  2448. struct se_cmd *cmd,
  2449. int *ret)
  2450. {
  2451. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2452. /*
  2453. * Assume TYPE_DISK for non struct se_device objects.
  2454. * Use 16-bit sector value.
  2455. */
  2456. if (!dev)
  2457. goto type_disk;
  2458. /*
  2459. * XXX_10 is not defined in SSC, throw an exception
  2460. */
  2461. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2462. *ret = -1;
  2463. return 0;
  2464. }
  2465. /*
  2466. * Everything else assume TYPE_DISK Sector CDB location.
  2467. * Use 16-bit sector value.
  2468. */
  2469. type_disk:
  2470. return (u32)(cdb[7] << 8) + cdb[8];
  2471. }
  2472. static inline u32 transport_get_sectors_12(
  2473. unsigned char *cdb,
  2474. struct se_cmd *cmd,
  2475. int *ret)
  2476. {
  2477. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2478. /*
  2479. * Assume TYPE_DISK for non struct se_device objects.
  2480. * Use 32-bit sector value.
  2481. */
  2482. if (!dev)
  2483. goto type_disk;
  2484. /*
  2485. * XXX_12 is not defined in SSC, throw an exception
  2486. */
  2487. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2488. *ret = -1;
  2489. return 0;
  2490. }
  2491. /*
  2492. * Everything else assume TYPE_DISK Sector CDB location.
  2493. * Use 32-bit sector value.
  2494. */
  2495. type_disk:
  2496. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  2497. }
  2498. static inline u32 transport_get_sectors_16(
  2499. unsigned char *cdb,
  2500. struct se_cmd *cmd,
  2501. int *ret)
  2502. {
  2503. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2504. /*
  2505. * Assume TYPE_DISK for non struct se_device objects.
  2506. * Use 32-bit sector value.
  2507. */
  2508. if (!dev)
  2509. goto type_disk;
  2510. /*
  2511. * Use 24-bit allocation length for TYPE_TAPE.
  2512. */
  2513. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
  2514. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2515. type_disk:
  2516. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2517. (cdb[12] << 8) + cdb[13];
  2518. }
  2519. /*
  2520. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2521. */
  2522. static inline u32 transport_get_sectors_32(
  2523. unsigned char *cdb,
  2524. struct se_cmd *cmd,
  2525. int *ret)
  2526. {
  2527. /*
  2528. * Assume TYPE_DISK for non struct se_device objects.
  2529. * Use 32-bit sector value.
  2530. */
  2531. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2532. (cdb[30] << 8) + cdb[31];
  2533. }
  2534. static inline u32 transport_get_size(
  2535. u32 sectors,
  2536. unsigned char *cdb,
  2537. struct se_cmd *cmd)
  2538. {
  2539. struct se_device *dev = SE_DEV(cmd);
  2540. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2541. if (cdb[1] & 1) { /* sectors */
  2542. return DEV_ATTRIB(dev)->block_size * sectors;
  2543. } else /* bytes */
  2544. return sectors;
  2545. }
  2546. #if 0
  2547. printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
  2548. " %s object\n", DEV_ATTRIB(dev)->block_size, sectors,
  2549. DEV_ATTRIB(dev)->block_size * sectors,
  2550. TRANSPORT(dev)->name);
  2551. #endif
  2552. return DEV_ATTRIB(dev)->block_size * sectors;
  2553. }
  2554. unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
  2555. {
  2556. unsigned char result = 0;
  2557. /*
  2558. * MSB
  2559. */
  2560. if ((val[0] >= 'a') && (val[0] <= 'f'))
  2561. result = ((val[0] - 'a' + 10) & 0xf) << 4;
  2562. else
  2563. if ((val[0] >= 'A') && (val[0] <= 'F'))
  2564. result = ((val[0] - 'A' + 10) & 0xf) << 4;
  2565. else /* digit */
  2566. result = ((val[0] - '0') & 0xf) << 4;
  2567. /*
  2568. * LSB
  2569. */
  2570. if ((val[1] >= 'a') && (val[1] <= 'f'))
  2571. result |= ((val[1] - 'a' + 10) & 0xf);
  2572. else
  2573. if ((val[1] >= 'A') && (val[1] <= 'F'))
  2574. result |= ((val[1] - 'A' + 10) & 0xf);
  2575. else /* digit */
  2576. result |= ((val[1] - '0') & 0xf);
  2577. return result;
  2578. }
  2579. EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
  2580. static void transport_xor_callback(struct se_cmd *cmd)
  2581. {
  2582. unsigned char *buf, *addr;
  2583. struct se_mem *se_mem;
  2584. unsigned int offset;
  2585. int i;
  2586. /*
  2587. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2588. *
  2589. * 1) read the specified logical block(s);
  2590. * 2) transfer logical blocks from the data-out buffer;
  2591. * 3) XOR the logical blocks transferred from the data-out buffer with
  2592. * the logical blocks read, storing the resulting XOR data in a buffer;
  2593. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2594. * blocks transferred from the data-out buffer; and
  2595. * 5) transfer the resulting XOR data to the data-in buffer.
  2596. */
  2597. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2598. if (!(buf)) {
  2599. printk(KERN_ERR "Unable to allocate xor_callback buf\n");
  2600. return;
  2601. }
  2602. /*
  2603. * Copy the scatterlist WRITE buffer located at T_TASK(cmd)->t_mem_list
  2604. * into the locally allocated *buf
  2605. */
  2606. transport_memcpy_se_mem_read_contig(cmd, buf, T_TASK(cmd)->t_mem_list);
  2607. /*
  2608. * Now perform the XOR against the BIDI read memory located at
  2609. * T_TASK(cmd)->t_mem_bidi_list
  2610. */
  2611. offset = 0;
  2612. list_for_each_entry(se_mem, T_TASK(cmd)->t_mem_bidi_list, se_list) {
  2613. addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
  2614. if (!(addr))
  2615. goto out;
  2616. for (i = 0; i < se_mem->se_len; i++)
  2617. *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
  2618. offset += se_mem->se_len;
  2619. kunmap_atomic(addr, KM_USER0);
  2620. }
  2621. out:
  2622. kfree(buf);
  2623. }
  2624. /*
  2625. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2626. */
  2627. static int transport_get_sense_data(struct se_cmd *cmd)
  2628. {
  2629. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2630. struct se_device *dev;
  2631. struct se_task *task = NULL, *task_tmp;
  2632. unsigned long flags;
  2633. u32 offset = 0;
  2634. if (!SE_LUN(cmd)) {
  2635. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  2636. return -1;
  2637. }
  2638. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2639. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2640. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2641. return 0;
  2642. }
  2643. list_for_each_entry_safe(task, task_tmp,
  2644. &T_TASK(cmd)->t_task_list, t_list) {
  2645. if (!task->task_sense)
  2646. continue;
  2647. dev = task->se_dev;
  2648. if (!(dev))
  2649. continue;
  2650. if (!TRANSPORT(dev)->get_sense_buffer) {
  2651. printk(KERN_ERR "TRANSPORT(dev)->get_sense_buffer"
  2652. " is NULL\n");
  2653. continue;
  2654. }
  2655. sense_buffer = TRANSPORT(dev)->get_sense_buffer(task);
  2656. if (!(sense_buffer)) {
  2657. printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
  2658. " sense buffer for task with sense\n",
  2659. CMD_TFO(cmd)->get_task_tag(cmd), task->task_no);
  2660. continue;
  2661. }
  2662. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2663. offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
  2664. TRANSPORT_SENSE_BUFFER);
  2665. memcpy((void *)&buffer[offset], (void *)sense_buffer,
  2666. TRANSPORT_SENSE_BUFFER);
  2667. cmd->scsi_status = task->task_scsi_status;
  2668. /* Automatically padded */
  2669. cmd->scsi_sense_length =
  2670. (TRANSPORT_SENSE_BUFFER + offset);
  2671. printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
  2672. " and sense\n",
  2673. dev->se_hba->hba_id, TRANSPORT(dev)->name,
  2674. cmd->scsi_status);
  2675. return 0;
  2676. }
  2677. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2678. return -1;
  2679. }
  2680. static int transport_allocate_resources(struct se_cmd *cmd)
  2681. {
  2682. u32 length = cmd->data_length;
  2683. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  2684. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB))
  2685. return transport_generic_get_mem(cmd, length, PAGE_SIZE);
  2686. else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB)
  2687. return transport_generic_allocate_buf(cmd, length);
  2688. else
  2689. return 0;
  2690. }
  2691. static int
  2692. transport_handle_reservation_conflict(struct se_cmd *cmd)
  2693. {
  2694. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2695. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2696. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2697. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2698. /*
  2699. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  2700. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  2701. * CONFLICT STATUS.
  2702. *
  2703. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  2704. */
  2705. if (SE_SESS(cmd) &&
  2706. DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
  2707. core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
  2708. cmd->orig_fe_lun, 0x2C,
  2709. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  2710. return -2;
  2711. }
  2712. /* transport_generic_cmd_sequencer():
  2713. *
  2714. * Generic Command Sequencer that should work for most DAS transport
  2715. * drivers.
  2716. *
  2717. * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
  2718. * RX Thread.
  2719. *
  2720. * FIXME: Need to support other SCSI OPCODES where as well.
  2721. */
  2722. static int transport_generic_cmd_sequencer(
  2723. struct se_cmd *cmd,
  2724. unsigned char *cdb)
  2725. {
  2726. struct se_device *dev = SE_DEV(cmd);
  2727. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2728. int ret = 0, sector_ret = 0, passthrough;
  2729. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2730. u16 service_action;
  2731. u8 alua_ascq = 0;
  2732. /*
  2733. * Check for an existing UNIT ATTENTION condition
  2734. */
  2735. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2736. cmd->transport_wait_for_tasks =
  2737. &transport_nop_wait_for_tasks;
  2738. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2739. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2740. return -2;
  2741. }
  2742. /*
  2743. * Check status of Asymmetric Logical Unit Assignment port
  2744. */
  2745. ret = T10_ALUA(su_dev)->alua_state_check(cmd, cdb, &alua_ascq);
  2746. if (ret != 0) {
  2747. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2748. /*
  2749. * Set SCSI additional sense code (ASC) to 'LUN Not Accessable';
  2750. * The ALUA additional sense code qualifier (ASCQ) is determined
  2751. * by the ALUA primary or secondary access state..
  2752. */
  2753. if (ret > 0) {
  2754. #if 0
  2755. printk(KERN_INFO "[%s]: ALUA TG Port not available,"
  2756. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2757. CMD_TFO(cmd)->get_fabric_name(), alua_ascq);
  2758. #endif
  2759. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2760. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2761. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2762. return -2;
  2763. }
  2764. goto out_invalid_cdb_field;
  2765. }
  2766. /*
  2767. * Check status for SPC-3 Persistent Reservations
  2768. */
  2769. if (T10_PR_OPS(su_dev)->t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2770. if (T10_PR_OPS(su_dev)->t10_seq_non_holder(
  2771. cmd, cdb, pr_reg_type) != 0)
  2772. return transport_handle_reservation_conflict(cmd);
  2773. /*
  2774. * This means the CDB is allowed for the SCSI Initiator port
  2775. * when said port is *NOT* holding the legacy SPC-2 or
  2776. * SPC-3 Persistent Reservation.
  2777. */
  2778. }
  2779. switch (cdb[0]) {
  2780. case READ_6:
  2781. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2782. if (sector_ret)
  2783. goto out_unsupported_cdb;
  2784. size = transport_get_size(sectors, cdb, cmd);
  2785. cmd->transport_split_cdb = &split_cdb_XX_6;
  2786. T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
  2787. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2788. break;
  2789. case READ_10:
  2790. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2791. if (sector_ret)
  2792. goto out_unsupported_cdb;
  2793. size = transport_get_size(sectors, cdb, cmd);
  2794. cmd->transport_split_cdb = &split_cdb_XX_10;
  2795. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2796. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2797. break;
  2798. case READ_12:
  2799. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2800. if (sector_ret)
  2801. goto out_unsupported_cdb;
  2802. size = transport_get_size(sectors, cdb, cmd);
  2803. cmd->transport_split_cdb = &split_cdb_XX_12;
  2804. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2805. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2806. break;
  2807. case READ_16:
  2808. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2809. if (sector_ret)
  2810. goto out_unsupported_cdb;
  2811. size = transport_get_size(sectors, cdb, cmd);
  2812. cmd->transport_split_cdb = &split_cdb_XX_16;
  2813. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  2814. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2815. break;
  2816. case WRITE_6:
  2817. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2818. if (sector_ret)
  2819. goto out_unsupported_cdb;
  2820. size = transport_get_size(sectors, cdb, cmd);
  2821. cmd->transport_split_cdb = &split_cdb_XX_6;
  2822. T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
  2823. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2824. break;
  2825. case WRITE_10:
  2826. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2827. if (sector_ret)
  2828. goto out_unsupported_cdb;
  2829. size = transport_get_size(sectors, cdb, cmd);
  2830. cmd->transport_split_cdb = &split_cdb_XX_10;
  2831. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2832. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2833. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2834. break;
  2835. case WRITE_12:
  2836. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2837. if (sector_ret)
  2838. goto out_unsupported_cdb;
  2839. size = transport_get_size(sectors, cdb, cmd);
  2840. cmd->transport_split_cdb = &split_cdb_XX_12;
  2841. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2842. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2843. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2844. break;
  2845. case WRITE_16:
  2846. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2847. if (sector_ret)
  2848. goto out_unsupported_cdb;
  2849. size = transport_get_size(sectors, cdb, cmd);
  2850. cmd->transport_split_cdb = &split_cdb_XX_16;
  2851. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  2852. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2853. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2854. break;
  2855. case XDWRITEREAD_10:
  2856. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2857. !(T_TASK(cmd)->t_tasks_bidi))
  2858. goto out_invalid_cdb_field;
  2859. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2860. if (sector_ret)
  2861. goto out_unsupported_cdb;
  2862. size = transport_get_size(sectors, cdb, cmd);
  2863. cmd->transport_split_cdb = &split_cdb_XX_10;
  2864. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2865. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2866. passthrough = (TRANSPORT(dev)->transport_type ==
  2867. TRANSPORT_PLUGIN_PHBA_PDEV);
  2868. /*
  2869. * Skip the remaining assignments for TCM/PSCSI passthrough
  2870. */
  2871. if (passthrough)
  2872. break;
  2873. /*
  2874. * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
  2875. */
  2876. cmd->transport_complete_callback = &transport_xor_callback;
  2877. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2878. break;
  2879. case VARIABLE_LENGTH_CMD:
  2880. service_action = get_unaligned_be16(&cdb[8]);
  2881. /*
  2882. * Determine if this is TCM/PSCSI device and we should disable
  2883. * internal emulation for this CDB.
  2884. */
  2885. passthrough = (TRANSPORT(dev)->transport_type ==
  2886. TRANSPORT_PLUGIN_PHBA_PDEV);
  2887. switch (service_action) {
  2888. case XDWRITEREAD_32:
  2889. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2890. if (sector_ret)
  2891. goto out_unsupported_cdb;
  2892. size = transport_get_size(sectors, cdb, cmd);
  2893. /*
  2894. * Use WRITE_32 and READ_32 opcodes for the emulated
  2895. * XDWRITE_READ_32 logic.
  2896. */
  2897. cmd->transport_split_cdb = &split_cdb_XX_32;
  2898. T_TASK(cmd)->t_task_lba = transport_lba_64_ext(cdb);
  2899. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2900. /*
  2901. * Skip the remaining assignments for TCM/PSCSI passthrough
  2902. */
  2903. if (passthrough)
  2904. break;
  2905. /*
  2906. * Setup BIDI XOR callback to be run during
  2907. * transport_generic_complete_ok()
  2908. */
  2909. cmd->transport_complete_callback = &transport_xor_callback;
  2910. T_TASK(cmd)->t_tasks_fua = (cdb[10] & 0x8);
  2911. break;
  2912. case WRITE_SAME_32:
  2913. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2914. if (sector_ret)
  2915. goto out_unsupported_cdb;
  2916. size = transport_get_size(sectors, cdb, cmd);
  2917. T_TASK(cmd)->t_task_lba = get_unaligned_be64(&cdb[12]);
  2918. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2919. /*
  2920. * Skip the remaining assignments for TCM/PSCSI passthrough
  2921. */
  2922. if (passthrough)
  2923. break;
  2924. if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
  2925. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  2926. " bits not supported for Block Discard"
  2927. " Emulation\n");
  2928. goto out_invalid_cdb_field;
  2929. }
  2930. /*
  2931. * Currently for the emulated case we only accept
  2932. * tpws with the UNMAP=1 bit set.
  2933. */
  2934. if (!(cdb[10] & 0x08)) {
  2935. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
  2936. " supported for Block Discard Emulation\n");
  2937. goto out_invalid_cdb_field;
  2938. }
  2939. break;
  2940. default:
  2941. printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
  2942. " 0x%04x not supported\n", service_action);
  2943. goto out_unsupported_cdb;
  2944. }
  2945. break;
  2946. case 0xa3:
  2947. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
  2948. /* MAINTENANCE_IN from SCC-2 */
  2949. /*
  2950. * Check for emulated MI_REPORT_TARGET_PGS.
  2951. */
  2952. if (cdb[1] == MI_REPORT_TARGET_PGS) {
  2953. cmd->transport_emulate_cdb =
  2954. (T10_ALUA(su_dev)->alua_type ==
  2955. SPC3_ALUA_EMULATED) ?
  2956. &core_emulate_report_target_port_groups :
  2957. NULL;
  2958. }
  2959. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2960. (cdb[8] << 8) | cdb[9];
  2961. } else {
  2962. /* GPCMD_SEND_KEY from multi media commands */
  2963. size = (cdb[8] << 8) + cdb[9];
  2964. }
  2965. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2966. break;
  2967. case MODE_SELECT:
  2968. size = cdb[4];
  2969. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2970. break;
  2971. case MODE_SELECT_10:
  2972. size = (cdb[7] << 8) + cdb[8];
  2973. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2974. break;
  2975. case MODE_SENSE:
  2976. size = cdb[4];
  2977. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2978. break;
  2979. case MODE_SENSE_10:
  2980. case GPCMD_READ_BUFFER_CAPACITY:
  2981. case GPCMD_SEND_OPC:
  2982. case LOG_SELECT:
  2983. case LOG_SENSE:
  2984. size = (cdb[7] << 8) + cdb[8];
  2985. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2986. break;
  2987. case READ_BLOCK_LIMITS:
  2988. size = READ_BLOCK_LEN;
  2989. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2990. break;
  2991. case GPCMD_GET_CONFIGURATION:
  2992. case GPCMD_READ_FORMAT_CAPACITIES:
  2993. case GPCMD_READ_DISC_INFO:
  2994. case GPCMD_READ_TRACK_RZONE_INFO:
  2995. size = (cdb[7] << 8) + cdb[8];
  2996. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2997. break;
  2998. case PERSISTENT_RESERVE_IN:
  2999. case PERSISTENT_RESERVE_OUT:
  3000. cmd->transport_emulate_cdb =
  3001. (T10_RES(su_dev)->res_type ==
  3002. SPC3_PERSISTENT_RESERVATIONS) ?
  3003. &core_scsi3_emulate_pr : NULL;
  3004. size = (cdb[7] << 8) + cdb[8];
  3005. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3006. break;
  3007. case GPCMD_MECHANISM_STATUS:
  3008. case GPCMD_READ_DVD_STRUCTURE:
  3009. size = (cdb[8] << 8) + cdb[9];
  3010. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3011. break;
  3012. case READ_POSITION:
  3013. size = READ_POSITION_LEN;
  3014. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3015. break;
  3016. case 0xa4:
  3017. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
  3018. /* MAINTENANCE_OUT from SCC-2
  3019. *
  3020. * Check for emulated MO_SET_TARGET_PGS.
  3021. */
  3022. if (cdb[1] == MO_SET_TARGET_PGS) {
  3023. cmd->transport_emulate_cdb =
  3024. (T10_ALUA(su_dev)->alua_type ==
  3025. SPC3_ALUA_EMULATED) ?
  3026. &core_emulate_set_target_port_groups :
  3027. NULL;
  3028. }
  3029. size = (cdb[6] << 24) | (cdb[7] << 16) |
  3030. (cdb[8] << 8) | cdb[9];
  3031. } else {
  3032. /* GPCMD_REPORT_KEY from multi media commands */
  3033. size = (cdb[8] << 8) + cdb[9];
  3034. }
  3035. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3036. break;
  3037. case INQUIRY:
  3038. size = (cdb[3] << 8) + cdb[4];
  3039. /*
  3040. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  3041. * See spc4r17 section 5.3
  3042. */
  3043. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3044. cmd->sam_task_attr = TASK_ATTR_HOQ;
  3045. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3046. break;
  3047. case READ_BUFFER:
  3048. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3049. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3050. break;
  3051. case READ_CAPACITY:
  3052. size = READ_CAP_LEN;
  3053. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3054. break;
  3055. case READ_MEDIA_SERIAL_NUMBER:
  3056. case SECURITY_PROTOCOL_IN:
  3057. case SECURITY_PROTOCOL_OUT:
  3058. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  3059. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3060. break;
  3061. case SERVICE_ACTION_IN:
  3062. case ACCESS_CONTROL_IN:
  3063. case ACCESS_CONTROL_OUT:
  3064. case EXTENDED_COPY:
  3065. case READ_ATTRIBUTE:
  3066. case RECEIVE_COPY_RESULTS:
  3067. case WRITE_ATTRIBUTE:
  3068. size = (cdb[10] << 24) | (cdb[11] << 16) |
  3069. (cdb[12] << 8) | cdb[13];
  3070. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3071. break;
  3072. case RECEIVE_DIAGNOSTIC:
  3073. case SEND_DIAGNOSTIC:
  3074. size = (cdb[3] << 8) | cdb[4];
  3075. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3076. break;
  3077. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  3078. #if 0
  3079. case GPCMD_READ_CD:
  3080. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3081. size = (2336 * sectors);
  3082. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3083. break;
  3084. #endif
  3085. case READ_TOC:
  3086. size = cdb[8];
  3087. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3088. break;
  3089. case REQUEST_SENSE:
  3090. size = cdb[4];
  3091. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3092. break;
  3093. case READ_ELEMENT_STATUS:
  3094. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  3095. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3096. break;
  3097. case WRITE_BUFFER:
  3098. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3099. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3100. break;
  3101. case RESERVE:
  3102. case RESERVE_10:
  3103. /*
  3104. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  3105. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3106. */
  3107. if (cdb[0] == RESERVE_10)
  3108. size = (cdb[7] << 8) | cdb[8];
  3109. else
  3110. size = cmd->data_length;
  3111. /*
  3112. * Setup the legacy emulated handler for SPC-2 and
  3113. * >= SPC-3 compatible reservation handling (CRH=1)
  3114. * Otherwise, we assume the underlying SCSI logic is
  3115. * is running in SPC_PASSTHROUGH, and wants reservations
  3116. * emulation disabled.
  3117. */
  3118. cmd->transport_emulate_cdb =
  3119. (T10_RES(su_dev)->res_type !=
  3120. SPC_PASSTHROUGH) ?
  3121. &core_scsi2_emulate_crh : NULL;
  3122. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3123. break;
  3124. case RELEASE:
  3125. case RELEASE_10:
  3126. /*
  3127. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  3128. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3129. */
  3130. if (cdb[0] == RELEASE_10)
  3131. size = (cdb[7] << 8) | cdb[8];
  3132. else
  3133. size = cmd->data_length;
  3134. cmd->transport_emulate_cdb =
  3135. (T10_RES(su_dev)->res_type !=
  3136. SPC_PASSTHROUGH) ?
  3137. &core_scsi2_emulate_crh : NULL;
  3138. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3139. break;
  3140. case SYNCHRONIZE_CACHE:
  3141. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  3142. /*
  3143. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  3144. */
  3145. if (cdb[0] == SYNCHRONIZE_CACHE) {
  3146. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  3147. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  3148. } else {
  3149. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3150. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  3151. }
  3152. if (sector_ret)
  3153. goto out_unsupported_cdb;
  3154. size = transport_get_size(sectors, cdb, cmd);
  3155. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3156. /*
  3157. * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
  3158. */
  3159. if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  3160. break;
  3161. /*
  3162. * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
  3163. * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
  3164. */
  3165. cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
  3166. /*
  3167. * Check to ensure that LBA + Range does not exceed past end of
  3168. * device.
  3169. */
  3170. if (transport_get_sectors(cmd) < 0)
  3171. goto out_invalid_cdb_field;
  3172. break;
  3173. case UNMAP:
  3174. size = get_unaligned_be16(&cdb[7]);
  3175. passthrough = (TRANSPORT(dev)->transport_type ==
  3176. TRANSPORT_PLUGIN_PHBA_PDEV);
  3177. /*
  3178. * Determine if the received UNMAP used to for direct passthrough
  3179. * into Linux/SCSI with struct request via TCM/pSCSI or we are
  3180. * signaling the use of internal transport_generic_unmap() emulation
  3181. * for UNMAP -> Linux/BLOCK disbard with TCM/IBLOCK and TCM/FILEIO
  3182. * subsystem plugin backstores.
  3183. */
  3184. if (!(passthrough))
  3185. cmd->se_cmd_flags |= SCF_EMULATE_SYNC_UNMAP;
  3186. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3187. break;
  3188. case WRITE_SAME_16:
  3189. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3190. if (sector_ret)
  3191. goto out_unsupported_cdb;
  3192. size = transport_get_size(sectors, cdb, cmd);
  3193. T_TASK(cmd)->t_task_lba = get_unaligned_be16(&cdb[2]);
  3194. passthrough = (TRANSPORT(dev)->transport_type ==
  3195. TRANSPORT_PLUGIN_PHBA_PDEV);
  3196. /*
  3197. * Determine if the received WRITE_SAME_16 is used to for direct
  3198. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  3199. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  3200. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
  3201. * TCM/FILEIO subsystem plugin backstores.
  3202. */
  3203. if (!(passthrough)) {
  3204. if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
  3205. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  3206. " bits not supported for Block Discard"
  3207. " Emulation\n");
  3208. goto out_invalid_cdb_field;
  3209. }
  3210. /*
  3211. * Currently for the emulated case we only accept
  3212. * tpws with the UNMAP=1 bit set.
  3213. */
  3214. if (!(cdb[1] & 0x08)) {
  3215. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
  3216. " supported for Block Discard Emulation\n");
  3217. goto out_invalid_cdb_field;
  3218. }
  3219. }
  3220. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3221. break;
  3222. case ALLOW_MEDIUM_REMOVAL:
  3223. case GPCMD_CLOSE_TRACK:
  3224. case ERASE:
  3225. case INITIALIZE_ELEMENT_STATUS:
  3226. case GPCMD_LOAD_UNLOAD:
  3227. case REZERO_UNIT:
  3228. case SEEK_10:
  3229. case GPCMD_SET_SPEED:
  3230. case SPACE:
  3231. case START_STOP:
  3232. case TEST_UNIT_READY:
  3233. case VERIFY:
  3234. case WRITE_FILEMARKS:
  3235. case MOVE_MEDIUM:
  3236. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3237. break;
  3238. case REPORT_LUNS:
  3239. cmd->transport_emulate_cdb =
  3240. &transport_core_report_lun_response;
  3241. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  3242. /*
  3243. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  3244. * See spc4r17 section 5.3
  3245. */
  3246. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3247. cmd->sam_task_attr = TASK_ATTR_HOQ;
  3248. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3249. break;
  3250. default:
  3251. printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
  3252. " 0x%02x, sending CHECK_CONDITION.\n",
  3253. CMD_TFO(cmd)->get_fabric_name(), cdb[0]);
  3254. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  3255. goto out_unsupported_cdb;
  3256. }
  3257. if (size != cmd->data_length) {
  3258. printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
  3259. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  3260. " 0x%02x\n", CMD_TFO(cmd)->get_fabric_name(),
  3261. cmd->data_length, size, cdb[0]);
  3262. cmd->cmd_spdtl = size;
  3263. if (cmd->data_direction == DMA_TO_DEVICE) {
  3264. printk(KERN_ERR "Rejecting underflow/overflow"
  3265. " WRITE data\n");
  3266. goto out_invalid_cdb_field;
  3267. }
  3268. /*
  3269. * Reject READ_* or WRITE_* with overflow/underflow for
  3270. * type SCF_SCSI_DATA_SG_IO_CDB.
  3271. */
  3272. if (!(ret) && (DEV_ATTRIB(dev)->block_size != 512)) {
  3273. printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
  3274. " CDB on non 512-byte sector setup subsystem"
  3275. " plugin: %s\n", TRANSPORT(dev)->name);
  3276. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  3277. goto out_invalid_cdb_field;
  3278. }
  3279. if (size > cmd->data_length) {
  3280. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  3281. cmd->residual_count = (size - cmd->data_length);
  3282. } else {
  3283. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  3284. cmd->residual_count = (cmd->data_length - size);
  3285. }
  3286. cmd->data_length = size;
  3287. }
  3288. transport_set_supported_SAM_opcode(cmd);
  3289. return ret;
  3290. out_unsupported_cdb:
  3291. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3292. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  3293. return -2;
  3294. out_invalid_cdb_field:
  3295. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3296. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3297. return -2;
  3298. }
  3299. static inline void transport_release_tasks(struct se_cmd *);
  3300. /*
  3301. * This function will copy a contiguous *src buffer into a destination
  3302. * struct scatterlist array.
  3303. */
  3304. static void transport_memcpy_write_contig(
  3305. struct se_cmd *cmd,
  3306. struct scatterlist *sg_d,
  3307. unsigned char *src)
  3308. {
  3309. u32 i = 0, length = 0, total_length = cmd->data_length;
  3310. void *dst;
  3311. while (total_length) {
  3312. length = sg_d[i].length;
  3313. if (length > total_length)
  3314. length = total_length;
  3315. dst = sg_virt(&sg_d[i]);
  3316. memcpy(dst, src, length);
  3317. if (!(total_length -= length))
  3318. return;
  3319. src += length;
  3320. i++;
  3321. }
  3322. }
  3323. /*
  3324. * This function will copy a struct scatterlist array *sg_s into a destination
  3325. * contiguous *dst buffer.
  3326. */
  3327. static void transport_memcpy_read_contig(
  3328. struct se_cmd *cmd,
  3329. unsigned char *dst,
  3330. struct scatterlist *sg_s)
  3331. {
  3332. u32 i = 0, length = 0, total_length = cmd->data_length;
  3333. void *src;
  3334. while (total_length) {
  3335. length = sg_s[i].length;
  3336. if (length > total_length)
  3337. length = total_length;
  3338. src = sg_virt(&sg_s[i]);
  3339. memcpy(dst, src, length);
  3340. if (!(total_length -= length))
  3341. return;
  3342. dst += length;
  3343. i++;
  3344. }
  3345. }
  3346. static void transport_memcpy_se_mem_read_contig(
  3347. struct se_cmd *cmd,
  3348. unsigned char *dst,
  3349. struct list_head *se_mem_list)
  3350. {
  3351. struct se_mem *se_mem;
  3352. void *src;
  3353. u32 length = 0, total_length = cmd->data_length;
  3354. list_for_each_entry(se_mem, se_mem_list, se_list) {
  3355. length = se_mem->se_len;
  3356. if (length > total_length)
  3357. length = total_length;
  3358. src = page_address(se_mem->se_page) + se_mem->se_off;
  3359. memcpy(dst, src, length);
  3360. if (!(total_length -= length))
  3361. return;
  3362. dst += length;
  3363. }
  3364. }
  3365. /*
  3366. * Called from transport_generic_complete_ok() and
  3367. * transport_generic_request_failure() to determine which dormant/delayed
  3368. * and ordered cmds need to have their tasks added to the execution queue.
  3369. */
  3370. static void transport_complete_task_attr(struct se_cmd *cmd)
  3371. {
  3372. struct se_device *dev = SE_DEV(cmd);
  3373. struct se_cmd *cmd_p, *cmd_tmp;
  3374. int new_active_tasks = 0;
  3375. if (cmd->sam_task_attr == TASK_ATTR_SIMPLE) {
  3376. atomic_dec(&dev->simple_cmds);
  3377. smp_mb__after_atomic_dec();
  3378. dev->dev_cur_ordered_id++;
  3379. DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
  3380. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  3381. cmd->se_ordered_id);
  3382. } else if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
  3383. atomic_dec(&dev->dev_hoq_count);
  3384. smp_mb__after_atomic_dec();
  3385. dev->dev_cur_ordered_id++;
  3386. DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
  3387. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  3388. cmd->se_ordered_id);
  3389. } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
  3390. spin_lock(&dev->ordered_cmd_lock);
  3391. list_del(&cmd->se_ordered_list);
  3392. atomic_dec(&dev->dev_ordered_sync);
  3393. smp_mb__after_atomic_dec();
  3394. spin_unlock(&dev->ordered_cmd_lock);
  3395. dev->dev_cur_ordered_id++;
  3396. DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
  3397. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  3398. }
  3399. /*
  3400. * Process all commands up to the last received
  3401. * ORDERED task attribute which requires another blocking
  3402. * boundary
  3403. */
  3404. spin_lock(&dev->delayed_cmd_lock);
  3405. list_for_each_entry_safe(cmd_p, cmd_tmp,
  3406. &dev->delayed_cmd_list, se_delayed_list) {
  3407. list_del(&cmd_p->se_delayed_list);
  3408. spin_unlock(&dev->delayed_cmd_lock);
  3409. DEBUG_STA("Calling add_tasks() for"
  3410. " cmd_p: 0x%02x Task Attr: 0x%02x"
  3411. " Dormant -> Active, se_ordered_id: %u\n",
  3412. T_TASK(cmd_p)->t_task_cdb[0],
  3413. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  3414. transport_add_tasks_from_cmd(cmd_p);
  3415. new_active_tasks++;
  3416. spin_lock(&dev->delayed_cmd_lock);
  3417. if (cmd_p->sam_task_attr == TASK_ATTR_ORDERED)
  3418. break;
  3419. }
  3420. spin_unlock(&dev->delayed_cmd_lock);
  3421. /*
  3422. * If new tasks have become active, wake up the transport thread
  3423. * to do the processing of the Active tasks.
  3424. */
  3425. if (new_active_tasks != 0)
  3426. wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
  3427. }
  3428. static void transport_generic_complete_ok(struct se_cmd *cmd)
  3429. {
  3430. int reason = 0;
  3431. /*
  3432. * Check if we need to move delayed/dormant tasks from cmds on the
  3433. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  3434. * Attribute.
  3435. */
  3436. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3437. transport_complete_task_attr(cmd);
  3438. /*
  3439. * Check if we need to retrieve a sense buffer from
  3440. * the struct se_cmd in question.
  3441. */
  3442. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  3443. if (transport_get_sense_data(cmd) < 0)
  3444. reason = TCM_NON_EXISTENT_LUN;
  3445. /*
  3446. * Only set when an struct se_task->task_scsi_status returned
  3447. * a non GOOD status.
  3448. */
  3449. if (cmd->scsi_status) {
  3450. transport_send_check_condition_and_sense(
  3451. cmd, reason, 1);
  3452. transport_lun_remove_cmd(cmd);
  3453. transport_cmd_check_stop_to_fabric(cmd);
  3454. return;
  3455. }
  3456. }
  3457. /*
  3458. * Check for a callback, used by amoungst other things
  3459. * XDWRITE_READ_10 emulation.
  3460. */
  3461. if (cmd->transport_complete_callback)
  3462. cmd->transport_complete_callback(cmd);
  3463. switch (cmd->data_direction) {
  3464. case DMA_FROM_DEVICE:
  3465. spin_lock(&cmd->se_lun->lun_sep_lock);
  3466. if (SE_LUN(cmd)->lun_sep) {
  3467. SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
  3468. cmd->data_length;
  3469. }
  3470. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3471. /*
  3472. * If enabled by TCM fabirc module pre-registered SGL
  3473. * memory, perform the memcpy() from the TCM internal
  3474. * contigious buffer back to the original SGL.
  3475. */
  3476. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
  3477. transport_memcpy_write_contig(cmd,
  3478. T_TASK(cmd)->t_task_pt_sgl,
  3479. T_TASK(cmd)->t_task_buf);
  3480. CMD_TFO(cmd)->queue_data_in(cmd);
  3481. break;
  3482. case DMA_TO_DEVICE:
  3483. spin_lock(&cmd->se_lun->lun_sep_lock);
  3484. if (SE_LUN(cmd)->lun_sep) {
  3485. SE_LUN(cmd)->lun_sep->sep_stats.rx_data_octets +=
  3486. cmd->data_length;
  3487. }
  3488. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3489. /*
  3490. * Check if we need to send READ payload for BIDI-COMMAND
  3491. */
  3492. if (T_TASK(cmd)->t_mem_bidi_list != NULL) {
  3493. spin_lock(&cmd->se_lun->lun_sep_lock);
  3494. if (SE_LUN(cmd)->lun_sep) {
  3495. SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
  3496. cmd->data_length;
  3497. }
  3498. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3499. CMD_TFO(cmd)->queue_data_in(cmd);
  3500. break;
  3501. }
  3502. /* Fall through for DMA_TO_DEVICE */
  3503. case DMA_NONE:
  3504. CMD_TFO(cmd)->queue_status(cmd);
  3505. break;
  3506. default:
  3507. break;
  3508. }
  3509. transport_lun_remove_cmd(cmd);
  3510. transport_cmd_check_stop_to_fabric(cmd);
  3511. }
  3512. static void transport_free_dev_tasks(struct se_cmd *cmd)
  3513. {
  3514. struct se_task *task, *task_tmp;
  3515. unsigned long flags;
  3516. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3517. list_for_each_entry_safe(task, task_tmp,
  3518. &T_TASK(cmd)->t_task_list, t_list) {
  3519. if (atomic_read(&task->task_active))
  3520. continue;
  3521. kfree(task->task_sg_bidi);
  3522. kfree(task->task_sg);
  3523. list_del(&task->t_list);
  3524. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3525. if (task->se_dev)
  3526. TRANSPORT(task->se_dev)->free_task(task);
  3527. else
  3528. printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
  3529. task->task_no);
  3530. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3531. }
  3532. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3533. }
  3534. static inline void transport_free_pages(struct se_cmd *cmd)
  3535. {
  3536. struct se_mem *se_mem, *se_mem_tmp;
  3537. int free_page = 1;
  3538. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3539. free_page = 0;
  3540. if (cmd->se_dev->transport->do_se_mem_map)
  3541. free_page = 0;
  3542. if (T_TASK(cmd)->t_task_buf) {
  3543. kfree(T_TASK(cmd)->t_task_buf);
  3544. T_TASK(cmd)->t_task_buf = NULL;
  3545. return;
  3546. }
  3547. /*
  3548. * Caller will handle releasing of struct se_mem.
  3549. */
  3550. if (cmd->se_cmd_flags & SCF_CMD_PASSTHROUGH_NOALLOC)
  3551. return;
  3552. if (!(T_TASK(cmd)->t_tasks_se_num))
  3553. return;
  3554. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3555. T_TASK(cmd)->t_mem_list, se_list) {
  3556. /*
  3557. * We only release call __free_page(struct se_mem->se_page) when
  3558. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3559. */
  3560. if (free_page)
  3561. __free_page(se_mem->se_page);
  3562. list_del(&se_mem->se_list);
  3563. kmem_cache_free(se_mem_cache, se_mem);
  3564. }
  3565. if (T_TASK(cmd)->t_mem_bidi_list && T_TASK(cmd)->t_tasks_se_bidi_num) {
  3566. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3567. T_TASK(cmd)->t_mem_bidi_list, se_list) {
  3568. /*
  3569. * We only release call __free_page(struct se_mem->se_page) when
  3570. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3571. */
  3572. if (free_page)
  3573. __free_page(se_mem->se_page);
  3574. list_del(&se_mem->se_list);
  3575. kmem_cache_free(se_mem_cache, se_mem);
  3576. }
  3577. }
  3578. kfree(T_TASK(cmd)->t_mem_bidi_list);
  3579. T_TASK(cmd)->t_mem_bidi_list = NULL;
  3580. kfree(T_TASK(cmd)->t_mem_list);
  3581. T_TASK(cmd)->t_mem_list = NULL;
  3582. T_TASK(cmd)->t_tasks_se_num = 0;
  3583. }
  3584. static inline void transport_release_tasks(struct se_cmd *cmd)
  3585. {
  3586. transport_free_dev_tasks(cmd);
  3587. }
  3588. static inline int transport_dec_and_check(struct se_cmd *cmd)
  3589. {
  3590. unsigned long flags;
  3591. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3592. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  3593. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_fe_count))) {
  3594. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3595. flags);
  3596. return 1;
  3597. }
  3598. }
  3599. if (atomic_read(&T_TASK(cmd)->t_se_count)) {
  3600. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_se_count))) {
  3601. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3602. flags);
  3603. return 1;
  3604. }
  3605. }
  3606. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3607. return 0;
  3608. }
  3609. static void transport_release_fe_cmd(struct se_cmd *cmd)
  3610. {
  3611. unsigned long flags;
  3612. if (transport_dec_and_check(cmd))
  3613. return;
  3614. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3615. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  3616. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3617. goto free_pages;
  3618. }
  3619. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  3620. transport_all_task_dev_remove_state(cmd);
  3621. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3622. transport_release_tasks(cmd);
  3623. free_pages:
  3624. transport_free_pages(cmd);
  3625. transport_free_se_cmd(cmd);
  3626. CMD_TFO(cmd)->release_cmd_direct(cmd);
  3627. }
  3628. static int transport_generic_remove(
  3629. struct se_cmd *cmd,
  3630. int release_to_pool,
  3631. int session_reinstatement)
  3632. {
  3633. unsigned long flags;
  3634. if (!(T_TASK(cmd)))
  3635. goto release_cmd;
  3636. if (transport_dec_and_check(cmd)) {
  3637. if (session_reinstatement) {
  3638. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3639. transport_all_task_dev_remove_state(cmd);
  3640. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3641. flags);
  3642. }
  3643. return 1;
  3644. }
  3645. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3646. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  3647. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3648. goto free_pages;
  3649. }
  3650. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  3651. transport_all_task_dev_remove_state(cmd);
  3652. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3653. transport_release_tasks(cmd);
  3654. free_pages:
  3655. transport_free_pages(cmd);
  3656. release_cmd:
  3657. if (release_to_pool) {
  3658. transport_release_cmd_to_pool(cmd);
  3659. } else {
  3660. transport_free_se_cmd(cmd);
  3661. CMD_TFO(cmd)->release_cmd_direct(cmd);
  3662. }
  3663. return 0;
  3664. }
  3665. /*
  3666. * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
  3667. * @cmd: Associated se_cmd descriptor
  3668. * @mem: SGL style memory for TCM WRITE / READ
  3669. * @sg_mem_num: Number of SGL elements
  3670. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3671. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3672. *
  3673. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3674. * of parameters.
  3675. */
  3676. int transport_generic_map_mem_to_cmd(
  3677. struct se_cmd *cmd,
  3678. struct scatterlist *mem,
  3679. u32 sg_mem_num,
  3680. struct scatterlist *mem_bidi_in,
  3681. u32 sg_mem_bidi_num)
  3682. {
  3683. u32 se_mem_cnt_out = 0;
  3684. int ret;
  3685. if (!(mem) || !(sg_mem_num))
  3686. return 0;
  3687. /*
  3688. * Passed *mem will contain a list_head containing preformatted
  3689. * struct se_mem elements...
  3690. */
  3691. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM)) {
  3692. if ((mem_bidi_in) || (sg_mem_bidi_num)) {
  3693. printk(KERN_ERR "SCF_CMD_PASSTHROUGH_NOALLOC not supported"
  3694. " with BIDI-COMMAND\n");
  3695. return -ENOSYS;
  3696. }
  3697. T_TASK(cmd)->t_mem_list = (struct list_head *)mem;
  3698. T_TASK(cmd)->t_tasks_se_num = sg_mem_num;
  3699. cmd->se_cmd_flags |= SCF_CMD_PASSTHROUGH_NOALLOC;
  3700. return 0;
  3701. }
  3702. /*
  3703. * Otherwise, assume the caller is passing a struct scatterlist
  3704. * array from include/linux/scatterlist.h
  3705. */
  3706. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3707. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3708. /*
  3709. * For CDB using TCM struct se_mem linked list scatterlist memory
  3710. * processed into a TCM struct se_subsystem_dev, we do the mapping
  3711. * from the passed physical memory to struct se_mem->se_page here.
  3712. */
  3713. T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
  3714. if (!(T_TASK(cmd)->t_mem_list))
  3715. return -ENOMEM;
  3716. ret = transport_map_sg_to_mem(cmd,
  3717. T_TASK(cmd)->t_mem_list, mem, &se_mem_cnt_out);
  3718. if (ret < 0)
  3719. return -ENOMEM;
  3720. T_TASK(cmd)->t_tasks_se_num = se_mem_cnt_out;
  3721. /*
  3722. * Setup BIDI READ list of struct se_mem elements
  3723. */
  3724. if ((mem_bidi_in) && (sg_mem_bidi_num)) {
  3725. T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
  3726. if (!(T_TASK(cmd)->t_mem_bidi_list)) {
  3727. kfree(T_TASK(cmd)->t_mem_list);
  3728. return -ENOMEM;
  3729. }
  3730. se_mem_cnt_out = 0;
  3731. ret = transport_map_sg_to_mem(cmd,
  3732. T_TASK(cmd)->t_mem_bidi_list, mem_bidi_in,
  3733. &se_mem_cnt_out);
  3734. if (ret < 0) {
  3735. kfree(T_TASK(cmd)->t_mem_list);
  3736. return -ENOMEM;
  3737. }
  3738. T_TASK(cmd)->t_tasks_se_bidi_num = se_mem_cnt_out;
  3739. }
  3740. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3741. } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
  3742. if (mem_bidi_in || sg_mem_bidi_num) {
  3743. printk(KERN_ERR "BIDI-Commands not supported using "
  3744. "SCF_SCSI_CONTROL_NONSG_IO_CDB\n");
  3745. return -ENOSYS;
  3746. }
  3747. /*
  3748. * For incoming CDBs using a contiguous buffer internall with TCM,
  3749. * save the passed struct scatterlist memory. After TCM storage object
  3750. * processing has completed for this struct se_cmd, TCM core will call
  3751. * transport_memcpy_[write,read]_contig() as necessary from
  3752. * transport_generic_complete_ok() and transport_write_pending() in order
  3753. * to copy the TCM buffer to/from the original passed *mem in SGL ->
  3754. * struct scatterlist format.
  3755. */
  3756. cmd->se_cmd_flags |= SCF_PASSTHROUGH_CONTIG_TO_SG;
  3757. T_TASK(cmd)->t_task_pt_sgl = mem;
  3758. }
  3759. return 0;
  3760. }
  3761. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3762. static inline long long transport_dev_end_lba(struct se_device *dev)
  3763. {
  3764. return dev->transport->get_blocks(dev) + 1;
  3765. }
  3766. static int transport_get_sectors(struct se_cmd *cmd)
  3767. {
  3768. struct se_device *dev = SE_DEV(cmd);
  3769. T_TASK(cmd)->t_tasks_sectors =
  3770. (cmd->data_length / DEV_ATTRIB(dev)->block_size);
  3771. if (!(T_TASK(cmd)->t_tasks_sectors))
  3772. T_TASK(cmd)->t_tasks_sectors = 1;
  3773. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_DISK)
  3774. return 0;
  3775. if ((T_TASK(cmd)->t_task_lba + T_TASK(cmd)->t_tasks_sectors) >
  3776. transport_dev_end_lba(dev)) {
  3777. printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
  3778. " transport_dev_end_lba(): %llu\n",
  3779. T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
  3780. transport_dev_end_lba(dev));
  3781. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3782. cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
  3783. return PYX_TRANSPORT_REQ_TOO_MANY_SECTORS;
  3784. }
  3785. return 0;
  3786. }
  3787. static int transport_new_cmd_obj(struct se_cmd *cmd)
  3788. {
  3789. struct se_device *dev = SE_DEV(cmd);
  3790. u32 task_cdbs = 0, rc;
  3791. if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
  3792. task_cdbs++;
  3793. T_TASK(cmd)->t_task_cdbs++;
  3794. } else {
  3795. int set_counts = 1;
  3796. /*
  3797. * Setup any BIDI READ tasks and memory from
  3798. * T_TASK(cmd)->t_mem_bidi_list so the READ struct se_tasks
  3799. * are queued first for the non pSCSI passthrough case.
  3800. */
  3801. if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
  3802. (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3803. rc = transport_generic_get_cdb_count(cmd,
  3804. T_TASK(cmd)->t_task_lba,
  3805. T_TASK(cmd)->t_tasks_sectors,
  3806. DMA_FROM_DEVICE, T_TASK(cmd)->t_mem_bidi_list,
  3807. set_counts);
  3808. if (!(rc)) {
  3809. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3810. cmd->scsi_sense_reason =
  3811. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3812. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3813. }
  3814. set_counts = 0;
  3815. }
  3816. /*
  3817. * Setup the tasks and memory from T_TASK(cmd)->t_mem_list
  3818. * Note for BIDI transfers this will contain the WRITE payload
  3819. */
  3820. task_cdbs = transport_generic_get_cdb_count(cmd,
  3821. T_TASK(cmd)->t_task_lba,
  3822. T_TASK(cmd)->t_tasks_sectors,
  3823. cmd->data_direction, T_TASK(cmd)->t_mem_list,
  3824. set_counts);
  3825. if (!(task_cdbs)) {
  3826. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3827. cmd->scsi_sense_reason =
  3828. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3829. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3830. }
  3831. T_TASK(cmd)->t_task_cdbs += task_cdbs;
  3832. #if 0
  3833. printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
  3834. " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
  3835. T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
  3836. T_TASK(cmd)->t_task_cdbs);
  3837. #endif
  3838. }
  3839. atomic_set(&T_TASK(cmd)->t_task_cdbs_left, task_cdbs);
  3840. atomic_set(&T_TASK(cmd)->t_task_cdbs_ex_left, task_cdbs);
  3841. atomic_set(&T_TASK(cmd)->t_task_cdbs_timeout_left, task_cdbs);
  3842. return 0;
  3843. }
  3844. static struct list_head *transport_init_se_mem_list(void)
  3845. {
  3846. struct list_head *se_mem_list;
  3847. se_mem_list = kzalloc(sizeof(struct list_head), GFP_KERNEL);
  3848. if (!(se_mem_list)) {
  3849. printk(KERN_ERR "Unable to allocate memory for se_mem_list\n");
  3850. return NULL;
  3851. }
  3852. INIT_LIST_HEAD(se_mem_list);
  3853. return se_mem_list;
  3854. }
  3855. static int
  3856. transport_generic_get_mem(struct se_cmd *cmd, u32 length, u32 dma_size)
  3857. {
  3858. unsigned char *buf;
  3859. struct se_mem *se_mem;
  3860. T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
  3861. if (!(T_TASK(cmd)->t_mem_list))
  3862. return -ENOMEM;
  3863. /*
  3864. * If the device uses memory mapping this is enough.
  3865. */
  3866. if (cmd->se_dev->transport->do_se_mem_map)
  3867. return 0;
  3868. /*
  3869. * Setup BIDI-COMMAND READ list of struct se_mem elements
  3870. */
  3871. if (T_TASK(cmd)->t_tasks_bidi) {
  3872. T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
  3873. if (!(T_TASK(cmd)->t_mem_bidi_list)) {
  3874. kfree(T_TASK(cmd)->t_mem_list);
  3875. return -ENOMEM;
  3876. }
  3877. }
  3878. while (length) {
  3879. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  3880. if (!(se_mem)) {
  3881. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  3882. goto out;
  3883. }
  3884. INIT_LIST_HEAD(&se_mem->se_list);
  3885. se_mem->se_len = (length > dma_size) ? dma_size : length;
  3886. /* #warning FIXME Allocate contigous pages for struct se_mem elements */
  3887. se_mem->se_page = (struct page *) alloc_pages(GFP_KERNEL, 0);
  3888. if (!(se_mem->se_page)) {
  3889. printk(KERN_ERR "alloc_pages() failed\n");
  3890. goto out;
  3891. }
  3892. buf = kmap_atomic(se_mem->se_page, KM_IRQ0);
  3893. if (!(buf)) {
  3894. printk(KERN_ERR "kmap_atomic() failed\n");
  3895. goto out;
  3896. }
  3897. memset(buf, 0, se_mem->se_len);
  3898. kunmap_atomic(buf, KM_IRQ0);
  3899. list_add_tail(&se_mem->se_list, T_TASK(cmd)->t_mem_list);
  3900. T_TASK(cmd)->t_tasks_se_num++;
  3901. DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
  3902. " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
  3903. se_mem->se_off);
  3904. length -= se_mem->se_len;
  3905. }
  3906. DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
  3907. T_TASK(cmd)->t_tasks_se_num);
  3908. return 0;
  3909. out:
  3910. return -1;
  3911. }
  3912. extern u32 transport_calc_sg_num(
  3913. struct se_task *task,
  3914. struct se_mem *in_se_mem,
  3915. u32 task_offset)
  3916. {
  3917. struct se_cmd *se_cmd = task->task_se_cmd;
  3918. struct se_device *se_dev = SE_DEV(se_cmd);
  3919. struct se_mem *se_mem = in_se_mem;
  3920. struct target_core_fabric_ops *tfo = CMD_TFO(se_cmd);
  3921. u32 sg_length, task_size = task->task_size, task_sg_num_padded;
  3922. while (task_size != 0) {
  3923. DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
  3924. " se_mem->se_off(%u) task_offset(%u)\n",
  3925. se_mem->se_page, se_mem->se_len,
  3926. se_mem->se_off, task_offset);
  3927. if (task_offset == 0) {
  3928. if (task_size >= se_mem->se_len) {
  3929. sg_length = se_mem->se_len;
  3930. if (!(list_is_last(&se_mem->se_list,
  3931. T_TASK(se_cmd)->t_mem_list)))
  3932. se_mem = list_entry(se_mem->se_list.next,
  3933. struct se_mem, se_list);
  3934. } else {
  3935. sg_length = task_size;
  3936. task_size -= sg_length;
  3937. goto next;
  3938. }
  3939. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3940. sg_length, task_size);
  3941. } else {
  3942. if ((se_mem->se_len - task_offset) > task_size) {
  3943. sg_length = task_size;
  3944. task_size -= sg_length;
  3945. goto next;
  3946. } else {
  3947. sg_length = (se_mem->se_len - task_offset);
  3948. if (!(list_is_last(&se_mem->se_list,
  3949. T_TASK(se_cmd)->t_mem_list)))
  3950. se_mem = list_entry(se_mem->se_list.next,
  3951. struct se_mem, se_list);
  3952. }
  3953. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3954. sg_length, task_size);
  3955. task_offset = 0;
  3956. }
  3957. task_size -= sg_length;
  3958. next:
  3959. DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
  3960. task->task_no, task_size);
  3961. task->task_sg_num++;
  3962. }
  3963. /*
  3964. * Check if the fabric module driver is requesting that all
  3965. * struct se_task->task_sg[] be chained together.. If so,
  3966. * then allocate an extra padding SG entry for linking and
  3967. * marking the end of the chained SGL.
  3968. */
  3969. if (tfo->task_sg_chaining) {
  3970. task_sg_num_padded = (task->task_sg_num + 1);
  3971. task->task_padded_sg = 1;
  3972. } else
  3973. task_sg_num_padded = task->task_sg_num;
  3974. task->task_sg = kzalloc(task_sg_num_padded *
  3975. sizeof(struct scatterlist), GFP_KERNEL);
  3976. if (!(task->task_sg)) {
  3977. printk(KERN_ERR "Unable to allocate memory for"
  3978. " task->task_sg\n");
  3979. return 0;
  3980. }
  3981. sg_init_table(&task->task_sg[0], task_sg_num_padded);
  3982. /*
  3983. * Setup task->task_sg_bidi for SCSI READ payload for
  3984. * TCM/pSCSI passthrough if present for BIDI-COMMAND
  3985. */
  3986. if ((T_TASK(se_cmd)->t_mem_bidi_list != NULL) &&
  3987. (TRANSPORT(se_dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3988. task->task_sg_bidi = kzalloc(task_sg_num_padded *
  3989. sizeof(struct scatterlist), GFP_KERNEL);
  3990. if (!(task->task_sg_bidi)) {
  3991. printk(KERN_ERR "Unable to allocate memory for"
  3992. " task->task_sg_bidi\n");
  3993. return 0;
  3994. }
  3995. sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
  3996. }
  3997. /*
  3998. * For the chaining case, setup the proper end of SGL for the
  3999. * initial submission struct task into struct se_subsystem_api.
  4000. * This will be cleared later by transport_do_task_sg_chain()
  4001. */
  4002. if (task->task_padded_sg) {
  4003. sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
  4004. /*
  4005. * Added the 'if' check before marking end of bi-directional
  4006. * scatterlist (which gets created only in case of request
  4007. * (RD + WR).
  4008. */
  4009. if (task->task_sg_bidi)
  4010. sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
  4011. }
  4012. DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
  4013. " task_sg_num_padded(%u)\n", task->task_sg_num,
  4014. task_sg_num_padded);
  4015. return task->task_sg_num;
  4016. }
  4017. static inline int transport_set_tasks_sectors_disk(
  4018. struct se_task *task,
  4019. struct se_device *dev,
  4020. unsigned long long lba,
  4021. u32 sectors,
  4022. int *max_sectors_set)
  4023. {
  4024. if ((lba + sectors) > transport_dev_end_lba(dev)) {
  4025. task->task_sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  4026. if (task->task_sectors > DEV_ATTRIB(dev)->max_sectors) {
  4027. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4028. *max_sectors_set = 1;
  4029. }
  4030. } else {
  4031. if (sectors > DEV_ATTRIB(dev)->max_sectors) {
  4032. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4033. *max_sectors_set = 1;
  4034. } else
  4035. task->task_sectors = sectors;
  4036. }
  4037. return 0;
  4038. }
  4039. static inline int transport_set_tasks_sectors_non_disk(
  4040. struct se_task *task,
  4041. struct se_device *dev,
  4042. unsigned long long lba,
  4043. u32 sectors,
  4044. int *max_sectors_set)
  4045. {
  4046. if (sectors > DEV_ATTRIB(dev)->max_sectors) {
  4047. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4048. *max_sectors_set = 1;
  4049. } else
  4050. task->task_sectors = sectors;
  4051. return 0;
  4052. }
  4053. static inline int transport_set_tasks_sectors(
  4054. struct se_task *task,
  4055. struct se_device *dev,
  4056. unsigned long long lba,
  4057. u32 sectors,
  4058. int *max_sectors_set)
  4059. {
  4060. return (TRANSPORT(dev)->get_device_type(dev) == TYPE_DISK) ?
  4061. transport_set_tasks_sectors_disk(task, dev, lba, sectors,
  4062. max_sectors_set) :
  4063. transport_set_tasks_sectors_non_disk(task, dev, lba, sectors,
  4064. max_sectors_set);
  4065. }
  4066. static int transport_map_sg_to_mem(
  4067. struct se_cmd *cmd,
  4068. struct list_head *se_mem_list,
  4069. void *in_mem,
  4070. u32 *se_mem_cnt)
  4071. {
  4072. struct se_mem *se_mem;
  4073. struct scatterlist *sg;
  4074. u32 sg_count = 1, cmd_size = cmd->data_length;
  4075. if (!in_mem) {
  4076. printk(KERN_ERR "No source scatterlist\n");
  4077. return -1;
  4078. }
  4079. sg = (struct scatterlist *)in_mem;
  4080. while (cmd_size) {
  4081. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  4082. if (!(se_mem)) {
  4083. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  4084. return -1;
  4085. }
  4086. INIT_LIST_HEAD(&se_mem->se_list);
  4087. DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
  4088. " sg_page: %p offset: %d length: %d\n", cmd_size,
  4089. sg_page(sg), sg->offset, sg->length);
  4090. se_mem->se_page = sg_page(sg);
  4091. se_mem->se_off = sg->offset;
  4092. if (cmd_size > sg->length) {
  4093. se_mem->se_len = sg->length;
  4094. sg = sg_next(sg);
  4095. sg_count++;
  4096. } else
  4097. se_mem->se_len = cmd_size;
  4098. cmd_size -= se_mem->se_len;
  4099. DEBUG_MEM("sg_to_mem: *se_mem_cnt: %u cmd_size: %u\n",
  4100. *se_mem_cnt, cmd_size);
  4101. DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
  4102. se_mem->se_page, se_mem->se_off, se_mem->se_len);
  4103. list_add_tail(&se_mem->se_list, se_mem_list);
  4104. (*se_mem_cnt)++;
  4105. }
  4106. DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments to(%u)"
  4107. " struct se_mem\n", sg_count, *se_mem_cnt);
  4108. if (sg_count != *se_mem_cnt)
  4109. BUG();
  4110. return 0;
  4111. }
  4112. /* transport_map_mem_to_sg():
  4113. *
  4114. *
  4115. */
  4116. int transport_map_mem_to_sg(
  4117. struct se_task *task,
  4118. struct list_head *se_mem_list,
  4119. void *in_mem,
  4120. struct se_mem *in_se_mem,
  4121. struct se_mem **out_se_mem,
  4122. u32 *se_mem_cnt,
  4123. u32 *task_offset)
  4124. {
  4125. struct se_cmd *se_cmd = task->task_se_cmd;
  4126. struct se_mem *se_mem = in_se_mem;
  4127. struct scatterlist *sg = (struct scatterlist *)in_mem;
  4128. u32 task_size = task->task_size, sg_no = 0;
  4129. if (!sg) {
  4130. printk(KERN_ERR "Unable to locate valid struct"
  4131. " scatterlist pointer\n");
  4132. return -1;
  4133. }
  4134. while (task_size != 0) {
  4135. /*
  4136. * Setup the contigious array of scatterlists for
  4137. * this struct se_task.
  4138. */
  4139. sg_assign_page(sg, se_mem->se_page);
  4140. if (*task_offset == 0) {
  4141. sg->offset = se_mem->se_off;
  4142. if (task_size >= se_mem->se_len) {
  4143. sg->length = se_mem->se_len;
  4144. if (!(list_is_last(&se_mem->se_list,
  4145. T_TASK(se_cmd)->t_mem_list))) {
  4146. se_mem = list_entry(se_mem->se_list.next,
  4147. struct se_mem, se_list);
  4148. (*se_mem_cnt)++;
  4149. }
  4150. } else {
  4151. sg->length = task_size;
  4152. /*
  4153. * Determine if we need to calculate an offset
  4154. * into the struct se_mem on the next go around..
  4155. */
  4156. task_size -= sg->length;
  4157. if (!(task_size))
  4158. *task_offset = sg->length;
  4159. goto next;
  4160. }
  4161. } else {
  4162. sg->offset = (*task_offset + se_mem->se_off);
  4163. if ((se_mem->se_len - *task_offset) > task_size) {
  4164. sg->length = task_size;
  4165. /*
  4166. * Determine if we need to calculate an offset
  4167. * into the struct se_mem on the next go around..
  4168. */
  4169. task_size -= sg->length;
  4170. if (!(task_size))
  4171. *task_offset += sg->length;
  4172. goto next;
  4173. } else {
  4174. sg->length = (se_mem->se_len - *task_offset);
  4175. if (!(list_is_last(&se_mem->se_list,
  4176. T_TASK(se_cmd)->t_mem_list))) {
  4177. se_mem = list_entry(se_mem->se_list.next,
  4178. struct se_mem, se_list);
  4179. (*se_mem_cnt)++;
  4180. }
  4181. }
  4182. *task_offset = 0;
  4183. }
  4184. task_size -= sg->length;
  4185. next:
  4186. DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
  4187. " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
  4188. sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
  4189. sg_no++;
  4190. if (!(task_size))
  4191. break;
  4192. sg = sg_next(sg);
  4193. if (task_size > se_cmd->data_length)
  4194. BUG();
  4195. }
  4196. *out_se_mem = se_mem;
  4197. DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
  4198. " SGs\n", task->task_no, *se_mem_cnt, sg_no);
  4199. return 0;
  4200. }
  4201. /*
  4202. * This function can be used by HW target mode drivers to create a linked
  4203. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  4204. * This is intended to be called during the completion path by TCM Core
  4205. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  4206. */
  4207. void transport_do_task_sg_chain(struct se_cmd *cmd)
  4208. {
  4209. struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
  4210. struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
  4211. struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
  4212. struct se_task *task;
  4213. struct target_core_fabric_ops *tfo = CMD_TFO(cmd);
  4214. u32 task_sg_num = 0, sg_count = 0;
  4215. int i;
  4216. if (tfo->task_sg_chaining == 0) {
  4217. printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
  4218. " %s\n", tfo->get_fabric_name());
  4219. dump_stack();
  4220. return;
  4221. }
  4222. /*
  4223. * Walk the struct se_task list and setup scatterlist chains
  4224. * for each contiguosly allocated struct se_task->task_sg[].
  4225. */
  4226. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  4227. if (!(task->task_sg) || !(task->task_padded_sg))
  4228. continue;
  4229. if (sg_head && sg_link) {
  4230. sg_head_cur = &task->task_sg[0];
  4231. sg_link_cur = &task->task_sg[task->task_sg_num];
  4232. /*
  4233. * Either add chain or mark end of scatterlist
  4234. */
  4235. if (!(list_is_last(&task->t_list,
  4236. &T_TASK(cmd)->t_task_list))) {
  4237. /*
  4238. * Clear existing SGL termination bit set in
  4239. * transport_calc_sg_num(), see sg_mark_end()
  4240. */
  4241. sg_end_cur = &task->task_sg[task->task_sg_num - 1];
  4242. sg_end_cur->page_link &= ~0x02;
  4243. sg_chain(sg_head, task_sg_num, sg_head_cur);
  4244. sg_count += (task->task_sg_num + 1);
  4245. } else
  4246. sg_count += task->task_sg_num;
  4247. sg_head = sg_head_cur;
  4248. sg_link = sg_link_cur;
  4249. task_sg_num = task->task_sg_num;
  4250. continue;
  4251. }
  4252. sg_head = sg_first = &task->task_sg[0];
  4253. sg_link = &task->task_sg[task->task_sg_num];
  4254. task_sg_num = task->task_sg_num;
  4255. /*
  4256. * Check for single task..
  4257. */
  4258. if (!(list_is_last(&task->t_list, &T_TASK(cmd)->t_task_list))) {
  4259. /*
  4260. * Clear existing SGL termination bit set in
  4261. * transport_calc_sg_num(), see sg_mark_end()
  4262. */
  4263. sg_end = &task->task_sg[task->task_sg_num - 1];
  4264. sg_end->page_link &= ~0x02;
  4265. sg_count += (task->task_sg_num + 1);
  4266. } else
  4267. sg_count += task->task_sg_num;
  4268. }
  4269. /*
  4270. * Setup the starting pointer and total t_tasks_sg_linked_no including
  4271. * padding SGs for linking and to mark the end.
  4272. */
  4273. T_TASK(cmd)->t_tasks_sg_chained = sg_first;
  4274. T_TASK(cmd)->t_tasks_sg_chained_no = sg_count;
  4275. DEBUG_CMD_M("Setup T_TASK(cmd)->t_tasks_sg_chained: %p and"
  4276. " t_tasks_sg_chained_no: %u\n", T_TASK(cmd)->t_tasks_sg_chained,
  4277. T_TASK(cmd)->t_tasks_sg_chained_no);
  4278. for_each_sg(T_TASK(cmd)->t_tasks_sg_chained, sg,
  4279. T_TASK(cmd)->t_tasks_sg_chained_no, i) {
  4280. DEBUG_CMD_M("SG: %p page: %p length: %d offset: %d\n",
  4281. sg, sg_page(sg), sg->length, sg->offset);
  4282. if (sg_is_chain(sg))
  4283. DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
  4284. if (sg_is_last(sg))
  4285. DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
  4286. }
  4287. }
  4288. EXPORT_SYMBOL(transport_do_task_sg_chain);
  4289. static int transport_do_se_mem_map(
  4290. struct se_device *dev,
  4291. struct se_task *task,
  4292. struct list_head *se_mem_list,
  4293. void *in_mem,
  4294. struct se_mem *in_se_mem,
  4295. struct se_mem **out_se_mem,
  4296. u32 *se_mem_cnt,
  4297. u32 *task_offset_in)
  4298. {
  4299. u32 task_offset = *task_offset_in;
  4300. int ret = 0;
  4301. /*
  4302. * se_subsystem_api_t->do_se_mem_map is used when internal allocation
  4303. * has been done by the transport plugin.
  4304. */
  4305. if (TRANSPORT(dev)->do_se_mem_map) {
  4306. ret = TRANSPORT(dev)->do_se_mem_map(task, se_mem_list,
  4307. in_mem, in_se_mem, out_se_mem, se_mem_cnt,
  4308. task_offset_in);
  4309. if (ret == 0)
  4310. T_TASK(task->task_se_cmd)->t_tasks_se_num += *se_mem_cnt;
  4311. return ret;
  4312. }
  4313. BUG_ON(list_empty(se_mem_list));
  4314. /*
  4315. * This is the normal path for all normal non BIDI and BIDI-COMMAND
  4316. * WRITE payloads.. If we need to do BIDI READ passthrough for
  4317. * TCM/pSCSI the first call to transport_do_se_mem_map ->
  4318. * transport_calc_sg_num() -> transport_map_mem_to_sg() will do the
  4319. * allocation for task->task_sg_bidi, and the subsequent call to
  4320. * transport_do_se_mem_map() from transport_generic_get_cdb_count()
  4321. */
  4322. if (!(task->task_sg_bidi)) {
  4323. /*
  4324. * Assume default that transport plugin speaks preallocated
  4325. * scatterlists.
  4326. */
  4327. if (!(transport_calc_sg_num(task, in_se_mem, task_offset)))
  4328. return -1;
  4329. /*
  4330. * struct se_task->task_sg now contains the struct scatterlist array.
  4331. */
  4332. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
  4333. in_se_mem, out_se_mem, se_mem_cnt,
  4334. task_offset_in);
  4335. }
  4336. /*
  4337. * Handle the se_mem_list -> struct task->task_sg_bidi
  4338. * memory map for the extra BIDI READ payload
  4339. */
  4340. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
  4341. in_se_mem, out_se_mem, se_mem_cnt,
  4342. task_offset_in);
  4343. }
  4344. static u32 transport_generic_get_cdb_count(
  4345. struct se_cmd *cmd,
  4346. unsigned long long lba,
  4347. u32 sectors,
  4348. enum dma_data_direction data_direction,
  4349. struct list_head *mem_list,
  4350. int set_counts)
  4351. {
  4352. unsigned char *cdb = NULL;
  4353. struct se_task *task;
  4354. struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
  4355. struct se_mem *se_mem_bidi = NULL, *se_mem_bidi_lout = NULL;
  4356. struct se_device *dev = SE_DEV(cmd);
  4357. int max_sectors_set = 0, ret;
  4358. u32 task_offset_in = 0, se_mem_cnt = 0, se_mem_bidi_cnt = 0, task_cdbs = 0;
  4359. if (!mem_list) {
  4360. printk(KERN_ERR "mem_list is NULL in transport_generic_get"
  4361. "_cdb_count()\n");
  4362. return 0;
  4363. }
  4364. /*
  4365. * While using RAMDISK_DR backstores is the only case where
  4366. * mem_list will ever be empty at this point.
  4367. */
  4368. if (!(list_empty(mem_list)))
  4369. se_mem = list_entry(mem_list->next, struct se_mem, se_list);
  4370. /*
  4371. * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
  4372. * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
  4373. */
  4374. if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
  4375. !(list_empty(T_TASK(cmd)->t_mem_bidi_list)) &&
  4376. (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
  4377. se_mem_bidi = list_entry(T_TASK(cmd)->t_mem_bidi_list->next,
  4378. struct se_mem, se_list);
  4379. while (sectors) {
  4380. DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
  4381. CMD_TFO(cmd)->get_task_tag(cmd), lba, sectors,
  4382. transport_dev_end_lba(dev));
  4383. task = transport_generic_get_task(cmd, data_direction);
  4384. if (!(task))
  4385. goto out;
  4386. transport_set_tasks_sectors(task, dev, lba, sectors,
  4387. &max_sectors_set);
  4388. task->task_lba = lba;
  4389. lba += task->task_sectors;
  4390. sectors -= task->task_sectors;
  4391. task->task_size = (task->task_sectors *
  4392. DEV_ATTRIB(dev)->block_size);
  4393. cdb = TRANSPORT(dev)->get_cdb(task);
  4394. if ((cdb)) {
  4395. memcpy(cdb, T_TASK(cmd)->t_task_cdb,
  4396. scsi_command_size(T_TASK(cmd)->t_task_cdb));
  4397. cmd->transport_split_cdb(task->task_lba,
  4398. &task->task_sectors, cdb);
  4399. }
  4400. /*
  4401. * Perform the SE OBJ plugin and/or Transport plugin specific
  4402. * mapping for T_TASK(cmd)->t_mem_list. And setup the
  4403. * task->task_sg and if necessary task->task_sg_bidi
  4404. */
  4405. ret = transport_do_se_mem_map(dev, task, mem_list,
  4406. NULL, se_mem, &se_mem_lout, &se_mem_cnt,
  4407. &task_offset_in);
  4408. if (ret < 0)
  4409. goto out;
  4410. se_mem = se_mem_lout;
  4411. /*
  4412. * Setup the T_TASK(cmd)->t_mem_bidi_list -> task->task_sg_bidi
  4413. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
  4414. *
  4415. * Note that the first call to transport_do_se_mem_map() above will
  4416. * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
  4417. * -> transport_calc_sg_num(), and the second here will do the
  4418. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
  4419. */
  4420. if (task->task_sg_bidi != NULL) {
  4421. ret = transport_do_se_mem_map(dev, task,
  4422. T_TASK(cmd)->t_mem_bidi_list, NULL,
  4423. se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
  4424. &task_offset_in);
  4425. if (ret < 0)
  4426. goto out;
  4427. se_mem_bidi = se_mem_bidi_lout;
  4428. }
  4429. task_cdbs++;
  4430. DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
  4431. task_cdbs, task->task_sg_num);
  4432. if (max_sectors_set) {
  4433. max_sectors_set = 0;
  4434. continue;
  4435. }
  4436. if (!sectors)
  4437. break;
  4438. }
  4439. if (set_counts) {
  4440. atomic_inc(&T_TASK(cmd)->t_fe_count);
  4441. atomic_inc(&T_TASK(cmd)->t_se_count);
  4442. }
  4443. DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
  4444. CMD_TFO(cmd)->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
  4445. ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
  4446. return task_cdbs;
  4447. out:
  4448. return 0;
  4449. }
  4450. static int
  4451. transport_map_control_cmd_to_task(struct se_cmd *cmd)
  4452. {
  4453. struct se_device *dev = SE_DEV(cmd);
  4454. unsigned char *cdb;
  4455. struct se_task *task;
  4456. int ret;
  4457. task = transport_generic_get_task(cmd, cmd->data_direction);
  4458. if (!task)
  4459. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4460. cdb = TRANSPORT(dev)->get_cdb(task);
  4461. if (cdb)
  4462. memcpy(cdb, cmd->t_task->t_task_cdb,
  4463. scsi_command_size(cmd->t_task->t_task_cdb));
  4464. task->task_size = cmd->data_length;
  4465. task->task_sg_num =
  4466. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
  4467. atomic_inc(&cmd->t_task->t_fe_count);
  4468. atomic_inc(&cmd->t_task->t_se_count);
  4469. if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
  4470. struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
  4471. u32 se_mem_cnt = 0, task_offset = 0;
  4472. if (!list_empty(T_TASK(cmd)->t_mem_list))
  4473. se_mem = list_entry(T_TASK(cmd)->t_mem_list->next,
  4474. struct se_mem, se_list);
  4475. ret = transport_do_se_mem_map(dev, task,
  4476. cmd->t_task->t_mem_list, NULL, se_mem,
  4477. &se_mem_lout, &se_mem_cnt, &task_offset);
  4478. if (ret < 0)
  4479. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4480. if (dev->transport->map_task_SG)
  4481. return dev->transport->map_task_SG(task);
  4482. return 0;
  4483. } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
  4484. if (dev->transport->map_task_non_SG)
  4485. return dev->transport->map_task_non_SG(task);
  4486. return 0;
  4487. } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
  4488. if (dev->transport->cdb_none)
  4489. return dev->transport->cdb_none(task);
  4490. return 0;
  4491. } else {
  4492. BUG();
  4493. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4494. }
  4495. }
  4496. /* transport_generic_new_cmd(): Called from transport_processing_thread()
  4497. *
  4498. * Allocate storage transport resources from a set of values predefined
  4499. * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
  4500. * Any non zero return here is treated as an "out of resource' op here.
  4501. */
  4502. /*
  4503. * Generate struct se_task(s) and/or their payloads for this CDB.
  4504. */
  4505. static int transport_generic_new_cmd(struct se_cmd *cmd)
  4506. {
  4507. struct se_portal_group *se_tpg;
  4508. struct se_task *task;
  4509. struct se_device *dev = SE_DEV(cmd);
  4510. int ret = 0;
  4511. /*
  4512. * Determine is the TCM fabric module has already allocated physical
  4513. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  4514. * to setup beforehand the linked list of physical memory at
  4515. * T_TASK(cmd)->t_mem_list of struct se_mem->se_page
  4516. */
  4517. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
  4518. ret = transport_allocate_resources(cmd);
  4519. if (ret < 0)
  4520. return ret;
  4521. }
  4522. ret = transport_get_sectors(cmd);
  4523. if (ret < 0)
  4524. return ret;
  4525. ret = transport_new_cmd_obj(cmd);
  4526. if (ret < 0)
  4527. return ret;
  4528. /*
  4529. * Determine if the calling TCM fabric module is talking to
  4530. * Linux/NET via kernel sockets and needs to allocate a
  4531. * struct iovec array to complete the struct se_cmd
  4532. */
  4533. se_tpg = SE_LUN(cmd)->lun_sep->sep_tpg;
  4534. if (TPG_TFO(se_tpg)->alloc_cmd_iovecs != NULL) {
  4535. ret = TPG_TFO(se_tpg)->alloc_cmd_iovecs(cmd);
  4536. if (ret < 0)
  4537. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4538. }
  4539. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  4540. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  4541. if (atomic_read(&task->task_sent))
  4542. continue;
  4543. if (!dev->transport->map_task_SG)
  4544. continue;
  4545. ret = dev->transport->map_task_SG(task);
  4546. if (ret < 0)
  4547. return ret;
  4548. }
  4549. } else {
  4550. ret = transport_map_control_cmd_to_task(cmd);
  4551. if (ret < 0)
  4552. return ret;
  4553. }
  4554. /*
  4555. * For WRITEs, let the iSCSI Target RX Thread know its buffer is ready..
  4556. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  4557. * will be added to the struct se_device execution queue after its WRITE
  4558. * data has arrived. (ie: It gets handled by the transport processing
  4559. * thread a second time)
  4560. */
  4561. if (cmd->data_direction == DMA_TO_DEVICE) {
  4562. transport_add_tasks_to_state_queue(cmd);
  4563. return transport_generic_write_pending(cmd);
  4564. }
  4565. /*
  4566. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  4567. * to the execution queue.
  4568. */
  4569. transport_execute_tasks(cmd);
  4570. return 0;
  4571. }
  4572. /* transport_generic_process_write():
  4573. *
  4574. *
  4575. */
  4576. void transport_generic_process_write(struct se_cmd *cmd)
  4577. {
  4578. #if 0
  4579. /*
  4580. * Copy SCSI Presented DTL sector(s) from received buffers allocated to
  4581. * original EDTL
  4582. */
  4583. if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  4584. if (!T_TASK(cmd)->t_tasks_se_num) {
  4585. unsigned char *dst, *buf =
  4586. (unsigned char *)T_TASK(cmd)->t_task_buf;
  4587. dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
  4588. if (!(dst)) {
  4589. printk(KERN_ERR "Unable to allocate memory for"
  4590. " WRITE underflow\n");
  4591. transport_generic_request_failure(cmd, NULL,
  4592. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4593. return;
  4594. }
  4595. memcpy(dst, buf, cmd->cmd_spdtl);
  4596. kfree(T_TASK(cmd)->t_task_buf);
  4597. T_TASK(cmd)->t_task_buf = dst;
  4598. } else {
  4599. struct scatterlist *sg =
  4600. (struct scatterlist *sg)T_TASK(cmd)->t_task_buf;
  4601. struct scatterlist *orig_sg;
  4602. orig_sg = kzalloc(sizeof(struct scatterlist) *
  4603. T_TASK(cmd)->t_tasks_se_num,
  4604. GFP_KERNEL))) {
  4605. if (!(orig_sg)) {
  4606. printk(KERN_ERR "Unable to allocate memory"
  4607. " for WRITE underflow\n");
  4608. transport_generic_request_failure(cmd, NULL,
  4609. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4610. return;
  4611. }
  4612. memcpy(orig_sg, T_TASK(cmd)->t_task_buf,
  4613. sizeof(struct scatterlist) *
  4614. T_TASK(cmd)->t_tasks_se_num);
  4615. cmd->data_length = cmd->cmd_spdtl;
  4616. /*
  4617. * FIXME, clear out original struct se_task and state
  4618. * information.
  4619. */
  4620. if (transport_generic_new_cmd(cmd) < 0) {
  4621. transport_generic_request_failure(cmd, NULL,
  4622. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4623. kfree(orig_sg);
  4624. return;
  4625. }
  4626. transport_memcpy_write_sg(cmd, orig_sg);
  4627. }
  4628. }
  4629. #endif
  4630. transport_execute_tasks(cmd);
  4631. }
  4632. EXPORT_SYMBOL(transport_generic_process_write);
  4633. /* transport_generic_write_pending():
  4634. *
  4635. *
  4636. */
  4637. static int transport_generic_write_pending(struct se_cmd *cmd)
  4638. {
  4639. unsigned long flags;
  4640. int ret;
  4641. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4642. cmd->t_state = TRANSPORT_WRITE_PENDING;
  4643. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4644. /*
  4645. * For the TCM control CDBs using a contiguous buffer, do the memcpy
  4646. * from the passed Linux/SCSI struct scatterlist located at
  4647. * T_TASK(se_cmd)->t_task_pt_buf to the contiguous buffer at
  4648. * T_TASK(se_cmd)->t_task_buf.
  4649. */
  4650. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
  4651. transport_memcpy_read_contig(cmd,
  4652. T_TASK(cmd)->t_task_buf,
  4653. T_TASK(cmd)->t_task_pt_sgl);
  4654. /*
  4655. * Clear the se_cmd for WRITE_PENDING status in order to set
  4656. * T_TASK(cmd)->t_transport_active=0 so that transport_generic_handle_data
  4657. * can be called from HW target mode interrupt code. This is safe
  4658. * to be called with transport_off=1 before the CMD_TFO(cmd)->write_pending
  4659. * because the se_cmd->se_lun pointer is not being cleared.
  4660. */
  4661. transport_cmd_check_stop(cmd, 1, 0);
  4662. /*
  4663. * Call the fabric write_pending function here to let the
  4664. * frontend know that WRITE buffers are ready.
  4665. */
  4666. ret = CMD_TFO(cmd)->write_pending(cmd);
  4667. if (ret < 0)
  4668. return ret;
  4669. return PYX_TRANSPORT_WRITE_PENDING;
  4670. }
  4671. /* transport_release_cmd_to_pool():
  4672. *
  4673. *
  4674. */
  4675. void transport_release_cmd_to_pool(struct se_cmd *cmd)
  4676. {
  4677. BUG_ON(!T_TASK(cmd));
  4678. BUG_ON(!CMD_TFO(cmd));
  4679. transport_free_se_cmd(cmd);
  4680. CMD_TFO(cmd)->release_cmd_to_pool(cmd);
  4681. }
  4682. EXPORT_SYMBOL(transport_release_cmd_to_pool);
  4683. /* transport_generic_free_cmd():
  4684. *
  4685. * Called from processing frontend to release storage engine resources
  4686. */
  4687. void transport_generic_free_cmd(
  4688. struct se_cmd *cmd,
  4689. int wait_for_tasks,
  4690. int release_to_pool,
  4691. int session_reinstatement)
  4692. {
  4693. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) || !T_TASK(cmd))
  4694. transport_release_cmd_to_pool(cmd);
  4695. else {
  4696. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  4697. if (SE_LUN(cmd)) {
  4698. #if 0
  4699. printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
  4700. " SE_LUN(cmd)\n", cmd,
  4701. CMD_TFO(cmd)->get_task_tag(cmd));
  4702. #endif
  4703. transport_lun_remove_cmd(cmd);
  4704. }
  4705. if (wait_for_tasks && cmd->transport_wait_for_tasks)
  4706. cmd->transport_wait_for_tasks(cmd, 0, 0);
  4707. transport_generic_remove(cmd, release_to_pool,
  4708. session_reinstatement);
  4709. }
  4710. }
  4711. EXPORT_SYMBOL(transport_generic_free_cmd);
  4712. static void transport_nop_wait_for_tasks(
  4713. struct se_cmd *cmd,
  4714. int remove_cmd,
  4715. int session_reinstatement)
  4716. {
  4717. return;
  4718. }
  4719. /* transport_lun_wait_for_tasks():
  4720. *
  4721. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  4722. * an struct se_lun to be successfully shutdown.
  4723. */
  4724. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  4725. {
  4726. unsigned long flags;
  4727. int ret;
  4728. /*
  4729. * If the frontend has already requested this struct se_cmd to
  4730. * be stopped, we can safely ignore this struct se_cmd.
  4731. */
  4732. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4733. if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
  4734. atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
  4735. DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
  4736. " TRUE, skipping\n", CMD_TFO(cmd)->get_task_tag(cmd));
  4737. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4738. transport_cmd_check_stop(cmd, 1, 0);
  4739. return -1;
  4740. }
  4741. atomic_set(&T_TASK(cmd)->transport_lun_fe_stop, 1);
  4742. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4743. wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
  4744. ret = transport_stop_tasks_for_cmd(cmd);
  4745. DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
  4746. " %d\n", cmd, T_TASK(cmd)->t_task_cdbs, ret);
  4747. if (!ret) {
  4748. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  4749. CMD_TFO(cmd)->get_task_tag(cmd));
  4750. wait_for_completion(&T_TASK(cmd)->transport_lun_stop_comp);
  4751. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  4752. CMD_TFO(cmd)->get_task_tag(cmd));
  4753. }
  4754. transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
  4755. return 0;
  4756. }
  4757. /* #define DEBUG_CLEAR_LUN */
  4758. #ifdef DEBUG_CLEAR_LUN
  4759. #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
  4760. #else
  4761. #define DEBUG_CLEAR_L(x...)
  4762. #endif
  4763. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  4764. {
  4765. struct se_cmd *cmd = NULL;
  4766. unsigned long lun_flags, cmd_flags;
  4767. /*
  4768. * Do exception processing and return CHECK_CONDITION status to the
  4769. * Initiator Port.
  4770. */
  4771. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4772. while (!list_empty_careful(&lun->lun_cmd_list)) {
  4773. cmd = list_entry(lun->lun_cmd_list.next,
  4774. struct se_cmd, se_lun_list);
  4775. list_del(&cmd->se_lun_list);
  4776. if (!(T_TASK(cmd))) {
  4777. printk(KERN_ERR "ITT: 0x%08x, T_TASK(cmd) = NULL"
  4778. "[i,t]_state: %u/%u\n",
  4779. CMD_TFO(cmd)->get_task_tag(cmd),
  4780. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
  4781. BUG();
  4782. }
  4783. atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
  4784. /*
  4785. * This will notify iscsi_target_transport.c:
  4786. * transport_cmd_check_stop() that a LUN shutdown is in
  4787. * progress for the iscsi_cmd_t.
  4788. */
  4789. spin_lock(&T_TASK(cmd)->t_state_lock);
  4790. DEBUG_CLEAR_L("SE_LUN[%d] - Setting T_TASK(cmd)->transport"
  4791. "_lun_stop for ITT: 0x%08x\n",
  4792. SE_LUN(cmd)->unpacked_lun,
  4793. CMD_TFO(cmd)->get_task_tag(cmd));
  4794. atomic_set(&T_TASK(cmd)->transport_lun_stop, 1);
  4795. spin_unlock(&T_TASK(cmd)->t_state_lock);
  4796. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4797. if (!(SE_LUN(cmd))) {
  4798. printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
  4799. CMD_TFO(cmd)->get_task_tag(cmd),
  4800. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
  4801. BUG();
  4802. }
  4803. /*
  4804. * If the Storage engine still owns the iscsi_cmd_t, determine
  4805. * and/or stop its context.
  4806. */
  4807. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
  4808. "_lun_wait_for_tasks()\n", SE_LUN(cmd)->unpacked_lun,
  4809. CMD_TFO(cmd)->get_task_tag(cmd));
  4810. if (transport_lun_wait_for_tasks(cmd, SE_LUN(cmd)) < 0) {
  4811. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4812. continue;
  4813. }
  4814. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  4815. "_wait_for_tasks(): SUCCESS\n",
  4816. SE_LUN(cmd)->unpacked_lun,
  4817. CMD_TFO(cmd)->get_task_tag(cmd));
  4818. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4819. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  4820. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4821. goto check_cond;
  4822. }
  4823. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  4824. transport_all_task_dev_remove_state(cmd);
  4825. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4826. transport_free_dev_tasks(cmd);
  4827. /*
  4828. * The Storage engine stopped this struct se_cmd before it was
  4829. * send to the fabric frontend for delivery back to the
  4830. * Initiator Node. Return this SCSI CDB back with an
  4831. * CHECK_CONDITION status.
  4832. */
  4833. check_cond:
  4834. transport_send_check_condition_and_sense(cmd,
  4835. TCM_NON_EXISTENT_LUN, 0);
  4836. /*
  4837. * If the fabric frontend is waiting for this iscsi_cmd_t to
  4838. * be released, notify the waiting thread now that LU has
  4839. * finished accessing it.
  4840. */
  4841. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4842. if (atomic_read(&T_TASK(cmd)->transport_lun_fe_stop)) {
  4843. DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
  4844. " struct se_cmd: %p ITT: 0x%08x\n",
  4845. lun->unpacked_lun,
  4846. cmd, CMD_TFO(cmd)->get_task_tag(cmd));
  4847. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  4848. cmd_flags);
  4849. transport_cmd_check_stop(cmd, 1, 0);
  4850. complete(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  4851. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4852. continue;
  4853. }
  4854. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  4855. lun->unpacked_lun, CMD_TFO(cmd)->get_task_tag(cmd));
  4856. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4857. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4858. }
  4859. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4860. }
  4861. static int transport_clear_lun_thread(void *p)
  4862. {
  4863. struct se_lun *lun = (struct se_lun *)p;
  4864. __transport_clear_lun_from_sessions(lun);
  4865. complete(&lun->lun_shutdown_comp);
  4866. return 0;
  4867. }
  4868. int transport_clear_lun_from_sessions(struct se_lun *lun)
  4869. {
  4870. struct task_struct *kt;
  4871. kt = kthread_run(transport_clear_lun_thread, (void *)lun,
  4872. "tcm_cl_%u", lun->unpacked_lun);
  4873. if (IS_ERR(kt)) {
  4874. printk(KERN_ERR "Unable to start clear_lun thread\n");
  4875. return -1;
  4876. }
  4877. wait_for_completion(&lun->lun_shutdown_comp);
  4878. return 0;
  4879. }
  4880. /* transport_generic_wait_for_tasks():
  4881. *
  4882. * Called from frontend or passthrough context to wait for storage engine
  4883. * to pause and/or release frontend generated struct se_cmd.
  4884. */
  4885. static void transport_generic_wait_for_tasks(
  4886. struct se_cmd *cmd,
  4887. int remove_cmd,
  4888. int session_reinstatement)
  4889. {
  4890. unsigned long flags;
  4891. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
  4892. return;
  4893. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4894. /*
  4895. * If we are already stopped due to an external event (ie: LUN shutdown)
  4896. * sleep until the connection can have the passed struct se_cmd back.
  4897. * The T_TASK(cmd)->transport_lun_stopped_sem will be upped by
  4898. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  4899. * has completed its operation on the struct se_cmd.
  4900. */
  4901. if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
  4902. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
  4903. " wait_for_completion(&T_TASK(cmd)transport_lun_fe"
  4904. "_stop_comp); for ITT: 0x%08x\n",
  4905. CMD_TFO(cmd)->get_task_tag(cmd));
  4906. /*
  4907. * There is a special case for WRITES where a FE exception +
  4908. * LUN shutdown means ConfigFS context is still sleeping on
  4909. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  4910. * We go ahead and up transport_lun_stop_comp just to be sure
  4911. * here.
  4912. */
  4913. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4914. complete(&T_TASK(cmd)->transport_lun_stop_comp);
  4915. wait_for_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  4916. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4917. transport_all_task_dev_remove_state(cmd);
  4918. /*
  4919. * At this point, the frontend who was the originator of this
  4920. * struct se_cmd, now owns the structure and can be released through
  4921. * normal means below.
  4922. */
  4923. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
  4924. " wait_for_completion(&T_TASK(cmd)transport_lun_fe_"
  4925. "stop_comp); for ITT: 0x%08x\n",
  4926. CMD_TFO(cmd)->get_task_tag(cmd));
  4927. atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
  4928. }
  4929. if (!atomic_read(&T_TASK(cmd)->t_transport_active))
  4930. goto remove;
  4931. atomic_set(&T_TASK(cmd)->t_transport_stop, 1);
  4932. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
  4933. " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
  4934. " = TRUE\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
  4935. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state,
  4936. cmd->deferred_t_state);
  4937. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4938. wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
  4939. wait_for_completion(&T_TASK(cmd)->t_transport_stop_comp);
  4940. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4941. atomic_set(&T_TASK(cmd)->t_transport_active, 0);
  4942. atomic_set(&T_TASK(cmd)->t_transport_stop, 0);
  4943. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
  4944. "&T_TASK(cmd)->t_transport_stop_comp) for ITT: 0x%08x\n",
  4945. CMD_TFO(cmd)->get_task_tag(cmd));
  4946. remove:
  4947. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4948. if (!remove_cmd)
  4949. return;
  4950. transport_generic_free_cmd(cmd, 0, 0, session_reinstatement);
  4951. }
  4952. static int transport_get_sense_codes(
  4953. struct se_cmd *cmd,
  4954. u8 *asc,
  4955. u8 *ascq)
  4956. {
  4957. *asc = cmd->scsi_asc;
  4958. *ascq = cmd->scsi_ascq;
  4959. return 0;
  4960. }
  4961. static int transport_set_sense_codes(
  4962. struct se_cmd *cmd,
  4963. u8 asc,
  4964. u8 ascq)
  4965. {
  4966. cmd->scsi_asc = asc;
  4967. cmd->scsi_ascq = ascq;
  4968. return 0;
  4969. }
  4970. int transport_send_check_condition_and_sense(
  4971. struct se_cmd *cmd,
  4972. u8 reason,
  4973. int from_transport)
  4974. {
  4975. unsigned char *buffer = cmd->sense_buffer;
  4976. unsigned long flags;
  4977. int offset;
  4978. u8 asc = 0, ascq = 0;
  4979. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4980. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4981. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4982. return 0;
  4983. }
  4984. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  4985. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4986. if (!reason && from_transport)
  4987. goto after_reason;
  4988. if (!from_transport)
  4989. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  4990. /*
  4991. * Data Segment and SenseLength of the fabric response PDU.
  4992. *
  4993. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  4994. * from include/scsi/scsi_cmnd.h
  4995. */
  4996. offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
  4997. TRANSPORT_SENSE_BUFFER);
  4998. /*
  4999. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  5000. * SENSE KEY values from include/scsi/scsi.h
  5001. */
  5002. switch (reason) {
  5003. case TCM_NON_EXISTENT_LUN:
  5004. case TCM_UNSUPPORTED_SCSI_OPCODE:
  5005. case TCM_SECTOR_COUNT_TOO_MANY:
  5006. /* CURRENT ERROR */
  5007. buffer[offset] = 0x70;
  5008. /* ILLEGAL REQUEST */
  5009. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5010. /* INVALID COMMAND OPERATION CODE */
  5011. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  5012. break;
  5013. case TCM_UNKNOWN_MODE_PAGE:
  5014. /* CURRENT ERROR */
  5015. buffer[offset] = 0x70;
  5016. /* ILLEGAL REQUEST */
  5017. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5018. /* INVALID FIELD IN CDB */
  5019. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  5020. break;
  5021. case TCM_CHECK_CONDITION_ABORT_CMD:
  5022. /* CURRENT ERROR */
  5023. buffer[offset] = 0x70;
  5024. /* ABORTED COMMAND */
  5025. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5026. /* BUS DEVICE RESET FUNCTION OCCURRED */
  5027. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  5028. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  5029. break;
  5030. case TCM_INCORRECT_AMOUNT_OF_DATA:
  5031. /* CURRENT ERROR */
  5032. buffer[offset] = 0x70;
  5033. /* ABORTED COMMAND */
  5034. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5035. /* WRITE ERROR */
  5036. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  5037. /* NOT ENOUGH UNSOLICITED DATA */
  5038. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  5039. break;
  5040. case TCM_INVALID_CDB_FIELD:
  5041. /* CURRENT ERROR */
  5042. buffer[offset] = 0x70;
  5043. /* ABORTED COMMAND */
  5044. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5045. /* INVALID FIELD IN CDB */
  5046. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  5047. break;
  5048. case TCM_INVALID_PARAMETER_LIST:
  5049. /* CURRENT ERROR */
  5050. buffer[offset] = 0x70;
  5051. /* ABORTED COMMAND */
  5052. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5053. /* INVALID FIELD IN PARAMETER LIST */
  5054. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  5055. break;
  5056. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  5057. /* CURRENT ERROR */
  5058. buffer[offset] = 0x70;
  5059. /* ABORTED COMMAND */
  5060. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5061. /* WRITE ERROR */
  5062. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  5063. /* UNEXPECTED_UNSOLICITED_DATA */
  5064. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  5065. break;
  5066. case TCM_SERVICE_CRC_ERROR:
  5067. /* CURRENT ERROR */
  5068. buffer[offset] = 0x70;
  5069. /* ABORTED COMMAND */
  5070. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5071. /* PROTOCOL SERVICE CRC ERROR */
  5072. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  5073. /* N/A */
  5074. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  5075. break;
  5076. case TCM_SNACK_REJECTED:
  5077. /* CURRENT ERROR */
  5078. buffer[offset] = 0x70;
  5079. /* ABORTED COMMAND */
  5080. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5081. /* READ ERROR */
  5082. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  5083. /* FAILED RETRANSMISSION REQUEST */
  5084. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  5085. break;
  5086. case TCM_WRITE_PROTECTED:
  5087. /* CURRENT ERROR */
  5088. buffer[offset] = 0x70;
  5089. /* DATA PROTECT */
  5090. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  5091. /* WRITE PROTECTED */
  5092. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  5093. break;
  5094. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  5095. /* CURRENT ERROR */
  5096. buffer[offset] = 0x70;
  5097. /* UNIT ATTENTION */
  5098. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  5099. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  5100. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  5101. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  5102. break;
  5103. case TCM_CHECK_CONDITION_NOT_READY:
  5104. /* CURRENT ERROR */
  5105. buffer[offset] = 0x70;
  5106. /* Not Ready */
  5107. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  5108. transport_get_sense_codes(cmd, &asc, &ascq);
  5109. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  5110. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  5111. break;
  5112. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  5113. default:
  5114. /* CURRENT ERROR */
  5115. buffer[offset] = 0x70;
  5116. /* ILLEGAL REQUEST */
  5117. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5118. /* LOGICAL UNIT COMMUNICATION FAILURE */
  5119. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  5120. break;
  5121. }
  5122. /*
  5123. * This code uses linux/include/scsi/scsi.h SAM status codes!
  5124. */
  5125. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  5126. /*
  5127. * Automatically padded, this value is encoded in the fabric's
  5128. * data_length response PDU containing the SCSI defined sense data.
  5129. */
  5130. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  5131. after_reason:
  5132. CMD_TFO(cmd)->queue_status(cmd);
  5133. return 0;
  5134. }
  5135. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  5136. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  5137. {
  5138. int ret = 0;
  5139. if (atomic_read(&T_TASK(cmd)->t_transport_aborted) != 0) {
  5140. if (!(send_status) ||
  5141. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  5142. return 1;
  5143. #if 0
  5144. printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
  5145. " status for CDB: 0x%02x ITT: 0x%08x\n",
  5146. T_TASK(cmd)->t_task_cdb[0],
  5147. CMD_TFO(cmd)->get_task_tag(cmd));
  5148. #endif
  5149. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  5150. CMD_TFO(cmd)->queue_status(cmd);
  5151. ret = 1;
  5152. }
  5153. return ret;
  5154. }
  5155. EXPORT_SYMBOL(transport_check_aborted_status);
  5156. void transport_send_task_abort(struct se_cmd *cmd)
  5157. {
  5158. /*
  5159. * If there are still expected incoming fabric WRITEs, we wait
  5160. * until until they have completed before sending a TASK_ABORTED
  5161. * response. This response with TASK_ABORTED status will be
  5162. * queued back to fabric module by transport_check_aborted_status().
  5163. */
  5164. if (cmd->data_direction == DMA_TO_DEVICE) {
  5165. if (CMD_TFO(cmd)->write_pending_status(cmd) != 0) {
  5166. atomic_inc(&T_TASK(cmd)->t_transport_aborted);
  5167. smp_mb__after_atomic_inc();
  5168. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  5169. transport_new_cmd_failure(cmd);
  5170. return;
  5171. }
  5172. }
  5173. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  5174. #if 0
  5175. printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  5176. " ITT: 0x%08x\n", T_TASK(cmd)->t_task_cdb[0],
  5177. CMD_TFO(cmd)->get_task_tag(cmd));
  5178. #endif
  5179. CMD_TFO(cmd)->queue_status(cmd);
  5180. }
  5181. /* transport_generic_do_tmr():
  5182. *
  5183. *
  5184. */
  5185. int transport_generic_do_tmr(struct se_cmd *cmd)
  5186. {
  5187. struct se_cmd *ref_cmd;
  5188. struct se_device *dev = SE_DEV(cmd);
  5189. struct se_tmr_req *tmr = cmd->se_tmr_req;
  5190. int ret;
  5191. switch (tmr->function) {
  5192. case ABORT_TASK:
  5193. ref_cmd = tmr->ref_cmd;
  5194. tmr->response = TMR_FUNCTION_REJECTED;
  5195. break;
  5196. case ABORT_TASK_SET:
  5197. case CLEAR_ACA:
  5198. case CLEAR_TASK_SET:
  5199. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  5200. break;
  5201. case LUN_RESET:
  5202. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  5203. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  5204. TMR_FUNCTION_REJECTED;
  5205. break;
  5206. #if 0
  5207. case TARGET_WARM_RESET:
  5208. transport_generic_host_reset(dev->se_hba);
  5209. tmr->response = TMR_FUNCTION_REJECTED;
  5210. break;
  5211. case TARGET_COLD_RESET:
  5212. transport_generic_host_reset(dev->se_hba);
  5213. transport_generic_cold_reset(dev->se_hba);
  5214. tmr->response = TMR_FUNCTION_REJECTED;
  5215. break;
  5216. #endif
  5217. default:
  5218. printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
  5219. tmr->function);
  5220. tmr->response = TMR_FUNCTION_REJECTED;
  5221. break;
  5222. }
  5223. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  5224. CMD_TFO(cmd)->queue_tm_rsp(cmd);
  5225. transport_cmd_check_stop(cmd, 2, 0);
  5226. return 0;
  5227. }
  5228. /*
  5229. * Called with spin_lock_irq(&dev->execute_task_lock); held
  5230. *
  5231. */
  5232. static struct se_task *
  5233. transport_get_task_from_state_list(struct se_device *dev)
  5234. {
  5235. struct se_task *task;
  5236. if (list_empty(&dev->state_task_list))
  5237. return NULL;
  5238. list_for_each_entry(task, &dev->state_task_list, t_state_list)
  5239. break;
  5240. list_del(&task->t_state_list);
  5241. atomic_set(&task->task_state_active, 0);
  5242. return task;
  5243. }
  5244. static void transport_processing_shutdown(struct se_device *dev)
  5245. {
  5246. struct se_cmd *cmd;
  5247. struct se_queue_req *qr;
  5248. struct se_task *task;
  5249. u8 state;
  5250. unsigned long flags;
  5251. /*
  5252. * Empty the struct se_device's struct se_task state list.
  5253. */
  5254. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5255. while ((task = transport_get_task_from_state_list(dev))) {
  5256. if (!(TASK_CMD(task))) {
  5257. printk(KERN_ERR "TASK_CMD(task) is NULL!\n");
  5258. continue;
  5259. }
  5260. cmd = TASK_CMD(task);
  5261. if (!T_TASK(cmd)) {
  5262. printk(KERN_ERR "T_TASK(cmd) is NULL for task: %p cmd:"
  5263. " %p ITT: 0x%08x\n", task, cmd,
  5264. CMD_TFO(cmd)->get_task_tag(cmd));
  5265. continue;
  5266. }
  5267. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5268. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  5269. DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
  5270. " i_state/def_i_state: %d/%d, t_state/def_t_state:"
  5271. " %d/%d cdb: 0x%02x\n", cmd, task,
  5272. CMD_TFO(cmd)->get_task_tag(cmd), cmd->cmd_sn,
  5273. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->deferred_i_state,
  5274. cmd->t_state, cmd->deferred_t_state,
  5275. T_TASK(cmd)->t_task_cdb[0]);
  5276. DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
  5277. " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
  5278. " t_transport_stop: %d t_transport_sent: %d\n",
  5279. CMD_TFO(cmd)->get_task_tag(cmd),
  5280. T_TASK(cmd)->t_task_cdbs,
  5281. atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
  5282. atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
  5283. atomic_read(&T_TASK(cmd)->t_transport_active),
  5284. atomic_read(&T_TASK(cmd)->t_transport_stop),
  5285. atomic_read(&T_TASK(cmd)->t_transport_sent));
  5286. if (atomic_read(&task->task_active)) {
  5287. atomic_set(&task->task_stop, 1);
  5288. spin_unlock_irqrestore(
  5289. &T_TASK(cmd)->t_state_lock, flags);
  5290. DEBUG_DO("Waiting for task: %p to shutdown for dev:"
  5291. " %p\n", task, dev);
  5292. wait_for_completion(&task->task_stop_comp);
  5293. DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
  5294. task, dev);
  5295. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  5296. atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
  5297. atomic_set(&task->task_active, 0);
  5298. atomic_set(&task->task_stop, 0);
  5299. }
  5300. __transport_stop_task_timer(task, &flags);
  5301. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_ex_left))) {
  5302. spin_unlock_irqrestore(
  5303. &T_TASK(cmd)->t_state_lock, flags);
  5304. DEBUG_DO("Skipping task: %p, dev: %p for"
  5305. " t_task_cdbs_ex_left: %d\n", task, dev,
  5306. atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left));
  5307. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5308. continue;
  5309. }
  5310. if (atomic_read(&T_TASK(cmd)->t_transport_active)) {
  5311. DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
  5312. " %p\n", task, dev);
  5313. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5314. spin_unlock_irqrestore(
  5315. &T_TASK(cmd)->t_state_lock, flags);
  5316. transport_send_check_condition_and_sense(
  5317. cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
  5318. 0);
  5319. transport_remove_cmd_from_queue(cmd,
  5320. SE_DEV(cmd)->dev_queue_obj);
  5321. transport_lun_remove_cmd(cmd);
  5322. transport_cmd_check_stop(cmd, 1, 0);
  5323. } else {
  5324. spin_unlock_irqrestore(
  5325. &T_TASK(cmd)->t_state_lock, flags);
  5326. transport_remove_cmd_from_queue(cmd,
  5327. SE_DEV(cmd)->dev_queue_obj);
  5328. transport_lun_remove_cmd(cmd);
  5329. if (transport_cmd_check_stop(cmd, 1, 0))
  5330. transport_generic_remove(cmd, 0, 0);
  5331. }
  5332. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5333. continue;
  5334. }
  5335. DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
  5336. task, dev);
  5337. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5338. spin_unlock_irqrestore(
  5339. &T_TASK(cmd)->t_state_lock, flags);
  5340. transport_send_check_condition_and_sense(cmd,
  5341. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5342. transport_remove_cmd_from_queue(cmd,
  5343. SE_DEV(cmd)->dev_queue_obj);
  5344. transport_lun_remove_cmd(cmd);
  5345. transport_cmd_check_stop(cmd, 1, 0);
  5346. } else {
  5347. spin_unlock_irqrestore(
  5348. &T_TASK(cmd)->t_state_lock, flags);
  5349. transport_remove_cmd_from_queue(cmd,
  5350. SE_DEV(cmd)->dev_queue_obj);
  5351. transport_lun_remove_cmd(cmd);
  5352. if (transport_cmd_check_stop(cmd, 1, 0))
  5353. transport_generic_remove(cmd, 0, 0);
  5354. }
  5355. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5356. }
  5357. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5358. /*
  5359. * Empty the struct se_device's struct se_cmd list.
  5360. */
  5361. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5362. while ((qr = __transport_get_qr_from_queue(dev->dev_queue_obj))) {
  5363. spin_unlock_irqrestore(
  5364. &dev->dev_queue_obj->cmd_queue_lock, flags);
  5365. cmd = (struct se_cmd *)qr->cmd;
  5366. state = qr->state;
  5367. kfree(qr);
  5368. DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
  5369. cmd, state);
  5370. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5371. transport_send_check_condition_and_sense(cmd,
  5372. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5373. transport_lun_remove_cmd(cmd);
  5374. transport_cmd_check_stop(cmd, 1, 0);
  5375. } else {
  5376. transport_lun_remove_cmd(cmd);
  5377. if (transport_cmd_check_stop(cmd, 1, 0))
  5378. transport_generic_remove(cmd, 0, 0);
  5379. }
  5380. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5381. }
  5382. spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5383. }
  5384. /* transport_processing_thread():
  5385. *
  5386. *
  5387. */
  5388. static int transport_processing_thread(void *param)
  5389. {
  5390. int ret, t_state;
  5391. struct se_cmd *cmd;
  5392. struct se_device *dev = (struct se_device *) param;
  5393. struct se_queue_req *qr;
  5394. set_user_nice(current, -20);
  5395. while (!kthread_should_stop()) {
  5396. ret = wait_event_interruptible(dev->dev_queue_obj->thread_wq,
  5397. atomic_read(&dev->dev_queue_obj->queue_cnt) ||
  5398. kthread_should_stop());
  5399. if (ret < 0)
  5400. goto out;
  5401. spin_lock_irq(&dev->dev_status_lock);
  5402. if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
  5403. spin_unlock_irq(&dev->dev_status_lock);
  5404. transport_processing_shutdown(dev);
  5405. continue;
  5406. }
  5407. spin_unlock_irq(&dev->dev_status_lock);
  5408. get_cmd:
  5409. __transport_execute_tasks(dev);
  5410. qr = transport_get_qr_from_queue(dev->dev_queue_obj);
  5411. if (!(qr))
  5412. continue;
  5413. cmd = (struct se_cmd *)qr->cmd;
  5414. t_state = qr->state;
  5415. kfree(qr);
  5416. switch (t_state) {
  5417. case TRANSPORT_NEW_CMD_MAP:
  5418. if (!(CMD_TFO(cmd)->new_cmd_map)) {
  5419. printk(KERN_ERR "CMD_TFO(cmd)->new_cmd_map is"
  5420. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  5421. BUG();
  5422. }
  5423. ret = CMD_TFO(cmd)->new_cmd_map(cmd);
  5424. if (ret < 0) {
  5425. cmd->transport_error_status = ret;
  5426. transport_generic_request_failure(cmd, NULL,
  5427. 0, (cmd->data_direction !=
  5428. DMA_TO_DEVICE));
  5429. break;
  5430. }
  5431. /* Fall through */
  5432. case TRANSPORT_NEW_CMD:
  5433. ret = transport_generic_new_cmd(cmd);
  5434. if (ret < 0) {
  5435. cmd->transport_error_status = ret;
  5436. transport_generic_request_failure(cmd, NULL,
  5437. 0, (cmd->data_direction !=
  5438. DMA_TO_DEVICE));
  5439. }
  5440. break;
  5441. case TRANSPORT_PROCESS_WRITE:
  5442. transport_generic_process_write(cmd);
  5443. break;
  5444. case TRANSPORT_COMPLETE_OK:
  5445. transport_stop_all_task_timers(cmd);
  5446. transport_generic_complete_ok(cmd);
  5447. break;
  5448. case TRANSPORT_REMOVE:
  5449. transport_generic_remove(cmd, 1, 0);
  5450. break;
  5451. case TRANSPORT_PROCESS_TMR:
  5452. transport_generic_do_tmr(cmd);
  5453. break;
  5454. case TRANSPORT_COMPLETE_FAILURE:
  5455. transport_generic_request_failure(cmd, NULL, 1, 1);
  5456. break;
  5457. case TRANSPORT_COMPLETE_TIMEOUT:
  5458. transport_stop_all_task_timers(cmd);
  5459. transport_generic_request_timeout(cmd);
  5460. break;
  5461. default:
  5462. printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
  5463. " %d for ITT: 0x%08x i_state: %d on SE LUN:"
  5464. " %u\n", t_state, cmd->deferred_t_state,
  5465. CMD_TFO(cmd)->get_task_tag(cmd),
  5466. CMD_TFO(cmd)->get_cmd_state(cmd),
  5467. SE_LUN(cmd)->unpacked_lun);
  5468. BUG();
  5469. }
  5470. goto get_cmd;
  5471. }
  5472. out:
  5473. transport_release_all_cmds(dev);
  5474. dev->process_thread = NULL;
  5475. return 0;
  5476. }