target_core_transport.c 169 KB

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