target_core_transport.c 161 KB

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