target_core_transport.c 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905
  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);
  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 inline u32 transport_lba_21(unsigned char *cdb)
  1985. {
  1986. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1987. }
  1988. static inline u32 transport_lba_32(unsigned char *cdb)
  1989. {
  1990. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1991. }
  1992. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1993. {
  1994. unsigned int __v1, __v2;
  1995. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1996. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1997. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1998. }
  1999. /*
  2000. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  2001. */
  2002. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  2003. {
  2004. unsigned int __v1, __v2;
  2005. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  2006. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  2007. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  2008. }
  2009. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  2010. {
  2011. unsigned long flags;
  2012. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  2013. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  2014. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  2015. }
  2016. /*
  2017. * Called from interrupt context.
  2018. */
  2019. static void transport_task_timeout_handler(unsigned long data)
  2020. {
  2021. struct se_task *task = (struct se_task *)data;
  2022. struct se_cmd *cmd = task->task_se_cmd;
  2023. unsigned long flags;
  2024. DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
  2025. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2026. if (task->task_flags & TF_STOP) {
  2027. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2028. return;
  2029. }
  2030. task->task_flags &= ~TF_RUNNING;
  2031. /*
  2032. * Determine if transport_complete_task() has already been called.
  2033. */
  2034. if (!(atomic_read(&task->task_active))) {
  2035. DEBUG_TT("transport task: %p cmd: %p timeout task_active"
  2036. " == 0\n", task, cmd);
  2037. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2038. return;
  2039. }
  2040. atomic_inc(&cmd->t_se_count);
  2041. atomic_inc(&cmd->t_transport_timeout);
  2042. cmd->t_tasks_failed = 1;
  2043. atomic_set(&task->task_timeout, 1);
  2044. task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
  2045. task->task_scsi_status = 1;
  2046. if (atomic_read(&task->task_stop)) {
  2047. DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
  2048. " == 1\n", task, cmd);
  2049. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2050. complete(&task->task_stop_comp);
  2051. return;
  2052. }
  2053. if (!(atomic_dec_and_test(&cmd->t_task_cdbs_left))) {
  2054. DEBUG_TT("transport task: %p cmd: %p timeout non zero"
  2055. " t_task_cdbs_left\n", task, cmd);
  2056. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2057. return;
  2058. }
  2059. DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
  2060. task, cmd);
  2061. cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
  2062. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2063. transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
  2064. }
  2065. /*
  2066. * Called with cmd->t_state_lock held.
  2067. */
  2068. static void transport_start_task_timer(struct se_task *task)
  2069. {
  2070. struct se_device *dev = task->se_dev;
  2071. int timeout;
  2072. if (task->task_flags & TF_RUNNING)
  2073. return;
  2074. /*
  2075. * If the task_timeout is disabled, exit now.
  2076. */
  2077. timeout = dev->se_sub_dev->se_dev_attrib.task_timeout;
  2078. if (!(timeout))
  2079. return;
  2080. init_timer(&task->task_timer);
  2081. task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
  2082. task->task_timer.data = (unsigned long) task;
  2083. task->task_timer.function = transport_task_timeout_handler;
  2084. task->task_flags |= TF_RUNNING;
  2085. add_timer(&task->task_timer);
  2086. #if 0
  2087. printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
  2088. " %d\n", task->task_se_cmd, task, timeout);
  2089. #endif
  2090. }
  2091. /*
  2092. * Called with spin_lock_irq(&cmd->t_state_lock) held.
  2093. */
  2094. void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
  2095. {
  2096. struct se_cmd *cmd = task->task_se_cmd;
  2097. if (!(task->task_flags & TF_RUNNING))
  2098. return;
  2099. task->task_flags |= TF_STOP;
  2100. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  2101. del_timer_sync(&task->task_timer);
  2102. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  2103. task->task_flags &= ~TF_RUNNING;
  2104. task->task_flags &= ~TF_STOP;
  2105. }
  2106. static void transport_stop_all_task_timers(struct se_cmd *cmd)
  2107. {
  2108. struct se_task *task = NULL, *task_tmp;
  2109. unsigned long flags;
  2110. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2111. list_for_each_entry_safe(task, task_tmp,
  2112. &cmd->t_task_list, t_list)
  2113. __transport_stop_task_timer(task, &flags);
  2114. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2115. }
  2116. static inline int transport_tcq_window_closed(struct se_device *dev)
  2117. {
  2118. if (dev->dev_tcq_window_closed++ <
  2119. PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
  2120. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
  2121. } else
  2122. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
  2123. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2124. return 0;
  2125. }
  2126. /*
  2127. * Called from Fabric Module context from transport_execute_tasks()
  2128. *
  2129. * The return of this function determins if the tasks from struct se_cmd
  2130. * get added to the execution queue in transport_execute_tasks(),
  2131. * or are added to the delayed or ordered lists here.
  2132. */
  2133. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  2134. {
  2135. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  2136. return 1;
  2137. /*
  2138. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  2139. * to allow the passed struct se_cmd list of tasks to the front of the list.
  2140. */
  2141. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2142. atomic_inc(&cmd->se_dev->dev_hoq_count);
  2143. smp_mb__after_atomic_inc();
  2144. DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
  2145. " 0x%02x, se_ordered_id: %u\n",
  2146. cmd->_task_cdb[0],
  2147. cmd->se_ordered_id);
  2148. return 1;
  2149. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2150. spin_lock(&cmd->se_dev->ordered_cmd_lock);
  2151. list_add_tail(&cmd->se_ordered_node,
  2152. &cmd->se_dev->ordered_cmd_list);
  2153. spin_unlock(&cmd->se_dev->ordered_cmd_lock);
  2154. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  2155. smp_mb__after_atomic_inc();
  2156. DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
  2157. " list, se_ordered_id: %u\n",
  2158. cmd->t_task_cdb[0],
  2159. cmd->se_ordered_id);
  2160. /*
  2161. * Add ORDERED command to tail of execution queue if
  2162. * no other older commands exist that need to be
  2163. * completed first.
  2164. */
  2165. if (!(atomic_read(&cmd->se_dev->simple_cmds)))
  2166. return 1;
  2167. } else {
  2168. /*
  2169. * For SIMPLE and UNTAGGED Task Attribute commands
  2170. */
  2171. atomic_inc(&cmd->se_dev->simple_cmds);
  2172. smp_mb__after_atomic_inc();
  2173. }
  2174. /*
  2175. * Otherwise if one or more outstanding ORDERED task attribute exist,
  2176. * add the dormant task(s) built for the passed struct se_cmd to the
  2177. * execution queue and become in Active state for this struct se_device.
  2178. */
  2179. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  2180. /*
  2181. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  2182. * will be drained upon completion of HEAD_OF_QUEUE task.
  2183. */
  2184. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  2185. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  2186. list_add_tail(&cmd->se_delayed_node,
  2187. &cmd->se_dev->delayed_cmd_list);
  2188. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  2189. DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
  2190. " delayed CMD list, se_ordered_id: %u\n",
  2191. cmd->t_task_cdb[0], cmd->sam_task_attr,
  2192. cmd->se_ordered_id);
  2193. /*
  2194. * Return zero to let transport_execute_tasks() know
  2195. * not to add the delayed tasks to the execution list.
  2196. */
  2197. return 0;
  2198. }
  2199. /*
  2200. * Otherwise, no ORDERED task attributes exist..
  2201. */
  2202. return 1;
  2203. }
  2204. /*
  2205. * Called from fabric module context in transport_generic_new_cmd() and
  2206. * transport_generic_process_write()
  2207. */
  2208. static int transport_execute_tasks(struct se_cmd *cmd)
  2209. {
  2210. int add_tasks;
  2211. if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
  2212. cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
  2213. transport_generic_request_failure(cmd, NULL, 0, 1);
  2214. return 0;
  2215. }
  2216. /*
  2217. * Call transport_cmd_check_stop() to see if a fabric exception
  2218. * has occurred that prevents execution.
  2219. */
  2220. if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
  2221. /*
  2222. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  2223. * attribute for the tasks of the received struct se_cmd CDB
  2224. */
  2225. add_tasks = transport_execute_task_attr(cmd);
  2226. if (!add_tasks)
  2227. goto execute_tasks;
  2228. /*
  2229. * This calls transport_add_tasks_from_cmd() to handle
  2230. * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
  2231. * (if enabled) in __transport_add_task_to_execute_queue() and
  2232. * transport_add_task_check_sam_attr().
  2233. */
  2234. transport_add_tasks_from_cmd(cmd);
  2235. }
  2236. /*
  2237. * Kick the execution queue for the cmd associated struct se_device
  2238. * storage object.
  2239. */
  2240. execute_tasks:
  2241. __transport_execute_tasks(cmd->se_dev);
  2242. return 0;
  2243. }
  2244. /*
  2245. * Called to check struct se_device tcq depth window, and once open pull struct se_task
  2246. * from struct se_device->execute_task_list and
  2247. *
  2248. * Called from transport_processing_thread()
  2249. */
  2250. static int __transport_execute_tasks(struct se_device *dev)
  2251. {
  2252. int error;
  2253. struct se_cmd *cmd = NULL;
  2254. struct se_task *task = NULL;
  2255. unsigned long flags;
  2256. /*
  2257. * Check if there is enough room in the device and HBA queue to send
  2258. * struct se_tasks to the selected transport.
  2259. */
  2260. check_depth:
  2261. if (!atomic_read(&dev->depth_left))
  2262. return transport_tcq_window_closed(dev);
  2263. dev->dev_tcq_window_closed = 0;
  2264. spin_lock_irq(&dev->execute_task_lock);
  2265. if (list_empty(&dev->execute_task_list)) {
  2266. spin_unlock_irq(&dev->execute_task_lock);
  2267. return 0;
  2268. }
  2269. task = list_first_entry(&dev->execute_task_list,
  2270. struct se_task, t_execute_list);
  2271. list_del(&task->t_execute_list);
  2272. atomic_set(&task->task_execute_queue, 0);
  2273. atomic_dec(&dev->execute_tasks);
  2274. spin_unlock_irq(&dev->execute_task_lock);
  2275. atomic_dec(&dev->depth_left);
  2276. cmd = task->task_se_cmd;
  2277. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2278. atomic_set(&task->task_active, 1);
  2279. atomic_set(&task->task_sent, 1);
  2280. atomic_inc(&cmd->t_task_cdbs_sent);
  2281. if (atomic_read(&cmd->t_task_cdbs_sent) ==
  2282. cmd->t_task_list_num)
  2283. atomic_set(&cmd->transport_sent, 1);
  2284. transport_start_task_timer(task);
  2285. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2286. /*
  2287. * The struct se_cmd->transport_emulate_cdb() function pointer is used
  2288. * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
  2289. * struct se_subsystem_api->do_task() caller below.
  2290. */
  2291. if (cmd->transport_emulate_cdb) {
  2292. error = cmd->transport_emulate_cdb(cmd);
  2293. if (error != 0) {
  2294. cmd->transport_error_status = error;
  2295. atomic_set(&task->task_active, 0);
  2296. atomic_set(&cmd->transport_sent, 0);
  2297. transport_stop_tasks_for_cmd(cmd);
  2298. transport_generic_request_failure(cmd, dev, 0, 1);
  2299. goto check_depth;
  2300. }
  2301. /*
  2302. * Handle the successful completion for transport_emulate_cdb()
  2303. * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
  2304. * Otherwise the caller is expected to complete the task with
  2305. * proper status.
  2306. */
  2307. if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
  2308. cmd->scsi_status = SAM_STAT_GOOD;
  2309. task->task_scsi_status = GOOD;
  2310. transport_complete_task(task, 1);
  2311. }
  2312. } else {
  2313. /*
  2314. * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
  2315. * RAMDISK we use the internal transport_emulate_control_cdb() logic
  2316. * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
  2317. * LUN emulation code.
  2318. *
  2319. * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
  2320. * call ->do_task() directly and let the underlying TCM subsystem plugin
  2321. * code handle the CDB emulation.
  2322. */
  2323. if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
  2324. (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2325. error = transport_emulate_control_cdb(task);
  2326. else
  2327. error = dev->transport->do_task(task);
  2328. if (error != 0) {
  2329. cmd->transport_error_status = error;
  2330. atomic_set(&task->task_active, 0);
  2331. atomic_set(&cmd->transport_sent, 0);
  2332. transport_stop_tasks_for_cmd(cmd);
  2333. transport_generic_request_failure(cmd, dev, 0, 1);
  2334. }
  2335. }
  2336. goto check_depth;
  2337. return 0;
  2338. }
  2339. void transport_new_cmd_failure(struct se_cmd *se_cmd)
  2340. {
  2341. unsigned long flags;
  2342. /*
  2343. * Any unsolicited data will get dumped for failed command inside of
  2344. * the fabric plugin
  2345. */
  2346. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  2347. se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
  2348. se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2349. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  2350. }
  2351. static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
  2352. static inline u32 transport_get_sectors_6(
  2353. unsigned char *cdb,
  2354. struct se_cmd *cmd,
  2355. int *ret)
  2356. {
  2357. struct se_device *dev = cmd->se_dev;
  2358. /*
  2359. * Assume TYPE_DISK for non struct se_device objects.
  2360. * Use 8-bit sector value.
  2361. */
  2362. if (!dev)
  2363. goto type_disk;
  2364. /*
  2365. * Use 24-bit allocation length for TYPE_TAPE.
  2366. */
  2367. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2368. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  2369. /*
  2370. * Everything else assume TYPE_DISK Sector CDB location.
  2371. * Use 8-bit sector value.
  2372. */
  2373. type_disk:
  2374. return (u32)cdb[4];
  2375. }
  2376. static inline u32 transport_get_sectors_10(
  2377. unsigned char *cdb,
  2378. struct se_cmd *cmd,
  2379. int *ret)
  2380. {
  2381. struct se_device *dev = cmd->se_dev;
  2382. /*
  2383. * Assume TYPE_DISK for non struct se_device objects.
  2384. * Use 16-bit sector value.
  2385. */
  2386. if (!dev)
  2387. goto type_disk;
  2388. /*
  2389. * XXX_10 is not defined in SSC, throw an exception
  2390. */
  2391. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2392. *ret = -EINVAL;
  2393. return 0;
  2394. }
  2395. /*
  2396. * Everything else assume TYPE_DISK Sector CDB location.
  2397. * Use 16-bit sector value.
  2398. */
  2399. type_disk:
  2400. return (u32)(cdb[7] << 8) + cdb[8];
  2401. }
  2402. static inline u32 transport_get_sectors_12(
  2403. unsigned char *cdb,
  2404. struct se_cmd *cmd,
  2405. int *ret)
  2406. {
  2407. struct se_device *dev = cmd->se_dev;
  2408. /*
  2409. * Assume TYPE_DISK for non struct se_device objects.
  2410. * Use 32-bit sector value.
  2411. */
  2412. if (!dev)
  2413. goto type_disk;
  2414. /*
  2415. * XXX_12 is not defined in SSC, throw an exception
  2416. */
  2417. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2418. *ret = -EINVAL;
  2419. return 0;
  2420. }
  2421. /*
  2422. * Everything else assume TYPE_DISK Sector CDB location.
  2423. * Use 32-bit sector value.
  2424. */
  2425. type_disk:
  2426. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  2427. }
  2428. static inline u32 transport_get_sectors_16(
  2429. unsigned char *cdb,
  2430. struct se_cmd *cmd,
  2431. int *ret)
  2432. {
  2433. struct se_device *dev = cmd->se_dev;
  2434. /*
  2435. * Assume TYPE_DISK for non struct se_device objects.
  2436. * Use 32-bit sector value.
  2437. */
  2438. if (!dev)
  2439. goto type_disk;
  2440. /*
  2441. * Use 24-bit allocation length for TYPE_TAPE.
  2442. */
  2443. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2444. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2445. type_disk:
  2446. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2447. (cdb[12] << 8) + cdb[13];
  2448. }
  2449. /*
  2450. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2451. */
  2452. static inline u32 transport_get_sectors_32(
  2453. unsigned char *cdb,
  2454. struct se_cmd *cmd,
  2455. int *ret)
  2456. {
  2457. /*
  2458. * Assume TYPE_DISK for non struct se_device objects.
  2459. * Use 32-bit sector value.
  2460. */
  2461. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2462. (cdb[30] << 8) + cdb[31];
  2463. }
  2464. static inline u32 transport_get_size(
  2465. u32 sectors,
  2466. unsigned char *cdb,
  2467. struct se_cmd *cmd)
  2468. {
  2469. struct se_device *dev = cmd->se_dev;
  2470. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2471. if (cdb[1] & 1) { /* sectors */
  2472. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2473. } else /* bytes */
  2474. return sectors;
  2475. }
  2476. #if 0
  2477. printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
  2478. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
  2479. dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  2480. dev->transport->name);
  2481. #endif
  2482. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2483. }
  2484. unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
  2485. {
  2486. unsigned char result = 0;
  2487. /*
  2488. * MSB
  2489. */
  2490. if ((val[0] >= 'a') && (val[0] <= 'f'))
  2491. result = ((val[0] - 'a' + 10) & 0xf) << 4;
  2492. else
  2493. if ((val[0] >= 'A') && (val[0] <= 'F'))
  2494. result = ((val[0] - 'A' + 10) & 0xf) << 4;
  2495. else /* digit */
  2496. result = ((val[0] - '0') & 0xf) << 4;
  2497. /*
  2498. * LSB
  2499. */
  2500. if ((val[1] >= 'a') && (val[1] <= 'f'))
  2501. result |= ((val[1] - 'a' + 10) & 0xf);
  2502. else
  2503. if ((val[1] >= 'A') && (val[1] <= 'F'))
  2504. result |= ((val[1] - 'A' + 10) & 0xf);
  2505. else /* digit */
  2506. result |= ((val[1] - '0') & 0xf);
  2507. return result;
  2508. }
  2509. EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
  2510. static void transport_xor_callback(struct se_cmd *cmd)
  2511. {
  2512. unsigned char *buf, *addr;
  2513. struct se_mem *se_mem;
  2514. unsigned int offset;
  2515. int i;
  2516. /*
  2517. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2518. *
  2519. * 1) read the specified logical block(s);
  2520. * 2) transfer logical blocks from the data-out buffer;
  2521. * 3) XOR the logical blocks transferred from the data-out buffer with
  2522. * the logical blocks read, storing the resulting XOR data in a buffer;
  2523. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2524. * blocks transferred from the data-out buffer; and
  2525. * 5) transfer the resulting XOR data to the data-in buffer.
  2526. */
  2527. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2528. if (!(buf)) {
  2529. printk(KERN_ERR "Unable to allocate xor_callback buf\n");
  2530. return;
  2531. }
  2532. /*
  2533. * Copy the scatterlist WRITE buffer located at cmd->t_mem_list
  2534. * into the locally allocated *buf
  2535. */
  2536. transport_memcpy_se_mem_read_contig(buf, &cmd->t_mem_list,
  2537. cmd->data_length);
  2538. /*
  2539. * Now perform the XOR against the BIDI read memory located at
  2540. * cmd->t_mem_bidi_list
  2541. */
  2542. offset = 0;
  2543. list_for_each_entry(se_mem, &cmd->t_mem_bidi_list, se_list) {
  2544. addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
  2545. if (!(addr))
  2546. goto out;
  2547. for (i = 0; i < se_mem->se_len; i++)
  2548. *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
  2549. offset += se_mem->se_len;
  2550. kunmap_atomic(addr, KM_USER0);
  2551. }
  2552. out:
  2553. kfree(buf);
  2554. }
  2555. /*
  2556. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2557. */
  2558. static int transport_get_sense_data(struct se_cmd *cmd)
  2559. {
  2560. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2561. struct se_device *dev;
  2562. struct se_task *task = NULL, *task_tmp;
  2563. unsigned long flags;
  2564. u32 offset = 0;
  2565. WARN_ON(!cmd->se_lun);
  2566. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2567. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2568. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2569. return 0;
  2570. }
  2571. list_for_each_entry_safe(task, task_tmp,
  2572. &cmd->t_task_list, t_list) {
  2573. if (!task->task_sense)
  2574. continue;
  2575. dev = task->se_dev;
  2576. if (!(dev))
  2577. continue;
  2578. if (!dev->transport->get_sense_buffer) {
  2579. printk(KERN_ERR "dev->transport->get_sense_buffer"
  2580. " is NULL\n");
  2581. continue;
  2582. }
  2583. sense_buffer = dev->transport->get_sense_buffer(task);
  2584. if (!(sense_buffer)) {
  2585. printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
  2586. " sense buffer for task with sense\n",
  2587. cmd->se_tfo->get_task_tag(cmd), task->task_no);
  2588. continue;
  2589. }
  2590. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2591. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  2592. TRANSPORT_SENSE_BUFFER);
  2593. memcpy(&buffer[offset], sense_buffer,
  2594. TRANSPORT_SENSE_BUFFER);
  2595. cmd->scsi_status = task->task_scsi_status;
  2596. /* Automatically padded */
  2597. cmd->scsi_sense_length =
  2598. (TRANSPORT_SENSE_BUFFER + offset);
  2599. printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
  2600. " and sense\n",
  2601. dev->se_hba->hba_id, dev->transport->name,
  2602. cmd->scsi_status);
  2603. return 0;
  2604. }
  2605. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2606. return -1;
  2607. }
  2608. static int
  2609. transport_handle_reservation_conflict(struct se_cmd *cmd)
  2610. {
  2611. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2612. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2613. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2614. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2615. /*
  2616. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  2617. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  2618. * CONFLICT STATUS.
  2619. *
  2620. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  2621. */
  2622. if (cmd->se_sess &&
  2623. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  2624. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  2625. cmd->orig_fe_lun, 0x2C,
  2626. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  2627. return -EINVAL;
  2628. }
  2629. /* transport_generic_cmd_sequencer():
  2630. *
  2631. * Generic Command Sequencer that should work for most DAS transport
  2632. * drivers.
  2633. *
  2634. * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
  2635. * RX Thread.
  2636. *
  2637. * FIXME: Need to support other SCSI OPCODES where as well.
  2638. */
  2639. static int transport_generic_cmd_sequencer(
  2640. struct se_cmd *cmd,
  2641. unsigned char *cdb)
  2642. {
  2643. struct se_device *dev = cmd->se_dev;
  2644. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2645. int ret = 0, sector_ret = 0, passthrough;
  2646. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2647. u16 service_action;
  2648. u8 alua_ascq = 0;
  2649. /*
  2650. * Check for an existing UNIT ATTENTION condition
  2651. */
  2652. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2653. cmd->transport_wait_for_tasks =
  2654. &transport_nop_wait_for_tasks;
  2655. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2656. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2657. return -EINVAL;
  2658. }
  2659. /*
  2660. * Check status of Asymmetric Logical Unit Assignment port
  2661. */
  2662. ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
  2663. if (ret != 0) {
  2664. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2665. /*
  2666. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  2667. * The ALUA additional sense code qualifier (ASCQ) is determined
  2668. * by the ALUA primary or secondary access state..
  2669. */
  2670. if (ret > 0) {
  2671. #if 0
  2672. printk(KERN_INFO "[%s]: ALUA TG Port not available,"
  2673. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2674. cmd->se_tfo->get_fabric_name(), alua_ascq);
  2675. #endif
  2676. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2677. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2678. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2679. return -EINVAL;
  2680. }
  2681. goto out_invalid_cdb_field;
  2682. }
  2683. /*
  2684. * Check status for SPC-3 Persistent Reservations
  2685. */
  2686. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2687. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  2688. cmd, cdb, pr_reg_type) != 0)
  2689. return transport_handle_reservation_conflict(cmd);
  2690. /*
  2691. * This means the CDB is allowed for the SCSI Initiator port
  2692. * when said port is *NOT* holding the legacy SPC-2 or
  2693. * SPC-3 Persistent Reservation.
  2694. */
  2695. }
  2696. switch (cdb[0]) {
  2697. case READ_6:
  2698. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2699. if (sector_ret)
  2700. goto out_unsupported_cdb;
  2701. size = transport_get_size(sectors, cdb, cmd);
  2702. cmd->transport_split_cdb = &split_cdb_XX_6;
  2703. cmd->t_task_lba = transport_lba_21(cdb);
  2704. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2705. break;
  2706. case READ_10:
  2707. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2708. if (sector_ret)
  2709. goto out_unsupported_cdb;
  2710. size = transport_get_size(sectors, cdb, cmd);
  2711. cmd->transport_split_cdb = &split_cdb_XX_10;
  2712. cmd->t_task_lba = transport_lba_32(cdb);
  2713. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2714. break;
  2715. case READ_12:
  2716. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2717. if (sector_ret)
  2718. goto out_unsupported_cdb;
  2719. size = transport_get_size(sectors, cdb, cmd);
  2720. cmd->transport_split_cdb = &split_cdb_XX_12;
  2721. cmd->t_task_lba = transport_lba_32(cdb);
  2722. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2723. break;
  2724. case READ_16:
  2725. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2726. if (sector_ret)
  2727. goto out_unsupported_cdb;
  2728. size = transport_get_size(sectors, cdb, cmd);
  2729. cmd->transport_split_cdb = &split_cdb_XX_16;
  2730. cmd->t_task_lba = transport_lba_64(cdb);
  2731. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2732. break;
  2733. case WRITE_6:
  2734. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2735. if (sector_ret)
  2736. goto out_unsupported_cdb;
  2737. size = transport_get_size(sectors, cdb, cmd);
  2738. cmd->transport_split_cdb = &split_cdb_XX_6;
  2739. cmd->t_task_lba = transport_lba_21(cdb);
  2740. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2741. break;
  2742. case WRITE_10:
  2743. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2744. if (sector_ret)
  2745. goto out_unsupported_cdb;
  2746. size = transport_get_size(sectors, cdb, cmd);
  2747. cmd->transport_split_cdb = &split_cdb_XX_10;
  2748. cmd->t_task_lba = transport_lba_32(cdb);
  2749. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2750. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2751. break;
  2752. case WRITE_12:
  2753. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2754. if (sector_ret)
  2755. goto out_unsupported_cdb;
  2756. size = transport_get_size(sectors, cdb, cmd);
  2757. cmd->transport_split_cdb = &split_cdb_XX_12;
  2758. cmd->t_task_lba = transport_lba_32(cdb);
  2759. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2760. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2761. break;
  2762. case WRITE_16:
  2763. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2764. if (sector_ret)
  2765. goto out_unsupported_cdb;
  2766. size = transport_get_size(sectors, cdb, cmd);
  2767. cmd->transport_split_cdb = &split_cdb_XX_16;
  2768. cmd->t_task_lba = transport_lba_64(cdb);
  2769. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2770. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2771. break;
  2772. case XDWRITEREAD_10:
  2773. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2774. !(cmd->t_tasks_bidi))
  2775. goto out_invalid_cdb_field;
  2776. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2777. if (sector_ret)
  2778. goto out_unsupported_cdb;
  2779. size = transport_get_size(sectors, cdb, cmd);
  2780. cmd->transport_split_cdb = &split_cdb_XX_10;
  2781. cmd->t_task_lba = transport_lba_32(cdb);
  2782. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2783. passthrough = (dev->transport->transport_type ==
  2784. TRANSPORT_PLUGIN_PHBA_PDEV);
  2785. /*
  2786. * Skip the remaining assignments for TCM/PSCSI passthrough
  2787. */
  2788. if (passthrough)
  2789. break;
  2790. /*
  2791. * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
  2792. */
  2793. cmd->transport_complete_callback = &transport_xor_callback;
  2794. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2795. break;
  2796. case VARIABLE_LENGTH_CMD:
  2797. service_action = get_unaligned_be16(&cdb[8]);
  2798. /*
  2799. * Determine if this is TCM/PSCSI device and we should disable
  2800. * internal emulation for this CDB.
  2801. */
  2802. passthrough = (dev->transport->transport_type ==
  2803. TRANSPORT_PLUGIN_PHBA_PDEV);
  2804. switch (service_action) {
  2805. case XDWRITEREAD_32:
  2806. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2807. if (sector_ret)
  2808. goto out_unsupported_cdb;
  2809. size = transport_get_size(sectors, cdb, cmd);
  2810. /*
  2811. * Use WRITE_32 and READ_32 opcodes for the emulated
  2812. * XDWRITE_READ_32 logic.
  2813. */
  2814. cmd->transport_split_cdb = &split_cdb_XX_32;
  2815. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2816. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2817. /*
  2818. * Skip the remaining assignments for TCM/PSCSI passthrough
  2819. */
  2820. if (passthrough)
  2821. break;
  2822. /*
  2823. * Setup BIDI XOR callback to be run during
  2824. * transport_generic_complete_ok()
  2825. */
  2826. cmd->transport_complete_callback = &transport_xor_callback;
  2827. cmd->t_tasks_fua = (cdb[10] & 0x8);
  2828. break;
  2829. case WRITE_SAME_32:
  2830. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2831. if (sector_ret)
  2832. goto out_unsupported_cdb;
  2833. if (sectors != 0)
  2834. size = transport_get_size(sectors, cdb, cmd);
  2835. else
  2836. size = dev->se_sub_dev->se_dev_attrib.block_size;
  2837. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2838. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2839. /*
  2840. * Skip the remaining assignments for TCM/PSCSI passthrough
  2841. */
  2842. if (passthrough)
  2843. break;
  2844. if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
  2845. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  2846. " bits not supported for Block Discard"
  2847. " Emulation\n");
  2848. goto out_invalid_cdb_field;
  2849. }
  2850. /*
  2851. * Currently for the emulated case we only accept
  2852. * tpws with the UNMAP=1 bit set.
  2853. */
  2854. if (!(cdb[10] & 0x08)) {
  2855. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
  2856. " supported for Block Discard Emulation\n");
  2857. goto out_invalid_cdb_field;
  2858. }
  2859. break;
  2860. default:
  2861. printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
  2862. " 0x%04x not supported\n", service_action);
  2863. goto out_unsupported_cdb;
  2864. }
  2865. break;
  2866. case MAINTENANCE_IN:
  2867. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2868. /* MAINTENANCE_IN from SCC-2 */
  2869. /*
  2870. * Check for emulated MI_REPORT_TARGET_PGS.
  2871. */
  2872. if (cdb[1] == MI_REPORT_TARGET_PGS) {
  2873. cmd->transport_emulate_cdb =
  2874. (su_dev->t10_alua.alua_type ==
  2875. SPC3_ALUA_EMULATED) ?
  2876. core_emulate_report_target_port_groups :
  2877. NULL;
  2878. }
  2879. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2880. (cdb[8] << 8) | cdb[9];
  2881. } else {
  2882. /* GPCMD_SEND_KEY from multi media commands */
  2883. size = (cdb[8] << 8) + cdb[9];
  2884. }
  2885. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2886. break;
  2887. case MODE_SELECT:
  2888. size = cdb[4];
  2889. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2890. break;
  2891. case MODE_SELECT_10:
  2892. size = (cdb[7] << 8) + cdb[8];
  2893. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2894. break;
  2895. case MODE_SENSE:
  2896. size = cdb[4];
  2897. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2898. break;
  2899. case MODE_SENSE_10:
  2900. case GPCMD_READ_BUFFER_CAPACITY:
  2901. case GPCMD_SEND_OPC:
  2902. case LOG_SELECT:
  2903. case LOG_SENSE:
  2904. size = (cdb[7] << 8) + cdb[8];
  2905. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2906. break;
  2907. case READ_BLOCK_LIMITS:
  2908. size = READ_BLOCK_LEN;
  2909. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2910. break;
  2911. case GPCMD_GET_CONFIGURATION:
  2912. case GPCMD_READ_FORMAT_CAPACITIES:
  2913. case GPCMD_READ_DISC_INFO:
  2914. case GPCMD_READ_TRACK_RZONE_INFO:
  2915. size = (cdb[7] << 8) + cdb[8];
  2916. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2917. break;
  2918. case PERSISTENT_RESERVE_IN:
  2919. case PERSISTENT_RESERVE_OUT:
  2920. cmd->transport_emulate_cdb =
  2921. (su_dev->t10_pr.res_type ==
  2922. SPC3_PERSISTENT_RESERVATIONS) ?
  2923. core_scsi3_emulate_pr : NULL;
  2924. size = (cdb[7] << 8) + cdb[8];
  2925. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2926. break;
  2927. case GPCMD_MECHANISM_STATUS:
  2928. case GPCMD_READ_DVD_STRUCTURE:
  2929. size = (cdb[8] << 8) + cdb[9];
  2930. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2931. break;
  2932. case READ_POSITION:
  2933. size = READ_POSITION_LEN;
  2934. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2935. break;
  2936. case MAINTENANCE_OUT:
  2937. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2938. /* MAINTENANCE_OUT from SCC-2
  2939. *
  2940. * Check for emulated MO_SET_TARGET_PGS.
  2941. */
  2942. if (cdb[1] == MO_SET_TARGET_PGS) {
  2943. cmd->transport_emulate_cdb =
  2944. (su_dev->t10_alua.alua_type ==
  2945. SPC3_ALUA_EMULATED) ?
  2946. core_emulate_set_target_port_groups :
  2947. NULL;
  2948. }
  2949. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2950. (cdb[8] << 8) | cdb[9];
  2951. } else {
  2952. /* GPCMD_REPORT_KEY from multi media commands */
  2953. size = (cdb[8] << 8) + cdb[9];
  2954. }
  2955. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2956. break;
  2957. case INQUIRY:
  2958. size = (cdb[3] << 8) + cdb[4];
  2959. /*
  2960. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2961. * See spc4r17 section 5.3
  2962. */
  2963. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2964. cmd->sam_task_attr = MSG_HEAD_TAG;
  2965. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2966. break;
  2967. case READ_BUFFER:
  2968. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2969. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2970. break;
  2971. case READ_CAPACITY:
  2972. size = READ_CAP_LEN;
  2973. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2974. break;
  2975. case READ_MEDIA_SERIAL_NUMBER:
  2976. case SECURITY_PROTOCOL_IN:
  2977. case SECURITY_PROTOCOL_OUT:
  2978. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2979. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2980. break;
  2981. case SERVICE_ACTION_IN:
  2982. case ACCESS_CONTROL_IN:
  2983. case ACCESS_CONTROL_OUT:
  2984. case EXTENDED_COPY:
  2985. case READ_ATTRIBUTE:
  2986. case RECEIVE_COPY_RESULTS:
  2987. case WRITE_ATTRIBUTE:
  2988. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2989. (cdb[12] << 8) | cdb[13];
  2990. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2991. break;
  2992. case RECEIVE_DIAGNOSTIC:
  2993. case SEND_DIAGNOSTIC:
  2994. size = (cdb[3] << 8) | cdb[4];
  2995. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2996. break;
  2997. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2998. #if 0
  2999. case GPCMD_READ_CD:
  3000. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3001. size = (2336 * sectors);
  3002. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3003. break;
  3004. #endif
  3005. case READ_TOC:
  3006. size = cdb[8];
  3007. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3008. break;
  3009. case REQUEST_SENSE:
  3010. size = cdb[4];
  3011. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3012. break;
  3013. case READ_ELEMENT_STATUS:
  3014. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  3015. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3016. break;
  3017. case WRITE_BUFFER:
  3018. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3019. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3020. break;
  3021. case RESERVE:
  3022. case RESERVE_10:
  3023. /*
  3024. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  3025. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3026. */
  3027. if (cdb[0] == RESERVE_10)
  3028. size = (cdb[7] << 8) | cdb[8];
  3029. else
  3030. size = cmd->data_length;
  3031. /*
  3032. * Setup the legacy emulated handler for SPC-2 and
  3033. * >= SPC-3 compatible reservation handling (CRH=1)
  3034. * Otherwise, we assume the underlying SCSI logic is
  3035. * is running in SPC_PASSTHROUGH, and wants reservations
  3036. * emulation disabled.
  3037. */
  3038. cmd->transport_emulate_cdb =
  3039. (su_dev->t10_pr.res_type !=
  3040. SPC_PASSTHROUGH) ?
  3041. core_scsi2_emulate_crh : NULL;
  3042. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3043. break;
  3044. case RELEASE:
  3045. case RELEASE_10:
  3046. /*
  3047. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  3048. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3049. */
  3050. if (cdb[0] == RELEASE_10)
  3051. size = (cdb[7] << 8) | cdb[8];
  3052. else
  3053. size = cmd->data_length;
  3054. cmd->transport_emulate_cdb =
  3055. (su_dev->t10_pr.res_type !=
  3056. SPC_PASSTHROUGH) ?
  3057. core_scsi2_emulate_crh : NULL;
  3058. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3059. break;
  3060. case SYNCHRONIZE_CACHE:
  3061. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  3062. /*
  3063. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  3064. */
  3065. if (cdb[0] == SYNCHRONIZE_CACHE) {
  3066. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  3067. cmd->t_task_lba = transport_lba_32(cdb);
  3068. } else {
  3069. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3070. cmd->t_task_lba = transport_lba_64(cdb);
  3071. }
  3072. if (sector_ret)
  3073. goto out_unsupported_cdb;
  3074. size = transport_get_size(sectors, cdb, cmd);
  3075. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3076. /*
  3077. * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
  3078. */
  3079. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  3080. break;
  3081. /*
  3082. * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
  3083. * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
  3084. */
  3085. cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
  3086. /*
  3087. * Check to ensure that LBA + Range does not exceed past end of
  3088. * device.
  3089. */
  3090. if (!transport_cmd_get_valid_sectors(cmd))
  3091. goto out_invalid_cdb_field;
  3092. break;
  3093. case UNMAP:
  3094. size = get_unaligned_be16(&cdb[7]);
  3095. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3096. break;
  3097. case WRITE_SAME_16:
  3098. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3099. if (sector_ret)
  3100. goto out_unsupported_cdb;
  3101. if (sectors != 0)
  3102. size = transport_get_size(sectors, cdb, cmd);
  3103. else
  3104. size = dev->se_sub_dev->se_dev_attrib.block_size;
  3105. cmd->t_task_lba = get_unaligned_be16(&cdb[2]);
  3106. passthrough = (dev->transport->transport_type ==
  3107. TRANSPORT_PLUGIN_PHBA_PDEV);
  3108. /*
  3109. * Determine if the received WRITE_SAME_16 is used to for direct
  3110. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  3111. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  3112. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
  3113. * TCM/FILEIO subsystem plugin backstores.
  3114. */
  3115. if (!(passthrough)) {
  3116. if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
  3117. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  3118. " bits not supported for Block Discard"
  3119. " Emulation\n");
  3120. goto out_invalid_cdb_field;
  3121. }
  3122. /*
  3123. * Currently for the emulated case we only accept
  3124. * tpws with the UNMAP=1 bit set.
  3125. */
  3126. if (!(cdb[1] & 0x08)) {
  3127. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
  3128. " supported for Block Discard Emulation\n");
  3129. goto out_invalid_cdb_field;
  3130. }
  3131. }
  3132. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3133. break;
  3134. case ALLOW_MEDIUM_REMOVAL:
  3135. case GPCMD_CLOSE_TRACK:
  3136. case ERASE:
  3137. case INITIALIZE_ELEMENT_STATUS:
  3138. case GPCMD_LOAD_UNLOAD:
  3139. case REZERO_UNIT:
  3140. case SEEK_10:
  3141. case GPCMD_SET_SPEED:
  3142. case SPACE:
  3143. case START_STOP:
  3144. case TEST_UNIT_READY:
  3145. case VERIFY:
  3146. case WRITE_FILEMARKS:
  3147. case MOVE_MEDIUM:
  3148. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3149. break;
  3150. case REPORT_LUNS:
  3151. cmd->transport_emulate_cdb =
  3152. transport_core_report_lun_response;
  3153. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  3154. /*
  3155. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  3156. * See spc4r17 section 5.3
  3157. */
  3158. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3159. cmd->sam_task_attr = MSG_HEAD_TAG;
  3160. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3161. break;
  3162. default:
  3163. printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
  3164. " 0x%02x, sending CHECK_CONDITION.\n",
  3165. cmd->se_tfo->get_fabric_name(), cdb[0]);
  3166. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  3167. goto out_unsupported_cdb;
  3168. }
  3169. if (size != cmd->data_length) {
  3170. printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
  3171. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  3172. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  3173. cmd->data_length, size, cdb[0]);
  3174. cmd->cmd_spdtl = size;
  3175. if (cmd->data_direction == DMA_TO_DEVICE) {
  3176. printk(KERN_ERR "Rejecting underflow/overflow"
  3177. " WRITE data\n");
  3178. goto out_invalid_cdb_field;
  3179. }
  3180. /*
  3181. * Reject READ_* or WRITE_* with overflow/underflow for
  3182. * type SCF_SCSI_DATA_SG_IO_CDB.
  3183. */
  3184. if (!(ret) && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
  3185. printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
  3186. " CDB on non 512-byte sector setup subsystem"
  3187. " plugin: %s\n", dev->transport->name);
  3188. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  3189. goto out_invalid_cdb_field;
  3190. }
  3191. if (size > cmd->data_length) {
  3192. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  3193. cmd->residual_count = (size - cmd->data_length);
  3194. } else {
  3195. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  3196. cmd->residual_count = (cmd->data_length - size);
  3197. }
  3198. cmd->data_length = size;
  3199. }
  3200. /* Let's limit control cdbs to a page, for simplicity's sake. */
  3201. if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
  3202. size > PAGE_SIZE)
  3203. goto out_invalid_cdb_field;
  3204. transport_set_supported_SAM_opcode(cmd);
  3205. return ret;
  3206. out_unsupported_cdb:
  3207. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3208. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  3209. return -EINVAL;
  3210. out_invalid_cdb_field:
  3211. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3212. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3213. return -EINVAL;
  3214. }
  3215. static inline void transport_release_tasks(struct se_cmd *);
  3216. static void transport_memcpy_se_mem_read_contig(
  3217. unsigned char *dst,
  3218. struct list_head *se_mem_list,
  3219. u32 tot_len)
  3220. {
  3221. struct se_mem *se_mem;
  3222. void *src;
  3223. u32 length;
  3224. list_for_each_entry(se_mem, se_mem_list, se_list) {
  3225. length = min_t(u32, se_mem->se_len, tot_len);
  3226. src = page_address(se_mem->se_page) + se_mem->se_off;
  3227. memcpy(dst, src, length);
  3228. tot_len -= length;
  3229. if (!tot_len)
  3230. break;
  3231. dst += length;
  3232. }
  3233. }
  3234. /*
  3235. * Called from transport_generic_complete_ok() and
  3236. * transport_generic_request_failure() to determine which dormant/delayed
  3237. * and ordered cmds need to have their tasks added to the execution queue.
  3238. */
  3239. static void transport_complete_task_attr(struct se_cmd *cmd)
  3240. {
  3241. struct se_device *dev = cmd->se_dev;
  3242. struct se_cmd *cmd_p, *cmd_tmp;
  3243. int new_active_tasks = 0;
  3244. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  3245. atomic_dec(&dev->simple_cmds);
  3246. smp_mb__after_atomic_dec();
  3247. dev->dev_cur_ordered_id++;
  3248. DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
  3249. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  3250. cmd->se_ordered_id);
  3251. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  3252. atomic_dec(&dev->dev_hoq_count);
  3253. smp_mb__after_atomic_dec();
  3254. dev->dev_cur_ordered_id++;
  3255. DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
  3256. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  3257. cmd->se_ordered_id);
  3258. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  3259. spin_lock(&dev->ordered_cmd_lock);
  3260. list_del(&cmd->se_ordered_node);
  3261. atomic_dec(&dev->dev_ordered_sync);
  3262. smp_mb__after_atomic_dec();
  3263. spin_unlock(&dev->ordered_cmd_lock);
  3264. dev->dev_cur_ordered_id++;
  3265. DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
  3266. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  3267. }
  3268. /*
  3269. * Process all commands up to the last received
  3270. * ORDERED task attribute which requires another blocking
  3271. * boundary
  3272. */
  3273. spin_lock(&dev->delayed_cmd_lock);
  3274. list_for_each_entry_safe(cmd_p, cmd_tmp,
  3275. &dev->delayed_cmd_list, se_delayed_node) {
  3276. list_del(&cmd_p->se_delayed_node);
  3277. spin_unlock(&dev->delayed_cmd_lock);
  3278. DEBUG_STA("Calling add_tasks() for"
  3279. " cmd_p: 0x%02x Task Attr: 0x%02x"
  3280. " Dormant -> Active, se_ordered_id: %u\n",
  3281. T_TASK(cmd_p)->t_task_cdb[0],
  3282. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  3283. transport_add_tasks_from_cmd(cmd_p);
  3284. new_active_tasks++;
  3285. spin_lock(&dev->delayed_cmd_lock);
  3286. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  3287. break;
  3288. }
  3289. spin_unlock(&dev->delayed_cmd_lock);
  3290. /*
  3291. * If new tasks have become active, wake up the transport thread
  3292. * to do the processing of the Active tasks.
  3293. */
  3294. if (new_active_tasks != 0)
  3295. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  3296. }
  3297. static int transport_complete_qf(struct se_cmd *cmd)
  3298. {
  3299. int ret = 0;
  3300. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
  3301. return cmd->se_tfo->queue_status(cmd);
  3302. switch (cmd->data_direction) {
  3303. case DMA_FROM_DEVICE:
  3304. ret = cmd->se_tfo->queue_data_in(cmd);
  3305. break;
  3306. case DMA_TO_DEVICE:
  3307. if (!list_empty(&cmd->t_mem_bidi_list)) {
  3308. ret = cmd->se_tfo->queue_data_in(cmd);
  3309. if (ret < 0)
  3310. return ret;
  3311. }
  3312. /* Fall through for DMA_TO_DEVICE */
  3313. case DMA_NONE:
  3314. ret = cmd->se_tfo->queue_status(cmd);
  3315. break;
  3316. default:
  3317. break;
  3318. }
  3319. return ret;
  3320. }
  3321. static void transport_handle_queue_full(
  3322. struct se_cmd *cmd,
  3323. struct se_device *dev,
  3324. int (*qf_callback)(struct se_cmd *))
  3325. {
  3326. spin_lock_irq(&dev->qf_cmd_lock);
  3327. cmd->se_cmd_flags |= SCF_EMULATE_QUEUE_FULL;
  3328. cmd->transport_qf_callback = qf_callback;
  3329. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  3330. atomic_inc(&dev->dev_qf_count);
  3331. smp_mb__after_atomic_inc();
  3332. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  3333. schedule_work(&cmd->se_dev->qf_work_queue);
  3334. }
  3335. static void transport_generic_complete_ok(struct se_cmd *cmd)
  3336. {
  3337. int reason = 0, ret;
  3338. /*
  3339. * Check if we need to move delayed/dormant tasks from cmds on the
  3340. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  3341. * Attribute.
  3342. */
  3343. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3344. transport_complete_task_attr(cmd);
  3345. /*
  3346. * Check to schedule QUEUE_FULL work, or execute an existing
  3347. * cmd->transport_qf_callback()
  3348. */
  3349. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  3350. schedule_work(&cmd->se_dev->qf_work_queue);
  3351. if (cmd->transport_qf_callback) {
  3352. ret = cmd->transport_qf_callback(cmd);
  3353. if (ret < 0)
  3354. goto queue_full;
  3355. cmd->transport_qf_callback = NULL;
  3356. goto done;
  3357. }
  3358. /*
  3359. * Check if we need to retrieve a sense buffer from
  3360. * the struct se_cmd in question.
  3361. */
  3362. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  3363. if (transport_get_sense_data(cmd) < 0)
  3364. reason = TCM_NON_EXISTENT_LUN;
  3365. /*
  3366. * Only set when an struct se_task->task_scsi_status returned
  3367. * a non GOOD status.
  3368. */
  3369. if (cmd->scsi_status) {
  3370. ret = transport_send_check_condition_and_sense(
  3371. cmd, reason, 1);
  3372. if (ret == -EAGAIN)
  3373. goto queue_full;
  3374. transport_lun_remove_cmd(cmd);
  3375. transport_cmd_check_stop_to_fabric(cmd);
  3376. return;
  3377. }
  3378. }
  3379. /*
  3380. * Check for a callback, used by amongst other things
  3381. * XDWRITE_READ_10 emulation.
  3382. */
  3383. if (cmd->transport_complete_callback)
  3384. cmd->transport_complete_callback(cmd);
  3385. switch (cmd->data_direction) {
  3386. case DMA_FROM_DEVICE:
  3387. spin_lock(&cmd->se_lun->lun_sep_lock);
  3388. if (cmd->se_lun->lun_sep) {
  3389. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3390. cmd->data_length;
  3391. }
  3392. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3393. ret = cmd->se_tfo->queue_data_in(cmd);
  3394. if (ret == -EAGAIN)
  3395. goto queue_full;
  3396. break;
  3397. case DMA_TO_DEVICE:
  3398. spin_lock(&cmd->se_lun->lun_sep_lock);
  3399. if (cmd->se_lun->lun_sep) {
  3400. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  3401. cmd->data_length;
  3402. }
  3403. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3404. /*
  3405. * Check if we need to send READ payload for BIDI-COMMAND
  3406. */
  3407. if (!list_empty(&cmd->t_mem_bidi_list)) {
  3408. spin_lock(&cmd->se_lun->lun_sep_lock);
  3409. if (cmd->se_lun->lun_sep) {
  3410. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3411. cmd->data_length;
  3412. }
  3413. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3414. ret = cmd->se_tfo->queue_data_in(cmd);
  3415. if (ret == -EAGAIN)
  3416. goto queue_full;
  3417. break;
  3418. }
  3419. /* Fall through for DMA_TO_DEVICE */
  3420. case DMA_NONE:
  3421. ret = cmd->se_tfo->queue_status(cmd);
  3422. if (ret == -EAGAIN)
  3423. goto queue_full;
  3424. break;
  3425. default:
  3426. break;
  3427. }
  3428. done:
  3429. transport_lun_remove_cmd(cmd);
  3430. transport_cmd_check_stop_to_fabric(cmd);
  3431. return;
  3432. queue_full:
  3433. printk(KERN_INFO "Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  3434. " data_direction: %d\n", cmd, cmd->data_direction);
  3435. transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf);
  3436. }
  3437. static void transport_free_dev_tasks(struct se_cmd *cmd)
  3438. {
  3439. struct se_task *task, *task_tmp;
  3440. unsigned long flags;
  3441. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3442. list_for_each_entry_safe(task, task_tmp,
  3443. &cmd->t_task_list, t_list) {
  3444. if (atomic_read(&task->task_active))
  3445. continue;
  3446. kfree(task->task_sg_bidi);
  3447. kfree(task->task_sg);
  3448. list_del(&task->t_list);
  3449. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3450. if (task->se_dev)
  3451. task->se_dev->transport->free_task(task);
  3452. else
  3453. printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
  3454. task->task_no);
  3455. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3456. }
  3457. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3458. }
  3459. static inline void transport_free_pages(struct se_cmd *cmd)
  3460. {
  3461. struct se_mem *se_mem, *se_mem_tmp;
  3462. int free_page = 1;
  3463. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3464. free_page = 0;
  3465. if (cmd->se_dev->transport->do_se_mem_map)
  3466. free_page = 0;
  3467. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3468. &cmd->t_mem_list, se_list) {
  3469. /*
  3470. * We only release call __free_page(struct se_mem->se_page) when
  3471. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3472. */
  3473. if (free_page)
  3474. __free_page(se_mem->se_page);
  3475. list_del(&se_mem->se_list);
  3476. kmem_cache_free(se_mem_cache, se_mem);
  3477. }
  3478. cmd->t_tasks_se_num = 0;
  3479. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3480. &cmd->t_mem_bidi_list, se_list) {
  3481. /*
  3482. * We only release call __free_page(struct se_mem->se_page) when
  3483. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3484. */
  3485. if (free_page)
  3486. __free_page(se_mem->se_page);
  3487. list_del(&se_mem->se_list);
  3488. kmem_cache_free(se_mem_cache, se_mem);
  3489. }
  3490. cmd->t_tasks_se_bidi_num = 0;
  3491. }
  3492. static inline void transport_release_tasks(struct se_cmd *cmd)
  3493. {
  3494. transport_free_dev_tasks(cmd);
  3495. }
  3496. static inline int transport_dec_and_check(struct se_cmd *cmd)
  3497. {
  3498. unsigned long flags;
  3499. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3500. if (atomic_read(&cmd->t_fe_count)) {
  3501. if (!(atomic_dec_and_test(&cmd->t_fe_count))) {
  3502. spin_unlock_irqrestore(&cmd->t_state_lock,
  3503. flags);
  3504. return 1;
  3505. }
  3506. }
  3507. if (atomic_read(&cmd->t_se_count)) {
  3508. if (!(atomic_dec_and_test(&cmd->t_se_count))) {
  3509. spin_unlock_irqrestore(&cmd->t_state_lock,
  3510. flags);
  3511. return 1;
  3512. }
  3513. }
  3514. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3515. return 0;
  3516. }
  3517. static void transport_release_fe_cmd(struct se_cmd *cmd)
  3518. {
  3519. unsigned long flags;
  3520. if (transport_dec_and_check(cmd))
  3521. return;
  3522. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3523. if (!(atomic_read(&cmd->transport_dev_active))) {
  3524. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3525. goto free_pages;
  3526. }
  3527. atomic_set(&cmd->transport_dev_active, 0);
  3528. transport_all_task_dev_remove_state(cmd);
  3529. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3530. transport_release_tasks(cmd);
  3531. free_pages:
  3532. transport_free_pages(cmd);
  3533. transport_free_se_cmd(cmd);
  3534. cmd->se_tfo->release_cmd(cmd);
  3535. }
  3536. static int
  3537. transport_generic_remove(struct se_cmd *cmd, int session_reinstatement)
  3538. {
  3539. unsigned long flags;
  3540. if (transport_dec_and_check(cmd)) {
  3541. if (session_reinstatement) {
  3542. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3543. transport_all_task_dev_remove_state(cmd);
  3544. spin_unlock_irqrestore(&cmd->t_state_lock,
  3545. flags);
  3546. }
  3547. return 1;
  3548. }
  3549. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3550. if (!(atomic_read(&cmd->transport_dev_active))) {
  3551. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3552. goto free_pages;
  3553. }
  3554. atomic_set(&cmd->transport_dev_active, 0);
  3555. transport_all_task_dev_remove_state(cmd);
  3556. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3557. transport_release_tasks(cmd);
  3558. free_pages:
  3559. transport_free_pages(cmd);
  3560. transport_release_cmd(cmd);
  3561. return 0;
  3562. }
  3563. /*
  3564. * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
  3565. * @cmd: Associated se_cmd descriptor
  3566. * @mem: SGL style memory for TCM WRITE / READ
  3567. * @sg_mem_num: Number of SGL elements
  3568. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3569. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3570. *
  3571. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3572. * of parameters.
  3573. */
  3574. int transport_generic_map_mem_to_cmd(
  3575. struct se_cmd *cmd,
  3576. struct scatterlist *sgl,
  3577. u32 sgl_count,
  3578. struct scatterlist *sgl_bidi,
  3579. u32 sgl_bidi_count)
  3580. {
  3581. int ret;
  3582. if (!sgl || !sgl_count)
  3583. return 0;
  3584. /*
  3585. * Convert sgls (sgl, sgl_bidi) to list of se_mems
  3586. */
  3587. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3588. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3589. /*
  3590. * For CDB using TCM struct se_mem linked list scatterlist memory
  3591. * processed into a TCM struct se_subsystem_dev, we do the mapping
  3592. * from the passed physical memory to struct se_mem->se_page here.
  3593. */
  3594. ret = transport_map_sg_to_mem(cmd, &cmd->t_mem_list, sgl);
  3595. if (ret < 0)
  3596. return -ENOMEM;
  3597. cmd->t_tasks_se_num = ret;
  3598. /*
  3599. * Setup BIDI READ list of struct se_mem elements
  3600. */
  3601. if (sgl_bidi && sgl_bidi_count) {
  3602. ret = transport_map_sg_to_mem(cmd, &cmd->t_mem_bidi_list, sgl_bidi);
  3603. if (ret < 0)
  3604. return -ENOMEM;
  3605. cmd->t_tasks_se_bidi_num = ret;
  3606. }
  3607. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3608. }
  3609. return 0;
  3610. }
  3611. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3612. static inline long long transport_dev_end_lba(struct se_device *dev)
  3613. {
  3614. return dev->transport->get_blocks(dev) + 1;
  3615. }
  3616. static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
  3617. {
  3618. struct se_device *dev = cmd->se_dev;
  3619. u32 sectors;
  3620. if (dev->transport->get_device_type(dev) != TYPE_DISK)
  3621. return 0;
  3622. sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
  3623. if ((cmd->t_task_lba + sectors) >
  3624. transport_dev_end_lba(dev)) {
  3625. printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
  3626. " transport_dev_end_lba(): %llu\n",
  3627. cmd->t_task_lba, sectors,
  3628. transport_dev_end_lba(dev));
  3629. return 0;
  3630. }
  3631. return sectors;
  3632. }
  3633. static int transport_new_cmd_obj(struct se_cmd *cmd)
  3634. {
  3635. struct se_device *dev = cmd->se_dev;
  3636. u32 task_cdbs;
  3637. u32 rc;
  3638. if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
  3639. task_cdbs = 1;
  3640. cmd->t_task_list_num = 1;
  3641. } else {
  3642. int set_counts = 1;
  3643. /*
  3644. * Setup any BIDI READ tasks and memory from
  3645. * cmd->t_mem_bidi_list so the READ struct se_tasks
  3646. * are queued first for the non pSCSI passthrough case.
  3647. */
  3648. if (!list_empty(&cmd->t_mem_bidi_list) &&
  3649. (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3650. rc = transport_allocate_tasks(cmd,
  3651. cmd->t_task_lba,
  3652. transport_cmd_get_valid_sectors(cmd),
  3653. DMA_FROM_DEVICE, &cmd->t_mem_bidi_list,
  3654. set_counts);
  3655. if (!(rc)) {
  3656. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3657. cmd->scsi_sense_reason =
  3658. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3659. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3660. }
  3661. set_counts = 0;
  3662. }
  3663. /*
  3664. * Setup the tasks and memory from cmd->t_mem_list
  3665. * Note for BIDI transfers this will contain the WRITE payload
  3666. */
  3667. task_cdbs = transport_allocate_tasks(cmd,
  3668. cmd->t_task_lba,
  3669. transport_cmd_get_valid_sectors(cmd),
  3670. cmd->data_direction, &cmd->t_mem_list,
  3671. set_counts);
  3672. if (!(task_cdbs)) {
  3673. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3674. cmd->scsi_sense_reason =
  3675. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3676. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3677. }
  3678. cmd->t_task_list_num = task_cdbs;
  3679. #if 0
  3680. printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
  3681. " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
  3682. cmd->t_task_lba, cmd->t_tasks_sectors,
  3683. cmd->t_task_cdbs);
  3684. #endif
  3685. }
  3686. atomic_set(&cmd->t_task_cdbs_left, task_cdbs);
  3687. atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs);
  3688. atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs);
  3689. return 0;
  3690. }
  3691. void *transport_kmap_first_data_page(struct se_cmd *cmd)
  3692. {
  3693. struct se_mem *se_mem;
  3694. BUG_ON(list_empty(&cmd->t_mem_list));
  3695. se_mem = list_first_entry(&cmd->t_mem_list, struct se_mem, se_list);
  3696. /*
  3697. * 1st se_mem should point to a page, and we shouldn't need more than
  3698. * that for this cmd
  3699. */
  3700. BUG_ON(cmd->data_length > PAGE_SIZE);
  3701. return kmap(se_mem->se_page);
  3702. }
  3703. EXPORT_SYMBOL(transport_kmap_first_data_page);
  3704. void transport_kunmap_first_data_page(struct se_cmd *cmd)
  3705. {
  3706. struct se_mem *se_mem;
  3707. BUG_ON(list_empty(&cmd->t_mem_list));
  3708. se_mem = list_first_entry(&cmd->t_mem_list, struct se_mem, se_list);
  3709. kunmap(se_mem->se_page);
  3710. }
  3711. EXPORT_SYMBOL(transport_kunmap_first_data_page);
  3712. static int
  3713. transport_generic_get_mem(struct se_cmd *cmd)
  3714. {
  3715. struct se_mem *se_mem;
  3716. int length = cmd->data_length;
  3717. /*
  3718. * If the device uses memory mapping this is enough.
  3719. */
  3720. if (cmd->se_dev->transport->do_se_mem_map)
  3721. return 0;
  3722. /* Even cmds with length 0 will get here, btw */
  3723. while (length) {
  3724. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  3725. if (!(se_mem)) {
  3726. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  3727. goto out;
  3728. }
  3729. /* #warning FIXME Allocate contigous pages for struct se_mem elements */
  3730. se_mem->se_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
  3731. if (!(se_mem->se_page)) {
  3732. printk(KERN_ERR "alloc_pages() failed\n");
  3733. goto out;
  3734. }
  3735. INIT_LIST_HEAD(&se_mem->se_list);
  3736. se_mem->se_len = min_t(u32, length, PAGE_SIZE);
  3737. list_add_tail(&se_mem->se_list, &cmd->t_mem_list);
  3738. cmd->t_tasks_se_num++;
  3739. DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
  3740. " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
  3741. se_mem->se_off);
  3742. length -= se_mem->se_len;
  3743. }
  3744. DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
  3745. cmd->t_tasks_se_num);
  3746. return 0;
  3747. out:
  3748. if (se_mem)
  3749. __free_pages(se_mem->se_page, 0);
  3750. kmem_cache_free(se_mem_cache, se_mem);
  3751. return -ENOMEM;
  3752. }
  3753. int transport_init_task_sg(
  3754. struct se_task *task,
  3755. struct se_mem *in_se_mem,
  3756. u32 task_offset)
  3757. {
  3758. struct se_cmd *se_cmd = task->task_se_cmd;
  3759. struct se_device *se_dev = se_cmd->se_dev;
  3760. struct se_mem *se_mem = in_se_mem;
  3761. struct target_core_fabric_ops *tfo = se_cmd->se_tfo;
  3762. u32 sg_length, task_size = task->task_size, task_sg_num_padded;
  3763. while (task_size != 0) {
  3764. DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
  3765. " se_mem->se_off(%u) task_offset(%u)\n",
  3766. se_mem->se_page, se_mem->se_len,
  3767. se_mem->se_off, task_offset);
  3768. if (task_offset == 0) {
  3769. if (task_size >= se_mem->se_len) {
  3770. sg_length = se_mem->se_len;
  3771. if (!(list_is_last(&se_mem->se_list,
  3772. &se_cmd->t_mem_list)))
  3773. se_mem = list_entry(se_mem->se_list.next,
  3774. struct se_mem, se_list);
  3775. } else {
  3776. sg_length = task_size;
  3777. task_size -= sg_length;
  3778. goto next;
  3779. }
  3780. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3781. sg_length, task_size);
  3782. } else {
  3783. if ((se_mem->se_len - task_offset) > task_size) {
  3784. sg_length = task_size;
  3785. task_size -= sg_length;
  3786. goto next;
  3787. } else {
  3788. sg_length = (se_mem->se_len - task_offset);
  3789. if (!(list_is_last(&se_mem->se_list,
  3790. &se_cmd->t_mem_list)))
  3791. se_mem = list_entry(se_mem->se_list.next,
  3792. struct se_mem, se_list);
  3793. }
  3794. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3795. sg_length, task_size);
  3796. task_offset = 0;
  3797. }
  3798. task_size -= sg_length;
  3799. next:
  3800. DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
  3801. task->task_no, task_size);
  3802. task->task_sg_num++;
  3803. }
  3804. /*
  3805. * Check if the fabric module driver is requesting that all
  3806. * struct se_task->task_sg[] be chained together.. If so,
  3807. * then allocate an extra padding SG entry for linking and
  3808. * marking the end of the chained SGL.
  3809. */
  3810. if (tfo->task_sg_chaining) {
  3811. task_sg_num_padded = (task->task_sg_num + 1);
  3812. task->task_padded_sg = 1;
  3813. } else
  3814. task_sg_num_padded = task->task_sg_num;
  3815. task->task_sg = kzalloc(task_sg_num_padded *
  3816. sizeof(struct scatterlist), GFP_KERNEL);
  3817. if (!(task->task_sg)) {
  3818. printk(KERN_ERR "Unable to allocate memory for"
  3819. " task->task_sg\n");
  3820. return -ENOMEM;
  3821. }
  3822. sg_init_table(&task->task_sg[0], task_sg_num_padded);
  3823. /*
  3824. * Setup task->task_sg_bidi for SCSI READ payload for
  3825. * TCM/pSCSI passthrough if present for BIDI-COMMAND
  3826. */
  3827. if (!list_empty(&se_cmd->t_mem_bidi_list) &&
  3828. (se_dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3829. task->task_sg_bidi = kzalloc(task_sg_num_padded *
  3830. sizeof(struct scatterlist), GFP_KERNEL);
  3831. if (!(task->task_sg_bidi)) {
  3832. kfree(task->task_sg);
  3833. task->task_sg = NULL;
  3834. printk(KERN_ERR "Unable to allocate memory for"
  3835. " task->task_sg_bidi\n");
  3836. return -ENOMEM;
  3837. }
  3838. sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
  3839. }
  3840. /*
  3841. * For the chaining case, setup the proper end of SGL for the
  3842. * initial submission struct task into struct se_subsystem_api.
  3843. * This will be cleared later by transport_do_task_sg_chain()
  3844. */
  3845. if (task->task_padded_sg) {
  3846. sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
  3847. /*
  3848. * Added the 'if' check before marking end of bi-directional
  3849. * scatterlist (which gets created only in case of request
  3850. * (RD + WR).
  3851. */
  3852. if (task->task_sg_bidi)
  3853. sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
  3854. }
  3855. DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
  3856. " task_sg_num_padded(%u)\n", task->task_sg_num,
  3857. task_sg_num_padded);
  3858. return task->task_sg_num;
  3859. }
  3860. /* Reduce sectors if they are too long for the device */
  3861. static inline sector_t transport_limit_task_sectors(
  3862. struct se_device *dev,
  3863. unsigned long long lba,
  3864. sector_t sectors)
  3865. {
  3866. sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
  3867. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  3868. if ((lba + sectors) > transport_dev_end_lba(dev))
  3869. sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  3870. return sectors;
  3871. }
  3872. /*
  3873. * Convert a sgl into a linked list of se_mems.
  3874. */
  3875. static int transport_map_sg_to_mem(
  3876. struct se_cmd *cmd,
  3877. struct list_head *se_mem_list,
  3878. struct scatterlist *sg)
  3879. {
  3880. struct se_mem *se_mem;
  3881. u32 cmd_size = cmd->data_length;
  3882. int sg_count = 0;
  3883. WARN_ON(!sg);
  3884. while (cmd_size) {
  3885. /*
  3886. * NOTE: it is safe to return -ENOMEM at any time in creating this
  3887. * list because transport_free_pages() will eventually be called, and is
  3888. * smart enough to deallocate all list items for sg and sg_bidi lists.
  3889. */
  3890. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  3891. if (!(se_mem)) {
  3892. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  3893. return -ENOMEM;
  3894. }
  3895. INIT_LIST_HEAD(&se_mem->se_list);
  3896. DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
  3897. " sg_page: %p offset: %d length: %d\n", cmd_size,
  3898. sg_page(sg), sg->offset, sg->length);
  3899. se_mem->se_page = sg_page(sg);
  3900. se_mem->se_off = sg->offset;
  3901. if (cmd_size > sg->length) {
  3902. se_mem->se_len = sg->length;
  3903. sg = sg_next(sg);
  3904. } else
  3905. se_mem->se_len = cmd_size;
  3906. cmd_size -= se_mem->se_len;
  3907. sg_count++;
  3908. DEBUG_MEM("sg_to_mem: sg_count: %u cmd_size: %u\n",
  3909. sg_count, cmd_size);
  3910. DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
  3911. se_mem->se_page, se_mem->se_off, se_mem->se_len);
  3912. list_add_tail(&se_mem->se_list, se_mem_list);
  3913. }
  3914. DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments\n", sg_count);
  3915. return sg_count;
  3916. }
  3917. /* transport_map_mem_to_sg():
  3918. *
  3919. *
  3920. */
  3921. int transport_map_mem_to_sg(
  3922. struct se_task *task,
  3923. struct list_head *se_mem_list,
  3924. struct scatterlist *sg,
  3925. struct se_mem *in_se_mem,
  3926. struct se_mem **out_se_mem,
  3927. u32 *se_mem_cnt,
  3928. u32 *task_offset)
  3929. {
  3930. struct se_cmd *se_cmd = task->task_se_cmd;
  3931. struct se_mem *se_mem = in_se_mem;
  3932. u32 task_size = task->task_size, sg_no = 0;
  3933. if (!sg) {
  3934. printk(KERN_ERR "Unable to locate valid struct"
  3935. " scatterlist pointer\n");
  3936. return -EINVAL;
  3937. }
  3938. while (task_size != 0) {
  3939. /*
  3940. * Setup the contiguous array of scatterlists for
  3941. * this struct se_task.
  3942. */
  3943. sg_assign_page(sg, se_mem->se_page);
  3944. if (*task_offset == 0) {
  3945. sg->offset = se_mem->se_off;
  3946. if (task_size >= se_mem->se_len) {
  3947. sg->length = se_mem->se_len;
  3948. if (!(list_is_last(&se_mem->se_list,
  3949. &se_cmd->t_mem_list))) {
  3950. se_mem = list_entry(se_mem->se_list.next,
  3951. struct se_mem, se_list);
  3952. (*se_mem_cnt)++;
  3953. }
  3954. } else {
  3955. sg->length = task_size;
  3956. /*
  3957. * Determine if we need to calculate an offset
  3958. * into the struct se_mem on the next go around..
  3959. */
  3960. task_size -= sg->length;
  3961. if (!(task_size))
  3962. *task_offset = sg->length;
  3963. goto next;
  3964. }
  3965. } else {
  3966. sg->offset = (*task_offset + se_mem->se_off);
  3967. if ((se_mem->se_len - *task_offset) > task_size) {
  3968. sg->length = task_size;
  3969. /*
  3970. * Determine if we need to calculate an offset
  3971. * into the struct se_mem on the next go around..
  3972. */
  3973. task_size -= sg->length;
  3974. if (!(task_size))
  3975. *task_offset += sg->length;
  3976. goto next;
  3977. } else {
  3978. sg->length = (se_mem->se_len - *task_offset);
  3979. if (!(list_is_last(&se_mem->se_list,
  3980. &se_cmd->t_mem_list))) {
  3981. se_mem = list_entry(se_mem->se_list.next,
  3982. struct se_mem, se_list);
  3983. (*se_mem_cnt)++;
  3984. }
  3985. }
  3986. *task_offset = 0;
  3987. }
  3988. task_size -= sg->length;
  3989. next:
  3990. DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
  3991. " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
  3992. sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
  3993. sg_no++;
  3994. if (!(task_size))
  3995. break;
  3996. sg = sg_next(sg);
  3997. if (task_size > se_cmd->data_length)
  3998. BUG();
  3999. }
  4000. *out_se_mem = se_mem;
  4001. DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
  4002. " SGs\n", task->task_no, *se_mem_cnt, sg_no);
  4003. return 0;
  4004. }
  4005. /*
  4006. * This function can be used by HW target mode drivers to create a linked
  4007. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  4008. * This is intended to be called during the completion path by TCM Core
  4009. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  4010. */
  4011. void transport_do_task_sg_chain(struct se_cmd *cmd)
  4012. {
  4013. struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
  4014. struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
  4015. struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
  4016. struct se_task *task;
  4017. struct target_core_fabric_ops *tfo = cmd->se_tfo;
  4018. u32 task_sg_num = 0, sg_count = 0;
  4019. int i;
  4020. if (tfo->task_sg_chaining == 0) {
  4021. printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
  4022. " %s\n", tfo->get_fabric_name());
  4023. dump_stack();
  4024. return;
  4025. }
  4026. /*
  4027. * Walk the struct se_task list and setup scatterlist chains
  4028. * for each contiguously allocated struct se_task->task_sg[].
  4029. */
  4030. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  4031. if (!(task->task_sg) || !(task->task_padded_sg))
  4032. continue;
  4033. if (sg_head && sg_link) {
  4034. sg_head_cur = &task->task_sg[0];
  4035. sg_link_cur = &task->task_sg[task->task_sg_num];
  4036. /*
  4037. * Either add chain or mark end of scatterlist
  4038. */
  4039. if (!(list_is_last(&task->t_list,
  4040. &cmd->t_task_list))) {
  4041. /*
  4042. * Clear existing SGL termination bit set in
  4043. * transport_init_task_sg(), see sg_mark_end()
  4044. */
  4045. sg_end_cur = &task->task_sg[task->task_sg_num - 1];
  4046. sg_end_cur->page_link &= ~0x02;
  4047. sg_chain(sg_head, task_sg_num, sg_head_cur);
  4048. sg_count += task->task_sg_num;
  4049. task_sg_num = (task->task_sg_num + 1);
  4050. } else {
  4051. sg_chain(sg_head, task_sg_num, sg_head_cur);
  4052. sg_count += task->task_sg_num;
  4053. task_sg_num = task->task_sg_num;
  4054. }
  4055. sg_head = sg_head_cur;
  4056. sg_link = sg_link_cur;
  4057. continue;
  4058. }
  4059. sg_head = sg_first = &task->task_sg[0];
  4060. sg_link = &task->task_sg[task->task_sg_num];
  4061. /*
  4062. * Check for single task..
  4063. */
  4064. if (!(list_is_last(&task->t_list, &cmd->t_task_list))) {
  4065. /*
  4066. * Clear existing SGL termination bit set in
  4067. * transport_init_task_sg(), see sg_mark_end()
  4068. */
  4069. sg_end = &task->task_sg[task->task_sg_num - 1];
  4070. sg_end->page_link &= ~0x02;
  4071. sg_count += task->task_sg_num;
  4072. task_sg_num = (task->task_sg_num + 1);
  4073. } else {
  4074. sg_count += task->task_sg_num;
  4075. task_sg_num = task->task_sg_num;
  4076. }
  4077. }
  4078. /*
  4079. * Setup the starting pointer and total t_tasks_sg_linked_no including
  4080. * padding SGs for linking and to mark the end.
  4081. */
  4082. cmd->t_tasks_sg_chained = sg_first;
  4083. cmd->t_tasks_sg_chained_no = sg_count;
  4084. DEBUG_CMD_M("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
  4085. " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
  4086. cmd->t_tasks_sg_chained_no);
  4087. for_each_sg(cmd->t_tasks_sg_chained, sg,
  4088. cmd->t_tasks_sg_chained_no, i) {
  4089. DEBUG_CMD_M("SG[%d]: %p page: %p length: %d offset: %d\n",
  4090. i, sg, sg_page(sg), sg->length, sg->offset);
  4091. if (sg_is_chain(sg))
  4092. DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
  4093. if (sg_is_last(sg))
  4094. DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
  4095. }
  4096. }
  4097. EXPORT_SYMBOL(transport_do_task_sg_chain);
  4098. static int transport_do_se_mem_map(
  4099. struct se_device *dev,
  4100. struct se_task *task,
  4101. struct list_head *se_mem_list,
  4102. void *in_mem,
  4103. struct se_mem *in_se_mem,
  4104. struct se_mem **out_se_mem,
  4105. u32 *se_mem_cnt,
  4106. u32 *task_offset_in)
  4107. {
  4108. u32 task_offset = *task_offset_in;
  4109. int ret = 0;
  4110. /*
  4111. * se_subsystem_api_t->do_se_mem_map is used when internal allocation
  4112. * has been done by the transport plugin.
  4113. */
  4114. if (dev->transport->do_se_mem_map) {
  4115. ret = dev->transport->do_se_mem_map(task, se_mem_list,
  4116. in_mem, in_se_mem, out_se_mem, se_mem_cnt,
  4117. task_offset_in);
  4118. if (ret == 0)
  4119. task->task_se_cmd->t_tasks_se_num += *se_mem_cnt;
  4120. return ret;
  4121. }
  4122. BUG_ON(list_empty(se_mem_list));
  4123. /*
  4124. * This is the normal path for all normal non BIDI and BIDI-COMMAND
  4125. * WRITE payloads.. If we need to do BIDI READ passthrough for
  4126. * TCM/pSCSI the first call to transport_do_se_mem_map ->
  4127. * transport_init_task_sg() -> transport_map_mem_to_sg() will do the
  4128. * allocation for task->task_sg_bidi, and the subsequent call to
  4129. * transport_do_se_mem_map() from transport_generic_get_cdb_count()
  4130. */
  4131. if (!(task->task_sg_bidi)) {
  4132. /*
  4133. * Assume default that transport plugin speaks preallocated
  4134. * scatterlists.
  4135. */
  4136. ret = transport_init_task_sg(task, in_se_mem, task_offset);
  4137. if (ret <= 0)
  4138. return ret;
  4139. /*
  4140. * struct se_task->task_sg now contains the struct scatterlist array.
  4141. */
  4142. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
  4143. in_se_mem, out_se_mem, se_mem_cnt,
  4144. task_offset_in);
  4145. }
  4146. /*
  4147. * Handle the se_mem_list -> struct task->task_sg_bidi
  4148. * memory map for the extra BIDI READ payload
  4149. */
  4150. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
  4151. in_se_mem, out_se_mem, se_mem_cnt,
  4152. task_offset_in);
  4153. }
  4154. /*
  4155. * Break up cmd into chunks transport can handle
  4156. */
  4157. static u32 transport_allocate_tasks(
  4158. struct se_cmd *cmd,
  4159. unsigned long long lba,
  4160. u32 sectors,
  4161. enum dma_data_direction data_direction,
  4162. struct list_head *mem_list,
  4163. int set_counts)
  4164. {
  4165. unsigned char *cdb = NULL;
  4166. struct se_task *task;
  4167. struct se_mem *se_mem = NULL;
  4168. struct se_mem *se_mem_lout = NULL;
  4169. struct se_mem *se_mem_bidi = NULL;
  4170. struct se_mem *se_mem_bidi_lout = NULL;
  4171. struct se_device *dev = cmd->se_dev;
  4172. int ret;
  4173. u32 task_offset_in = 0;
  4174. u32 se_mem_cnt = 0;
  4175. u32 se_mem_bidi_cnt = 0;
  4176. u32 task_cdbs = 0;
  4177. BUG_ON(!mem_list);
  4178. /*
  4179. * While using RAMDISK_DR backstores is the only case where
  4180. * mem_list will ever be empty at this point.
  4181. */
  4182. if (!(list_empty(mem_list)))
  4183. se_mem = list_first_entry(mem_list, struct se_mem, se_list);
  4184. /*
  4185. * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
  4186. * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
  4187. */
  4188. if (!list_empty(&cmd->t_mem_bidi_list) &&
  4189. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
  4190. se_mem_bidi = list_first_entry(&cmd->t_mem_bidi_list,
  4191. struct se_mem, se_list);
  4192. while (sectors) {
  4193. sector_t limited_sectors;
  4194. DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
  4195. cmd->se_tfo->get_task_tag(cmd), lba, sectors,
  4196. transport_dev_end_lba(dev));
  4197. limited_sectors = transport_limit_task_sectors(dev, lba, sectors);
  4198. if (!limited_sectors)
  4199. break;
  4200. task = transport_generic_get_task(cmd, data_direction);
  4201. if (!task)
  4202. goto out;
  4203. task->task_lba = lba;
  4204. task->task_sectors = limited_sectors;
  4205. lba += task->task_sectors;
  4206. sectors -= task->task_sectors;
  4207. task->task_size = (task->task_sectors *
  4208. dev->se_sub_dev->se_dev_attrib.block_size);
  4209. cdb = dev->transport->get_cdb(task);
  4210. /* Should be part of task, can't fail */
  4211. BUG_ON(!cdb);
  4212. memcpy(cdb, cmd->t_task_cdb,
  4213. scsi_command_size(cmd->t_task_cdb));
  4214. /* Update new cdb with updated lba/sectors */
  4215. cmd->transport_split_cdb(task->task_lba,
  4216. &task->task_sectors, cdb);
  4217. /*
  4218. * Perform the SE OBJ plugin and/or Transport plugin specific
  4219. * mapping for cmd->t_mem_list. And setup the
  4220. * task->task_sg and if necessary task->task_sg_bidi
  4221. */
  4222. ret = transport_do_se_mem_map(dev, task, mem_list,
  4223. NULL, se_mem, &se_mem_lout, &se_mem_cnt,
  4224. &task_offset_in);
  4225. if (ret < 0)
  4226. goto out;
  4227. se_mem = se_mem_lout;
  4228. /*
  4229. * Setup the cmd->t_mem_bidi_list -> task->task_sg_bidi
  4230. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
  4231. *
  4232. * Note that the first call to transport_do_se_mem_map() above will
  4233. * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
  4234. * -> transport_init_task_sg(), and the second here will do the
  4235. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
  4236. */
  4237. if (task->task_sg_bidi != NULL) {
  4238. ret = transport_do_se_mem_map(dev, task,
  4239. &cmd->t_mem_bidi_list, NULL,
  4240. se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
  4241. &task_offset_in);
  4242. if (ret < 0)
  4243. goto out;
  4244. se_mem_bidi = se_mem_bidi_lout;
  4245. }
  4246. task_cdbs++;
  4247. DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
  4248. task_cdbs, task->task_sg_num);
  4249. }
  4250. if (set_counts) {
  4251. atomic_inc(&cmd->t_fe_count);
  4252. atomic_inc(&cmd->t_se_count);
  4253. }
  4254. DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
  4255. cmd->se_tfo->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
  4256. ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
  4257. return task_cdbs;
  4258. out:
  4259. return 0;
  4260. }
  4261. static int
  4262. transport_map_control_cmd_to_task(struct se_cmd *cmd)
  4263. {
  4264. struct se_device *dev = cmd->se_dev;
  4265. unsigned char *cdb;
  4266. struct se_task *task;
  4267. int ret;
  4268. task = transport_generic_get_task(cmd, cmd->data_direction);
  4269. if (!task)
  4270. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4271. cdb = dev->transport->get_cdb(task);
  4272. BUG_ON(!cdb);
  4273. memcpy(cdb, cmd->t_task_cdb,
  4274. scsi_command_size(cmd->t_task_cdb));
  4275. task->task_size = cmd->data_length;
  4276. task->task_sg_num =
  4277. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
  4278. atomic_inc(&cmd->t_fe_count);
  4279. atomic_inc(&cmd->t_se_count);
  4280. if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
  4281. struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
  4282. u32 se_mem_cnt = 0, task_offset = 0;
  4283. if (!list_empty(&cmd->t_mem_list))
  4284. se_mem = list_first_entry(&cmd->t_mem_list,
  4285. struct se_mem, se_list);
  4286. ret = transport_do_se_mem_map(dev, task,
  4287. &cmd->t_mem_list, NULL, se_mem,
  4288. &se_mem_lout, &se_mem_cnt, &task_offset);
  4289. if (ret < 0)
  4290. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4291. if (dev->transport->map_task_SG)
  4292. return dev->transport->map_task_SG(task);
  4293. return 0;
  4294. } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
  4295. if (dev->transport->cdb_none)
  4296. return dev->transport->cdb_none(task);
  4297. return 0;
  4298. } else {
  4299. BUG();
  4300. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4301. }
  4302. }
  4303. /* transport_generic_new_cmd(): Called from transport_processing_thread()
  4304. *
  4305. * Allocate storage transport resources from a set of values predefined
  4306. * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
  4307. * Any non zero return here is treated as an "out of resource' op here.
  4308. */
  4309. /*
  4310. * Generate struct se_task(s) and/or their payloads for this CDB.
  4311. */
  4312. int transport_generic_new_cmd(struct se_cmd *cmd)
  4313. {
  4314. struct se_task *task;
  4315. struct se_device *dev = cmd->se_dev;
  4316. int ret = 0;
  4317. /*
  4318. * Determine is the TCM fabric module has already allocated physical
  4319. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  4320. * to setup beforehand the linked list of physical memory at
  4321. * cmd->t_mem_list of struct se_mem->se_page
  4322. */
  4323. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
  4324. ret = transport_generic_get_mem(cmd);
  4325. if (ret < 0)
  4326. return ret;
  4327. }
  4328. ret = transport_new_cmd_obj(cmd);
  4329. if (ret < 0)
  4330. return ret;
  4331. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  4332. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  4333. if (atomic_read(&task->task_sent))
  4334. continue;
  4335. if (!dev->transport->map_task_SG)
  4336. continue;
  4337. ret = dev->transport->map_task_SG(task);
  4338. if (ret < 0)
  4339. return ret;
  4340. }
  4341. } else {
  4342. ret = transport_map_control_cmd_to_task(cmd);
  4343. if (ret < 0)
  4344. return ret;
  4345. }
  4346. /*
  4347. * For WRITEs, let the fabric know its buffer is ready..
  4348. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  4349. * will be added to the struct se_device execution queue after its WRITE
  4350. * data has arrived. (ie: It gets handled by the transport processing
  4351. * thread a second time)
  4352. */
  4353. if (cmd->data_direction == DMA_TO_DEVICE) {
  4354. transport_add_tasks_to_state_queue(cmd);
  4355. return transport_generic_write_pending(cmd);
  4356. }
  4357. /*
  4358. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  4359. * to the execution queue.
  4360. */
  4361. transport_execute_tasks(cmd);
  4362. return 0;
  4363. }
  4364. EXPORT_SYMBOL(transport_generic_new_cmd);
  4365. /* transport_generic_process_write():
  4366. *
  4367. *
  4368. */
  4369. void transport_generic_process_write(struct se_cmd *cmd)
  4370. {
  4371. #if 0
  4372. /*
  4373. * Copy SCSI Presented DTL sector(s) from received buffers allocated to
  4374. * original EDTL
  4375. */
  4376. if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  4377. if (!cmd->t_tasks_se_num) {
  4378. unsigned char *dst, *buf =
  4379. (unsigned char *)cmd->t_task_buf;
  4380. dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
  4381. if (!(dst)) {
  4382. printk(KERN_ERR "Unable to allocate memory for"
  4383. " WRITE underflow\n");
  4384. transport_generic_request_failure(cmd, NULL,
  4385. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4386. return;
  4387. }
  4388. memcpy(dst, buf, cmd->cmd_spdtl);
  4389. kfree(cmd->t_task_buf);
  4390. cmd->t_task_buf = dst;
  4391. } else {
  4392. struct scatterlist *sg =
  4393. (struct scatterlist *sg)cmd->t_task_buf;
  4394. struct scatterlist *orig_sg;
  4395. orig_sg = kzalloc(sizeof(struct scatterlist) *
  4396. cmd->t_tasks_se_num,
  4397. GFP_KERNEL))) {
  4398. if (!(orig_sg)) {
  4399. printk(KERN_ERR "Unable to allocate memory"
  4400. " for WRITE underflow\n");
  4401. transport_generic_request_failure(cmd, NULL,
  4402. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4403. return;
  4404. }
  4405. memcpy(orig_sg, cmd->t_task_buf,
  4406. sizeof(struct scatterlist) *
  4407. cmd->t_tasks_se_num);
  4408. cmd->data_length = cmd->cmd_spdtl;
  4409. /*
  4410. * FIXME, clear out original struct se_task and state
  4411. * information.
  4412. */
  4413. if (transport_generic_new_cmd(cmd) < 0) {
  4414. transport_generic_request_failure(cmd, NULL,
  4415. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4416. kfree(orig_sg);
  4417. return;
  4418. }
  4419. transport_memcpy_write_sg(cmd, orig_sg);
  4420. }
  4421. }
  4422. #endif
  4423. transport_execute_tasks(cmd);
  4424. }
  4425. EXPORT_SYMBOL(transport_generic_process_write);
  4426. static int transport_write_pending_qf(struct se_cmd *cmd)
  4427. {
  4428. return cmd->se_tfo->write_pending(cmd);
  4429. }
  4430. /* transport_generic_write_pending():
  4431. *
  4432. *
  4433. */
  4434. static int transport_generic_write_pending(struct se_cmd *cmd)
  4435. {
  4436. unsigned long flags;
  4437. int ret;
  4438. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4439. cmd->t_state = TRANSPORT_WRITE_PENDING;
  4440. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4441. if (cmd->transport_qf_callback) {
  4442. ret = cmd->transport_qf_callback(cmd);
  4443. if (ret == -EAGAIN)
  4444. goto queue_full;
  4445. else if (ret < 0)
  4446. return ret;
  4447. cmd->transport_qf_callback = NULL;
  4448. return 0;
  4449. }
  4450. /*
  4451. * Clear the se_cmd for WRITE_PENDING status in order to set
  4452. * cmd->t_transport_active=0 so that transport_generic_handle_data
  4453. * can be called from HW target mode interrupt code. This is safe
  4454. * to be called with transport_off=1 before the cmd->se_tfo->write_pending
  4455. * because the se_cmd->se_lun pointer is not being cleared.
  4456. */
  4457. transport_cmd_check_stop(cmd, 1, 0);
  4458. /*
  4459. * Call the fabric write_pending function here to let the
  4460. * frontend know that WRITE buffers are ready.
  4461. */
  4462. ret = cmd->se_tfo->write_pending(cmd);
  4463. if (ret == -EAGAIN)
  4464. goto queue_full;
  4465. else if (ret < 0)
  4466. return ret;
  4467. return PYX_TRANSPORT_WRITE_PENDING;
  4468. queue_full:
  4469. printk(KERN_INFO "Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  4470. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  4471. transport_handle_queue_full(cmd, cmd->se_dev,
  4472. transport_write_pending_qf);
  4473. return ret;
  4474. }
  4475. void transport_release_cmd(struct se_cmd *cmd)
  4476. {
  4477. BUG_ON(!cmd->se_tfo);
  4478. transport_free_se_cmd(cmd);
  4479. cmd->se_tfo->release_cmd(cmd);
  4480. }
  4481. EXPORT_SYMBOL(transport_release_cmd);
  4482. /* transport_generic_free_cmd():
  4483. *
  4484. * Called from processing frontend to release storage engine resources
  4485. */
  4486. void transport_generic_free_cmd(
  4487. struct se_cmd *cmd,
  4488. int wait_for_tasks,
  4489. int session_reinstatement)
  4490. {
  4491. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD))
  4492. transport_release_cmd(cmd);
  4493. else {
  4494. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  4495. if (cmd->se_lun) {
  4496. #if 0
  4497. printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
  4498. " cmd->se_lun\n", cmd,
  4499. cmd->se_tfo->get_task_tag(cmd));
  4500. #endif
  4501. transport_lun_remove_cmd(cmd);
  4502. }
  4503. if (wait_for_tasks && cmd->transport_wait_for_tasks)
  4504. cmd->transport_wait_for_tasks(cmd, 0, 0);
  4505. transport_free_dev_tasks(cmd);
  4506. transport_generic_remove(cmd, session_reinstatement);
  4507. }
  4508. }
  4509. EXPORT_SYMBOL(transport_generic_free_cmd);
  4510. static void transport_nop_wait_for_tasks(
  4511. struct se_cmd *cmd,
  4512. int remove_cmd,
  4513. int session_reinstatement)
  4514. {
  4515. return;
  4516. }
  4517. /* transport_lun_wait_for_tasks():
  4518. *
  4519. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  4520. * an struct se_lun to be successfully shutdown.
  4521. */
  4522. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  4523. {
  4524. unsigned long flags;
  4525. int ret;
  4526. /*
  4527. * If the frontend has already requested this struct se_cmd to
  4528. * be stopped, we can safely ignore this struct se_cmd.
  4529. */
  4530. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4531. if (atomic_read(&cmd->t_transport_stop)) {
  4532. atomic_set(&cmd->transport_lun_stop, 0);
  4533. DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
  4534. " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
  4535. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4536. transport_cmd_check_stop(cmd, 1, 0);
  4537. return -EPERM;
  4538. }
  4539. atomic_set(&cmd->transport_lun_fe_stop, 1);
  4540. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4541. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  4542. ret = transport_stop_tasks_for_cmd(cmd);
  4543. DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
  4544. " %d\n", cmd, cmd->t_task_cdbs, ret);
  4545. if (!ret) {
  4546. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  4547. cmd->se_tfo->get_task_tag(cmd));
  4548. wait_for_completion(&cmd->transport_lun_stop_comp);
  4549. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  4550. cmd->se_tfo->get_task_tag(cmd));
  4551. }
  4552. transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
  4553. return 0;
  4554. }
  4555. /* #define DEBUG_CLEAR_LUN */
  4556. #ifdef DEBUG_CLEAR_LUN
  4557. #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
  4558. #else
  4559. #define DEBUG_CLEAR_L(x...)
  4560. #endif
  4561. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  4562. {
  4563. struct se_cmd *cmd = NULL;
  4564. unsigned long lun_flags, cmd_flags;
  4565. /*
  4566. * Do exception processing and return CHECK_CONDITION status to the
  4567. * Initiator Port.
  4568. */
  4569. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4570. while (!list_empty(&lun->lun_cmd_list)) {
  4571. cmd = list_first_entry(&lun->lun_cmd_list,
  4572. struct se_cmd, se_lun_node);
  4573. list_del(&cmd->se_lun_node);
  4574. atomic_set(&cmd->transport_lun_active, 0);
  4575. /*
  4576. * This will notify iscsi_target_transport.c:
  4577. * transport_cmd_check_stop() that a LUN shutdown is in
  4578. * progress for the iscsi_cmd_t.
  4579. */
  4580. spin_lock(&cmd->t_state_lock);
  4581. DEBUG_CLEAR_L("SE_LUN[%d] - Setting cmd->transport"
  4582. "_lun_stop for ITT: 0x%08x\n",
  4583. cmd->se_lun->unpacked_lun,
  4584. cmd->se_tfo->get_task_tag(cmd));
  4585. atomic_set(&cmd->transport_lun_stop, 1);
  4586. spin_unlock(&cmd->t_state_lock);
  4587. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4588. if (!(cmd->se_lun)) {
  4589. printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
  4590. cmd->se_tfo->get_task_tag(cmd),
  4591. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  4592. BUG();
  4593. }
  4594. /*
  4595. * If the Storage engine still owns the iscsi_cmd_t, determine
  4596. * and/or stop its context.
  4597. */
  4598. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
  4599. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  4600. cmd->se_tfo->get_task_tag(cmd));
  4601. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  4602. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4603. continue;
  4604. }
  4605. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  4606. "_wait_for_tasks(): SUCCESS\n",
  4607. cmd->se_lun->unpacked_lun,
  4608. cmd->se_tfo->get_task_tag(cmd));
  4609. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  4610. if (!(atomic_read(&cmd->transport_dev_active))) {
  4611. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  4612. goto check_cond;
  4613. }
  4614. atomic_set(&cmd->transport_dev_active, 0);
  4615. transport_all_task_dev_remove_state(cmd);
  4616. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  4617. transport_free_dev_tasks(cmd);
  4618. /*
  4619. * The Storage engine stopped this struct se_cmd before it was
  4620. * send to the fabric frontend for delivery back to the
  4621. * Initiator Node. Return this SCSI CDB back with an
  4622. * CHECK_CONDITION status.
  4623. */
  4624. check_cond:
  4625. transport_send_check_condition_and_sense(cmd,
  4626. TCM_NON_EXISTENT_LUN, 0);
  4627. /*
  4628. * If the fabric frontend is waiting for this iscsi_cmd_t to
  4629. * be released, notify the waiting thread now that LU has
  4630. * finished accessing it.
  4631. */
  4632. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  4633. if (atomic_read(&cmd->transport_lun_fe_stop)) {
  4634. DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
  4635. " struct se_cmd: %p ITT: 0x%08x\n",
  4636. lun->unpacked_lun,
  4637. cmd, cmd->se_tfo->get_task_tag(cmd));
  4638. spin_unlock_irqrestore(&cmd->t_state_lock,
  4639. cmd_flags);
  4640. transport_cmd_check_stop(cmd, 1, 0);
  4641. complete(&cmd->transport_lun_fe_stop_comp);
  4642. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4643. continue;
  4644. }
  4645. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  4646. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  4647. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  4648. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4649. }
  4650. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4651. }
  4652. static int transport_clear_lun_thread(void *p)
  4653. {
  4654. struct se_lun *lun = (struct se_lun *)p;
  4655. __transport_clear_lun_from_sessions(lun);
  4656. complete(&lun->lun_shutdown_comp);
  4657. return 0;
  4658. }
  4659. int transport_clear_lun_from_sessions(struct se_lun *lun)
  4660. {
  4661. struct task_struct *kt;
  4662. kt = kthread_run(transport_clear_lun_thread, lun,
  4663. "tcm_cl_%u", lun->unpacked_lun);
  4664. if (IS_ERR(kt)) {
  4665. printk(KERN_ERR "Unable to start clear_lun thread\n");
  4666. return PTR_ERR(kt);
  4667. }
  4668. wait_for_completion(&lun->lun_shutdown_comp);
  4669. return 0;
  4670. }
  4671. /* transport_generic_wait_for_tasks():
  4672. *
  4673. * Called from frontend or passthrough context to wait for storage engine
  4674. * to pause and/or release frontend generated struct se_cmd.
  4675. */
  4676. static void transport_generic_wait_for_tasks(
  4677. struct se_cmd *cmd,
  4678. int remove_cmd,
  4679. int session_reinstatement)
  4680. {
  4681. unsigned long flags;
  4682. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
  4683. return;
  4684. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4685. /*
  4686. * If we are already stopped due to an external event (ie: LUN shutdown)
  4687. * sleep until the connection can have the passed struct se_cmd back.
  4688. * The cmd->transport_lun_stopped_sem will be upped by
  4689. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  4690. * has completed its operation on the struct se_cmd.
  4691. */
  4692. if (atomic_read(&cmd->transport_lun_stop)) {
  4693. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
  4694. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  4695. "_stop_comp); for ITT: 0x%08x\n",
  4696. cmd->se_tfo->get_task_tag(cmd));
  4697. /*
  4698. * There is a special case for WRITES where a FE exception +
  4699. * LUN shutdown means ConfigFS context is still sleeping on
  4700. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  4701. * We go ahead and up transport_lun_stop_comp just to be sure
  4702. * here.
  4703. */
  4704. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4705. complete(&cmd->transport_lun_stop_comp);
  4706. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  4707. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4708. transport_all_task_dev_remove_state(cmd);
  4709. /*
  4710. * At this point, the frontend who was the originator of this
  4711. * struct se_cmd, now owns the structure and can be released through
  4712. * normal means below.
  4713. */
  4714. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
  4715. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  4716. "stop_comp); for ITT: 0x%08x\n",
  4717. cmd->se_tfo->get_task_tag(cmd));
  4718. atomic_set(&cmd->transport_lun_stop, 0);
  4719. }
  4720. if (!atomic_read(&cmd->t_transport_active) ||
  4721. atomic_read(&cmd->t_transport_aborted))
  4722. goto remove;
  4723. atomic_set(&cmd->t_transport_stop, 1);
  4724. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
  4725. " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
  4726. " = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  4727. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
  4728. cmd->deferred_t_state);
  4729. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4730. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  4731. wait_for_completion(&cmd->t_transport_stop_comp);
  4732. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4733. atomic_set(&cmd->t_transport_active, 0);
  4734. atomic_set(&cmd->t_transport_stop, 0);
  4735. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
  4736. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  4737. cmd->se_tfo->get_task_tag(cmd));
  4738. remove:
  4739. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4740. if (!remove_cmd)
  4741. return;
  4742. transport_generic_free_cmd(cmd, 0, session_reinstatement);
  4743. }
  4744. static int transport_get_sense_codes(
  4745. struct se_cmd *cmd,
  4746. u8 *asc,
  4747. u8 *ascq)
  4748. {
  4749. *asc = cmd->scsi_asc;
  4750. *ascq = cmd->scsi_ascq;
  4751. return 0;
  4752. }
  4753. static int transport_set_sense_codes(
  4754. struct se_cmd *cmd,
  4755. u8 asc,
  4756. u8 ascq)
  4757. {
  4758. cmd->scsi_asc = asc;
  4759. cmd->scsi_ascq = ascq;
  4760. return 0;
  4761. }
  4762. int transport_send_check_condition_and_sense(
  4763. struct se_cmd *cmd,
  4764. u8 reason,
  4765. int from_transport)
  4766. {
  4767. unsigned char *buffer = cmd->sense_buffer;
  4768. unsigned long flags;
  4769. int offset;
  4770. u8 asc = 0, ascq = 0;
  4771. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4772. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4773. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4774. return 0;
  4775. }
  4776. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  4777. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4778. if (!reason && from_transport)
  4779. goto after_reason;
  4780. if (!from_transport)
  4781. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  4782. /*
  4783. * Data Segment and SenseLength of the fabric response PDU.
  4784. *
  4785. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  4786. * from include/scsi/scsi_cmnd.h
  4787. */
  4788. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  4789. TRANSPORT_SENSE_BUFFER);
  4790. /*
  4791. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  4792. * SENSE KEY values from include/scsi/scsi.h
  4793. */
  4794. switch (reason) {
  4795. case TCM_NON_EXISTENT_LUN:
  4796. case TCM_UNSUPPORTED_SCSI_OPCODE:
  4797. case TCM_SECTOR_COUNT_TOO_MANY:
  4798. /* CURRENT ERROR */
  4799. buffer[offset] = 0x70;
  4800. /* ILLEGAL REQUEST */
  4801. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4802. /* INVALID COMMAND OPERATION CODE */
  4803. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  4804. break;
  4805. case TCM_UNKNOWN_MODE_PAGE:
  4806. /* CURRENT ERROR */
  4807. buffer[offset] = 0x70;
  4808. /* ILLEGAL REQUEST */
  4809. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4810. /* INVALID FIELD IN CDB */
  4811. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4812. break;
  4813. case TCM_CHECK_CONDITION_ABORT_CMD:
  4814. /* CURRENT ERROR */
  4815. buffer[offset] = 0x70;
  4816. /* ABORTED COMMAND */
  4817. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4818. /* BUS DEVICE RESET FUNCTION OCCURRED */
  4819. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  4820. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  4821. break;
  4822. case TCM_INCORRECT_AMOUNT_OF_DATA:
  4823. /* CURRENT ERROR */
  4824. buffer[offset] = 0x70;
  4825. /* ABORTED COMMAND */
  4826. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4827. /* WRITE ERROR */
  4828. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4829. /* NOT ENOUGH UNSOLICITED DATA */
  4830. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  4831. break;
  4832. case TCM_INVALID_CDB_FIELD:
  4833. /* CURRENT ERROR */
  4834. buffer[offset] = 0x70;
  4835. /* ABORTED COMMAND */
  4836. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4837. /* INVALID FIELD IN CDB */
  4838. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4839. break;
  4840. case TCM_INVALID_PARAMETER_LIST:
  4841. /* CURRENT ERROR */
  4842. buffer[offset] = 0x70;
  4843. /* ABORTED COMMAND */
  4844. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4845. /* INVALID FIELD IN PARAMETER LIST */
  4846. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  4847. break;
  4848. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  4849. /* CURRENT ERROR */
  4850. buffer[offset] = 0x70;
  4851. /* ABORTED COMMAND */
  4852. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4853. /* WRITE ERROR */
  4854. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4855. /* UNEXPECTED_UNSOLICITED_DATA */
  4856. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  4857. break;
  4858. case TCM_SERVICE_CRC_ERROR:
  4859. /* CURRENT ERROR */
  4860. buffer[offset] = 0x70;
  4861. /* ABORTED COMMAND */
  4862. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4863. /* PROTOCOL SERVICE CRC ERROR */
  4864. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  4865. /* N/A */
  4866. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  4867. break;
  4868. case TCM_SNACK_REJECTED:
  4869. /* CURRENT ERROR */
  4870. buffer[offset] = 0x70;
  4871. /* ABORTED COMMAND */
  4872. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4873. /* READ ERROR */
  4874. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  4875. /* FAILED RETRANSMISSION REQUEST */
  4876. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  4877. break;
  4878. case TCM_WRITE_PROTECTED:
  4879. /* CURRENT ERROR */
  4880. buffer[offset] = 0x70;
  4881. /* DATA PROTECT */
  4882. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  4883. /* WRITE PROTECTED */
  4884. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  4885. break;
  4886. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  4887. /* CURRENT ERROR */
  4888. buffer[offset] = 0x70;
  4889. /* UNIT ATTENTION */
  4890. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  4891. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  4892. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4893. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4894. break;
  4895. case TCM_CHECK_CONDITION_NOT_READY:
  4896. /* CURRENT ERROR */
  4897. buffer[offset] = 0x70;
  4898. /* Not Ready */
  4899. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  4900. transport_get_sense_codes(cmd, &asc, &ascq);
  4901. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4902. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4903. break;
  4904. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  4905. default:
  4906. /* CURRENT ERROR */
  4907. buffer[offset] = 0x70;
  4908. /* ILLEGAL REQUEST */
  4909. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4910. /* LOGICAL UNIT COMMUNICATION FAILURE */
  4911. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  4912. break;
  4913. }
  4914. /*
  4915. * This code uses linux/include/scsi/scsi.h SAM status codes!
  4916. */
  4917. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  4918. /*
  4919. * Automatically padded, this value is encoded in the fabric's
  4920. * data_length response PDU containing the SCSI defined sense data.
  4921. */
  4922. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  4923. after_reason:
  4924. return cmd->se_tfo->queue_status(cmd);
  4925. }
  4926. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  4927. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  4928. {
  4929. int ret = 0;
  4930. if (atomic_read(&cmd->t_transport_aborted) != 0) {
  4931. if (!(send_status) ||
  4932. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  4933. return 1;
  4934. #if 0
  4935. printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
  4936. " status for CDB: 0x%02x ITT: 0x%08x\n",
  4937. cmd->t_task_cdb[0],
  4938. cmd->se_tfo->get_task_tag(cmd));
  4939. #endif
  4940. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  4941. cmd->se_tfo->queue_status(cmd);
  4942. ret = 1;
  4943. }
  4944. return ret;
  4945. }
  4946. EXPORT_SYMBOL(transport_check_aborted_status);
  4947. void transport_send_task_abort(struct se_cmd *cmd)
  4948. {
  4949. /*
  4950. * If there are still expected incoming fabric WRITEs, we wait
  4951. * until until they have completed before sending a TASK_ABORTED
  4952. * response. This response with TASK_ABORTED status will be
  4953. * queued back to fabric module by transport_check_aborted_status().
  4954. */
  4955. if (cmd->data_direction == DMA_TO_DEVICE) {
  4956. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  4957. atomic_inc(&cmd->t_transport_aborted);
  4958. smp_mb__after_atomic_inc();
  4959. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4960. transport_new_cmd_failure(cmd);
  4961. return;
  4962. }
  4963. }
  4964. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4965. #if 0
  4966. printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  4967. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  4968. cmd->se_tfo->get_task_tag(cmd));
  4969. #endif
  4970. cmd->se_tfo->queue_status(cmd);
  4971. }
  4972. /* transport_generic_do_tmr():
  4973. *
  4974. *
  4975. */
  4976. int transport_generic_do_tmr(struct se_cmd *cmd)
  4977. {
  4978. struct se_device *dev = cmd->se_dev;
  4979. struct se_tmr_req *tmr = cmd->se_tmr_req;
  4980. int ret;
  4981. switch (tmr->function) {
  4982. case TMR_ABORT_TASK:
  4983. tmr->response = TMR_FUNCTION_REJECTED;
  4984. break;
  4985. case TMR_ABORT_TASK_SET:
  4986. case TMR_CLEAR_ACA:
  4987. case TMR_CLEAR_TASK_SET:
  4988. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  4989. break;
  4990. case TMR_LUN_RESET:
  4991. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  4992. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  4993. TMR_FUNCTION_REJECTED;
  4994. break;
  4995. case TMR_TARGET_WARM_RESET:
  4996. tmr->response = TMR_FUNCTION_REJECTED;
  4997. break;
  4998. case TMR_TARGET_COLD_RESET:
  4999. tmr->response = TMR_FUNCTION_REJECTED;
  5000. break;
  5001. default:
  5002. printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
  5003. tmr->function);
  5004. tmr->response = TMR_FUNCTION_REJECTED;
  5005. break;
  5006. }
  5007. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  5008. cmd->se_tfo->queue_tm_rsp(cmd);
  5009. transport_cmd_check_stop(cmd, 2, 0);
  5010. return 0;
  5011. }
  5012. /*
  5013. * Called with spin_lock_irq(&dev->execute_task_lock); held
  5014. *
  5015. */
  5016. static struct se_task *
  5017. transport_get_task_from_state_list(struct se_device *dev)
  5018. {
  5019. struct se_task *task;
  5020. if (list_empty(&dev->state_task_list))
  5021. return NULL;
  5022. list_for_each_entry(task, &dev->state_task_list, t_state_list)
  5023. break;
  5024. list_del(&task->t_state_list);
  5025. atomic_set(&task->task_state_active, 0);
  5026. return task;
  5027. }
  5028. static void transport_processing_shutdown(struct se_device *dev)
  5029. {
  5030. struct se_cmd *cmd;
  5031. struct se_task *task;
  5032. unsigned long flags;
  5033. /*
  5034. * Empty the struct se_device's struct se_task state list.
  5035. */
  5036. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5037. while ((task = transport_get_task_from_state_list(dev))) {
  5038. if (!task->task_se_cmd) {
  5039. printk(KERN_ERR "task->task_se_cmd is NULL!\n");
  5040. continue;
  5041. }
  5042. cmd = task->task_se_cmd;
  5043. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5044. spin_lock_irqsave(&cmd->t_state_lock, flags);
  5045. DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
  5046. " i_state/def_i_state: %d/%d, t_state/def_t_state:"
  5047. " %d/%d cdb: 0x%02x\n", cmd, task,
  5048. cmd->se_tfo->get_task_tag(cmd), cmd->cmd_sn,
  5049. cmd->se_tfo->get_cmd_state(cmd), cmd->deferred_i_state,
  5050. cmd->t_state, cmd->deferred_t_state,
  5051. cmd->t_task_cdb[0]);
  5052. DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
  5053. " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
  5054. " t_transport_stop: %d t_transport_sent: %d\n",
  5055. cmd->se_tfo->get_task_tag(cmd),
  5056. cmd->t_task_cdbs,
  5057. atomic_read(&cmd->t_task_cdbs_left),
  5058. atomic_read(&cmd->t_task_cdbs_sent),
  5059. atomic_read(&cmd->t_transport_active),
  5060. atomic_read(&cmd->t_transport_stop),
  5061. atomic_read(&cmd->t_transport_sent));
  5062. if (atomic_read(&task->task_active)) {
  5063. atomic_set(&task->task_stop, 1);
  5064. spin_unlock_irqrestore(
  5065. &cmd->t_state_lock, flags);
  5066. DEBUG_DO("Waiting for task: %p to shutdown for dev:"
  5067. " %p\n", task, dev);
  5068. wait_for_completion(&task->task_stop_comp);
  5069. DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
  5070. task, dev);
  5071. spin_lock_irqsave(&cmd->t_state_lock, flags);
  5072. atomic_dec(&cmd->t_task_cdbs_left);
  5073. atomic_set(&task->task_active, 0);
  5074. atomic_set(&task->task_stop, 0);
  5075. } else {
  5076. if (atomic_read(&task->task_execute_queue) != 0)
  5077. transport_remove_task_from_execute_queue(task, dev);
  5078. }
  5079. __transport_stop_task_timer(task, &flags);
  5080. if (!(atomic_dec_and_test(&cmd->t_task_cdbs_ex_left))) {
  5081. spin_unlock_irqrestore(
  5082. &cmd->t_state_lock, flags);
  5083. DEBUG_DO("Skipping task: %p, dev: %p for"
  5084. " t_task_cdbs_ex_left: %d\n", task, dev,
  5085. atomic_read(&cmd->t_task_cdbs_ex_left));
  5086. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5087. continue;
  5088. }
  5089. if (atomic_read(&cmd->t_transport_active)) {
  5090. DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
  5091. " %p\n", task, dev);
  5092. if (atomic_read(&cmd->t_fe_count)) {
  5093. spin_unlock_irqrestore(
  5094. &cmd->t_state_lock, flags);
  5095. transport_send_check_condition_and_sense(
  5096. cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
  5097. 0);
  5098. transport_remove_cmd_from_queue(cmd,
  5099. &cmd->se_dev->dev_queue_obj);
  5100. transport_lun_remove_cmd(cmd);
  5101. transport_cmd_check_stop(cmd, 1, 0);
  5102. } else {
  5103. spin_unlock_irqrestore(
  5104. &cmd->t_state_lock, flags);
  5105. transport_remove_cmd_from_queue(cmd,
  5106. &cmd->se_dev->dev_queue_obj);
  5107. transport_lun_remove_cmd(cmd);
  5108. if (transport_cmd_check_stop(cmd, 1, 0))
  5109. transport_generic_remove(cmd, 0);
  5110. }
  5111. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5112. continue;
  5113. }
  5114. DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
  5115. task, dev);
  5116. if (atomic_read(&cmd->t_fe_count)) {
  5117. spin_unlock_irqrestore(
  5118. &cmd->t_state_lock, flags);
  5119. transport_send_check_condition_and_sense(cmd,
  5120. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5121. transport_remove_cmd_from_queue(cmd,
  5122. &cmd->se_dev->dev_queue_obj);
  5123. transport_lun_remove_cmd(cmd);
  5124. transport_cmd_check_stop(cmd, 1, 0);
  5125. } else {
  5126. spin_unlock_irqrestore(
  5127. &cmd->t_state_lock, flags);
  5128. transport_remove_cmd_from_queue(cmd,
  5129. &cmd->se_dev->dev_queue_obj);
  5130. transport_lun_remove_cmd(cmd);
  5131. if (transport_cmd_check_stop(cmd, 1, 0))
  5132. transport_generic_remove(cmd, 0);
  5133. }
  5134. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5135. }
  5136. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5137. /*
  5138. * Empty the struct se_device's struct se_cmd list.
  5139. */
  5140. while ((cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj))) {
  5141. DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
  5142. cmd, cmd->t_state);
  5143. if (atomic_read(&cmd->t_fe_count)) {
  5144. transport_send_check_condition_and_sense(cmd,
  5145. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5146. transport_lun_remove_cmd(cmd);
  5147. transport_cmd_check_stop(cmd, 1, 0);
  5148. } else {
  5149. transport_lun_remove_cmd(cmd);
  5150. if (transport_cmd_check_stop(cmd, 1, 0))
  5151. transport_generic_remove(cmd, 0);
  5152. }
  5153. }
  5154. }
  5155. /* transport_processing_thread():
  5156. *
  5157. *
  5158. */
  5159. static int transport_processing_thread(void *param)
  5160. {
  5161. int ret;
  5162. struct se_cmd *cmd;
  5163. struct se_device *dev = (struct se_device *) param;
  5164. set_user_nice(current, -20);
  5165. while (!kthread_should_stop()) {
  5166. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  5167. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  5168. kthread_should_stop());
  5169. if (ret < 0)
  5170. goto out;
  5171. spin_lock_irq(&dev->dev_status_lock);
  5172. if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
  5173. spin_unlock_irq(&dev->dev_status_lock);
  5174. transport_processing_shutdown(dev);
  5175. continue;
  5176. }
  5177. spin_unlock_irq(&dev->dev_status_lock);
  5178. get_cmd:
  5179. __transport_execute_tasks(dev);
  5180. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  5181. if (!cmd)
  5182. continue;
  5183. switch (cmd->t_state) {
  5184. case TRANSPORT_NEW_CMD_MAP:
  5185. if (!(cmd->se_tfo->new_cmd_map)) {
  5186. printk(KERN_ERR "cmd->se_tfo->new_cmd_map is"
  5187. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  5188. BUG();
  5189. }
  5190. ret = cmd->se_tfo->new_cmd_map(cmd);
  5191. if (ret < 0) {
  5192. cmd->transport_error_status = ret;
  5193. transport_generic_request_failure(cmd, NULL,
  5194. 0, (cmd->data_direction !=
  5195. DMA_TO_DEVICE));
  5196. break;
  5197. }
  5198. /* Fall through */
  5199. case TRANSPORT_NEW_CMD:
  5200. ret = transport_generic_new_cmd(cmd);
  5201. if (ret == -EAGAIN)
  5202. break;
  5203. else if (ret < 0) {
  5204. cmd->transport_error_status = ret;
  5205. transport_generic_request_failure(cmd, NULL,
  5206. 0, (cmd->data_direction !=
  5207. DMA_TO_DEVICE));
  5208. }
  5209. break;
  5210. case TRANSPORT_PROCESS_WRITE:
  5211. transport_generic_process_write(cmd);
  5212. break;
  5213. case TRANSPORT_COMPLETE_OK:
  5214. transport_stop_all_task_timers(cmd);
  5215. transport_generic_complete_ok(cmd);
  5216. break;
  5217. case TRANSPORT_REMOVE:
  5218. transport_generic_remove(cmd, 0);
  5219. break;
  5220. case TRANSPORT_FREE_CMD_INTR:
  5221. transport_generic_free_cmd(cmd, 0, 0);
  5222. break;
  5223. case TRANSPORT_PROCESS_TMR:
  5224. transport_generic_do_tmr(cmd);
  5225. break;
  5226. case TRANSPORT_COMPLETE_FAILURE:
  5227. transport_generic_request_failure(cmd, NULL, 1, 1);
  5228. break;
  5229. case TRANSPORT_COMPLETE_TIMEOUT:
  5230. transport_stop_all_task_timers(cmd);
  5231. transport_generic_request_timeout(cmd);
  5232. break;
  5233. case TRANSPORT_COMPLETE_QF_WP:
  5234. transport_generic_write_pending(cmd);
  5235. break;
  5236. default:
  5237. printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
  5238. " %d for ITT: 0x%08x i_state: %d on SE LUN:"
  5239. " %u\n", cmd->t_state, cmd->deferred_t_state,
  5240. cmd->se_tfo->get_task_tag(cmd),
  5241. cmd->se_tfo->get_cmd_state(cmd),
  5242. cmd->se_lun->unpacked_lun);
  5243. BUG();
  5244. }
  5245. goto get_cmd;
  5246. }
  5247. out:
  5248. transport_release_all_cmds(dev);
  5249. dev->process_thread = NULL;
  5250. return 0;
  5251. }