target_core_transport.c 120 KB

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