target_core_transport.c 157 KB

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