target_core_transport.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825
  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/net.h>
  29. #include <linux/delay.h>
  30. #include <linux/string.h>
  31. #include <linux/timer.h>
  32. #include <linux/slab.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/kthread.h>
  36. #include <linux/in.h>
  37. #include <linux/cdrom.h>
  38. #include <linux/module.h>
  39. #include <linux/ratelimit.h>
  40. #include <asm/unaligned.h>
  41. #include <net/sock.h>
  42. #include <net/tcp.h>
  43. #include <scsi/scsi.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include <target/target_core_base.h>
  47. #include <target/target_core_backend.h>
  48. #include <target/target_core_fabric.h>
  49. #include <target/target_core_configfs.h>
  50. #include "target_core_internal.h"
  51. #include "target_core_alua.h"
  52. #include "target_core_pr.h"
  53. #include "target_core_ua.h"
  54. static int sub_api_initialized;
  55. static struct workqueue_struct *target_completion_wq;
  56. static struct kmem_cache *se_sess_cache;
  57. struct kmem_cache *se_ua_cache;
  58. struct kmem_cache *t10_pr_reg_cache;
  59. struct kmem_cache *t10_alua_lu_gp_cache;
  60. struct kmem_cache *t10_alua_lu_gp_mem_cache;
  61. struct kmem_cache *t10_alua_tg_pt_gp_cache;
  62. struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
  63. static int transport_generic_write_pending(struct se_cmd *);
  64. static int transport_processing_thread(void *param);
  65. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
  66. static void transport_complete_task_attr(struct se_cmd *cmd);
  67. static void transport_handle_queue_full(struct se_cmd *cmd,
  68. struct se_device *dev);
  69. static void transport_free_dev_tasks(struct se_cmd *cmd);
  70. static int transport_generic_get_mem(struct se_cmd *cmd);
  71. static void transport_put_cmd(struct se_cmd *cmd);
  72. static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
  73. static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
  74. static void target_complete_ok_work(struct work_struct *work);
  75. int init_se_kmem_caches(void)
  76. {
  77. se_sess_cache = kmem_cache_create("se_sess_cache",
  78. sizeof(struct se_session), __alignof__(struct se_session),
  79. 0, NULL);
  80. if (!se_sess_cache) {
  81. pr_err("kmem_cache_create() for struct se_session"
  82. " failed\n");
  83. goto out;
  84. }
  85. se_ua_cache = kmem_cache_create("se_ua_cache",
  86. sizeof(struct se_ua), __alignof__(struct se_ua),
  87. 0, NULL);
  88. if (!se_ua_cache) {
  89. pr_err("kmem_cache_create() for struct se_ua failed\n");
  90. goto out_free_sess_cache;
  91. }
  92. t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
  93. sizeof(struct t10_pr_registration),
  94. __alignof__(struct t10_pr_registration), 0, NULL);
  95. if (!t10_pr_reg_cache) {
  96. pr_err("kmem_cache_create() for struct t10_pr_registration"
  97. " failed\n");
  98. goto out_free_ua_cache;
  99. }
  100. t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
  101. sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
  102. 0, NULL);
  103. if (!t10_alua_lu_gp_cache) {
  104. pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
  105. " failed\n");
  106. goto out_free_pr_reg_cache;
  107. }
  108. t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
  109. sizeof(struct t10_alua_lu_gp_member),
  110. __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
  111. if (!t10_alua_lu_gp_mem_cache) {
  112. pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
  113. "cache failed\n");
  114. goto out_free_lu_gp_cache;
  115. }
  116. t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
  117. sizeof(struct t10_alua_tg_pt_gp),
  118. __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
  119. if (!t10_alua_tg_pt_gp_cache) {
  120. pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
  121. "cache failed\n");
  122. goto out_free_lu_gp_mem_cache;
  123. }
  124. t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
  125. "t10_alua_tg_pt_gp_mem_cache",
  126. sizeof(struct t10_alua_tg_pt_gp_member),
  127. __alignof__(struct t10_alua_tg_pt_gp_member),
  128. 0, NULL);
  129. if (!t10_alua_tg_pt_gp_mem_cache) {
  130. pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
  131. "mem_t failed\n");
  132. goto out_free_tg_pt_gp_cache;
  133. }
  134. target_completion_wq = alloc_workqueue("target_completion",
  135. WQ_MEM_RECLAIM, 0);
  136. if (!target_completion_wq)
  137. goto out_free_tg_pt_gp_mem_cache;
  138. return 0;
  139. out_free_tg_pt_gp_mem_cache:
  140. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  141. out_free_tg_pt_gp_cache:
  142. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  143. out_free_lu_gp_mem_cache:
  144. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  145. out_free_lu_gp_cache:
  146. kmem_cache_destroy(t10_alua_lu_gp_cache);
  147. out_free_pr_reg_cache:
  148. kmem_cache_destroy(t10_pr_reg_cache);
  149. out_free_ua_cache:
  150. kmem_cache_destroy(se_ua_cache);
  151. out_free_sess_cache:
  152. kmem_cache_destroy(se_sess_cache);
  153. out:
  154. return -ENOMEM;
  155. }
  156. void release_se_kmem_caches(void)
  157. {
  158. destroy_workqueue(target_completion_wq);
  159. kmem_cache_destroy(se_sess_cache);
  160. kmem_cache_destroy(se_ua_cache);
  161. kmem_cache_destroy(t10_pr_reg_cache);
  162. kmem_cache_destroy(t10_alua_lu_gp_cache);
  163. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  164. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  165. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  166. }
  167. /* This code ensures unique mib indexes are handed out. */
  168. static DEFINE_SPINLOCK(scsi_mib_index_lock);
  169. static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
  170. /*
  171. * Allocate a new row index for the entry type specified
  172. */
  173. u32 scsi_get_new_index(scsi_index_t type)
  174. {
  175. u32 new_index;
  176. BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
  177. spin_lock(&scsi_mib_index_lock);
  178. new_index = ++scsi_mib_index[type];
  179. spin_unlock(&scsi_mib_index_lock);
  180. return new_index;
  181. }
  182. static void transport_init_queue_obj(struct se_queue_obj *qobj)
  183. {
  184. atomic_set(&qobj->queue_cnt, 0);
  185. INIT_LIST_HEAD(&qobj->qobj_list);
  186. init_waitqueue_head(&qobj->thread_wq);
  187. spin_lock_init(&qobj->cmd_queue_lock);
  188. }
  189. void transport_subsystem_check_init(void)
  190. {
  191. int ret;
  192. if (sub_api_initialized)
  193. return;
  194. ret = request_module("target_core_iblock");
  195. if (ret != 0)
  196. pr_err("Unable to load target_core_iblock\n");
  197. ret = request_module("target_core_file");
  198. if (ret != 0)
  199. pr_err("Unable to load target_core_file\n");
  200. ret = request_module("target_core_pscsi");
  201. if (ret != 0)
  202. pr_err("Unable to load target_core_pscsi\n");
  203. ret = request_module("target_core_stgt");
  204. if (ret != 0)
  205. pr_err("Unable to load target_core_stgt\n");
  206. sub_api_initialized = 1;
  207. return;
  208. }
  209. struct se_session *transport_init_session(void)
  210. {
  211. struct se_session *se_sess;
  212. se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
  213. if (!se_sess) {
  214. pr_err("Unable to allocate struct se_session from"
  215. " se_sess_cache\n");
  216. return ERR_PTR(-ENOMEM);
  217. }
  218. INIT_LIST_HEAD(&se_sess->sess_list);
  219. INIT_LIST_HEAD(&se_sess->sess_acl_list);
  220. INIT_LIST_HEAD(&se_sess->sess_cmd_list);
  221. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  222. spin_lock_init(&se_sess->sess_cmd_lock);
  223. return se_sess;
  224. }
  225. EXPORT_SYMBOL(transport_init_session);
  226. /*
  227. * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
  228. */
  229. void __transport_register_session(
  230. struct se_portal_group *se_tpg,
  231. struct se_node_acl *se_nacl,
  232. struct se_session *se_sess,
  233. void *fabric_sess_ptr)
  234. {
  235. unsigned char buf[PR_REG_ISID_LEN];
  236. se_sess->se_tpg = se_tpg;
  237. se_sess->fabric_sess_ptr = fabric_sess_ptr;
  238. /*
  239. * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
  240. *
  241. * Only set for struct se_session's that will actually be moving I/O.
  242. * eg: *NOT* discovery sessions.
  243. */
  244. if (se_nacl) {
  245. /*
  246. * If the fabric module supports an ISID based TransportID,
  247. * save this value in binary from the fabric I_T Nexus now.
  248. */
  249. if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
  250. memset(&buf[0], 0, PR_REG_ISID_LEN);
  251. se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
  252. &buf[0], PR_REG_ISID_LEN);
  253. se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
  254. }
  255. spin_lock_irq(&se_nacl->nacl_sess_lock);
  256. /*
  257. * The se_nacl->nacl_sess pointer will be set to the
  258. * last active I_T Nexus for each struct se_node_acl.
  259. */
  260. se_nacl->nacl_sess = se_sess;
  261. list_add_tail(&se_sess->sess_acl_list,
  262. &se_nacl->acl_sess_list);
  263. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  264. }
  265. list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
  266. pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
  267. se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
  268. }
  269. EXPORT_SYMBOL(__transport_register_session);
  270. void transport_register_session(
  271. struct se_portal_group *se_tpg,
  272. struct se_node_acl *se_nacl,
  273. struct se_session *se_sess,
  274. void *fabric_sess_ptr)
  275. {
  276. unsigned long flags;
  277. spin_lock_irqsave(&se_tpg->session_lock, flags);
  278. __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
  279. spin_unlock_irqrestore(&se_tpg->session_lock, flags);
  280. }
  281. EXPORT_SYMBOL(transport_register_session);
  282. void transport_deregister_session_configfs(struct se_session *se_sess)
  283. {
  284. struct se_node_acl *se_nacl;
  285. unsigned long flags;
  286. /*
  287. * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
  288. */
  289. se_nacl = se_sess->se_node_acl;
  290. if (se_nacl) {
  291. spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
  292. list_del(&se_sess->sess_acl_list);
  293. /*
  294. * If the session list is empty, then clear the pointer.
  295. * Otherwise, set the struct se_session pointer from the tail
  296. * element of the per struct se_node_acl active session list.
  297. */
  298. if (list_empty(&se_nacl->acl_sess_list))
  299. se_nacl->nacl_sess = NULL;
  300. else {
  301. se_nacl->nacl_sess = container_of(
  302. se_nacl->acl_sess_list.prev,
  303. struct se_session, sess_acl_list);
  304. }
  305. spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
  306. }
  307. }
  308. EXPORT_SYMBOL(transport_deregister_session_configfs);
  309. void transport_free_session(struct se_session *se_sess)
  310. {
  311. kmem_cache_free(se_sess_cache, se_sess);
  312. }
  313. EXPORT_SYMBOL(transport_free_session);
  314. void transport_deregister_session(struct se_session *se_sess)
  315. {
  316. struct se_portal_group *se_tpg = se_sess->se_tpg;
  317. struct se_node_acl *se_nacl;
  318. unsigned long flags;
  319. if (!se_tpg) {
  320. transport_free_session(se_sess);
  321. return;
  322. }
  323. spin_lock_irqsave(&se_tpg->session_lock, flags);
  324. list_del(&se_sess->sess_list);
  325. se_sess->se_tpg = NULL;
  326. se_sess->fabric_sess_ptr = NULL;
  327. spin_unlock_irqrestore(&se_tpg->session_lock, flags);
  328. /*
  329. * Determine if we need to do extra work for this initiator node's
  330. * struct se_node_acl if it had been previously dynamically generated.
  331. */
  332. se_nacl = se_sess->se_node_acl;
  333. if (se_nacl) {
  334. spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
  335. if (se_nacl->dynamic_node_acl) {
  336. if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
  337. se_tpg)) {
  338. list_del(&se_nacl->acl_list);
  339. se_tpg->num_node_acls--;
  340. spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
  341. core_tpg_wait_for_nacl_pr_ref(se_nacl);
  342. core_free_device_list_for_node(se_nacl, se_tpg);
  343. se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
  344. se_nacl);
  345. spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
  346. }
  347. }
  348. spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
  349. }
  350. transport_free_session(se_sess);
  351. pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
  352. se_tpg->se_tpg_tfo->get_fabric_name());
  353. }
  354. EXPORT_SYMBOL(transport_deregister_session);
  355. /*
  356. * Called with cmd->t_state_lock held.
  357. */
  358. static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
  359. {
  360. struct se_device *dev = cmd->se_dev;
  361. struct se_task *task;
  362. unsigned long flags;
  363. if (!dev)
  364. return;
  365. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  366. if (task->task_flags & TF_ACTIVE)
  367. continue;
  368. spin_lock_irqsave(&dev->execute_task_lock, flags);
  369. if (task->t_state_active) {
  370. pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
  371. cmd->se_tfo->get_task_tag(cmd), dev, task);
  372. list_del(&task->t_state_list);
  373. atomic_dec(&cmd->t_task_cdbs_ex_left);
  374. task->t_state_active = false;
  375. }
  376. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  377. }
  378. }
  379. /* transport_cmd_check_stop():
  380. *
  381. * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
  382. * 'transport_off = 2' determines if task_dev_state should be removed.
  383. *
  384. * A non-zero u8 t_state sets cmd->t_state.
  385. * Returns 1 when command is stopped, else 0.
  386. */
  387. static int transport_cmd_check_stop(
  388. struct se_cmd *cmd,
  389. int transport_off,
  390. u8 t_state)
  391. {
  392. unsigned long flags;
  393. spin_lock_irqsave(&cmd->t_state_lock, flags);
  394. /*
  395. * Determine if IOCTL context caller in requesting the stopping of this
  396. * command for LUN shutdown purposes.
  397. */
  398. if (cmd->transport_state & CMD_T_LUN_STOP) {
  399. pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
  400. __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
  401. cmd->transport_state &= ~CMD_T_ACTIVE;
  402. if (transport_off == 2)
  403. transport_all_task_dev_remove_state(cmd);
  404. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  405. complete(&cmd->transport_lun_stop_comp);
  406. return 1;
  407. }
  408. /*
  409. * Determine if frontend context caller is requesting the stopping of
  410. * this command for frontend exceptions.
  411. */
  412. if (cmd->transport_state & CMD_T_STOP) {
  413. pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
  414. __func__, __LINE__,
  415. cmd->se_tfo->get_task_tag(cmd));
  416. if (transport_off == 2)
  417. transport_all_task_dev_remove_state(cmd);
  418. /*
  419. * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
  420. * to FE.
  421. */
  422. if (transport_off == 2)
  423. cmd->se_lun = NULL;
  424. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  425. complete(&cmd->t_transport_stop_comp);
  426. return 1;
  427. }
  428. if (transport_off) {
  429. cmd->transport_state &= ~CMD_T_ACTIVE;
  430. if (transport_off == 2) {
  431. transport_all_task_dev_remove_state(cmd);
  432. /*
  433. * Clear struct se_cmd->se_lun before the transport_off == 2
  434. * handoff to fabric module.
  435. */
  436. cmd->se_lun = NULL;
  437. /*
  438. * Some fabric modules like tcm_loop can release
  439. * their internally allocated I/O reference now and
  440. * struct se_cmd now.
  441. *
  442. * Fabric modules are expected to return '1' here if the
  443. * se_cmd being passed is released at this point,
  444. * or zero if not being released.
  445. */
  446. if (cmd->se_tfo->check_stop_free != NULL) {
  447. spin_unlock_irqrestore(
  448. &cmd->t_state_lock, flags);
  449. return cmd->se_tfo->check_stop_free(cmd);
  450. }
  451. }
  452. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  453. return 0;
  454. } else if (t_state)
  455. cmd->t_state = t_state;
  456. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  457. return 0;
  458. }
  459. static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
  460. {
  461. return transport_cmd_check_stop(cmd, 2, 0);
  462. }
  463. static void transport_lun_remove_cmd(struct se_cmd *cmd)
  464. {
  465. struct se_lun *lun = cmd->se_lun;
  466. unsigned long flags;
  467. if (!lun)
  468. return;
  469. spin_lock_irqsave(&cmd->t_state_lock, flags);
  470. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  471. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  472. transport_all_task_dev_remove_state(cmd);
  473. }
  474. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  475. spin_lock_irqsave(&lun->lun_cmd_lock, flags);
  476. if (!list_empty(&cmd->se_lun_node))
  477. list_del_init(&cmd->se_lun_node);
  478. spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
  479. }
  480. void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
  481. {
  482. if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  483. transport_lun_remove_cmd(cmd);
  484. if (transport_cmd_check_stop_to_fabric(cmd))
  485. return;
  486. if (remove) {
  487. transport_remove_cmd_from_queue(cmd);
  488. transport_put_cmd(cmd);
  489. }
  490. }
  491. static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
  492. bool at_head)
  493. {
  494. struct se_device *dev = cmd->se_dev;
  495. struct se_queue_obj *qobj = &dev->dev_queue_obj;
  496. unsigned long flags;
  497. if (t_state) {
  498. spin_lock_irqsave(&cmd->t_state_lock, flags);
  499. cmd->t_state = t_state;
  500. cmd->transport_state |= CMD_T_ACTIVE;
  501. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  502. }
  503. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  504. /* If the cmd is already on the list, remove it before we add it */
  505. if (!list_empty(&cmd->se_queue_node))
  506. list_del(&cmd->se_queue_node);
  507. else
  508. atomic_inc(&qobj->queue_cnt);
  509. if (at_head)
  510. list_add(&cmd->se_queue_node, &qobj->qobj_list);
  511. else
  512. list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
  513. cmd->transport_state |= CMD_T_QUEUED;
  514. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  515. wake_up_interruptible(&qobj->thread_wq);
  516. }
  517. static struct se_cmd *
  518. transport_get_cmd_from_queue(struct se_queue_obj *qobj)
  519. {
  520. struct se_cmd *cmd;
  521. unsigned long flags;
  522. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  523. if (list_empty(&qobj->qobj_list)) {
  524. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  525. return NULL;
  526. }
  527. cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
  528. cmd->transport_state &= ~CMD_T_QUEUED;
  529. list_del_init(&cmd->se_queue_node);
  530. atomic_dec(&qobj->queue_cnt);
  531. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  532. return cmd;
  533. }
  534. static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
  535. {
  536. struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
  537. unsigned long flags;
  538. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  539. if (!(cmd->transport_state & CMD_T_QUEUED)) {
  540. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  541. return;
  542. }
  543. cmd->transport_state &= ~CMD_T_QUEUED;
  544. atomic_dec(&qobj->queue_cnt);
  545. list_del_init(&cmd->se_queue_node);
  546. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  547. }
  548. /*
  549. * Completion function used by TCM subsystem plugins (such as FILEIO)
  550. * for queueing up response from struct se_subsystem_api->do_task()
  551. */
  552. void transport_complete_sync_cache(struct se_cmd *cmd, int good)
  553. {
  554. struct se_task *task = list_entry(cmd->t_task_list.next,
  555. struct se_task, t_list);
  556. if (good) {
  557. cmd->scsi_status = SAM_STAT_GOOD;
  558. task->task_scsi_status = GOOD;
  559. } else {
  560. task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
  561. task->task_se_cmd->scsi_sense_reason =
  562. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  563. }
  564. transport_complete_task(task, good);
  565. }
  566. EXPORT_SYMBOL(transport_complete_sync_cache);
  567. static void target_complete_failure_work(struct work_struct *work)
  568. {
  569. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  570. transport_generic_request_failure(cmd);
  571. }
  572. /* transport_complete_task():
  573. *
  574. * Called from interrupt and non interrupt context depending
  575. * on the transport plugin.
  576. */
  577. void transport_complete_task(struct se_task *task, int success)
  578. {
  579. struct se_cmd *cmd = task->task_se_cmd;
  580. struct se_device *dev = cmd->se_dev;
  581. unsigned long flags;
  582. spin_lock_irqsave(&cmd->t_state_lock, flags);
  583. task->task_flags &= ~TF_ACTIVE;
  584. /*
  585. * See if any sense data exists, if so set the TASK_SENSE flag.
  586. * Also check for any other post completion work that needs to be
  587. * done by the plugins.
  588. */
  589. if (dev && dev->transport->transport_complete) {
  590. if (dev->transport->transport_complete(task) != 0) {
  591. cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
  592. task->task_flags |= TF_HAS_SENSE;
  593. success = 1;
  594. }
  595. }
  596. /*
  597. * See if we are waiting for outstanding struct se_task
  598. * to complete for an exception condition
  599. */
  600. if (task->task_flags & TF_REQUEST_STOP) {
  601. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  602. complete(&task->task_stop_comp);
  603. return;
  604. }
  605. if (!success)
  606. cmd->transport_state |= CMD_T_FAILED;
  607. /*
  608. * Decrement the outstanding t_task_cdbs_left count. The last
  609. * struct se_task from struct se_cmd will complete itself into the
  610. * device queue depending upon int success.
  611. */
  612. if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
  613. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  614. return;
  615. }
  616. /*
  617. * Check for case where an explict ABORT_TASK has been received
  618. * and transport_wait_for_tasks() will be waiting for completion..
  619. */
  620. if (cmd->transport_state & CMD_T_ABORTED &&
  621. cmd->transport_state & CMD_T_STOP) {
  622. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  623. complete(&cmd->t_transport_stop_comp);
  624. return;
  625. } else if (cmd->transport_state & CMD_T_FAILED) {
  626. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  627. INIT_WORK(&cmd->work, target_complete_failure_work);
  628. } else {
  629. INIT_WORK(&cmd->work, target_complete_ok_work);
  630. }
  631. cmd->t_state = TRANSPORT_COMPLETE;
  632. cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
  633. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  634. queue_work(target_completion_wq, &cmd->work);
  635. }
  636. EXPORT_SYMBOL(transport_complete_task);
  637. /*
  638. * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
  639. * struct se_task list are ready to be added to the active execution list
  640. * struct se_device
  641. * Called with se_dev_t->execute_task_lock called.
  642. */
  643. static inline int transport_add_task_check_sam_attr(
  644. struct se_task *task,
  645. struct se_task *task_prev,
  646. struct se_device *dev)
  647. {
  648. /*
  649. * No SAM Task attribute emulation enabled, add to tail of
  650. * execution queue
  651. */
  652. if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
  653. list_add_tail(&task->t_execute_list, &dev->execute_task_list);
  654. return 0;
  655. }
  656. /*
  657. * HEAD_OF_QUEUE attribute for received CDB, which means
  658. * the first task that is associated with a struct se_cmd goes to
  659. * head of the struct se_device->execute_task_list, and task_prev
  660. * after that for each subsequent task
  661. */
  662. if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
  663. list_add(&task->t_execute_list,
  664. (task_prev != NULL) ?
  665. &task_prev->t_execute_list :
  666. &dev->execute_task_list);
  667. pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
  668. " in execution queue\n",
  669. task->task_se_cmd->t_task_cdb[0]);
  670. return 1;
  671. }
  672. /*
  673. * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
  674. * transitioned from Dermant -> Active state, and are added to the end
  675. * of the struct se_device->execute_task_list
  676. */
  677. list_add_tail(&task->t_execute_list, &dev->execute_task_list);
  678. return 0;
  679. }
  680. /* __transport_add_task_to_execute_queue():
  681. *
  682. * Called with se_dev_t->execute_task_lock called.
  683. */
  684. static void __transport_add_task_to_execute_queue(
  685. struct se_task *task,
  686. struct se_task *task_prev,
  687. struct se_device *dev)
  688. {
  689. int head_of_queue;
  690. head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
  691. atomic_inc(&dev->execute_tasks);
  692. if (task->t_state_active)
  693. return;
  694. /*
  695. * Determine if this task needs to go to HEAD_OF_QUEUE for the
  696. * state list as well. Running with SAM Task Attribute emulation
  697. * will always return head_of_queue == 0 here
  698. */
  699. if (head_of_queue)
  700. list_add(&task->t_state_list, (task_prev) ?
  701. &task_prev->t_state_list :
  702. &dev->state_task_list);
  703. else
  704. list_add_tail(&task->t_state_list, &dev->state_task_list);
  705. task->t_state_active = true;
  706. pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
  707. task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
  708. task, dev);
  709. }
  710. static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
  711. {
  712. struct se_device *dev = cmd->se_dev;
  713. struct se_task *task;
  714. unsigned long flags;
  715. spin_lock_irqsave(&cmd->t_state_lock, flags);
  716. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  717. spin_lock(&dev->execute_task_lock);
  718. if (!task->t_state_active) {
  719. list_add_tail(&task->t_state_list,
  720. &dev->state_task_list);
  721. task->t_state_active = true;
  722. pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
  723. task->task_se_cmd->se_tfo->get_task_tag(
  724. task->task_se_cmd), task, dev);
  725. }
  726. spin_unlock(&dev->execute_task_lock);
  727. }
  728. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  729. }
  730. static void __transport_add_tasks_from_cmd(struct se_cmd *cmd)
  731. {
  732. struct se_device *dev = cmd->se_dev;
  733. struct se_task *task, *task_prev = NULL;
  734. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  735. if (!list_empty(&task->t_execute_list))
  736. continue;
  737. /*
  738. * __transport_add_task_to_execute_queue() handles the
  739. * SAM Task Attribute emulation if enabled
  740. */
  741. __transport_add_task_to_execute_queue(task, task_prev, dev);
  742. task_prev = task;
  743. }
  744. }
  745. static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
  746. {
  747. unsigned long flags;
  748. struct se_device *dev = cmd->se_dev;
  749. spin_lock_irqsave(&dev->execute_task_lock, flags);
  750. __transport_add_tasks_from_cmd(cmd);
  751. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  752. }
  753. void __transport_remove_task_from_execute_queue(struct se_task *task,
  754. struct se_device *dev)
  755. {
  756. list_del_init(&task->t_execute_list);
  757. atomic_dec(&dev->execute_tasks);
  758. }
  759. static void transport_remove_task_from_execute_queue(
  760. struct se_task *task,
  761. struct se_device *dev)
  762. {
  763. unsigned long flags;
  764. if (WARN_ON(list_empty(&task->t_execute_list)))
  765. return;
  766. spin_lock_irqsave(&dev->execute_task_lock, flags);
  767. __transport_remove_task_from_execute_queue(task, dev);
  768. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  769. }
  770. /*
  771. * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
  772. */
  773. static void target_qf_do_work(struct work_struct *work)
  774. {
  775. struct se_device *dev = container_of(work, struct se_device,
  776. qf_work_queue);
  777. LIST_HEAD(qf_cmd_list);
  778. struct se_cmd *cmd, *cmd_tmp;
  779. spin_lock_irq(&dev->qf_cmd_lock);
  780. list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
  781. spin_unlock_irq(&dev->qf_cmd_lock);
  782. list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
  783. list_del(&cmd->se_qf_node);
  784. atomic_dec(&dev->dev_qf_count);
  785. smp_mb__after_atomic_dec();
  786. pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
  787. " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
  788. (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
  789. (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
  790. : "UNKNOWN");
  791. transport_add_cmd_to_queue(cmd, cmd->t_state, true);
  792. }
  793. }
  794. unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
  795. {
  796. switch (cmd->data_direction) {
  797. case DMA_NONE:
  798. return "NONE";
  799. case DMA_FROM_DEVICE:
  800. return "READ";
  801. case DMA_TO_DEVICE:
  802. return "WRITE";
  803. case DMA_BIDIRECTIONAL:
  804. return "BIDI";
  805. default:
  806. break;
  807. }
  808. return "UNKNOWN";
  809. }
  810. void transport_dump_dev_state(
  811. struct se_device *dev,
  812. char *b,
  813. int *bl)
  814. {
  815. *bl += sprintf(b + *bl, "Status: ");
  816. switch (dev->dev_status) {
  817. case TRANSPORT_DEVICE_ACTIVATED:
  818. *bl += sprintf(b + *bl, "ACTIVATED");
  819. break;
  820. case TRANSPORT_DEVICE_DEACTIVATED:
  821. *bl += sprintf(b + *bl, "DEACTIVATED");
  822. break;
  823. case TRANSPORT_DEVICE_SHUTDOWN:
  824. *bl += sprintf(b + *bl, "SHUTDOWN");
  825. break;
  826. case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
  827. case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
  828. *bl += sprintf(b + *bl, "OFFLINE");
  829. break;
  830. default:
  831. *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
  832. break;
  833. }
  834. *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
  835. atomic_read(&dev->execute_tasks), dev->queue_depth);
  836. *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
  837. dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
  838. *bl += sprintf(b + *bl, " ");
  839. }
  840. void transport_dump_vpd_proto_id(
  841. struct t10_vpd *vpd,
  842. unsigned char *p_buf,
  843. int p_buf_len)
  844. {
  845. unsigned char buf[VPD_TMP_BUF_SIZE];
  846. int len;
  847. memset(buf, 0, VPD_TMP_BUF_SIZE);
  848. len = sprintf(buf, "T10 VPD Protocol Identifier: ");
  849. switch (vpd->protocol_identifier) {
  850. case 0x00:
  851. sprintf(buf+len, "Fibre Channel\n");
  852. break;
  853. case 0x10:
  854. sprintf(buf+len, "Parallel SCSI\n");
  855. break;
  856. case 0x20:
  857. sprintf(buf+len, "SSA\n");
  858. break;
  859. case 0x30:
  860. sprintf(buf+len, "IEEE 1394\n");
  861. break;
  862. case 0x40:
  863. sprintf(buf+len, "SCSI Remote Direct Memory Access"
  864. " Protocol\n");
  865. break;
  866. case 0x50:
  867. sprintf(buf+len, "Internet SCSI (iSCSI)\n");
  868. break;
  869. case 0x60:
  870. sprintf(buf+len, "SAS Serial SCSI Protocol\n");
  871. break;
  872. case 0x70:
  873. sprintf(buf+len, "Automation/Drive Interface Transport"
  874. " Protocol\n");
  875. break;
  876. case 0x80:
  877. sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
  878. break;
  879. default:
  880. sprintf(buf+len, "Unknown 0x%02x\n",
  881. vpd->protocol_identifier);
  882. break;
  883. }
  884. if (p_buf)
  885. strncpy(p_buf, buf, p_buf_len);
  886. else
  887. pr_debug("%s", buf);
  888. }
  889. void
  890. transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
  891. {
  892. /*
  893. * Check if the Protocol Identifier Valid (PIV) bit is set..
  894. *
  895. * from spc3r23.pdf section 7.5.1
  896. */
  897. if (page_83[1] & 0x80) {
  898. vpd->protocol_identifier = (page_83[0] & 0xf0);
  899. vpd->protocol_identifier_set = 1;
  900. transport_dump_vpd_proto_id(vpd, NULL, 0);
  901. }
  902. }
  903. EXPORT_SYMBOL(transport_set_vpd_proto_id);
  904. int transport_dump_vpd_assoc(
  905. struct t10_vpd *vpd,
  906. unsigned char *p_buf,
  907. int p_buf_len)
  908. {
  909. unsigned char buf[VPD_TMP_BUF_SIZE];
  910. int ret = 0;
  911. int len;
  912. memset(buf, 0, VPD_TMP_BUF_SIZE);
  913. len = sprintf(buf, "T10 VPD Identifier Association: ");
  914. switch (vpd->association) {
  915. case 0x00:
  916. sprintf(buf+len, "addressed logical unit\n");
  917. break;
  918. case 0x10:
  919. sprintf(buf+len, "target port\n");
  920. break;
  921. case 0x20:
  922. sprintf(buf+len, "SCSI target device\n");
  923. break;
  924. default:
  925. sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
  926. ret = -EINVAL;
  927. break;
  928. }
  929. if (p_buf)
  930. strncpy(p_buf, buf, p_buf_len);
  931. else
  932. pr_debug("%s", buf);
  933. return ret;
  934. }
  935. int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
  936. {
  937. /*
  938. * The VPD identification association..
  939. *
  940. * from spc3r23.pdf Section 7.6.3.1 Table 297
  941. */
  942. vpd->association = (page_83[1] & 0x30);
  943. return transport_dump_vpd_assoc(vpd, NULL, 0);
  944. }
  945. EXPORT_SYMBOL(transport_set_vpd_assoc);
  946. int transport_dump_vpd_ident_type(
  947. struct t10_vpd *vpd,
  948. unsigned char *p_buf,
  949. int p_buf_len)
  950. {
  951. unsigned char buf[VPD_TMP_BUF_SIZE];
  952. int ret = 0;
  953. int len;
  954. memset(buf, 0, VPD_TMP_BUF_SIZE);
  955. len = sprintf(buf, "T10 VPD Identifier Type: ");
  956. switch (vpd->device_identifier_type) {
  957. case 0x00:
  958. sprintf(buf+len, "Vendor specific\n");
  959. break;
  960. case 0x01:
  961. sprintf(buf+len, "T10 Vendor ID based\n");
  962. break;
  963. case 0x02:
  964. sprintf(buf+len, "EUI-64 based\n");
  965. break;
  966. case 0x03:
  967. sprintf(buf+len, "NAA\n");
  968. break;
  969. case 0x04:
  970. sprintf(buf+len, "Relative target port identifier\n");
  971. break;
  972. case 0x08:
  973. sprintf(buf+len, "SCSI name string\n");
  974. break;
  975. default:
  976. sprintf(buf+len, "Unsupported: 0x%02x\n",
  977. vpd->device_identifier_type);
  978. ret = -EINVAL;
  979. break;
  980. }
  981. if (p_buf) {
  982. if (p_buf_len < strlen(buf)+1)
  983. return -EINVAL;
  984. strncpy(p_buf, buf, p_buf_len);
  985. } else {
  986. pr_debug("%s", buf);
  987. }
  988. return ret;
  989. }
  990. int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
  991. {
  992. /*
  993. * The VPD identifier type..
  994. *
  995. * from spc3r23.pdf Section 7.6.3.1 Table 298
  996. */
  997. vpd->device_identifier_type = (page_83[1] & 0x0f);
  998. return transport_dump_vpd_ident_type(vpd, NULL, 0);
  999. }
  1000. EXPORT_SYMBOL(transport_set_vpd_ident_type);
  1001. int transport_dump_vpd_ident(
  1002. struct t10_vpd *vpd,
  1003. unsigned char *p_buf,
  1004. int p_buf_len)
  1005. {
  1006. unsigned char buf[VPD_TMP_BUF_SIZE];
  1007. int ret = 0;
  1008. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1009. switch (vpd->device_identifier_code_set) {
  1010. case 0x01: /* Binary */
  1011. sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
  1012. &vpd->device_identifier[0]);
  1013. break;
  1014. case 0x02: /* ASCII */
  1015. sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
  1016. &vpd->device_identifier[0]);
  1017. break;
  1018. case 0x03: /* UTF-8 */
  1019. sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
  1020. &vpd->device_identifier[0]);
  1021. break;
  1022. default:
  1023. sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
  1024. " 0x%02x", vpd->device_identifier_code_set);
  1025. ret = -EINVAL;
  1026. break;
  1027. }
  1028. if (p_buf)
  1029. strncpy(p_buf, buf, p_buf_len);
  1030. else
  1031. pr_debug("%s", buf);
  1032. return ret;
  1033. }
  1034. int
  1035. transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
  1036. {
  1037. static const char hex_str[] = "0123456789abcdef";
  1038. int j = 0, i = 4; /* offset to start of the identifer */
  1039. /*
  1040. * The VPD Code Set (encoding)
  1041. *
  1042. * from spc3r23.pdf Section 7.6.3.1 Table 296
  1043. */
  1044. vpd->device_identifier_code_set = (page_83[0] & 0x0f);
  1045. switch (vpd->device_identifier_code_set) {
  1046. case 0x01: /* Binary */
  1047. vpd->device_identifier[j++] =
  1048. hex_str[vpd->device_identifier_type];
  1049. while (i < (4 + page_83[3])) {
  1050. vpd->device_identifier[j++] =
  1051. hex_str[(page_83[i] & 0xf0) >> 4];
  1052. vpd->device_identifier[j++] =
  1053. hex_str[page_83[i] & 0x0f];
  1054. i++;
  1055. }
  1056. break;
  1057. case 0x02: /* ASCII */
  1058. case 0x03: /* UTF-8 */
  1059. while (i < (4 + page_83[3]))
  1060. vpd->device_identifier[j++] = page_83[i++];
  1061. break;
  1062. default:
  1063. break;
  1064. }
  1065. return transport_dump_vpd_ident(vpd, NULL, 0);
  1066. }
  1067. EXPORT_SYMBOL(transport_set_vpd_ident);
  1068. static void core_setup_task_attr_emulation(struct se_device *dev)
  1069. {
  1070. /*
  1071. * If this device is from Target_Core_Mod/pSCSI, disable the
  1072. * SAM Task Attribute emulation.
  1073. *
  1074. * This is currently not available in upsream Linux/SCSI Target
  1075. * mode code, and is assumed to be disabled while using TCM/pSCSI.
  1076. */
  1077. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1078. dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
  1079. return;
  1080. }
  1081. dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
  1082. pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
  1083. " device\n", dev->transport->name,
  1084. dev->transport->get_device_rev(dev));
  1085. }
  1086. static void scsi_dump_inquiry(struct se_device *dev)
  1087. {
  1088. struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
  1089. char buf[17];
  1090. int i, device_type;
  1091. /*
  1092. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1093. */
  1094. for (i = 0; i < 8; i++)
  1095. if (wwn->vendor[i] >= 0x20)
  1096. buf[i] = wwn->vendor[i];
  1097. else
  1098. buf[i] = ' ';
  1099. buf[i] = '\0';
  1100. pr_debug(" Vendor: %s\n", buf);
  1101. for (i = 0; i < 16; i++)
  1102. if (wwn->model[i] >= 0x20)
  1103. buf[i] = wwn->model[i];
  1104. else
  1105. buf[i] = ' ';
  1106. buf[i] = '\0';
  1107. pr_debug(" Model: %s\n", buf);
  1108. for (i = 0; i < 4; i++)
  1109. if (wwn->revision[i] >= 0x20)
  1110. buf[i] = wwn->revision[i];
  1111. else
  1112. buf[i] = ' ';
  1113. buf[i] = '\0';
  1114. pr_debug(" Revision: %s\n", buf);
  1115. device_type = dev->transport->get_device_type(dev);
  1116. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1117. pr_debug(" ANSI SCSI revision: %02x\n",
  1118. dev->transport->get_device_rev(dev));
  1119. }
  1120. struct se_device *transport_add_device_to_core_hba(
  1121. struct se_hba *hba,
  1122. struct se_subsystem_api *transport,
  1123. struct se_subsystem_dev *se_dev,
  1124. u32 device_flags,
  1125. void *transport_dev,
  1126. struct se_dev_limits *dev_limits,
  1127. const char *inquiry_prod,
  1128. const char *inquiry_rev)
  1129. {
  1130. int force_pt;
  1131. struct se_device *dev;
  1132. dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
  1133. if (!dev) {
  1134. pr_err("Unable to allocate memory for se_dev_t\n");
  1135. return NULL;
  1136. }
  1137. transport_init_queue_obj(&dev->dev_queue_obj);
  1138. dev->dev_flags = device_flags;
  1139. dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
  1140. dev->dev_ptr = transport_dev;
  1141. dev->se_hba = hba;
  1142. dev->se_sub_dev = se_dev;
  1143. dev->transport = transport;
  1144. INIT_LIST_HEAD(&dev->dev_list);
  1145. INIT_LIST_HEAD(&dev->dev_sep_list);
  1146. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1147. INIT_LIST_HEAD(&dev->execute_task_list);
  1148. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1149. INIT_LIST_HEAD(&dev->state_task_list);
  1150. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1151. spin_lock_init(&dev->execute_task_lock);
  1152. spin_lock_init(&dev->delayed_cmd_lock);
  1153. spin_lock_init(&dev->dev_reservation_lock);
  1154. spin_lock_init(&dev->dev_status_lock);
  1155. spin_lock_init(&dev->se_port_lock);
  1156. spin_lock_init(&dev->se_tmr_lock);
  1157. spin_lock_init(&dev->qf_cmd_lock);
  1158. atomic_set(&dev->dev_ordered_id, 0);
  1159. se_dev_set_default_attribs(dev, dev_limits);
  1160. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1161. dev->creation_time = get_jiffies_64();
  1162. spin_lock_init(&dev->stats_lock);
  1163. spin_lock(&hba->device_lock);
  1164. list_add_tail(&dev->dev_list, &hba->hba_dev_list);
  1165. hba->dev_count++;
  1166. spin_unlock(&hba->device_lock);
  1167. /*
  1168. * Setup the SAM Task Attribute emulation for struct se_device
  1169. */
  1170. core_setup_task_attr_emulation(dev);
  1171. /*
  1172. * Force PR and ALUA passthrough emulation with internal object use.
  1173. */
  1174. force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
  1175. /*
  1176. * Setup the Reservations infrastructure for struct se_device
  1177. */
  1178. core_setup_reservations(dev, force_pt);
  1179. /*
  1180. * Setup the Asymmetric Logical Unit Assignment for struct se_device
  1181. */
  1182. if (core_setup_alua(dev, force_pt) < 0)
  1183. goto out;
  1184. /*
  1185. * Startup the struct se_device processing thread
  1186. */
  1187. dev->process_thread = kthread_run(transport_processing_thread, dev,
  1188. "LIO_%s", dev->transport->name);
  1189. if (IS_ERR(dev->process_thread)) {
  1190. pr_err("Unable to create kthread: LIO_%s\n",
  1191. dev->transport->name);
  1192. goto out;
  1193. }
  1194. /*
  1195. * Setup work_queue for QUEUE_FULL
  1196. */
  1197. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1198. /*
  1199. * Preload the initial INQUIRY const values if we are doing
  1200. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1201. * passthrough because this is being provided by the backend LLD.
  1202. * This is required so that transport_get_inquiry() copies these
  1203. * originals once back into DEV_T10_WWN(dev) for the virtual device
  1204. * setup.
  1205. */
  1206. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1207. if (!inquiry_prod || !inquiry_rev) {
  1208. pr_err("All non TCM/pSCSI plugins require"
  1209. " INQUIRY consts\n");
  1210. goto out;
  1211. }
  1212. strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1213. strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
  1214. strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
  1215. }
  1216. scsi_dump_inquiry(dev);
  1217. return dev;
  1218. out:
  1219. kthread_stop(dev->process_thread);
  1220. spin_lock(&hba->device_lock);
  1221. list_del(&dev->dev_list);
  1222. hba->dev_count--;
  1223. spin_unlock(&hba->device_lock);
  1224. se_release_vpd_for_dev(dev);
  1225. kfree(dev);
  1226. return NULL;
  1227. }
  1228. EXPORT_SYMBOL(transport_add_device_to_core_hba);
  1229. /* transport_generic_prepare_cdb():
  1230. *
  1231. * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
  1232. * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
  1233. * The point of this is since we are mapping iSCSI LUNs to
  1234. * SCSI Target IDs having a non-zero LUN in the CDB will throw the
  1235. * devices and HBAs for a loop.
  1236. */
  1237. static inline void transport_generic_prepare_cdb(
  1238. unsigned char *cdb)
  1239. {
  1240. switch (cdb[0]) {
  1241. case READ_10: /* SBC - RDProtect */
  1242. case READ_12: /* SBC - RDProtect */
  1243. case READ_16: /* SBC - RDProtect */
  1244. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  1245. case VERIFY: /* SBC - VRProtect */
  1246. case VERIFY_16: /* SBC - VRProtect */
  1247. case WRITE_VERIFY: /* SBC - VRProtect */
  1248. case WRITE_VERIFY_12: /* SBC - VRProtect */
  1249. break;
  1250. default:
  1251. cdb[1] &= 0x1f; /* clear logical unit number */
  1252. break;
  1253. }
  1254. }
  1255. static struct se_task *
  1256. transport_generic_get_task(struct se_cmd *cmd,
  1257. enum dma_data_direction data_direction)
  1258. {
  1259. struct se_task *task;
  1260. struct se_device *dev = cmd->se_dev;
  1261. task = dev->transport->alloc_task(cmd->t_task_cdb);
  1262. if (!task) {
  1263. pr_err("Unable to allocate struct se_task\n");
  1264. return NULL;
  1265. }
  1266. INIT_LIST_HEAD(&task->t_list);
  1267. INIT_LIST_HEAD(&task->t_execute_list);
  1268. INIT_LIST_HEAD(&task->t_state_list);
  1269. init_completion(&task->task_stop_comp);
  1270. task->task_se_cmd = cmd;
  1271. task->task_data_direction = data_direction;
  1272. return task;
  1273. }
  1274. static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
  1275. /*
  1276. * Used by fabric modules containing a local struct se_cmd within their
  1277. * fabric dependent per I/O descriptor.
  1278. */
  1279. void transport_init_se_cmd(
  1280. struct se_cmd *cmd,
  1281. struct target_core_fabric_ops *tfo,
  1282. struct se_session *se_sess,
  1283. u32 data_length,
  1284. int data_direction,
  1285. int task_attr,
  1286. unsigned char *sense_buffer)
  1287. {
  1288. INIT_LIST_HEAD(&cmd->se_lun_node);
  1289. INIT_LIST_HEAD(&cmd->se_delayed_node);
  1290. INIT_LIST_HEAD(&cmd->se_qf_node);
  1291. INIT_LIST_HEAD(&cmd->se_queue_node);
  1292. INIT_LIST_HEAD(&cmd->se_cmd_list);
  1293. INIT_LIST_HEAD(&cmd->t_task_list);
  1294. init_completion(&cmd->transport_lun_fe_stop_comp);
  1295. init_completion(&cmd->transport_lun_stop_comp);
  1296. init_completion(&cmd->t_transport_stop_comp);
  1297. init_completion(&cmd->cmd_wait_comp);
  1298. spin_lock_init(&cmd->t_state_lock);
  1299. cmd->transport_state = CMD_T_DEV_ACTIVE;
  1300. cmd->se_tfo = tfo;
  1301. cmd->se_sess = se_sess;
  1302. cmd->data_length = data_length;
  1303. cmd->data_direction = data_direction;
  1304. cmd->sam_task_attr = task_attr;
  1305. cmd->sense_buffer = sense_buffer;
  1306. }
  1307. EXPORT_SYMBOL(transport_init_se_cmd);
  1308. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1309. {
  1310. /*
  1311. * Check if SAM Task Attribute emulation is enabled for this
  1312. * struct se_device storage object
  1313. */
  1314. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1315. return 0;
  1316. if (cmd->sam_task_attr == MSG_ACA_TAG) {
  1317. pr_debug("SAM Task Attribute ACA"
  1318. " emulation is not supported\n");
  1319. return -EINVAL;
  1320. }
  1321. /*
  1322. * Used to determine when ORDERED commands should go from
  1323. * Dormant to Active status.
  1324. */
  1325. cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
  1326. smp_mb__after_atomic_inc();
  1327. pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1328. cmd->se_ordered_id, cmd->sam_task_attr,
  1329. cmd->se_dev->transport->name);
  1330. return 0;
  1331. }
  1332. /* transport_generic_allocate_tasks():
  1333. *
  1334. * Called from fabric RX Thread.
  1335. */
  1336. int transport_generic_allocate_tasks(
  1337. struct se_cmd *cmd,
  1338. unsigned char *cdb)
  1339. {
  1340. int ret;
  1341. transport_generic_prepare_cdb(cdb);
  1342. /*
  1343. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1344. * for VARIABLE_LENGTH_CMD
  1345. */
  1346. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1347. pr_err("Received SCSI CDB with command_size: %d that"
  1348. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1349. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1350. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1351. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1352. return -EINVAL;
  1353. }
  1354. /*
  1355. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1356. * allocate the additional extended CDB buffer now.. Otherwise
  1357. * setup the pointer from __t_task_cdb to t_task_cdb.
  1358. */
  1359. if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
  1360. cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1361. GFP_KERNEL);
  1362. if (!cmd->t_task_cdb) {
  1363. pr_err("Unable to allocate cmd->t_task_cdb"
  1364. " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
  1365. scsi_command_size(cdb),
  1366. (unsigned long)sizeof(cmd->__t_task_cdb));
  1367. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1368. cmd->scsi_sense_reason =
  1369. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1370. return -ENOMEM;
  1371. }
  1372. } else
  1373. cmd->t_task_cdb = &cmd->__t_task_cdb[0];
  1374. /*
  1375. * Copy the original CDB into cmd->
  1376. */
  1377. memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
  1378. /*
  1379. * Setup the received CDB based on SCSI defined opcodes and
  1380. * perform unit attention, persistent reservations and ALUA
  1381. * checks for virtual device backends. The cmd->t_task_cdb
  1382. * pointer is expected to be setup before we reach this point.
  1383. */
  1384. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1385. if (ret < 0)
  1386. return ret;
  1387. /*
  1388. * Check for SAM Task Attribute Emulation
  1389. */
  1390. if (transport_check_alloc_task_attr(cmd) < 0) {
  1391. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1392. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1393. return -EINVAL;
  1394. }
  1395. spin_lock(&cmd->se_lun->lun_sep_lock);
  1396. if (cmd->se_lun->lun_sep)
  1397. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1398. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1399. return 0;
  1400. }
  1401. EXPORT_SYMBOL(transport_generic_allocate_tasks);
  1402. /*
  1403. * Used by fabric module frontends to queue tasks directly.
  1404. * Many only be used from process context only
  1405. */
  1406. int transport_handle_cdb_direct(
  1407. struct se_cmd *cmd)
  1408. {
  1409. int ret;
  1410. if (!cmd->se_lun) {
  1411. dump_stack();
  1412. pr_err("cmd->se_lun is NULL\n");
  1413. return -EINVAL;
  1414. }
  1415. if (in_interrupt()) {
  1416. dump_stack();
  1417. pr_err("transport_generic_handle_cdb cannot be called"
  1418. " from interrupt context\n");
  1419. return -EINVAL;
  1420. }
  1421. /*
  1422. * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
  1423. * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
  1424. * in existing usage to ensure that outstanding descriptors are handled
  1425. * correctly during shutdown via transport_wait_for_tasks()
  1426. *
  1427. * Also, we don't take cmd->t_state_lock here as we only expect
  1428. * this to be called for initial descriptor submission.
  1429. */
  1430. cmd->t_state = TRANSPORT_NEW_CMD;
  1431. cmd->transport_state |= CMD_T_ACTIVE;
  1432. /*
  1433. * transport_generic_new_cmd() is already handling QUEUE_FULL,
  1434. * so follow TRANSPORT_NEW_CMD processing thread context usage
  1435. * and call transport_generic_request_failure() if necessary..
  1436. */
  1437. ret = transport_generic_new_cmd(cmd);
  1438. if (ret < 0)
  1439. transport_generic_request_failure(cmd);
  1440. return 0;
  1441. }
  1442. EXPORT_SYMBOL(transport_handle_cdb_direct);
  1443. /**
  1444. * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
  1445. *
  1446. * @se_cmd: command descriptor to submit
  1447. * @se_sess: associated se_sess for endpoint
  1448. * @cdb: pointer to SCSI CDB
  1449. * @sense: pointer to SCSI sense buffer
  1450. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1451. * @data_length: fabric expected data transfer length
  1452. * @task_addr: SAM task attribute
  1453. * @data_dir: DMA data direction
  1454. * @flags: flags for command submission from target_sc_flags_tables
  1455. *
  1456. * This may only be called from process context, and also currently
  1457. * assumes internal allocation of fabric payload buffer by target-core.
  1458. **/
  1459. void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
  1460. unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
  1461. u32 data_length, int task_attr, int data_dir, int flags)
  1462. {
  1463. struct se_portal_group *se_tpg;
  1464. int rc;
  1465. se_tpg = se_sess->se_tpg;
  1466. BUG_ON(!se_tpg);
  1467. BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
  1468. BUG_ON(in_interrupt());
  1469. /*
  1470. * Initialize se_cmd for target operation. From this point
  1471. * exceptions are handled by sending exception status via
  1472. * target_core_fabric_ops->queue_status() callback
  1473. */
  1474. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1475. data_length, data_dir, task_attr, sense);
  1476. /*
  1477. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1478. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1479. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1480. * kref_put() to happen during fabric packet acknowledgement.
  1481. */
  1482. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1483. /*
  1484. * Signal bidirectional data payloads to target-core
  1485. */
  1486. if (flags & TARGET_SCF_BIDI_OP)
  1487. se_cmd->se_cmd_flags |= SCF_BIDI;
  1488. /*
  1489. * Locate se_lun pointer and attach it to struct se_cmd
  1490. */
  1491. if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
  1492. transport_send_check_condition_and_sense(se_cmd,
  1493. se_cmd->scsi_sense_reason, 0);
  1494. target_put_sess_cmd(se_sess, se_cmd);
  1495. return;
  1496. }
  1497. /*
  1498. * Sanitize CDBs via transport_generic_cmd_sequencer() and
  1499. * allocate the necessary tasks to complete the received CDB+data
  1500. */
  1501. rc = transport_generic_allocate_tasks(se_cmd, cdb);
  1502. if (rc != 0) {
  1503. transport_generic_request_failure(se_cmd);
  1504. return;
  1505. }
  1506. /*
  1507. * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
  1508. * for immediate execution of READs, otherwise wait for
  1509. * transport_generic_handle_data() to be called for WRITEs
  1510. * when fabric has filled the incoming buffer.
  1511. */
  1512. transport_handle_cdb_direct(se_cmd);
  1513. return;
  1514. }
  1515. EXPORT_SYMBOL(target_submit_cmd);
  1516. static void target_complete_tmr_failure(struct work_struct *work)
  1517. {
  1518. struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
  1519. se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1520. se_cmd->se_tfo->queue_tm_rsp(se_cmd);
  1521. transport_generic_free_cmd(se_cmd, 0);
  1522. }
  1523. /**
  1524. * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
  1525. * for TMR CDBs
  1526. *
  1527. * @se_cmd: command descriptor to submit
  1528. * @se_sess: associated se_sess for endpoint
  1529. * @sense: pointer to SCSI sense buffer
  1530. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1531. * @fabric_context: fabric context for TMR req
  1532. * @tm_type: Type of TM request
  1533. * @gfp: gfp type for caller
  1534. * @tag: referenced task tag for TMR_ABORT_TASK
  1535. * @flags: submit cmd flags
  1536. *
  1537. * Callable from all contexts.
  1538. **/
  1539. int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
  1540. unsigned char *sense, u32 unpacked_lun,
  1541. void *fabric_tmr_ptr, unsigned char tm_type,
  1542. gfp_t gfp, unsigned int tag, int flags)
  1543. {
  1544. struct se_portal_group *se_tpg;
  1545. int ret;
  1546. se_tpg = se_sess->se_tpg;
  1547. BUG_ON(!se_tpg);
  1548. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1549. 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
  1550. /*
  1551. * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
  1552. * allocation failure.
  1553. */
  1554. ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
  1555. if (ret < 0)
  1556. return -ENOMEM;
  1557. if (tm_type == TMR_ABORT_TASK)
  1558. se_cmd->se_tmr_req->ref_task_tag = tag;
  1559. /* See target_submit_cmd for commentary */
  1560. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1561. ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
  1562. if (ret) {
  1563. /*
  1564. * For callback during failure handling, push this work off
  1565. * to process context with TMR_LUN_DOES_NOT_EXIST status.
  1566. */
  1567. INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
  1568. schedule_work(&se_cmd->work);
  1569. return 0;
  1570. }
  1571. transport_generic_handle_tmr(se_cmd);
  1572. return 0;
  1573. }
  1574. EXPORT_SYMBOL(target_submit_tmr);
  1575. /*
  1576. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1577. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1578. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1579. */
  1580. int transport_generic_handle_cdb_map(
  1581. struct se_cmd *cmd)
  1582. {
  1583. if (!cmd->se_lun) {
  1584. dump_stack();
  1585. pr_err("cmd->se_lun is NULL\n");
  1586. return -EINVAL;
  1587. }
  1588. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
  1589. return 0;
  1590. }
  1591. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1592. /* transport_generic_handle_data():
  1593. *
  1594. *
  1595. */
  1596. int transport_generic_handle_data(
  1597. struct se_cmd *cmd)
  1598. {
  1599. /*
  1600. * For the software fabric case, then we assume the nexus is being
  1601. * failed/shutdown when signals are pending from the kthread context
  1602. * caller, so we return a failure. For the HW target mode case running
  1603. * in interrupt code, the signal_pending() check is skipped.
  1604. */
  1605. if (!in_interrupt() && signal_pending(current))
  1606. return -EPERM;
  1607. /*
  1608. * If the received CDB has aleady been ABORTED by the generic
  1609. * target engine, we now call transport_check_aborted_status()
  1610. * to queue any delated TASK_ABORTED status for the received CDB to the
  1611. * fabric module as we are expecting no further incoming DATA OUT
  1612. * sequences at this point.
  1613. */
  1614. if (transport_check_aborted_status(cmd, 1) != 0)
  1615. return 0;
  1616. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
  1617. return 0;
  1618. }
  1619. EXPORT_SYMBOL(transport_generic_handle_data);
  1620. /* transport_generic_handle_tmr():
  1621. *
  1622. *
  1623. */
  1624. int transport_generic_handle_tmr(
  1625. struct se_cmd *cmd)
  1626. {
  1627. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
  1628. return 0;
  1629. }
  1630. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1631. /*
  1632. * If the task is active, request it to be stopped and sleep until it
  1633. * has completed.
  1634. */
  1635. bool target_stop_task(struct se_task *task, unsigned long *flags)
  1636. {
  1637. struct se_cmd *cmd = task->task_se_cmd;
  1638. bool was_active = false;
  1639. if (task->task_flags & TF_ACTIVE) {
  1640. task->task_flags |= TF_REQUEST_STOP;
  1641. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  1642. pr_debug("Task %p waiting to complete\n", task);
  1643. wait_for_completion(&task->task_stop_comp);
  1644. pr_debug("Task %p stopped successfully\n", task);
  1645. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  1646. atomic_dec(&cmd->t_task_cdbs_left);
  1647. task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
  1648. was_active = true;
  1649. }
  1650. return was_active;
  1651. }
  1652. static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
  1653. {
  1654. struct se_task *task, *task_tmp;
  1655. unsigned long flags;
  1656. int ret = 0;
  1657. pr_debug("ITT[0x%08x] - Stopping tasks\n",
  1658. cmd->se_tfo->get_task_tag(cmd));
  1659. /*
  1660. * No tasks remain in the execution queue
  1661. */
  1662. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1663. list_for_each_entry_safe(task, task_tmp,
  1664. &cmd->t_task_list, t_list) {
  1665. pr_debug("Processing task %p\n", task);
  1666. /*
  1667. * If the struct se_task has not been sent and is not active,
  1668. * remove the struct se_task from the execution queue.
  1669. */
  1670. if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
  1671. spin_unlock_irqrestore(&cmd->t_state_lock,
  1672. flags);
  1673. transport_remove_task_from_execute_queue(task,
  1674. cmd->se_dev);
  1675. pr_debug("Task %p removed from execute queue\n", task);
  1676. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1677. continue;
  1678. }
  1679. if (!target_stop_task(task, &flags)) {
  1680. pr_debug("Task %p - did nothing\n", task);
  1681. ret++;
  1682. }
  1683. }
  1684. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1685. return ret;
  1686. }
  1687. /*
  1688. * Handle SAM-esque emulation for generic transport request failures.
  1689. */
  1690. void transport_generic_request_failure(struct se_cmd *cmd)
  1691. {
  1692. int ret = 0;
  1693. pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1694. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1695. cmd->t_task_cdb[0]);
  1696. pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
  1697. cmd->se_tfo->get_cmd_state(cmd),
  1698. cmd->t_state, cmd->scsi_sense_reason);
  1699. pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
  1700. " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
  1701. " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
  1702. cmd->t_task_list_num,
  1703. atomic_read(&cmd->t_task_cdbs_left),
  1704. atomic_read(&cmd->t_task_cdbs_sent),
  1705. atomic_read(&cmd->t_task_cdbs_ex_left),
  1706. (cmd->transport_state & CMD_T_ACTIVE) != 0,
  1707. (cmd->transport_state & CMD_T_STOP) != 0,
  1708. (cmd->transport_state & CMD_T_SENT) != 0);
  1709. /*
  1710. * For SAM Task Attribute emulation for failed struct se_cmd
  1711. */
  1712. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1713. transport_complete_task_attr(cmd);
  1714. switch (cmd->scsi_sense_reason) {
  1715. case TCM_NON_EXISTENT_LUN:
  1716. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1717. case TCM_INVALID_CDB_FIELD:
  1718. case TCM_INVALID_PARAMETER_LIST:
  1719. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1720. case TCM_UNKNOWN_MODE_PAGE:
  1721. case TCM_WRITE_PROTECTED:
  1722. case TCM_CHECK_CONDITION_ABORT_CMD:
  1723. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1724. case TCM_CHECK_CONDITION_NOT_READY:
  1725. break;
  1726. case TCM_RESERVATION_CONFLICT:
  1727. /*
  1728. * No SENSE Data payload for this case, set SCSI Status
  1729. * and queue the response to $FABRIC_MOD.
  1730. *
  1731. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1732. */
  1733. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1734. /*
  1735. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1736. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1737. * CONFLICT STATUS.
  1738. *
  1739. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1740. */
  1741. if (cmd->se_sess &&
  1742. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1743. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1744. cmd->orig_fe_lun, 0x2C,
  1745. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1746. ret = cmd->se_tfo->queue_status(cmd);
  1747. if (ret == -EAGAIN || ret == -ENOMEM)
  1748. goto queue_full;
  1749. goto check_stop;
  1750. default:
  1751. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1752. cmd->t_task_cdb[0], cmd->scsi_sense_reason);
  1753. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1754. break;
  1755. }
  1756. /*
  1757. * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
  1758. * make the call to transport_send_check_condition_and_sense()
  1759. * directly. Otherwise expect the fabric to make the call to
  1760. * transport_send_check_condition_and_sense() after handling
  1761. * possible unsoliticied write data payloads.
  1762. */
  1763. ret = transport_send_check_condition_and_sense(cmd,
  1764. cmd->scsi_sense_reason, 0);
  1765. if (ret == -EAGAIN || ret == -ENOMEM)
  1766. goto queue_full;
  1767. check_stop:
  1768. transport_lun_remove_cmd(cmd);
  1769. if (!transport_cmd_check_stop_to_fabric(cmd))
  1770. ;
  1771. return;
  1772. queue_full:
  1773. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  1774. transport_handle_queue_full(cmd, cmd->se_dev);
  1775. }
  1776. EXPORT_SYMBOL(transport_generic_request_failure);
  1777. static inline u32 transport_lba_21(unsigned char *cdb)
  1778. {
  1779. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1780. }
  1781. static inline u32 transport_lba_32(unsigned char *cdb)
  1782. {
  1783. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1784. }
  1785. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1786. {
  1787. unsigned int __v1, __v2;
  1788. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1789. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1790. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1791. }
  1792. /*
  1793. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1794. */
  1795. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1796. {
  1797. unsigned int __v1, __v2;
  1798. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1799. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1800. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1801. }
  1802. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  1803. {
  1804. unsigned long flags;
  1805. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  1806. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1807. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  1808. }
  1809. /*
  1810. * Called from Fabric Module context from transport_execute_tasks()
  1811. *
  1812. * The return of this function determins if the tasks from struct se_cmd
  1813. * get added to the execution queue in transport_execute_tasks(),
  1814. * or are added to the delayed or ordered lists here.
  1815. */
  1816. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  1817. {
  1818. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1819. return 1;
  1820. /*
  1821. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1822. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1823. */
  1824. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  1825. pr_debug("Added HEAD_OF_QUEUE for CDB:"
  1826. " 0x%02x, se_ordered_id: %u\n",
  1827. cmd->t_task_cdb[0],
  1828. cmd->se_ordered_id);
  1829. return 1;
  1830. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  1831. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  1832. smp_mb__after_atomic_inc();
  1833. pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
  1834. " list, se_ordered_id: %u\n",
  1835. cmd->t_task_cdb[0],
  1836. cmd->se_ordered_id);
  1837. /*
  1838. * Add ORDERED command to tail of execution queue if
  1839. * no other older commands exist that need to be
  1840. * completed first.
  1841. */
  1842. if (!atomic_read(&cmd->se_dev->simple_cmds))
  1843. return 1;
  1844. } else {
  1845. /*
  1846. * For SIMPLE and UNTAGGED Task Attribute commands
  1847. */
  1848. atomic_inc(&cmd->se_dev->simple_cmds);
  1849. smp_mb__after_atomic_inc();
  1850. }
  1851. /*
  1852. * Otherwise if one or more outstanding ORDERED task attribute exist,
  1853. * add the dormant task(s) built for the passed struct se_cmd to the
  1854. * execution queue and become in Active state for this struct se_device.
  1855. */
  1856. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  1857. /*
  1858. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  1859. * will be drained upon completion of HEAD_OF_QUEUE task.
  1860. */
  1861. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  1862. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  1863. list_add_tail(&cmd->se_delayed_node,
  1864. &cmd->se_dev->delayed_cmd_list);
  1865. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  1866. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
  1867. " delayed CMD list, se_ordered_id: %u\n",
  1868. cmd->t_task_cdb[0], cmd->sam_task_attr,
  1869. cmd->se_ordered_id);
  1870. /*
  1871. * Return zero to let transport_execute_tasks() know
  1872. * not to add the delayed tasks to the execution list.
  1873. */
  1874. return 0;
  1875. }
  1876. /*
  1877. * Otherwise, no ORDERED task attributes exist..
  1878. */
  1879. return 1;
  1880. }
  1881. /*
  1882. * Called from fabric module context in transport_generic_new_cmd() and
  1883. * transport_generic_process_write()
  1884. */
  1885. static int transport_execute_tasks(struct se_cmd *cmd)
  1886. {
  1887. int add_tasks;
  1888. struct se_device *se_dev = cmd->se_dev;
  1889. /*
  1890. * Call transport_cmd_check_stop() to see if a fabric exception
  1891. * has occurred that prevents execution.
  1892. */
  1893. if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
  1894. /*
  1895. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  1896. * attribute for the tasks of the received struct se_cmd CDB
  1897. */
  1898. add_tasks = transport_execute_task_attr(cmd);
  1899. if (!add_tasks)
  1900. goto execute_tasks;
  1901. /*
  1902. * __transport_execute_tasks() -> __transport_add_tasks_from_cmd()
  1903. * adds associated se_tasks while holding dev->execute_task_lock
  1904. * before I/O dispath to avoid a double spinlock access.
  1905. */
  1906. __transport_execute_tasks(se_dev, cmd);
  1907. return 0;
  1908. }
  1909. execute_tasks:
  1910. __transport_execute_tasks(se_dev, NULL);
  1911. return 0;
  1912. }
  1913. /*
  1914. * Called to check struct se_device tcq depth window, and once open pull struct se_task
  1915. * from struct se_device->execute_task_list and
  1916. *
  1917. * Called from transport_processing_thread()
  1918. */
  1919. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
  1920. {
  1921. int error;
  1922. struct se_cmd *cmd = NULL;
  1923. struct se_task *task = NULL;
  1924. unsigned long flags;
  1925. check_depth:
  1926. spin_lock_irq(&dev->execute_task_lock);
  1927. if (new_cmd != NULL)
  1928. __transport_add_tasks_from_cmd(new_cmd);
  1929. if (list_empty(&dev->execute_task_list)) {
  1930. spin_unlock_irq(&dev->execute_task_lock);
  1931. return 0;
  1932. }
  1933. task = list_first_entry(&dev->execute_task_list,
  1934. struct se_task, t_execute_list);
  1935. __transport_remove_task_from_execute_queue(task, dev);
  1936. spin_unlock_irq(&dev->execute_task_lock);
  1937. cmd = task->task_se_cmd;
  1938. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1939. task->task_flags |= (TF_ACTIVE | TF_SENT);
  1940. atomic_inc(&cmd->t_task_cdbs_sent);
  1941. if (atomic_read(&cmd->t_task_cdbs_sent) ==
  1942. cmd->t_task_list_num)
  1943. cmd->transport_state |= CMD_T_SENT;
  1944. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1945. if (cmd->execute_task)
  1946. error = cmd->execute_task(task);
  1947. else
  1948. error = dev->transport->do_task(task);
  1949. if (error != 0) {
  1950. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1951. task->task_flags &= ~TF_ACTIVE;
  1952. cmd->transport_state &= ~CMD_T_SENT;
  1953. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1954. transport_stop_tasks_for_cmd(cmd);
  1955. transport_generic_request_failure(cmd);
  1956. }
  1957. new_cmd = NULL;
  1958. goto check_depth;
  1959. return 0;
  1960. }
  1961. static inline u32 transport_get_sectors_6(
  1962. unsigned char *cdb,
  1963. struct se_cmd *cmd,
  1964. int *ret)
  1965. {
  1966. struct se_device *dev = cmd->se_dev;
  1967. /*
  1968. * Assume TYPE_DISK for non struct se_device objects.
  1969. * Use 8-bit sector value.
  1970. */
  1971. if (!dev)
  1972. goto type_disk;
  1973. /*
  1974. * Use 24-bit allocation length for TYPE_TAPE.
  1975. */
  1976. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  1977. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  1978. /*
  1979. * Everything else assume TYPE_DISK Sector CDB location.
  1980. * Use 8-bit sector value. SBC-3 says:
  1981. *
  1982. * A TRANSFER LENGTH field set to zero specifies that 256
  1983. * logical blocks shall be written. Any other value
  1984. * specifies the number of logical blocks that shall be
  1985. * written.
  1986. */
  1987. type_disk:
  1988. return cdb[4] ? : 256;
  1989. }
  1990. static inline u32 transport_get_sectors_10(
  1991. unsigned char *cdb,
  1992. struct se_cmd *cmd,
  1993. int *ret)
  1994. {
  1995. struct se_device *dev = cmd->se_dev;
  1996. /*
  1997. * Assume TYPE_DISK for non struct se_device objects.
  1998. * Use 16-bit sector value.
  1999. */
  2000. if (!dev)
  2001. goto type_disk;
  2002. /*
  2003. * XXX_10 is not defined in SSC, throw an exception
  2004. */
  2005. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2006. *ret = -EINVAL;
  2007. return 0;
  2008. }
  2009. /*
  2010. * Everything else assume TYPE_DISK Sector CDB location.
  2011. * Use 16-bit sector value.
  2012. */
  2013. type_disk:
  2014. return (u32)(cdb[7] << 8) + cdb[8];
  2015. }
  2016. static inline u32 transport_get_sectors_12(
  2017. unsigned char *cdb,
  2018. struct se_cmd *cmd,
  2019. int *ret)
  2020. {
  2021. struct se_device *dev = cmd->se_dev;
  2022. /*
  2023. * Assume TYPE_DISK for non struct se_device objects.
  2024. * Use 32-bit sector value.
  2025. */
  2026. if (!dev)
  2027. goto type_disk;
  2028. /*
  2029. * XXX_12 is not defined in SSC, throw an exception
  2030. */
  2031. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2032. *ret = -EINVAL;
  2033. return 0;
  2034. }
  2035. /*
  2036. * Everything else assume TYPE_DISK Sector CDB location.
  2037. * Use 32-bit sector value.
  2038. */
  2039. type_disk:
  2040. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  2041. }
  2042. static inline u32 transport_get_sectors_16(
  2043. unsigned char *cdb,
  2044. struct se_cmd *cmd,
  2045. int *ret)
  2046. {
  2047. struct se_device *dev = cmd->se_dev;
  2048. /*
  2049. * Assume TYPE_DISK for non struct se_device objects.
  2050. * Use 32-bit sector value.
  2051. */
  2052. if (!dev)
  2053. goto type_disk;
  2054. /*
  2055. * Use 24-bit allocation length for TYPE_TAPE.
  2056. */
  2057. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2058. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2059. type_disk:
  2060. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2061. (cdb[12] << 8) + cdb[13];
  2062. }
  2063. /*
  2064. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2065. */
  2066. static inline u32 transport_get_sectors_32(
  2067. unsigned char *cdb,
  2068. struct se_cmd *cmd,
  2069. int *ret)
  2070. {
  2071. /*
  2072. * Assume TYPE_DISK for non struct se_device objects.
  2073. * Use 32-bit sector value.
  2074. */
  2075. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2076. (cdb[30] << 8) + cdb[31];
  2077. }
  2078. static inline u32 transport_get_size(
  2079. u32 sectors,
  2080. unsigned char *cdb,
  2081. struct se_cmd *cmd)
  2082. {
  2083. struct se_device *dev = cmd->se_dev;
  2084. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2085. if (cdb[1] & 1) { /* sectors */
  2086. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2087. } else /* bytes */
  2088. return sectors;
  2089. }
  2090. #if 0
  2091. pr_debug("Returning block_size: %u, sectors: %u == %u for"
  2092. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
  2093. dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  2094. dev->transport->name);
  2095. #endif
  2096. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2097. }
  2098. static void transport_xor_callback(struct se_cmd *cmd)
  2099. {
  2100. unsigned char *buf, *addr;
  2101. struct scatterlist *sg;
  2102. unsigned int offset;
  2103. int i;
  2104. int count;
  2105. /*
  2106. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2107. *
  2108. * 1) read the specified logical block(s);
  2109. * 2) transfer logical blocks from the data-out buffer;
  2110. * 3) XOR the logical blocks transferred from the data-out buffer with
  2111. * the logical blocks read, storing the resulting XOR data in a buffer;
  2112. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2113. * blocks transferred from the data-out buffer; and
  2114. * 5) transfer the resulting XOR data to the data-in buffer.
  2115. */
  2116. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2117. if (!buf) {
  2118. pr_err("Unable to allocate xor_callback buf\n");
  2119. return;
  2120. }
  2121. /*
  2122. * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
  2123. * into the locally allocated *buf
  2124. */
  2125. sg_copy_to_buffer(cmd->t_data_sg,
  2126. cmd->t_data_nents,
  2127. buf,
  2128. cmd->data_length);
  2129. /*
  2130. * Now perform the XOR against the BIDI read memory located at
  2131. * cmd->t_mem_bidi_list
  2132. */
  2133. offset = 0;
  2134. for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
  2135. addr = kmap_atomic(sg_page(sg), KM_USER0);
  2136. if (!addr)
  2137. goto out;
  2138. for (i = 0; i < sg->length; i++)
  2139. *(addr + sg->offset + i) ^= *(buf + offset + i);
  2140. offset += sg->length;
  2141. kunmap_atomic(addr, KM_USER0);
  2142. }
  2143. out:
  2144. kfree(buf);
  2145. }
  2146. /*
  2147. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2148. */
  2149. static int transport_get_sense_data(struct se_cmd *cmd)
  2150. {
  2151. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2152. struct se_device *dev = cmd->se_dev;
  2153. struct se_task *task = NULL, *task_tmp;
  2154. unsigned long flags;
  2155. u32 offset = 0;
  2156. WARN_ON(!cmd->se_lun);
  2157. if (!dev)
  2158. return 0;
  2159. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2160. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2161. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2162. return 0;
  2163. }
  2164. list_for_each_entry_safe(task, task_tmp,
  2165. &cmd->t_task_list, t_list) {
  2166. if (!(task->task_flags & TF_HAS_SENSE))
  2167. continue;
  2168. if (!dev->transport->get_sense_buffer) {
  2169. pr_err("dev->transport->get_sense_buffer"
  2170. " is NULL\n");
  2171. continue;
  2172. }
  2173. sense_buffer = dev->transport->get_sense_buffer(task);
  2174. if (!sense_buffer) {
  2175. pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
  2176. " sense buffer for task with sense\n",
  2177. cmd->se_tfo->get_task_tag(cmd), task);
  2178. continue;
  2179. }
  2180. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2181. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  2182. TRANSPORT_SENSE_BUFFER);
  2183. memcpy(&buffer[offset], sense_buffer,
  2184. TRANSPORT_SENSE_BUFFER);
  2185. cmd->scsi_status = task->task_scsi_status;
  2186. /* Automatically padded */
  2187. cmd->scsi_sense_length =
  2188. (TRANSPORT_SENSE_BUFFER + offset);
  2189. pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
  2190. " and sense\n",
  2191. dev->se_hba->hba_id, dev->transport->name,
  2192. cmd->scsi_status);
  2193. return 0;
  2194. }
  2195. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2196. return -1;
  2197. }
  2198. static inline long long transport_dev_end_lba(struct se_device *dev)
  2199. {
  2200. return dev->transport->get_blocks(dev) + 1;
  2201. }
  2202. static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
  2203. {
  2204. struct se_device *dev = cmd->se_dev;
  2205. u32 sectors;
  2206. if (dev->transport->get_device_type(dev) != TYPE_DISK)
  2207. return 0;
  2208. sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
  2209. if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
  2210. pr_err("LBA: %llu Sectors: %u exceeds"
  2211. " transport_dev_end_lba(): %llu\n",
  2212. cmd->t_task_lba, sectors,
  2213. transport_dev_end_lba(dev));
  2214. return -EINVAL;
  2215. }
  2216. return 0;
  2217. }
  2218. static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
  2219. {
  2220. /*
  2221. * Determine if the received WRITE_SAME is used to for direct
  2222. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  2223. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  2224. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
  2225. */
  2226. int passthrough = (dev->transport->transport_type ==
  2227. TRANSPORT_PLUGIN_PHBA_PDEV);
  2228. if (!passthrough) {
  2229. if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
  2230. pr_err("WRITE_SAME PBDATA and LBDATA"
  2231. " bits not supported for Block Discard"
  2232. " Emulation\n");
  2233. return -ENOSYS;
  2234. }
  2235. /*
  2236. * Currently for the emulated case we only accept
  2237. * tpws with the UNMAP=1 bit set.
  2238. */
  2239. if (!(flags[0] & 0x08)) {
  2240. pr_err("WRITE_SAME w/o UNMAP bit not"
  2241. " supported for Block Discard Emulation\n");
  2242. return -ENOSYS;
  2243. }
  2244. }
  2245. return 0;
  2246. }
  2247. /* transport_generic_cmd_sequencer():
  2248. *
  2249. * Generic Command Sequencer that should work for most DAS transport
  2250. * drivers.
  2251. *
  2252. * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
  2253. * RX Thread.
  2254. *
  2255. * FIXME: Need to support other SCSI OPCODES where as well.
  2256. */
  2257. static int transport_generic_cmd_sequencer(
  2258. struct se_cmd *cmd,
  2259. unsigned char *cdb)
  2260. {
  2261. struct se_device *dev = cmd->se_dev;
  2262. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2263. int ret = 0, sector_ret = 0, passthrough;
  2264. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2265. u16 service_action;
  2266. u8 alua_ascq = 0;
  2267. /*
  2268. * Check for an existing UNIT ATTENTION condition
  2269. */
  2270. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2271. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2272. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2273. return -EINVAL;
  2274. }
  2275. /*
  2276. * Check status of Asymmetric Logical Unit Assignment port
  2277. */
  2278. ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
  2279. if (ret != 0) {
  2280. /*
  2281. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  2282. * The ALUA additional sense code qualifier (ASCQ) is determined
  2283. * by the ALUA primary or secondary access state..
  2284. */
  2285. if (ret > 0) {
  2286. #if 0
  2287. pr_debug("[%s]: ALUA TG Port not available,"
  2288. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2289. cmd->se_tfo->get_fabric_name(), alua_ascq);
  2290. #endif
  2291. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2292. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2293. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2294. return -EINVAL;
  2295. }
  2296. goto out_invalid_cdb_field;
  2297. }
  2298. /*
  2299. * Check status for SPC-3 Persistent Reservations
  2300. */
  2301. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2302. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  2303. cmd, cdb, pr_reg_type) != 0) {
  2304. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2305. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2306. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2307. return -EBUSY;
  2308. }
  2309. /*
  2310. * This means the CDB is allowed for the SCSI Initiator port
  2311. * when said port is *NOT* holding the legacy SPC-2 or
  2312. * SPC-3 Persistent Reservation.
  2313. */
  2314. }
  2315. /*
  2316. * If we operate in passthrough mode we skip most CDB emulation and
  2317. * instead hand the commands down to the physical SCSI device.
  2318. */
  2319. passthrough =
  2320. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
  2321. switch (cdb[0]) {
  2322. case READ_6:
  2323. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2324. if (sector_ret)
  2325. goto out_unsupported_cdb;
  2326. size = transport_get_size(sectors, cdb, cmd);
  2327. cmd->t_task_lba = transport_lba_21(cdb);
  2328. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2329. break;
  2330. case READ_10:
  2331. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2332. if (sector_ret)
  2333. goto out_unsupported_cdb;
  2334. size = transport_get_size(sectors, cdb, cmd);
  2335. cmd->t_task_lba = transport_lba_32(cdb);
  2336. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2337. break;
  2338. case READ_12:
  2339. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2340. if (sector_ret)
  2341. goto out_unsupported_cdb;
  2342. size = transport_get_size(sectors, cdb, cmd);
  2343. cmd->t_task_lba = transport_lba_32(cdb);
  2344. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2345. break;
  2346. case READ_16:
  2347. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2348. if (sector_ret)
  2349. goto out_unsupported_cdb;
  2350. size = transport_get_size(sectors, cdb, cmd);
  2351. cmd->t_task_lba = transport_lba_64(cdb);
  2352. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2353. break;
  2354. case WRITE_6:
  2355. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2356. if (sector_ret)
  2357. goto out_unsupported_cdb;
  2358. size = transport_get_size(sectors, cdb, cmd);
  2359. cmd->t_task_lba = transport_lba_21(cdb);
  2360. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2361. break;
  2362. case WRITE_10:
  2363. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2364. if (sector_ret)
  2365. goto out_unsupported_cdb;
  2366. size = transport_get_size(sectors, cdb, cmd);
  2367. cmd->t_task_lba = transport_lba_32(cdb);
  2368. if (cdb[1] & 0x8)
  2369. cmd->se_cmd_flags |= SCF_FUA;
  2370. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2371. break;
  2372. case WRITE_12:
  2373. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2374. if (sector_ret)
  2375. goto out_unsupported_cdb;
  2376. size = transport_get_size(sectors, cdb, cmd);
  2377. cmd->t_task_lba = transport_lba_32(cdb);
  2378. if (cdb[1] & 0x8)
  2379. cmd->se_cmd_flags |= SCF_FUA;
  2380. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2381. break;
  2382. case WRITE_16:
  2383. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2384. if (sector_ret)
  2385. goto out_unsupported_cdb;
  2386. size = transport_get_size(sectors, cdb, cmd);
  2387. cmd->t_task_lba = transport_lba_64(cdb);
  2388. if (cdb[1] & 0x8)
  2389. cmd->se_cmd_flags |= SCF_FUA;
  2390. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2391. break;
  2392. case XDWRITEREAD_10:
  2393. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2394. !(cmd->se_cmd_flags & SCF_BIDI))
  2395. goto out_invalid_cdb_field;
  2396. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2397. if (sector_ret)
  2398. goto out_unsupported_cdb;
  2399. size = transport_get_size(sectors, cdb, cmd);
  2400. cmd->t_task_lba = transport_lba_32(cdb);
  2401. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2402. /*
  2403. * Do now allow BIDI commands for passthrough mode.
  2404. */
  2405. if (passthrough)
  2406. goto out_unsupported_cdb;
  2407. /*
  2408. * Setup BIDI XOR callback to be run after I/O completion.
  2409. */
  2410. cmd->transport_complete_callback = &transport_xor_callback;
  2411. if (cdb[1] & 0x8)
  2412. cmd->se_cmd_flags |= SCF_FUA;
  2413. break;
  2414. case VARIABLE_LENGTH_CMD:
  2415. service_action = get_unaligned_be16(&cdb[8]);
  2416. switch (service_action) {
  2417. case XDWRITEREAD_32:
  2418. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2419. if (sector_ret)
  2420. goto out_unsupported_cdb;
  2421. size = transport_get_size(sectors, cdb, cmd);
  2422. /*
  2423. * Use WRITE_32 and READ_32 opcodes for the emulated
  2424. * XDWRITE_READ_32 logic.
  2425. */
  2426. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2427. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2428. /*
  2429. * Do now allow BIDI commands for passthrough mode.
  2430. */
  2431. if (passthrough)
  2432. goto out_unsupported_cdb;
  2433. /*
  2434. * Setup BIDI XOR callback to be run during after I/O
  2435. * completion.
  2436. */
  2437. cmd->transport_complete_callback = &transport_xor_callback;
  2438. if (cdb[1] & 0x8)
  2439. cmd->se_cmd_flags |= SCF_FUA;
  2440. break;
  2441. case WRITE_SAME_32:
  2442. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2443. if (sector_ret)
  2444. goto out_unsupported_cdb;
  2445. if (sectors)
  2446. size = transport_get_size(1, cdb, cmd);
  2447. else {
  2448. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  2449. " supported\n");
  2450. goto out_invalid_cdb_field;
  2451. }
  2452. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2453. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2454. if (target_check_write_same_discard(&cdb[10], dev) < 0)
  2455. goto out_unsupported_cdb;
  2456. if (!passthrough)
  2457. cmd->execute_task = target_emulate_write_same;
  2458. break;
  2459. default:
  2460. pr_err("VARIABLE_LENGTH_CMD service action"
  2461. " 0x%04x not supported\n", service_action);
  2462. goto out_unsupported_cdb;
  2463. }
  2464. break;
  2465. case MAINTENANCE_IN:
  2466. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2467. /* MAINTENANCE_IN from SCC-2 */
  2468. /*
  2469. * Check for emulated MI_REPORT_TARGET_PGS.
  2470. */
  2471. if (cdb[1] == MI_REPORT_TARGET_PGS &&
  2472. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2473. cmd->execute_task =
  2474. target_emulate_report_target_port_groups;
  2475. }
  2476. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2477. (cdb[8] << 8) | cdb[9];
  2478. } else {
  2479. /* GPCMD_SEND_KEY from multi media commands */
  2480. size = (cdb[8] << 8) + cdb[9];
  2481. }
  2482. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2483. break;
  2484. case MODE_SELECT:
  2485. size = cdb[4];
  2486. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2487. break;
  2488. case MODE_SELECT_10:
  2489. size = (cdb[7] << 8) + cdb[8];
  2490. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2491. break;
  2492. case MODE_SENSE:
  2493. size = cdb[4];
  2494. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2495. if (!passthrough)
  2496. cmd->execute_task = target_emulate_modesense;
  2497. break;
  2498. case MODE_SENSE_10:
  2499. size = (cdb[7] << 8) + cdb[8];
  2500. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2501. if (!passthrough)
  2502. cmd->execute_task = target_emulate_modesense;
  2503. break;
  2504. case GPCMD_READ_BUFFER_CAPACITY:
  2505. case GPCMD_SEND_OPC:
  2506. case LOG_SELECT:
  2507. case LOG_SENSE:
  2508. size = (cdb[7] << 8) + cdb[8];
  2509. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2510. break;
  2511. case READ_BLOCK_LIMITS:
  2512. size = READ_BLOCK_LEN;
  2513. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2514. break;
  2515. case GPCMD_GET_CONFIGURATION:
  2516. case GPCMD_READ_FORMAT_CAPACITIES:
  2517. case GPCMD_READ_DISC_INFO:
  2518. case GPCMD_READ_TRACK_RZONE_INFO:
  2519. size = (cdb[7] << 8) + cdb[8];
  2520. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2521. break;
  2522. case PERSISTENT_RESERVE_IN:
  2523. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2524. cmd->execute_task = target_scsi3_emulate_pr_in;
  2525. size = (cdb[7] << 8) + cdb[8];
  2526. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2527. break;
  2528. case PERSISTENT_RESERVE_OUT:
  2529. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2530. cmd->execute_task = target_scsi3_emulate_pr_out;
  2531. size = (cdb[7] << 8) + cdb[8];
  2532. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2533. break;
  2534. case GPCMD_MECHANISM_STATUS:
  2535. case GPCMD_READ_DVD_STRUCTURE:
  2536. size = (cdb[8] << 8) + cdb[9];
  2537. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2538. break;
  2539. case READ_POSITION:
  2540. size = READ_POSITION_LEN;
  2541. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2542. break;
  2543. case MAINTENANCE_OUT:
  2544. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2545. /* MAINTENANCE_OUT from SCC-2
  2546. *
  2547. * Check for emulated MO_SET_TARGET_PGS.
  2548. */
  2549. if (cdb[1] == MO_SET_TARGET_PGS &&
  2550. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2551. cmd->execute_task =
  2552. target_emulate_set_target_port_groups;
  2553. }
  2554. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2555. (cdb[8] << 8) | cdb[9];
  2556. } else {
  2557. /* GPCMD_REPORT_KEY from multi media commands */
  2558. size = (cdb[8] << 8) + cdb[9];
  2559. }
  2560. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2561. break;
  2562. case INQUIRY:
  2563. size = (cdb[3] << 8) + cdb[4];
  2564. /*
  2565. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2566. * See spc4r17 section 5.3
  2567. */
  2568. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2569. cmd->sam_task_attr = MSG_HEAD_TAG;
  2570. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2571. if (!passthrough)
  2572. cmd->execute_task = target_emulate_inquiry;
  2573. break;
  2574. case READ_BUFFER:
  2575. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2576. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2577. break;
  2578. case READ_CAPACITY:
  2579. size = READ_CAP_LEN;
  2580. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2581. if (!passthrough)
  2582. cmd->execute_task = target_emulate_readcapacity;
  2583. break;
  2584. case READ_MEDIA_SERIAL_NUMBER:
  2585. case SECURITY_PROTOCOL_IN:
  2586. case SECURITY_PROTOCOL_OUT:
  2587. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2588. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2589. break;
  2590. case SERVICE_ACTION_IN:
  2591. switch (cmd->t_task_cdb[1] & 0x1f) {
  2592. case SAI_READ_CAPACITY_16:
  2593. if (!passthrough)
  2594. cmd->execute_task =
  2595. target_emulate_readcapacity_16;
  2596. break;
  2597. default:
  2598. if (passthrough)
  2599. break;
  2600. pr_err("Unsupported SA: 0x%02x\n",
  2601. cmd->t_task_cdb[1] & 0x1f);
  2602. goto out_unsupported_cdb;
  2603. }
  2604. /*FALLTHROUGH*/
  2605. case ACCESS_CONTROL_IN:
  2606. case ACCESS_CONTROL_OUT:
  2607. case EXTENDED_COPY:
  2608. case READ_ATTRIBUTE:
  2609. case RECEIVE_COPY_RESULTS:
  2610. case WRITE_ATTRIBUTE:
  2611. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2612. (cdb[12] << 8) | cdb[13];
  2613. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2614. break;
  2615. case RECEIVE_DIAGNOSTIC:
  2616. case SEND_DIAGNOSTIC:
  2617. size = (cdb[3] << 8) | cdb[4];
  2618. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2619. break;
  2620. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2621. #if 0
  2622. case GPCMD_READ_CD:
  2623. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2624. size = (2336 * sectors);
  2625. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2626. break;
  2627. #endif
  2628. case READ_TOC:
  2629. size = cdb[8];
  2630. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2631. break;
  2632. case REQUEST_SENSE:
  2633. size = cdb[4];
  2634. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2635. if (!passthrough)
  2636. cmd->execute_task = target_emulate_request_sense;
  2637. break;
  2638. case READ_ELEMENT_STATUS:
  2639. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2640. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2641. break;
  2642. case WRITE_BUFFER:
  2643. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2644. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2645. break;
  2646. case RESERVE:
  2647. case RESERVE_10:
  2648. /*
  2649. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2650. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2651. */
  2652. if (cdb[0] == RESERVE_10)
  2653. size = (cdb[7] << 8) | cdb[8];
  2654. else
  2655. size = cmd->data_length;
  2656. /*
  2657. * Setup the legacy emulated handler for SPC-2 and
  2658. * >= SPC-3 compatible reservation handling (CRH=1)
  2659. * Otherwise, we assume the underlying SCSI logic is
  2660. * is running in SPC_PASSTHROUGH, and wants reservations
  2661. * emulation disabled.
  2662. */
  2663. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2664. cmd->execute_task = target_scsi2_reservation_reserve;
  2665. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2666. break;
  2667. case RELEASE:
  2668. case RELEASE_10:
  2669. /*
  2670. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  2671. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2672. */
  2673. if (cdb[0] == RELEASE_10)
  2674. size = (cdb[7] << 8) | cdb[8];
  2675. else
  2676. size = cmd->data_length;
  2677. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2678. cmd->execute_task = target_scsi2_reservation_release;
  2679. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2680. break;
  2681. case SYNCHRONIZE_CACHE:
  2682. case SYNCHRONIZE_CACHE_16:
  2683. /*
  2684. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  2685. */
  2686. if (cdb[0] == SYNCHRONIZE_CACHE) {
  2687. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2688. cmd->t_task_lba = transport_lba_32(cdb);
  2689. } else {
  2690. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2691. cmd->t_task_lba = transport_lba_64(cdb);
  2692. }
  2693. if (sector_ret)
  2694. goto out_unsupported_cdb;
  2695. size = transport_get_size(sectors, cdb, cmd);
  2696. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2697. if (passthrough)
  2698. break;
  2699. /*
  2700. * Check to ensure that LBA + Range does not exceed past end of
  2701. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  2702. */
  2703. if ((cmd->t_task_lba != 0) || (sectors != 0)) {
  2704. if (transport_cmd_get_valid_sectors(cmd) < 0)
  2705. goto out_invalid_cdb_field;
  2706. }
  2707. cmd->execute_task = target_emulate_synchronize_cache;
  2708. break;
  2709. case UNMAP:
  2710. size = get_unaligned_be16(&cdb[7]);
  2711. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2712. if (!passthrough)
  2713. cmd->execute_task = target_emulate_unmap;
  2714. break;
  2715. case WRITE_SAME_16:
  2716. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2717. if (sector_ret)
  2718. goto out_unsupported_cdb;
  2719. if (sectors)
  2720. size = transport_get_size(1, cdb, cmd);
  2721. else {
  2722. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2723. goto out_invalid_cdb_field;
  2724. }
  2725. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  2726. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2727. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2728. goto out_unsupported_cdb;
  2729. if (!passthrough)
  2730. cmd->execute_task = target_emulate_write_same;
  2731. break;
  2732. case WRITE_SAME:
  2733. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2734. if (sector_ret)
  2735. goto out_unsupported_cdb;
  2736. if (sectors)
  2737. size = transport_get_size(1, cdb, cmd);
  2738. else {
  2739. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2740. goto out_invalid_cdb_field;
  2741. }
  2742. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  2743. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2744. /*
  2745. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  2746. * of byte 1 bit 3 UNMAP instead of original reserved field
  2747. */
  2748. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2749. goto out_unsupported_cdb;
  2750. if (!passthrough)
  2751. cmd->execute_task = target_emulate_write_same;
  2752. break;
  2753. case ALLOW_MEDIUM_REMOVAL:
  2754. case ERASE:
  2755. case REZERO_UNIT:
  2756. case SEEK_10:
  2757. case SPACE:
  2758. case START_STOP:
  2759. case TEST_UNIT_READY:
  2760. case VERIFY:
  2761. case WRITE_FILEMARKS:
  2762. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2763. if (!passthrough)
  2764. cmd->execute_task = target_emulate_noop;
  2765. break;
  2766. case GPCMD_CLOSE_TRACK:
  2767. case INITIALIZE_ELEMENT_STATUS:
  2768. case GPCMD_LOAD_UNLOAD:
  2769. case GPCMD_SET_SPEED:
  2770. case MOVE_MEDIUM:
  2771. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2772. break;
  2773. case REPORT_LUNS:
  2774. cmd->execute_task = target_report_luns;
  2775. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2776. /*
  2777. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  2778. * See spc4r17 section 5.3
  2779. */
  2780. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2781. cmd->sam_task_attr = MSG_HEAD_TAG;
  2782. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2783. break;
  2784. default:
  2785. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  2786. " 0x%02x, sending CHECK_CONDITION.\n",
  2787. cmd->se_tfo->get_fabric_name(), cdb[0]);
  2788. goto out_unsupported_cdb;
  2789. }
  2790. if (size != cmd->data_length) {
  2791. pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
  2792. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  2793. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  2794. cmd->data_length, size, cdb[0]);
  2795. cmd->cmd_spdtl = size;
  2796. if (cmd->data_direction == DMA_TO_DEVICE) {
  2797. pr_err("Rejecting underflow/overflow"
  2798. " WRITE data\n");
  2799. goto out_invalid_cdb_field;
  2800. }
  2801. /*
  2802. * Reject READ_* or WRITE_* with overflow/underflow for
  2803. * type SCF_SCSI_DATA_SG_IO_CDB.
  2804. */
  2805. if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
  2806. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  2807. " CDB on non 512-byte sector setup subsystem"
  2808. " plugin: %s\n", dev->transport->name);
  2809. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  2810. goto out_invalid_cdb_field;
  2811. }
  2812. if (size > cmd->data_length) {
  2813. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  2814. cmd->residual_count = (size - cmd->data_length);
  2815. } else {
  2816. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  2817. cmd->residual_count = (cmd->data_length - size);
  2818. }
  2819. cmd->data_length = size;
  2820. }
  2821. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB &&
  2822. sectors > dev->se_sub_dev->se_dev_attrib.fabric_max_sectors) {
  2823. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too big sectors %u\n",
  2824. cdb[0], sectors);
  2825. goto out_invalid_cdb_field;
  2826. }
  2827. /* reject any command that we don't have a handler for */
  2828. if (!(passthrough || cmd->execute_task ||
  2829. (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2830. goto out_unsupported_cdb;
  2831. transport_set_supported_SAM_opcode(cmd);
  2832. return ret;
  2833. out_unsupported_cdb:
  2834. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2835. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  2836. return -EINVAL;
  2837. out_invalid_cdb_field:
  2838. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2839. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2840. return -EINVAL;
  2841. }
  2842. /*
  2843. * Called from I/O completion to determine which dormant/delayed
  2844. * and ordered cmds need to have their tasks added to the execution queue.
  2845. */
  2846. static void transport_complete_task_attr(struct se_cmd *cmd)
  2847. {
  2848. struct se_device *dev = cmd->se_dev;
  2849. struct se_cmd *cmd_p, *cmd_tmp;
  2850. int new_active_tasks = 0;
  2851. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  2852. atomic_dec(&dev->simple_cmds);
  2853. smp_mb__after_atomic_dec();
  2854. dev->dev_cur_ordered_id++;
  2855. pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
  2856. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  2857. cmd->se_ordered_id);
  2858. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2859. dev->dev_cur_ordered_id++;
  2860. pr_debug("Incremented dev_cur_ordered_id: %u for"
  2861. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  2862. cmd->se_ordered_id);
  2863. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2864. atomic_dec(&dev->dev_ordered_sync);
  2865. smp_mb__after_atomic_dec();
  2866. dev->dev_cur_ordered_id++;
  2867. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
  2868. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  2869. }
  2870. /*
  2871. * Process all commands up to the last received
  2872. * ORDERED task attribute which requires another blocking
  2873. * boundary
  2874. */
  2875. spin_lock(&dev->delayed_cmd_lock);
  2876. list_for_each_entry_safe(cmd_p, cmd_tmp,
  2877. &dev->delayed_cmd_list, se_delayed_node) {
  2878. list_del(&cmd_p->se_delayed_node);
  2879. spin_unlock(&dev->delayed_cmd_lock);
  2880. pr_debug("Calling add_tasks() for"
  2881. " cmd_p: 0x%02x Task Attr: 0x%02x"
  2882. " Dormant -> Active, se_ordered_id: %u\n",
  2883. cmd_p->t_task_cdb[0],
  2884. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  2885. transport_add_tasks_from_cmd(cmd_p);
  2886. new_active_tasks++;
  2887. spin_lock(&dev->delayed_cmd_lock);
  2888. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  2889. break;
  2890. }
  2891. spin_unlock(&dev->delayed_cmd_lock);
  2892. /*
  2893. * If new tasks have become active, wake up the transport thread
  2894. * to do the processing of the Active tasks.
  2895. */
  2896. if (new_active_tasks != 0)
  2897. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2898. }
  2899. static void transport_complete_qf(struct se_cmd *cmd)
  2900. {
  2901. int ret = 0;
  2902. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2903. transport_complete_task_attr(cmd);
  2904. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2905. ret = cmd->se_tfo->queue_status(cmd);
  2906. if (ret)
  2907. goto out;
  2908. }
  2909. switch (cmd->data_direction) {
  2910. case DMA_FROM_DEVICE:
  2911. ret = cmd->se_tfo->queue_data_in(cmd);
  2912. break;
  2913. case DMA_TO_DEVICE:
  2914. if (cmd->t_bidi_data_sg) {
  2915. ret = cmd->se_tfo->queue_data_in(cmd);
  2916. if (ret < 0)
  2917. break;
  2918. }
  2919. /* Fall through for DMA_TO_DEVICE */
  2920. case DMA_NONE:
  2921. ret = cmd->se_tfo->queue_status(cmd);
  2922. break;
  2923. default:
  2924. break;
  2925. }
  2926. out:
  2927. if (ret < 0) {
  2928. transport_handle_queue_full(cmd, cmd->se_dev);
  2929. return;
  2930. }
  2931. transport_lun_remove_cmd(cmd);
  2932. transport_cmd_check_stop_to_fabric(cmd);
  2933. }
  2934. static void transport_handle_queue_full(
  2935. struct se_cmd *cmd,
  2936. struct se_device *dev)
  2937. {
  2938. spin_lock_irq(&dev->qf_cmd_lock);
  2939. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  2940. atomic_inc(&dev->dev_qf_count);
  2941. smp_mb__after_atomic_inc();
  2942. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  2943. schedule_work(&cmd->se_dev->qf_work_queue);
  2944. }
  2945. static void target_complete_ok_work(struct work_struct *work)
  2946. {
  2947. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2948. int reason = 0, ret;
  2949. /*
  2950. * Check if we need to move delayed/dormant tasks from cmds on the
  2951. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  2952. * Attribute.
  2953. */
  2954. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2955. transport_complete_task_attr(cmd);
  2956. /*
  2957. * Check to schedule QUEUE_FULL work, or execute an existing
  2958. * cmd->transport_qf_callback()
  2959. */
  2960. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  2961. schedule_work(&cmd->se_dev->qf_work_queue);
  2962. /*
  2963. * Check if we need to retrieve a sense buffer from
  2964. * the struct se_cmd in question.
  2965. */
  2966. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2967. if (transport_get_sense_data(cmd) < 0)
  2968. reason = TCM_NON_EXISTENT_LUN;
  2969. /*
  2970. * Only set when an struct se_task->task_scsi_status returned
  2971. * a non GOOD status.
  2972. */
  2973. if (cmd->scsi_status) {
  2974. ret = transport_send_check_condition_and_sense(
  2975. cmd, reason, 1);
  2976. if (ret == -EAGAIN || ret == -ENOMEM)
  2977. goto queue_full;
  2978. transport_lun_remove_cmd(cmd);
  2979. transport_cmd_check_stop_to_fabric(cmd);
  2980. return;
  2981. }
  2982. }
  2983. /*
  2984. * Check for a callback, used by amongst other things
  2985. * XDWRITE_READ_10 emulation.
  2986. */
  2987. if (cmd->transport_complete_callback)
  2988. cmd->transport_complete_callback(cmd);
  2989. switch (cmd->data_direction) {
  2990. case DMA_FROM_DEVICE:
  2991. spin_lock(&cmd->se_lun->lun_sep_lock);
  2992. if (cmd->se_lun->lun_sep) {
  2993. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2994. cmd->data_length;
  2995. }
  2996. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2997. ret = cmd->se_tfo->queue_data_in(cmd);
  2998. if (ret == -EAGAIN || ret == -ENOMEM)
  2999. goto queue_full;
  3000. break;
  3001. case DMA_TO_DEVICE:
  3002. spin_lock(&cmd->se_lun->lun_sep_lock);
  3003. if (cmd->se_lun->lun_sep) {
  3004. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  3005. cmd->data_length;
  3006. }
  3007. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3008. /*
  3009. * Check if we need to send READ payload for BIDI-COMMAND
  3010. */
  3011. if (cmd->t_bidi_data_sg) {
  3012. spin_lock(&cmd->se_lun->lun_sep_lock);
  3013. if (cmd->se_lun->lun_sep) {
  3014. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3015. cmd->data_length;
  3016. }
  3017. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3018. ret = cmd->se_tfo->queue_data_in(cmd);
  3019. if (ret == -EAGAIN || ret == -ENOMEM)
  3020. goto queue_full;
  3021. break;
  3022. }
  3023. /* Fall through for DMA_TO_DEVICE */
  3024. case DMA_NONE:
  3025. ret = cmd->se_tfo->queue_status(cmd);
  3026. if (ret == -EAGAIN || ret == -ENOMEM)
  3027. goto queue_full;
  3028. break;
  3029. default:
  3030. break;
  3031. }
  3032. transport_lun_remove_cmd(cmd);
  3033. transport_cmd_check_stop_to_fabric(cmd);
  3034. return;
  3035. queue_full:
  3036. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  3037. " data_direction: %d\n", cmd, cmd->data_direction);
  3038. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  3039. transport_handle_queue_full(cmd, cmd->se_dev);
  3040. }
  3041. static void transport_free_dev_tasks(struct se_cmd *cmd)
  3042. {
  3043. struct se_task *task, *task_tmp;
  3044. unsigned long flags;
  3045. LIST_HEAD(dispose_list);
  3046. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3047. list_for_each_entry_safe(task, task_tmp,
  3048. &cmd->t_task_list, t_list) {
  3049. if (!(task->task_flags & TF_ACTIVE))
  3050. list_move_tail(&task->t_list, &dispose_list);
  3051. }
  3052. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3053. while (!list_empty(&dispose_list)) {
  3054. task = list_first_entry(&dispose_list, struct se_task, t_list);
  3055. if (task->task_sg != cmd->t_data_sg &&
  3056. task->task_sg != cmd->t_bidi_data_sg)
  3057. kfree(task->task_sg);
  3058. list_del(&task->t_list);
  3059. cmd->se_dev->transport->free_task(task);
  3060. }
  3061. }
  3062. static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
  3063. {
  3064. struct scatterlist *sg;
  3065. int count;
  3066. for_each_sg(sgl, sg, nents, count)
  3067. __free_page(sg_page(sg));
  3068. kfree(sgl);
  3069. }
  3070. static inline void transport_free_pages(struct se_cmd *cmd)
  3071. {
  3072. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3073. return;
  3074. transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  3075. cmd->t_data_sg = NULL;
  3076. cmd->t_data_nents = 0;
  3077. transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  3078. cmd->t_bidi_data_sg = NULL;
  3079. cmd->t_bidi_data_nents = 0;
  3080. }
  3081. /**
  3082. * transport_release_cmd - free a command
  3083. * @cmd: command to free
  3084. *
  3085. * This routine unconditionally frees a command, and reference counting
  3086. * or list removal must be done in the caller.
  3087. */
  3088. static void transport_release_cmd(struct se_cmd *cmd)
  3089. {
  3090. BUG_ON(!cmd->se_tfo);
  3091. if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  3092. core_tmr_release_req(cmd->se_tmr_req);
  3093. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  3094. kfree(cmd->t_task_cdb);
  3095. /*
  3096. * If this cmd has been setup with target_get_sess_cmd(), drop
  3097. * the kref and call ->release_cmd() in kref callback.
  3098. */
  3099. if (cmd->check_release != 0) {
  3100. target_put_sess_cmd(cmd->se_sess, cmd);
  3101. return;
  3102. }
  3103. cmd->se_tfo->release_cmd(cmd);
  3104. }
  3105. /**
  3106. * transport_put_cmd - release a reference to a command
  3107. * @cmd: command to release
  3108. *
  3109. * This routine releases our reference to the command and frees it if possible.
  3110. */
  3111. static void transport_put_cmd(struct se_cmd *cmd)
  3112. {
  3113. unsigned long flags;
  3114. int free_tasks = 0;
  3115. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3116. if (atomic_read(&cmd->t_fe_count)) {
  3117. if (!atomic_dec_and_test(&cmd->t_fe_count))
  3118. goto out_busy;
  3119. }
  3120. if (atomic_read(&cmd->t_se_count)) {
  3121. if (!atomic_dec_and_test(&cmd->t_se_count))
  3122. goto out_busy;
  3123. }
  3124. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  3125. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  3126. transport_all_task_dev_remove_state(cmd);
  3127. free_tasks = 1;
  3128. }
  3129. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3130. if (free_tasks != 0)
  3131. transport_free_dev_tasks(cmd);
  3132. transport_free_pages(cmd);
  3133. transport_release_cmd(cmd);
  3134. return;
  3135. out_busy:
  3136. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3137. }
  3138. /*
  3139. * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
  3140. * allocating in the core.
  3141. * @cmd: Associated se_cmd descriptor
  3142. * @mem: SGL style memory for TCM WRITE / READ
  3143. * @sg_mem_num: Number of SGL elements
  3144. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3145. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3146. *
  3147. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3148. * of parameters.
  3149. */
  3150. int transport_generic_map_mem_to_cmd(
  3151. struct se_cmd *cmd,
  3152. struct scatterlist *sgl,
  3153. u32 sgl_count,
  3154. struct scatterlist *sgl_bidi,
  3155. u32 sgl_bidi_count)
  3156. {
  3157. if (!sgl || !sgl_count)
  3158. return 0;
  3159. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3160. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3161. /*
  3162. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  3163. * scatterlists already have been set to follow what the fabric
  3164. * passes for the original expected data transfer length.
  3165. */
  3166. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  3167. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  3168. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  3169. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3170. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3171. return -EINVAL;
  3172. }
  3173. cmd->t_data_sg = sgl;
  3174. cmd->t_data_nents = sgl_count;
  3175. if (sgl_bidi && sgl_bidi_count) {
  3176. cmd->t_bidi_data_sg = sgl_bidi;
  3177. cmd->t_bidi_data_nents = sgl_bidi_count;
  3178. }
  3179. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3180. }
  3181. return 0;
  3182. }
  3183. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3184. void *transport_kmap_data_sg(struct se_cmd *cmd)
  3185. {
  3186. struct scatterlist *sg = cmd->t_data_sg;
  3187. struct page **pages;
  3188. int i;
  3189. BUG_ON(!sg);
  3190. /*
  3191. * We need to take into account a possible offset here for fabrics like
  3192. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  3193. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  3194. */
  3195. if (!cmd->t_data_nents)
  3196. return NULL;
  3197. else if (cmd->t_data_nents == 1)
  3198. return kmap(sg_page(sg)) + sg->offset;
  3199. /* >1 page. use vmap */
  3200. pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
  3201. if (!pages)
  3202. return NULL;
  3203. /* convert sg[] to pages[] */
  3204. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  3205. pages[i] = sg_page(sg);
  3206. }
  3207. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  3208. kfree(pages);
  3209. if (!cmd->t_data_vmap)
  3210. return NULL;
  3211. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  3212. }
  3213. EXPORT_SYMBOL(transport_kmap_data_sg);
  3214. void transport_kunmap_data_sg(struct se_cmd *cmd)
  3215. {
  3216. if (!cmd->t_data_nents) {
  3217. return;
  3218. } else if (cmd->t_data_nents == 1) {
  3219. kunmap(sg_page(cmd->t_data_sg));
  3220. return;
  3221. }
  3222. vunmap(cmd->t_data_vmap);
  3223. cmd->t_data_vmap = NULL;
  3224. }
  3225. EXPORT_SYMBOL(transport_kunmap_data_sg);
  3226. static int
  3227. transport_generic_get_mem(struct se_cmd *cmd)
  3228. {
  3229. u32 length = cmd->data_length;
  3230. unsigned int nents;
  3231. struct page *page;
  3232. gfp_t zero_flag;
  3233. int i = 0;
  3234. nents = DIV_ROUND_UP(length, PAGE_SIZE);
  3235. cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
  3236. if (!cmd->t_data_sg)
  3237. return -ENOMEM;
  3238. cmd->t_data_nents = nents;
  3239. sg_init_table(cmd->t_data_sg, nents);
  3240. zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
  3241. while (length) {
  3242. u32 page_len = min_t(u32, length, PAGE_SIZE);
  3243. page = alloc_page(GFP_KERNEL | zero_flag);
  3244. if (!page)
  3245. goto out;
  3246. sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
  3247. length -= page_len;
  3248. i++;
  3249. }
  3250. return 0;
  3251. out:
  3252. while (i >= 0) {
  3253. __free_page(sg_page(&cmd->t_data_sg[i]));
  3254. i--;
  3255. }
  3256. kfree(cmd->t_data_sg);
  3257. cmd->t_data_sg = NULL;
  3258. return -ENOMEM;
  3259. }
  3260. /* Reduce sectors if they are too long for the device */
  3261. static inline sector_t transport_limit_task_sectors(
  3262. struct se_device *dev,
  3263. unsigned long long lba,
  3264. sector_t sectors)
  3265. {
  3266. sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
  3267. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  3268. if ((lba + sectors) > transport_dev_end_lba(dev))
  3269. sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  3270. return sectors;
  3271. }
  3272. /*
  3273. * This function can be used by HW target mode drivers to create a linked
  3274. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  3275. * This is intended to be called during the completion path by TCM Core
  3276. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  3277. */
  3278. void transport_do_task_sg_chain(struct se_cmd *cmd)
  3279. {
  3280. struct scatterlist *sg_first = NULL;
  3281. struct scatterlist *sg_prev = NULL;
  3282. int sg_prev_nents = 0;
  3283. struct scatterlist *sg;
  3284. struct se_task *task;
  3285. u32 chained_nents = 0;
  3286. int i;
  3287. BUG_ON(!cmd->se_tfo->task_sg_chaining);
  3288. /*
  3289. * Walk the struct se_task list and setup scatterlist chains
  3290. * for each contiguously allocated struct se_task->task_sg[].
  3291. */
  3292. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  3293. if (!task->task_sg)
  3294. continue;
  3295. if (!sg_first) {
  3296. sg_first = task->task_sg;
  3297. chained_nents = task->task_sg_nents;
  3298. } else {
  3299. sg_chain(sg_prev, sg_prev_nents, task->task_sg);
  3300. chained_nents += task->task_sg_nents;
  3301. }
  3302. /*
  3303. * For the padded tasks, use the extra SGL vector allocated
  3304. * in transport_allocate_data_tasks() for the sg_prev_nents
  3305. * offset into sg_chain() above.
  3306. *
  3307. * We do not need the padding for the last task (or a single
  3308. * task), but in that case we will never use the sg_prev_nents
  3309. * value below which would be incorrect.
  3310. */
  3311. sg_prev_nents = (task->task_sg_nents + 1);
  3312. sg_prev = task->task_sg;
  3313. }
  3314. /*
  3315. * Setup the starting pointer and total t_tasks_sg_linked_no including
  3316. * padding SGs for linking and to mark the end.
  3317. */
  3318. cmd->t_tasks_sg_chained = sg_first;
  3319. cmd->t_tasks_sg_chained_no = chained_nents;
  3320. pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
  3321. " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
  3322. cmd->t_tasks_sg_chained_no);
  3323. for_each_sg(cmd->t_tasks_sg_chained, sg,
  3324. cmd->t_tasks_sg_chained_no, i) {
  3325. pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
  3326. i, sg, sg_page(sg), sg->length, sg->offset);
  3327. if (sg_is_chain(sg))
  3328. pr_debug("SG: %p sg_is_chain=1\n", sg);
  3329. if (sg_is_last(sg))
  3330. pr_debug("SG: %p sg_is_last=1\n", sg);
  3331. }
  3332. }
  3333. EXPORT_SYMBOL(transport_do_task_sg_chain);
  3334. /*
  3335. * Break up cmd into chunks transport can handle
  3336. */
  3337. static int
  3338. transport_allocate_data_tasks(struct se_cmd *cmd,
  3339. enum dma_data_direction data_direction,
  3340. struct scatterlist *cmd_sg, unsigned int sgl_nents)
  3341. {
  3342. struct se_device *dev = cmd->se_dev;
  3343. int task_count, i;
  3344. unsigned long long lba;
  3345. sector_t sectors, dev_max_sectors;
  3346. u32 sector_size;
  3347. if (transport_cmd_get_valid_sectors(cmd) < 0)
  3348. return -EINVAL;
  3349. dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
  3350. sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
  3351. WARN_ON(cmd->data_length % sector_size);
  3352. lba = cmd->t_task_lba;
  3353. sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
  3354. task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
  3355. /*
  3356. * If we need just a single task reuse the SG list in the command
  3357. * and avoid a lot of work.
  3358. */
  3359. if (task_count == 1) {
  3360. struct se_task *task;
  3361. unsigned long flags;
  3362. task = transport_generic_get_task(cmd, data_direction);
  3363. if (!task)
  3364. return -ENOMEM;
  3365. task->task_sg = cmd_sg;
  3366. task->task_sg_nents = sgl_nents;
  3367. task->task_lba = lba;
  3368. task->task_sectors = sectors;
  3369. task->task_size = task->task_sectors * sector_size;
  3370. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3371. list_add_tail(&task->t_list, &cmd->t_task_list);
  3372. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3373. return task_count;
  3374. }
  3375. for (i = 0; i < task_count; i++) {
  3376. struct se_task *task;
  3377. unsigned int task_size, task_sg_nents_padded;
  3378. struct scatterlist *sg;
  3379. unsigned long flags;
  3380. int count;
  3381. task = transport_generic_get_task(cmd, data_direction);
  3382. if (!task)
  3383. return -ENOMEM;
  3384. task->task_lba = lba;
  3385. task->task_sectors = min(sectors, dev_max_sectors);
  3386. task->task_size = task->task_sectors * sector_size;
  3387. /*
  3388. * This now assumes that passed sg_ents are in PAGE_SIZE chunks
  3389. * in order to calculate the number per task SGL entries
  3390. */
  3391. task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
  3392. /*
  3393. * Check if the fabric module driver is requesting that all
  3394. * struct se_task->task_sg[] be chained together.. If so,
  3395. * then allocate an extra padding SG entry for linking and
  3396. * marking the end of the chained SGL for every task except
  3397. * the last one for (task_count > 1) operation, or skipping
  3398. * the extra padding for the (task_count == 1) case.
  3399. */
  3400. if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
  3401. task_sg_nents_padded = (task->task_sg_nents + 1);
  3402. } else
  3403. task_sg_nents_padded = task->task_sg_nents;
  3404. task->task_sg = kmalloc(sizeof(struct scatterlist) *
  3405. task_sg_nents_padded, GFP_KERNEL);
  3406. if (!task->task_sg) {
  3407. cmd->se_dev->transport->free_task(task);
  3408. return -ENOMEM;
  3409. }
  3410. sg_init_table(task->task_sg, task_sg_nents_padded);
  3411. task_size = task->task_size;
  3412. /* Build new sgl, only up to task_size */
  3413. for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
  3414. if (cmd_sg->length > task_size)
  3415. break;
  3416. *sg = *cmd_sg;
  3417. task_size -= cmd_sg->length;
  3418. cmd_sg = sg_next(cmd_sg);
  3419. }
  3420. lba += task->task_sectors;
  3421. sectors -= task->task_sectors;
  3422. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3423. list_add_tail(&task->t_list, &cmd->t_task_list);
  3424. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3425. }
  3426. return task_count;
  3427. }
  3428. static int
  3429. transport_allocate_control_task(struct se_cmd *cmd)
  3430. {
  3431. struct se_task *task;
  3432. unsigned long flags;
  3433. /* Workaround for handling zero-length control CDBs */
  3434. if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
  3435. !cmd->data_length)
  3436. return 0;
  3437. task = transport_generic_get_task(cmd, cmd->data_direction);
  3438. if (!task)
  3439. return -ENOMEM;
  3440. task->task_sg = cmd->t_data_sg;
  3441. task->task_size = cmd->data_length;
  3442. task->task_sg_nents = cmd->t_data_nents;
  3443. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3444. list_add_tail(&task->t_list, &cmd->t_task_list);
  3445. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3446. /* Success! Return number of tasks allocated */
  3447. return 1;
  3448. }
  3449. /*
  3450. * Allocate any required ressources to execute the command, and either place
  3451. * it on the execution queue if possible. For writes we might not have the
  3452. * payload yet, thus notify the fabric via a call to ->write_pending instead.
  3453. */
  3454. int transport_generic_new_cmd(struct se_cmd *cmd)
  3455. {
  3456. struct se_device *dev = cmd->se_dev;
  3457. int task_cdbs, task_cdbs_bidi = 0;
  3458. int set_counts = 1;
  3459. int ret = 0;
  3460. /*
  3461. * Determine is the TCM fabric module has already allocated physical
  3462. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  3463. * beforehand.
  3464. */
  3465. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  3466. cmd->data_length) {
  3467. ret = transport_generic_get_mem(cmd);
  3468. if (ret < 0)
  3469. goto out_fail;
  3470. }
  3471. /*
  3472. * For BIDI command set up the read tasks first.
  3473. */
  3474. if (cmd->t_bidi_data_sg &&
  3475. dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  3476. BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
  3477. task_cdbs_bidi = transport_allocate_data_tasks(cmd,
  3478. DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
  3479. cmd->t_bidi_data_nents);
  3480. if (task_cdbs_bidi <= 0)
  3481. goto out_fail;
  3482. atomic_inc(&cmd->t_fe_count);
  3483. atomic_inc(&cmd->t_se_count);
  3484. set_counts = 0;
  3485. }
  3486. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  3487. task_cdbs = transport_allocate_data_tasks(cmd,
  3488. cmd->data_direction, cmd->t_data_sg,
  3489. cmd->t_data_nents);
  3490. } else {
  3491. task_cdbs = transport_allocate_control_task(cmd);
  3492. }
  3493. if (task_cdbs < 0)
  3494. goto out_fail;
  3495. else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
  3496. spin_lock_irq(&cmd->t_state_lock);
  3497. cmd->t_state = TRANSPORT_COMPLETE;
  3498. cmd->transport_state |= CMD_T_ACTIVE;
  3499. spin_unlock_irq(&cmd->t_state_lock);
  3500. if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
  3501. u8 ua_asc = 0, ua_ascq = 0;
  3502. core_scsi3_ua_clear_for_request_sense(cmd,
  3503. &ua_asc, &ua_ascq);
  3504. }
  3505. INIT_WORK(&cmd->work, target_complete_ok_work);
  3506. queue_work(target_completion_wq, &cmd->work);
  3507. return 0;
  3508. }
  3509. if (set_counts) {
  3510. atomic_inc(&cmd->t_fe_count);
  3511. atomic_inc(&cmd->t_se_count);
  3512. }
  3513. cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
  3514. atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
  3515. atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
  3516. /*
  3517. * For WRITEs, let the fabric know its buffer is ready..
  3518. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  3519. * will be added to the struct se_device execution queue after its WRITE
  3520. * data has arrived. (ie: It gets handled by the transport processing
  3521. * thread a second time)
  3522. */
  3523. if (cmd->data_direction == DMA_TO_DEVICE) {
  3524. transport_add_tasks_to_state_queue(cmd);
  3525. return transport_generic_write_pending(cmd);
  3526. }
  3527. /*
  3528. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  3529. * to the execution queue.
  3530. */
  3531. transport_execute_tasks(cmd);
  3532. return 0;
  3533. out_fail:
  3534. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3535. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3536. return -EINVAL;
  3537. }
  3538. EXPORT_SYMBOL(transport_generic_new_cmd);
  3539. /* transport_generic_process_write():
  3540. *
  3541. *
  3542. */
  3543. void transport_generic_process_write(struct se_cmd *cmd)
  3544. {
  3545. transport_execute_tasks(cmd);
  3546. }
  3547. EXPORT_SYMBOL(transport_generic_process_write);
  3548. static void transport_write_pending_qf(struct se_cmd *cmd)
  3549. {
  3550. int ret;
  3551. ret = cmd->se_tfo->write_pending(cmd);
  3552. if (ret == -EAGAIN || ret == -ENOMEM) {
  3553. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  3554. cmd);
  3555. transport_handle_queue_full(cmd, cmd->se_dev);
  3556. }
  3557. }
  3558. static int transport_generic_write_pending(struct se_cmd *cmd)
  3559. {
  3560. unsigned long flags;
  3561. int ret;
  3562. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3563. cmd->t_state = TRANSPORT_WRITE_PENDING;
  3564. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3565. /*
  3566. * Clear the se_cmd for WRITE_PENDING status in order to set
  3567. * CMD_T_ACTIVE so that transport_generic_handle_data can be called
  3568. * from HW target mode interrupt code. This is safe to be called
  3569. * with transport_off=1 before the cmd->se_tfo->write_pending
  3570. * because the se_cmd->se_lun pointer is not being cleared.
  3571. */
  3572. transport_cmd_check_stop(cmd, 1, 0);
  3573. /*
  3574. * Call the fabric write_pending function here to let the
  3575. * frontend know that WRITE buffers are ready.
  3576. */
  3577. ret = cmd->se_tfo->write_pending(cmd);
  3578. if (ret == -EAGAIN || ret == -ENOMEM)
  3579. goto queue_full;
  3580. else if (ret < 0)
  3581. return ret;
  3582. return 1;
  3583. queue_full:
  3584. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  3585. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  3586. transport_handle_queue_full(cmd, cmd->se_dev);
  3587. return 0;
  3588. }
  3589. void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  3590. {
  3591. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  3592. if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  3593. transport_wait_for_tasks(cmd);
  3594. transport_release_cmd(cmd);
  3595. } else {
  3596. if (wait_for_tasks)
  3597. transport_wait_for_tasks(cmd);
  3598. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  3599. if (cmd->se_lun)
  3600. transport_lun_remove_cmd(cmd);
  3601. transport_free_dev_tasks(cmd);
  3602. transport_put_cmd(cmd);
  3603. }
  3604. }
  3605. EXPORT_SYMBOL(transport_generic_free_cmd);
  3606. /* target_get_sess_cmd - Add command to active ->sess_cmd_list
  3607. * @se_sess: session to reference
  3608. * @se_cmd: command descriptor to add
  3609. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  3610. */
  3611. void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
  3612. bool ack_kref)
  3613. {
  3614. unsigned long flags;
  3615. kref_init(&se_cmd->cmd_kref);
  3616. /*
  3617. * Add a second kref if the fabric caller is expecting to handle
  3618. * fabric acknowledgement that requires two target_put_sess_cmd()
  3619. * invocations before se_cmd descriptor release.
  3620. */
  3621. if (ack_kref == true) {
  3622. kref_get(&se_cmd->cmd_kref);
  3623. se_cmd->se_cmd_flags |= SCF_ACK_KREF;
  3624. }
  3625. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3626. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  3627. se_cmd->check_release = 1;
  3628. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3629. }
  3630. EXPORT_SYMBOL(target_get_sess_cmd);
  3631. static void target_release_cmd_kref(struct kref *kref)
  3632. {
  3633. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  3634. struct se_session *se_sess = se_cmd->se_sess;
  3635. unsigned long flags;
  3636. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3637. if (list_empty(&se_cmd->se_cmd_list)) {
  3638. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3639. se_cmd->se_tfo->release_cmd(se_cmd);
  3640. return;
  3641. }
  3642. if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
  3643. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3644. complete(&se_cmd->cmd_wait_comp);
  3645. return;
  3646. }
  3647. list_del(&se_cmd->se_cmd_list);
  3648. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3649. se_cmd->se_tfo->release_cmd(se_cmd);
  3650. }
  3651. /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
  3652. * @se_sess: session to reference
  3653. * @se_cmd: command descriptor to drop
  3654. */
  3655. int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
  3656. {
  3657. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  3658. }
  3659. EXPORT_SYMBOL(target_put_sess_cmd);
  3660. /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
  3661. * @se_sess: session to split
  3662. */
  3663. void target_splice_sess_cmd_list(struct se_session *se_sess)
  3664. {
  3665. struct se_cmd *se_cmd;
  3666. unsigned long flags;
  3667. WARN_ON(!list_empty(&se_sess->sess_wait_list));
  3668. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  3669. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3670. se_sess->sess_tearing_down = 1;
  3671. list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
  3672. list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
  3673. se_cmd->cmd_wait_set = 1;
  3674. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3675. }
  3676. EXPORT_SYMBOL(target_splice_sess_cmd_list);
  3677. /* target_wait_for_sess_cmds - Wait for outstanding descriptors
  3678. * @se_sess: session to wait for active I/O
  3679. * @wait_for_tasks: Make extra transport_wait_for_tasks call
  3680. */
  3681. void target_wait_for_sess_cmds(
  3682. struct se_session *se_sess,
  3683. int wait_for_tasks)
  3684. {
  3685. struct se_cmd *se_cmd, *tmp_cmd;
  3686. bool rc = false;
  3687. list_for_each_entry_safe(se_cmd, tmp_cmd,
  3688. &se_sess->sess_wait_list, se_cmd_list) {
  3689. list_del(&se_cmd->se_cmd_list);
  3690. pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
  3691. " %d\n", se_cmd, se_cmd->t_state,
  3692. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3693. if (wait_for_tasks) {
  3694. pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3695. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3696. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3697. rc = transport_wait_for_tasks(se_cmd);
  3698. pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3699. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3700. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3701. }
  3702. if (!rc) {
  3703. wait_for_completion(&se_cmd->cmd_wait_comp);
  3704. pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
  3705. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3706. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3707. }
  3708. se_cmd->se_tfo->release_cmd(se_cmd);
  3709. }
  3710. }
  3711. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  3712. /* transport_lun_wait_for_tasks():
  3713. *
  3714. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  3715. * an struct se_lun to be successfully shutdown.
  3716. */
  3717. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  3718. {
  3719. unsigned long flags;
  3720. int ret;
  3721. /*
  3722. * If the frontend has already requested this struct se_cmd to
  3723. * be stopped, we can safely ignore this struct se_cmd.
  3724. */
  3725. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3726. if (cmd->transport_state & CMD_T_STOP) {
  3727. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3728. pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
  3729. cmd->se_tfo->get_task_tag(cmd));
  3730. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3731. transport_cmd_check_stop(cmd, 1, 0);
  3732. return -EPERM;
  3733. }
  3734. cmd->transport_state |= CMD_T_LUN_FE_STOP;
  3735. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3736. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3737. ret = transport_stop_tasks_for_cmd(cmd);
  3738. pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
  3739. " %d\n", cmd, cmd->t_task_list_num, ret);
  3740. if (!ret) {
  3741. pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  3742. cmd->se_tfo->get_task_tag(cmd));
  3743. wait_for_completion(&cmd->transport_lun_stop_comp);
  3744. pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  3745. cmd->se_tfo->get_task_tag(cmd));
  3746. }
  3747. transport_remove_cmd_from_queue(cmd);
  3748. return 0;
  3749. }
  3750. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  3751. {
  3752. struct se_cmd *cmd = NULL;
  3753. unsigned long lun_flags, cmd_flags;
  3754. /*
  3755. * Do exception processing and return CHECK_CONDITION status to the
  3756. * Initiator Port.
  3757. */
  3758. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3759. while (!list_empty(&lun->lun_cmd_list)) {
  3760. cmd = list_first_entry(&lun->lun_cmd_list,
  3761. struct se_cmd, se_lun_node);
  3762. list_del_init(&cmd->se_lun_node);
  3763. /*
  3764. * This will notify iscsi_target_transport.c:
  3765. * transport_cmd_check_stop() that a LUN shutdown is in
  3766. * progress for the iscsi_cmd_t.
  3767. */
  3768. spin_lock(&cmd->t_state_lock);
  3769. pr_debug("SE_LUN[%d] - Setting cmd->transport"
  3770. "_lun_stop for ITT: 0x%08x\n",
  3771. cmd->se_lun->unpacked_lun,
  3772. cmd->se_tfo->get_task_tag(cmd));
  3773. cmd->transport_state |= CMD_T_LUN_STOP;
  3774. spin_unlock(&cmd->t_state_lock);
  3775. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3776. if (!cmd->se_lun) {
  3777. pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
  3778. cmd->se_tfo->get_task_tag(cmd),
  3779. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3780. BUG();
  3781. }
  3782. /*
  3783. * If the Storage engine still owns the iscsi_cmd_t, determine
  3784. * and/or stop its context.
  3785. */
  3786. pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
  3787. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  3788. cmd->se_tfo->get_task_tag(cmd));
  3789. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  3790. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3791. continue;
  3792. }
  3793. pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  3794. "_wait_for_tasks(): SUCCESS\n",
  3795. cmd->se_lun->unpacked_lun,
  3796. cmd->se_tfo->get_task_tag(cmd));
  3797. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3798. if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
  3799. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3800. goto check_cond;
  3801. }
  3802. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  3803. transport_all_task_dev_remove_state(cmd);
  3804. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3805. transport_free_dev_tasks(cmd);
  3806. /*
  3807. * The Storage engine stopped this struct se_cmd before it was
  3808. * send to the fabric frontend for delivery back to the
  3809. * Initiator Node. Return this SCSI CDB back with an
  3810. * CHECK_CONDITION status.
  3811. */
  3812. check_cond:
  3813. transport_send_check_condition_and_sense(cmd,
  3814. TCM_NON_EXISTENT_LUN, 0);
  3815. /*
  3816. * If the fabric frontend is waiting for this iscsi_cmd_t to
  3817. * be released, notify the waiting thread now that LU has
  3818. * finished accessing it.
  3819. */
  3820. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3821. if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
  3822. pr_debug("SE_LUN[%d] - Detected FE stop for"
  3823. " struct se_cmd: %p ITT: 0x%08x\n",
  3824. lun->unpacked_lun,
  3825. cmd, cmd->se_tfo->get_task_tag(cmd));
  3826. spin_unlock_irqrestore(&cmd->t_state_lock,
  3827. cmd_flags);
  3828. transport_cmd_check_stop(cmd, 1, 0);
  3829. complete(&cmd->transport_lun_fe_stop_comp);
  3830. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3831. continue;
  3832. }
  3833. pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  3834. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  3835. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3836. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3837. }
  3838. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3839. }
  3840. static int transport_clear_lun_thread(void *p)
  3841. {
  3842. struct se_lun *lun = p;
  3843. __transport_clear_lun_from_sessions(lun);
  3844. complete(&lun->lun_shutdown_comp);
  3845. return 0;
  3846. }
  3847. int transport_clear_lun_from_sessions(struct se_lun *lun)
  3848. {
  3849. struct task_struct *kt;
  3850. kt = kthread_run(transport_clear_lun_thread, lun,
  3851. "tcm_cl_%u", lun->unpacked_lun);
  3852. if (IS_ERR(kt)) {
  3853. pr_err("Unable to start clear_lun thread\n");
  3854. return PTR_ERR(kt);
  3855. }
  3856. wait_for_completion(&lun->lun_shutdown_comp);
  3857. return 0;
  3858. }
  3859. /**
  3860. * transport_wait_for_tasks - wait for completion to occur
  3861. * @cmd: command to wait
  3862. *
  3863. * Called from frontend fabric context to wait for storage engine
  3864. * to pause and/or release frontend generated struct se_cmd.
  3865. */
  3866. bool transport_wait_for_tasks(struct se_cmd *cmd)
  3867. {
  3868. unsigned long flags;
  3869. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3870. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
  3871. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3872. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3873. return false;
  3874. }
  3875. /*
  3876. * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
  3877. * has been set in transport_set_supported_SAM_opcode().
  3878. */
  3879. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
  3880. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3881. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3882. return false;
  3883. }
  3884. /*
  3885. * If we are already stopped due to an external event (ie: LUN shutdown)
  3886. * sleep until the connection can have the passed struct se_cmd back.
  3887. * The cmd->transport_lun_stopped_sem will be upped by
  3888. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  3889. * has completed its operation on the struct se_cmd.
  3890. */
  3891. if (cmd->transport_state & CMD_T_LUN_STOP) {
  3892. pr_debug("wait_for_tasks: Stopping"
  3893. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  3894. "_stop_comp); for ITT: 0x%08x\n",
  3895. cmd->se_tfo->get_task_tag(cmd));
  3896. /*
  3897. * There is a special case for WRITES where a FE exception +
  3898. * LUN shutdown means ConfigFS context is still sleeping on
  3899. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  3900. * We go ahead and up transport_lun_stop_comp just to be sure
  3901. * here.
  3902. */
  3903. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3904. complete(&cmd->transport_lun_stop_comp);
  3905. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  3906. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3907. transport_all_task_dev_remove_state(cmd);
  3908. /*
  3909. * At this point, the frontend who was the originator of this
  3910. * struct se_cmd, now owns the structure and can be released through
  3911. * normal means below.
  3912. */
  3913. pr_debug("wait_for_tasks: Stopped"
  3914. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  3915. "stop_comp); for ITT: 0x%08x\n",
  3916. cmd->se_tfo->get_task_tag(cmd));
  3917. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3918. }
  3919. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  3920. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3921. return false;
  3922. }
  3923. cmd->transport_state |= CMD_T_STOP;
  3924. pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
  3925. " i_state: %d, t_state: %d, CMD_T_STOP\n",
  3926. cmd, cmd->se_tfo->get_task_tag(cmd),
  3927. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3928. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3929. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3930. wait_for_completion(&cmd->t_transport_stop_comp);
  3931. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3932. cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
  3933. pr_debug("wait_for_tasks: Stopped wait_for_compltion("
  3934. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  3935. cmd->se_tfo->get_task_tag(cmd));
  3936. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3937. return true;
  3938. }
  3939. EXPORT_SYMBOL(transport_wait_for_tasks);
  3940. static int transport_get_sense_codes(
  3941. struct se_cmd *cmd,
  3942. u8 *asc,
  3943. u8 *ascq)
  3944. {
  3945. *asc = cmd->scsi_asc;
  3946. *ascq = cmd->scsi_ascq;
  3947. return 0;
  3948. }
  3949. static int transport_set_sense_codes(
  3950. struct se_cmd *cmd,
  3951. u8 asc,
  3952. u8 ascq)
  3953. {
  3954. cmd->scsi_asc = asc;
  3955. cmd->scsi_ascq = ascq;
  3956. return 0;
  3957. }
  3958. int transport_send_check_condition_and_sense(
  3959. struct se_cmd *cmd,
  3960. u8 reason,
  3961. int from_transport)
  3962. {
  3963. unsigned char *buffer = cmd->sense_buffer;
  3964. unsigned long flags;
  3965. int offset;
  3966. u8 asc = 0, ascq = 0;
  3967. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3968. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3969. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3970. return 0;
  3971. }
  3972. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  3973. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3974. if (!reason && from_transport)
  3975. goto after_reason;
  3976. if (!from_transport)
  3977. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  3978. /*
  3979. * Data Segment and SenseLength of the fabric response PDU.
  3980. *
  3981. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  3982. * from include/scsi/scsi_cmnd.h
  3983. */
  3984. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  3985. TRANSPORT_SENSE_BUFFER);
  3986. /*
  3987. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  3988. * SENSE KEY values from include/scsi/scsi.h
  3989. */
  3990. switch (reason) {
  3991. case TCM_NON_EXISTENT_LUN:
  3992. /* CURRENT ERROR */
  3993. buffer[offset] = 0x70;
  3994. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3995. /* ILLEGAL REQUEST */
  3996. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3997. /* LOGICAL UNIT NOT SUPPORTED */
  3998. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
  3999. break;
  4000. case TCM_UNSUPPORTED_SCSI_OPCODE:
  4001. case TCM_SECTOR_COUNT_TOO_MANY:
  4002. /* CURRENT ERROR */
  4003. buffer[offset] = 0x70;
  4004. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4005. /* ILLEGAL REQUEST */
  4006. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4007. /* INVALID COMMAND OPERATION CODE */
  4008. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  4009. break;
  4010. case TCM_UNKNOWN_MODE_PAGE:
  4011. /* CURRENT ERROR */
  4012. buffer[offset] = 0x70;
  4013. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4014. /* ILLEGAL REQUEST */
  4015. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4016. /* INVALID FIELD IN CDB */
  4017. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4018. break;
  4019. case TCM_CHECK_CONDITION_ABORT_CMD:
  4020. /* CURRENT ERROR */
  4021. buffer[offset] = 0x70;
  4022. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4023. /* ABORTED COMMAND */
  4024. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4025. /* BUS DEVICE RESET FUNCTION OCCURRED */
  4026. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  4027. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  4028. break;
  4029. case TCM_INCORRECT_AMOUNT_OF_DATA:
  4030. /* CURRENT ERROR */
  4031. buffer[offset] = 0x70;
  4032. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4033. /* ABORTED COMMAND */
  4034. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4035. /* WRITE ERROR */
  4036. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4037. /* NOT ENOUGH UNSOLICITED DATA */
  4038. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  4039. break;
  4040. case TCM_INVALID_CDB_FIELD:
  4041. /* CURRENT ERROR */
  4042. buffer[offset] = 0x70;
  4043. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4044. /* ILLEGAL REQUEST */
  4045. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4046. /* INVALID FIELD IN CDB */
  4047. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4048. break;
  4049. case TCM_INVALID_PARAMETER_LIST:
  4050. /* CURRENT ERROR */
  4051. buffer[offset] = 0x70;
  4052. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4053. /* ILLEGAL REQUEST */
  4054. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4055. /* INVALID FIELD IN PARAMETER LIST */
  4056. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  4057. break;
  4058. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  4059. /* CURRENT ERROR */
  4060. buffer[offset] = 0x70;
  4061. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4062. /* ABORTED COMMAND */
  4063. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4064. /* WRITE ERROR */
  4065. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4066. /* UNEXPECTED_UNSOLICITED_DATA */
  4067. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  4068. break;
  4069. case TCM_SERVICE_CRC_ERROR:
  4070. /* CURRENT ERROR */
  4071. buffer[offset] = 0x70;
  4072. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4073. /* ABORTED COMMAND */
  4074. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4075. /* PROTOCOL SERVICE CRC ERROR */
  4076. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  4077. /* N/A */
  4078. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  4079. break;
  4080. case TCM_SNACK_REJECTED:
  4081. /* CURRENT ERROR */
  4082. buffer[offset] = 0x70;
  4083. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4084. /* ABORTED COMMAND */
  4085. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4086. /* READ ERROR */
  4087. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  4088. /* FAILED RETRANSMISSION REQUEST */
  4089. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  4090. break;
  4091. case TCM_WRITE_PROTECTED:
  4092. /* CURRENT ERROR */
  4093. buffer[offset] = 0x70;
  4094. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4095. /* DATA PROTECT */
  4096. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  4097. /* WRITE PROTECTED */
  4098. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  4099. break;
  4100. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  4101. /* CURRENT ERROR */
  4102. buffer[offset] = 0x70;
  4103. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4104. /* UNIT ATTENTION */
  4105. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  4106. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  4107. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4108. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4109. break;
  4110. case TCM_CHECK_CONDITION_NOT_READY:
  4111. /* CURRENT ERROR */
  4112. buffer[offset] = 0x70;
  4113. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4114. /* Not Ready */
  4115. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  4116. transport_get_sense_codes(cmd, &asc, &ascq);
  4117. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4118. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4119. break;
  4120. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  4121. default:
  4122. /* CURRENT ERROR */
  4123. buffer[offset] = 0x70;
  4124. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4125. /* ILLEGAL REQUEST */
  4126. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4127. /* LOGICAL UNIT COMMUNICATION FAILURE */
  4128. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  4129. break;
  4130. }
  4131. /*
  4132. * This code uses linux/include/scsi/scsi.h SAM status codes!
  4133. */
  4134. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  4135. /*
  4136. * Automatically padded, this value is encoded in the fabric's
  4137. * data_length response PDU containing the SCSI defined sense data.
  4138. */
  4139. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  4140. after_reason:
  4141. return cmd->se_tfo->queue_status(cmd);
  4142. }
  4143. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  4144. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  4145. {
  4146. int ret = 0;
  4147. if (cmd->transport_state & CMD_T_ABORTED) {
  4148. if (!send_status ||
  4149. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  4150. return 1;
  4151. #if 0
  4152. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
  4153. " status for CDB: 0x%02x ITT: 0x%08x\n",
  4154. cmd->t_task_cdb[0],
  4155. cmd->se_tfo->get_task_tag(cmd));
  4156. #endif
  4157. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  4158. cmd->se_tfo->queue_status(cmd);
  4159. ret = 1;
  4160. }
  4161. return ret;
  4162. }
  4163. EXPORT_SYMBOL(transport_check_aborted_status);
  4164. void transport_send_task_abort(struct se_cmd *cmd)
  4165. {
  4166. unsigned long flags;
  4167. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4168. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4169. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4170. return;
  4171. }
  4172. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4173. /*
  4174. * If there are still expected incoming fabric WRITEs, we wait
  4175. * until until they have completed before sending a TASK_ABORTED
  4176. * response. This response with TASK_ABORTED status will be
  4177. * queued back to fabric module by transport_check_aborted_status().
  4178. */
  4179. if (cmd->data_direction == DMA_TO_DEVICE) {
  4180. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  4181. cmd->transport_state |= CMD_T_ABORTED;
  4182. smp_mb__after_atomic_inc();
  4183. }
  4184. }
  4185. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4186. #if 0
  4187. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  4188. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  4189. cmd->se_tfo->get_task_tag(cmd));
  4190. #endif
  4191. cmd->se_tfo->queue_status(cmd);
  4192. }
  4193. static int transport_generic_do_tmr(struct se_cmd *cmd)
  4194. {
  4195. struct se_device *dev = cmd->se_dev;
  4196. struct se_tmr_req *tmr = cmd->se_tmr_req;
  4197. int ret;
  4198. switch (tmr->function) {
  4199. case TMR_ABORT_TASK:
  4200. core_tmr_abort_task(dev, tmr, cmd->se_sess);
  4201. break;
  4202. case TMR_ABORT_TASK_SET:
  4203. case TMR_CLEAR_ACA:
  4204. case TMR_CLEAR_TASK_SET:
  4205. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  4206. break;
  4207. case TMR_LUN_RESET:
  4208. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  4209. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  4210. TMR_FUNCTION_REJECTED;
  4211. break;
  4212. case TMR_TARGET_WARM_RESET:
  4213. tmr->response = TMR_FUNCTION_REJECTED;
  4214. break;
  4215. case TMR_TARGET_COLD_RESET:
  4216. tmr->response = TMR_FUNCTION_REJECTED;
  4217. break;
  4218. default:
  4219. pr_err("Uknown TMR function: 0x%02x.\n",
  4220. tmr->function);
  4221. tmr->response = TMR_FUNCTION_REJECTED;
  4222. break;
  4223. }
  4224. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  4225. cmd->se_tfo->queue_tm_rsp(cmd);
  4226. transport_cmd_check_stop_to_fabric(cmd);
  4227. return 0;
  4228. }
  4229. /* transport_processing_thread():
  4230. *
  4231. *
  4232. */
  4233. static int transport_processing_thread(void *param)
  4234. {
  4235. int ret;
  4236. struct se_cmd *cmd;
  4237. struct se_device *dev = param;
  4238. while (!kthread_should_stop()) {
  4239. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  4240. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  4241. kthread_should_stop());
  4242. if (ret < 0)
  4243. goto out;
  4244. get_cmd:
  4245. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  4246. if (!cmd)
  4247. continue;
  4248. switch (cmd->t_state) {
  4249. case TRANSPORT_NEW_CMD:
  4250. BUG();
  4251. break;
  4252. case TRANSPORT_NEW_CMD_MAP:
  4253. if (!cmd->se_tfo->new_cmd_map) {
  4254. pr_err("cmd->se_tfo->new_cmd_map is"
  4255. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  4256. BUG();
  4257. }
  4258. ret = cmd->se_tfo->new_cmd_map(cmd);
  4259. if (ret < 0) {
  4260. transport_generic_request_failure(cmd);
  4261. break;
  4262. }
  4263. ret = transport_generic_new_cmd(cmd);
  4264. if (ret < 0) {
  4265. transport_generic_request_failure(cmd);
  4266. break;
  4267. }
  4268. break;
  4269. case TRANSPORT_PROCESS_WRITE:
  4270. transport_generic_process_write(cmd);
  4271. break;
  4272. case TRANSPORT_PROCESS_TMR:
  4273. transport_generic_do_tmr(cmd);
  4274. break;
  4275. case TRANSPORT_COMPLETE_QF_WP:
  4276. transport_write_pending_qf(cmd);
  4277. break;
  4278. case TRANSPORT_COMPLETE_QF_OK:
  4279. transport_complete_qf(cmd);
  4280. break;
  4281. default:
  4282. pr_err("Unknown t_state: %d for ITT: 0x%08x "
  4283. "i_state: %d on SE LUN: %u\n",
  4284. cmd->t_state,
  4285. cmd->se_tfo->get_task_tag(cmd),
  4286. cmd->se_tfo->get_cmd_state(cmd),
  4287. cmd->se_lun->unpacked_lun);
  4288. BUG();
  4289. }
  4290. goto get_cmd;
  4291. }
  4292. out:
  4293. WARN_ON(!list_empty(&dev->state_task_list));
  4294. WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
  4295. dev->process_thread = NULL;
  4296. return 0;
  4297. }