target_core_transport.c 126 KB

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