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