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