target_core_transport.c 129 KB

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