target_core_transport.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436
  1. /*******************************************************************************
  2. * Filename: target_core_transport.c
  3. *
  4. * This file contains the Generic Target Engine Core.
  5. *
  6. * (c) Copyright 2002-2013 Datera, Inc.
  7. *
  8. * Nicholas A. Bellinger <nab@kernel.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. ******************************************************************************/
  25. #include <linux/net.h>
  26. #include <linux/delay.h>
  27. #include <linux/string.h>
  28. #include <linux/timer.h>
  29. #include <linux/slab.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/kthread.h>
  32. #include <linux/in.h>
  33. #include <linux/cdrom.h>
  34. #include <linux/module.h>
  35. #include <linux/ratelimit.h>
  36. #include <linux/vmalloc.h>
  37. #include <asm/unaligned.h>
  38. #include <net/sock.h>
  39. #include <net/tcp.h>
  40. #include <scsi/scsi_proto.h>
  41. #include <scsi/scsi_common.h>
  42. #include <target/target_core_base.h>
  43. #include <target/target_core_backend.h>
  44. #include <target/target_core_fabric.h>
  45. #include "target_core_internal.h"
  46. #include "target_core_alua.h"
  47. #include "target_core_pr.h"
  48. #include "target_core_ua.h"
  49. #define CREATE_TRACE_POINTS
  50. #include <trace/events/target.h>
  51. static struct workqueue_struct *target_completion_wq;
  52. static struct kmem_cache *se_sess_cache;
  53. struct kmem_cache *se_ua_cache;
  54. struct kmem_cache *t10_pr_reg_cache;
  55. struct kmem_cache *t10_alua_lu_gp_cache;
  56. struct kmem_cache *t10_alua_lu_gp_mem_cache;
  57. struct kmem_cache *t10_alua_tg_pt_gp_cache;
  58. struct kmem_cache *t10_alua_lba_map_cache;
  59. struct kmem_cache *t10_alua_lba_map_mem_cache;
  60. static void transport_complete_task_attr(struct se_cmd *cmd);
  61. static int translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason);
  62. static void transport_handle_queue_full(struct se_cmd *cmd,
  63. struct se_device *dev, int err, bool write_pending);
  64. static void target_complete_ok_work(struct work_struct *work);
  65. int init_se_kmem_caches(void)
  66. {
  67. se_sess_cache = kmem_cache_create("se_sess_cache",
  68. sizeof(struct se_session), __alignof__(struct se_session),
  69. 0, NULL);
  70. if (!se_sess_cache) {
  71. pr_err("kmem_cache_create() for struct se_session"
  72. " failed\n");
  73. goto out;
  74. }
  75. se_ua_cache = kmem_cache_create("se_ua_cache",
  76. sizeof(struct se_ua), __alignof__(struct se_ua),
  77. 0, NULL);
  78. if (!se_ua_cache) {
  79. pr_err("kmem_cache_create() for struct se_ua failed\n");
  80. goto out_free_sess_cache;
  81. }
  82. t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
  83. sizeof(struct t10_pr_registration),
  84. __alignof__(struct t10_pr_registration), 0, NULL);
  85. if (!t10_pr_reg_cache) {
  86. pr_err("kmem_cache_create() for struct t10_pr_registration"
  87. " failed\n");
  88. goto out_free_ua_cache;
  89. }
  90. t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
  91. sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
  92. 0, NULL);
  93. if (!t10_alua_lu_gp_cache) {
  94. pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
  95. " failed\n");
  96. goto out_free_pr_reg_cache;
  97. }
  98. t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
  99. sizeof(struct t10_alua_lu_gp_member),
  100. __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
  101. if (!t10_alua_lu_gp_mem_cache) {
  102. pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
  103. "cache failed\n");
  104. goto out_free_lu_gp_cache;
  105. }
  106. t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
  107. sizeof(struct t10_alua_tg_pt_gp),
  108. __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
  109. if (!t10_alua_tg_pt_gp_cache) {
  110. pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
  111. "cache failed\n");
  112. goto out_free_lu_gp_mem_cache;
  113. }
  114. t10_alua_lba_map_cache = kmem_cache_create(
  115. "t10_alua_lba_map_cache",
  116. sizeof(struct t10_alua_lba_map),
  117. __alignof__(struct t10_alua_lba_map), 0, NULL);
  118. if (!t10_alua_lba_map_cache) {
  119. pr_err("kmem_cache_create() for t10_alua_lba_map_"
  120. "cache failed\n");
  121. goto out_free_tg_pt_gp_cache;
  122. }
  123. t10_alua_lba_map_mem_cache = kmem_cache_create(
  124. "t10_alua_lba_map_mem_cache",
  125. sizeof(struct t10_alua_lba_map_member),
  126. __alignof__(struct t10_alua_lba_map_member), 0, NULL);
  127. if (!t10_alua_lba_map_mem_cache) {
  128. pr_err("kmem_cache_create() for t10_alua_lba_map_mem_"
  129. "cache failed\n");
  130. goto out_free_lba_map_cache;
  131. }
  132. target_completion_wq = alloc_workqueue("target_completion",
  133. WQ_MEM_RECLAIM, 0);
  134. if (!target_completion_wq)
  135. goto out_free_lba_map_mem_cache;
  136. return 0;
  137. out_free_lba_map_mem_cache:
  138. kmem_cache_destroy(t10_alua_lba_map_mem_cache);
  139. out_free_lba_map_cache:
  140. kmem_cache_destroy(t10_alua_lba_map_cache);
  141. out_free_tg_pt_gp_cache:
  142. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  143. out_free_lu_gp_mem_cache:
  144. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  145. out_free_lu_gp_cache:
  146. kmem_cache_destroy(t10_alua_lu_gp_cache);
  147. out_free_pr_reg_cache:
  148. kmem_cache_destroy(t10_pr_reg_cache);
  149. out_free_ua_cache:
  150. kmem_cache_destroy(se_ua_cache);
  151. out_free_sess_cache:
  152. kmem_cache_destroy(se_sess_cache);
  153. out:
  154. return -ENOMEM;
  155. }
  156. void release_se_kmem_caches(void)
  157. {
  158. destroy_workqueue(target_completion_wq);
  159. kmem_cache_destroy(se_sess_cache);
  160. kmem_cache_destroy(se_ua_cache);
  161. kmem_cache_destroy(t10_pr_reg_cache);
  162. kmem_cache_destroy(t10_alua_lu_gp_cache);
  163. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  164. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  165. kmem_cache_destroy(t10_alua_lba_map_cache);
  166. kmem_cache_destroy(t10_alua_lba_map_mem_cache);
  167. }
  168. /* This code ensures unique mib indexes are handed out. */
  169. static DEFINE_SPINLOCK(scsi_mib_index_lock);
  170. static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
  171. /*
  172. * Allocate a new row index for the entry type specified
  173. */
  174. u32 scsi_get_new_index(scsi_index_t type)
  175. {
  176. u32 new_index;
  177. BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
  178. spin_lock(&scsi_mib_index_lock);
  179. new_index = ++scsi_mib_index[type];
  180. spin_unlock(&scsi_mib_index_lock);
  181. return new_index;
  182. }
  183. void transport_subsystem_check_init(void)
  184. {
  185. int ret;
  186. static int sub_api_initialized;
  187. if (sub_api_initialized)
  188. return;
  189. ret = request_module("target_core_iblock");
  190. if (ret != 0)
  191. pr_err("Unable to load target_core_iblock\n");
  192. ret = request_module("target_core_file");
  193. if (ret != 0)
  194. pr_err("Unable to load target_core_file\n");
  195. ret = request_module("target_core_pscsi");
  196. if (ret != 0)
  197. pr_err("Unable to load target_core_pscsi\n");
  198. ret = request_module("target_core_user");
  199. if (ret != 0)
  200. pr_err("Unable to load target_core_user\n");
  201. sub_api_initialized = 1;
  202. }
  203. struct se_session *transport_init_session(enum target_prot_op sup_prot_ops)
  204. {
  205. struct se_session *se_sess;
  206. se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
  207. if (!se_sess) {
  208. pr_err("Unable to allocate struct se_session from"
  209. " se_sess_cache\n");
  210. return ERR_PTR(-ENOMEM);
  211. }
  212. INIT_LIST_HEAD(&se_sess->sess_list);
  213. INIT_LIST_HEAD(&se_sess->sess_acl_list);
  214. INIT_LIST_HEAD(&se_sess->sess_cmd_list);
  215. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  216. spin_lock_init(&se_sess->sess_cmd_lock);
  217. se_sess->sup_prot_ops = sup_prot_ops;
  218. return se_sess;
  219. }
  220. EXPORT_SYMBOL(transport_init_session);
  221. int transport_alloc_session_tags(struct se_session *se_sess,
  222. unsigned int tag_num, unsigned int tag_size)
  223. {
  224. int rc;
  225. se_sess->sess_cmd_map = kcalloc(tag_size, tag_num,
  226. GFP_KERNEL | __GFP_NOWARN | __GFP_RETRY_MAYFAIL);
  227. if (!se_sess->sess_cmd_map) {
  228. se_sess->sess_cmd_map = vzalloc(array_size(tag_size, tag_num));
  229. if (!se_sess->sess_cmd_map) {
  230. pr_err("Unable to allocate se_sess->sess_cmd_map\n");
  231. return -ENOMEM;
  232. }
  233. }
  234. rc = percpu_ida_init(&se_sess->sess_tag_pool, tag_num);
  235. if (rc < 0) {
  236. pr_err("Unable to init se_sess->sess_tag_pool,"
  237. " tag_num: %u\n", tag_num);
  238. kvfree(se_sess->sess_cmd_map);
  239. se_sess->sess_cmd_map = NULL;
  240. return -ENOMEM;
  241. }
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(transport_alloc_session_tags);
  245. struct se_session *transport_init_session_tags(unsigned int tag_num,
  246. unsigned int tag_size,
  247. enum target_prot_op sup_prot_ops)
  248. {
  249. struct se_session *se_sess;
  250. int rc;
  251. if (tag_num != 0 && !tag_size) {
  252. pr_err("init_session_tags called with percpu-ida tag_num:"
  253. " %u, but zero tag_size\n", tag_num);
  254. return ERR_PTR(-EINVAL);
  255. }
  256. if (!tag_num && tag_size) {
  257. pr_err("init_session_tags called with percpu-ida tag_size:"
  258. " %u, but zero tag_num\n", tag_size);
  259. return ERR_PTR(-EINVAL);
  260. }
  261. se_sess = transport_init_session(sup_prot_ops);
  262. if (IS_ERR(se_sess))
  263. return se_sess;
  264. rc = transport_alloc_session_tags(se_sess, tag_num, tag_size);
  265. if (rc < 0) {
  266. transport_free_session(se_sess);
  267. return ERR_PTR(-ENOMEM);
  268. }
  269. return se_sess;
  270. }
  271. EXPORT_SYMBOL(transport_init_session_tags);
  272. /*
  273. * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
  274. */
  275. void __transport_register_session(
  276. struct se_portal_group *se_tpg,
  277. struct se_node_acl *se_nacl,
  278. struct se_session *se_sess,
  279. void *fabric_sess_ptr)
  280. {
  281. const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
  282. unsigned char buf[PR_REG_ISID_LEN];
  283. se_sess->se_tpg = se_tpg;
  284. se_sess->fabric_sess_ptr = fabric_sess_ptr;
  285. /*
  286. * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
  287. *
  288. * Only set for struct se_session's that will actually be moving I/O.
  289. * eg: *NOT* discovery sessions.
  290. */
  291. if (se_nacl) {
  292. /*
  293. *
  294. * Determine if fabric allows for T10-PI feature bits exposed to
  295. * initiators for device backends with !dev->dev_attrib.pi_prot_type.
  296. *
  297. * If so, then always save prot_type on a per se_node_acl node
  298. * basis and re-instate the previous sess_prot_type to avoid
  299. * disabling PI from below any previously initiator side
  300. * registered LUNs.
  301. */
  302. if (se_nacl->saved_prot_type)
  303. se_sess->sess_prot_type = se_nacl->saved_prot_type;
  304. else if (tfo->tpg_check_prot_fabric_only)
  305. se_sess->sess_prot_type = se_nacl->saved_prot_type =
  306. tfo->tpg_check_prot_fabric_only(se_tpg);
  307. /*
  308. * If the fabric module supports an ISID based TransportID,
  309. * save this value in binary from the fabric I_T Nexus now.
  310. */
  311. if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
  312. memset(&buf[0], 0, PR_REG_ISID_LEN);
  313. se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
  314. &buf[0], PR_REG_ISID_LEN);
  315. se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
  316. }
  317. spin_lock_irq(&se_nacl->nacl_sess_lock);
  318. /*
  319. * The se_nacl->nacl_sess pointer will be set to the
  320. * last active I_T Nexus for each struct se_node_acl.
  321. */
  322. se_nacl->nacl_sess = se_sess;
  323. list_add_tail(&se_sess->sess_acl_list,
  324. &se_nacl->acl_sess_list);
  325. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  326. }
  327. list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
  328. pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
  329. se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
  330. }
  331. EXPORT_SYMBOL(__transport_register_session);
  332. void transport_register_session(
  333. struct se_portal_group *se_tpg,
  334. struct se_node_acl *se_nacl,
  335. struct se_session *se_sess,
  336. void *fabric_sess_ptr)
  337. {
  338. unsigned long flags;
  339. spin_lock_irqsave(&se_tpg->session_lock, flags);
  340. __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
  341. spin_unlock_irqrestore(&se_tpg->session_lock, flags);
  342. }
  343. EXPORT_SYMBOL(transport_register_session);
  344. struct se_session *
  345. target_alloc_session(struct se_portal_group *tpg,
  346. unsigned int tag_num, unsigned int tag_size,
  347. enum target_prot_op prot_op,
  348. const char *initiatorname, void *private,
  349. int (*callback)(struct se_portal_group *,
  350. struct se_session *, void *))
  351. {
  352. struct se_session *sess;
  353. /*
  354. * If the fabric driver is using percpu-ida based pre allocation
  355. * of I/O descriptor tags, go ahead and perform that setup now..
  356. */
  357. if (tag_num != 0)
  358. sess = transport_init_session_tags(tag_num, tag_size, prot_op);
  359. else
  360. sess = transport_init_session(prot_op);
  361. if (IS_ERR(sess))
  362. return sess;
  363. sess->se_node_acl = core_tpg_check_initiator_node_acl(tpg,
  364. (unsigned char *)initiatorname);
  365. if (!sess->se_node_acl) {
  366. transport_free_session(sess);
  367. return ERR_PTR(-EACCES);
  368. }
  369. /*
  370. * Go ahead and perform any remaining fabric setup that is
  371. * required before transport_register_session().
  372. */
  373. if (callback != NULL) {
  374. int rc = callback(tpg, sess, private);
  375. if (rc) {
  376. transport_free_session(sess);
  377. return ERR_PTR(rc);
  378. }
  379. }
  380. transport_register_session(tpg, sess->se_node_acl, sess, private);
  381. return sess;
  382. }
  383. EXPORT_SYMBOL(target_alloc_session);
  384. ssize_t target_show_dynamic_sessions(struct se_portal_group *se_tpg, char *page)
  385. {
  386. struct se_session *se_sess;
  387. ssize_t len = 0;
  388. spin_lock_bh(&se_tpg->session_lock);
  389. list_for_each_entry(se_sess, &se_tpg->tpg_sess_list, sess_list) {
  390. if (!se_sess->se_node_acl)
  391. continue;
  392. if (!se_sess->se_node_acl->dynamic_node_acl)
  393. continue;
  394. if (strlen(se_sess->se_node_acl->initiatorname) + 1 + len > PAGE_SIZE)
  395. break;
  396. len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
  397. se_sess->se_node_acl->initiatorname);
  398. len += 1; /* Include NULL terminator */
  399. }
  400. spin_unlock_bh(&se_tpg->session_lock);
  401. return len;
  402. }
  403. EXPORT_SYMBOL(target_show_dynamic_sessions);
  404. static void target_complete_nacl(struct kref *kref)
  405. {
  406. struct se_node_acl *nacl = container_of(kref,
  407. struct se_node_acl, acl_kref);
  408. struct se_portal_group *se_tpg = nacl->se_tpg;
  409. if (!nacl->dynamic_stop) {
  410. complete(&nacl->acl_free_comp);
  411. return;
  412. }
  413. mutex_lock(&se_tpg->acl_node_mutex);
  414. list_del_init(&nacl->acl_list);
  415. mutex_unlock(&se_tpg->acl_node_mutex);
  416. core_tpg_wait_for_nacl_pr_ref(nacl);
  417. core_free_device_list_for_node(nacl, se_tpg);
  418. kfree(nacl);
  419. }
  420. void target_put_nacl(struct se_node_acl *nacl)
  421. {
  422. kref_put(&nacl->acl_kref, target_complete_nacl);
  423. }
  424. EXPORT_SYMBOL(target_put_nacl);
  425. void transport_deregister_session_configfs(struct se_session *se_sess)
  426. {
  427. struct se_node_acl *se_nacl;
  428. unsigned long flags;
  429. /*
  430. * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
  431. */
  432. se_nacl = se_sess->se_node_acl;
  433. if (se_nacl) {
  434. spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
  435. if (!list_empty(&se_sess->sess_acl_list))
  436. list_del_init(&se_sess->sess_acl_list);
  437. /*
  438. * If the session list is empty, then clear the pointer.
  439. * Otherwise, set the struct se_session pointer from the tail
  440. * element of the per struct se_node_acl active session list.
  441. */
  442. if (list_empty(&se_nacl->acl_sess_list))
  443. se_nacl->nacl_sess = NULL;
  444. else {
  445. se_nacl->nacl_sess = container_of(
  446. se_nacl->acl_sess_list.prev,
  447. struct se_session, sess_acl_list);
  448. }
  449. spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
  450. }
  451. }
  452. EXPORT_SYMBOL(transport_deregister_session_configfs);
  453. void transport_free_session(struct se_session *se_sess)
  454. {
  455. struct se_node_acl *se_nacl = se_sess->se_node_acl;
  456. /*
  457. * Drop the se_node_acl->nacl_kref obtained from within
  458. * core_tpg_get_initiator_node_acl().
  459. */
  460. if (se_nacl) {
  461. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  462. const struct target_core_fabric_ops *se_tfo = se_tpg->se_tpg_tfo;
  463. unsigned long flags;
  464. se_sess->se_node_acl = NULL;
  465. /*
  466. * Also determine if we need to drop the extra ->cmd_kref if
  467. * it had been previously dynamically generated, and
  468. * the endpoint is not caching dynamic ACLs.
  469. */
  470. mutex_lock(&se_tpg->acl_node_mutex);
  471. if (se_nacl->dynamic_node_acl &&
  472. !se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
  473. spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
  474. if (list_empty(&se_nacl->acl_sess_list))
  475. se_nacl->dynamic_stop = true;
  476. spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
  477. if (se_nacl->dynamic_stop)
  478. list_del_init(&se_nacl->acl_list);
  479. }
  480. mutex_unlock(&se_tpg->acl_node_mutex);
  481. if (se_nacl->dynamic_stop)
  482. target_put_nacl(se_nacl);
  483. target_put_nacl(se_nacl);
  484. }
  485. if (se_sess->sess_cmd_map) {
  486. percpu_ida_destroy(&se_sess->sess_tag_pool);
  487. kvfree(se_sess->sess_cmd_map);
  488. }
  489. kmem_cache_free(se_sess_cache, se_sess);
  490. }
  491. EXPORT_SYMBOL(transport_free_session);
  492. void transport_deregister_session(struct se_session *se_sess)
  493. {
  494. struct se_portal_group *se_tpg = se_sess->se_tpg;
  495. unsigned long flags;
  496. if (!se_tpg) {
  497. transport_free_session(se_sess);
  498. return;
  499. }
  500. spin_lock_irqsave(&se_tpg->session_lock, flags);
  501. list_del(&se_sess->sess_list);
  502. se_sess->se_tpg = NULL;
  503. se_sess->fabric_sess_ptr = NULL;
  504. spin_unlock_irqrestore(&se_tpg->session_lock, flags);
  505. pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
  506. se_tpg->se_tpg_tfo->get_fabric_name());
  507. /*
  508. * If last kref is dropping now for an explicit NodeACL, awake sleeping
  509. * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
  510. * removal context from within transport_free_session() code.
  511. *
  512. * For dynamic ACL, target_put_nacl() uses target_complete_nacl()
  513. * to release all remaining generate_node_acl=1 created ACL resources.
  514. */
  515. transport_free_session(se_sess);
  516. }
  517. EXPORT_SYMBOL(transport_deregister_session);
  518. static void target_remove_from_state_list(struct se_cmd *cmd)
  519. {
  520. struct se_device *dev = cmd->se_dev;
  521. unsigned long flags;
  522. if (!dev)
  523. return;
  524. spin_lock_irqsave(&dev->execute_task_lock, flags);
  525. if (cmd->state_active) {
  526. list_del(&cmd->state_list);
  527. cmd->state_active = false;
  528. }
  529. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  530. }
  531. static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
  532. {
  533. unsigned long flags;
  534. target_remove_from_state_list(cmd);
  535. /*
  536. * Clear struct se_cmd->se_lun before the handoff to FE.
  537. */
  538. cmd->se_lun = NULL;
  539. spin_lock_irqsave(&cmd->t_state_lock, flags);
  540. /*
  541. * Determine if frontend context caller is requesting the stopping of
  542. * this command for frontend exceptions.
  543. */
  544. if (cmd->transport_state & CMD_T_STOP) {
  545. pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n",
  546. __func__, __LINE__, cmd->tag);
  547. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  548. complete_all(&cmd->t_transport_stop_comp);
  549. return 1;
  550. }
  551. cmd->transport_state &= ~CMD_T_ACTIVE;
  552. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  553. /*
  554. * Some fabric modules like tcm_loop can release their internally
  555. * allocated I/O reference and struct se_cmd now.
  556. *
  557. * Fabric modules are expected to return '1' here if the se_cmd being
  558. * passed is released at this point, or zero if not being released.
  559. */
  560. return cmd->se_tfo->check_stop_free(cmd);
  561. }
  562. static void transport_lun_remove_cmd(struct se_cmd *cmd)
  563. {
  564. struct se_lun *lun = cmd->se_lun;
  565. if (!lun)
  566. return;
  567. if (cmpxchg(&cmd->lun_ref_active, true, false))
  568. percpu_ref_put(&lun->lun_ref);
  569. }
  570. int transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
  571. {
  572. bool ack_kref = (cmd->se_cmd_flags & SCF_ACK_KREF);
  573. int ret = 0;
  574. if (cmd->se_cmd_flags & SCF_SE_LUN_CMD)
  575. transport_lun_remove_cmd(cmd);
  576. /*
  577. * Allow the fabric driver to unmap any resources before
  578. * releasing the descriptor via TFO->release_cmd()
  579. */
  580. if (remove)
  581. cmd->se_tfo->aborted_task(cmd);
  582. if (transport_cmd_check_stop_to_fabric(cmd))
  583. return 1;
  584. if (remove && ack_kref)
  585. ret = target_put_sess_cmd(cmd);
  586. return ret;
  587. }
  588. static void target_complete_failure_work(struct work_struct *work)
  589. {
  590. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  591. transport_generic_request_failure(cmd,
  592. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
  593. }
  594. /*
  595. * Used when asking transport to copy Sense Data from the underlying
  596. * Linux/SCSI struct scsi_cmnd
  597. */
  598. static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd)
  599. {
  600. struct se_device *dev = cmd->se_dev;
  601. WARN_ON(!cmd->se_lun);
  602. if (!dev)
  603. return NULL;
  604. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION)
  605. return NULL;
  606. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
  607. pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
  608. dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
  609. return cmd->sense_buffer;
  610. }
  611. void transport_copy_sense_to_cmd(struct se_cmd *cmd, unsigned char *sense)
  612. {
  613. unsigned char *cmd_sense_buf;
  614. unsigned long flags;
  615. spin_lock_irqsave(&cmd->t_state_lock, flags);
  616. cmd_sense_buf = transport_get_sense_buffer(cmd);
  617. if (!cmd_sense_buf) {
  618. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  619. return;
  620. }
  621. cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
  622. memcpy(cmd_sense_buf, sense, cmd->scsi_sense_length);
  623. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  624. }
  625. EXPORT_SYMBOL(transport_copy_sense_to_cmd);
  626. void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
  627. {
  628. struct se_device *dev = cmd->se_dev;
  629. int success;
  630. unsigned long flags;
  631. cmd->scsi_status = scsi_status;
  632. spin_lock_irqsave(&cmd->t_state_lock, flags);
  633. switch (cmd->scsi_status) {
  634. case SAM_STAT_CHECK_CONDITION:
  635. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
  636. success = 1;
  637. else
  638. success = 0;
  639. break;
  640. default:
  641. success = 1;
  642. break;
  643. }
  644. /*
  645. * Check for case where an explicit ABORT_TASK has been received
  646. * and transport_wait_for_tasks() will be waiting for completion..
  647. */
  648. if (cmd->transport_state & CMD_T_ABORTED ||
  649. cmd->transport_state & CMD_T_STOP) {
  650. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  651. /*
  652. * If COMPARE_AND_WRITE was stopped by __transport_wait_for_tasks(),
  653. * release se_device->caw_sem obtained by sbc_compare_and_write()
  654. * since target_complete_ok_work() or target_complete_failure_work()
  655. * won't be called to invoke the normal CAW completion callbacks.
  656. */
  657. if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) {
  658. up(&dev->caw_sem);
  659. }
  660. complete_all(&cmd->t_transport_stop_comp);
  661. return;
  662. } else if (!success) {
  663. INIT_WORK(&cmd->work, target_complete_failure_work);
  664. } else {
  665. INIT_WORK(&cmd->work, target_complete_ok_work);
  666. }
  667. cmd->t_state = TRANSPORT_COMPLETE;
  668. cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
  669. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  670. if (cmd->se_cmd_flags & SCF_USE_CPUID)
  671. queue_work_on(cmd->cpuid, target_completion_wq, &cmd->work);
  672. else
  673. queue_work(target_completion_wq, &cmd->work);
  674. }
  675. EXPORT_SYMBOL(target_complete_cmd);
  676. void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length)
  677. {
  678. if ((scsi_status == SAM_STAT_GOOD ||
  679. cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) &&
  680. length < cmd->data_length) {
  681. if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  682. cmd->residual_count += cmd->data_length - length;
  683. } else {
  684. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  685. cmd->residual_count = cmd->data_length - length;
  686. }
  687. cmd->data_length = length;
  688. }
  689. target_complete_cmd(cmd, scsi_status);
  690. }
  691. EXPORT_SYMBOL(target_complete_cmd_with_length);
  692. static void target_add_to_state_list(struct se_cmd *cmd)
  693. {
  694. struct se_device *dev = cmd->se_dev;
  695. unsigned long flags;
  696. spin_lock_irqsave(&dev->execute_task_lock, flags);
  697. if (!cmd->state_active) {
  698. list_add_tail(&cmd->state_list, &dev->state_list);
  699. cmd->state_active = true;
  700. }
  701. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  702. }
  703. /*
  704. * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
  705. */
  706. static void transport_write_pending_qf(struct se_cmd *cmd);
  707. static void transport_complete_qf(struct se_cmd *cmd);
  708. void target_qf_do_work(struct work_struct *work)
  709. {
  710. struct se_device *dev = container_of(work, struct se_device,
  711. qf_work_queue);
  712. LIST_HEAD(qf_cmd_list);
  713. struct se_cmd *cmd, *cmd_tmp;
  714. spin_lock_irq(&dev->qf_cmd_lock);
  715. list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
  716. spin_unlock_irq(&dev->qf_cmd_lock);
  717. list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
  718. list_del(&cmd->se_qf_node);
  719. atomic_dec_mb(&dev->dev_qf_count);
  720. pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
  721. " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
  722. (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
  723. (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
  724. : "UNKNOWN");
  725. if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
  726. transport_write_pending_qf(cmd);
  727. else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK ||
  728. cmd->t_state == TRANSPORT_COMPLETE_QF_ERR)
  729. transport_complete_qf(cmd);
  730. }
  731. }
  732. unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
  733. {
  734. switch (cmd->data_direction) {
  735. case DMA_NONE:
  736. return "NONE";
  737. case DMA_FROM_DEVICE:
  738. return "READ";
  739. case DMA_TO_DEVICE:
  740. return "WRITE";
  741. case DMA_BIDIRECTIONAL:
  742. return "BIDI";
  743. default:
  744. break;
  745. }
  746. return "UNKNOWN";
  747. }
  748. void transport_dump_dev_state(
  749. struct se_device *dev,
  750. char *b,
  751. int *bl)
  752. {
  753. *bl += sprintf(b + *bl, "Status: ");
  754. if (dev->export_count)
  755. *bl += sprintf(b + *bl, "ACTIVATED");
  756. else
  757. *bl += sprintf(b + *bl, "DEACTIVATED");
  758. *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
  759. *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
  760. dev->dev_attrib.block_size,
  761. dev->dev_attrib.hw_max_sectors);
  762. *bl += sprintf(b + *bl, " ");
  763. }
  764. void transport_dump_vpd_proto_id(
  765. struct t10_vpd *vpd,
  766. unsigned char *p_buf,
  767. int p_buf_len)
  768. {
  769. unsigned char buf[VPD_TMP_BUF_SIZE];
  770. int len;
  771. memset(buf, 0, VPD_TMP_BUF_SIZE);
  772. len = sprintf(buf, "T10 VPD Protocol Identifier: ");
  773. switch (vpd->protocol_identifier) {
  774. case 0x00:
  775. sprintf(buf+len, "Fibre Channel\n");
  776. break;
  777. case 0x10:
  778. sprintf(buf+len, "Parallel SCSI\n");
  779. break;
  780. case 0x20:
  781. sprintf(buf+len, "SSA\n");
  782. break;
  783. case 0x30:
  784. sprintf(buf+len, "IEEE 1394\n");
  785. break;
  786. case 0x40:
  787. sprintf(buf+len, "SCSI Remote Direct Memory Access"
  788. " Protocol\n");
  789. break;
  790. case 0x50:
  791. sprintf(buf+len, "Internet SCSI (iSCSI)\n");
  792. break;
  793. case 0x60:
  794. sprintf(buf+len, "SAS Serial SCSI Protocol\n");
  795. break;
  796. case 0x70:
  797. sprintf(buf+len, "Automation/Drive Interface Transport"
  798. " Protocol\n");
  799. break;
  800. case 0x80:
  801. sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
  802. break;
  803. default:
  804. sprintf(buf+len, "Unknown 0x%02x\n",
  805. vpd->protocol_identifier);
  806. break;
  807. }
  808. if (p_buf)
  809. strncpy(p_buf, buf, p_buf_len);
  810. else
  811. pr_debug("%s", buf);
  812. }
  813. void
  814. transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
  815. {
  816. /*
  817. * Check if the Protocol Identifier Valid (PIV) bit is set..
  818. *
  819. * from spc3r23.pdf section 7.5.1
  820. */
  821. if (page_83[1] & 0x80) {
  822. vpd->protocol_identifier = (page_83[0] & 0xf0);
  823. vpd->protocol_identifier_set = 1;
  824. transport_dump_vpd_proto_id(vpd, NULL, 0);
  825. }
  826. }
  827. EXPORT_SYMBOL(transport_set_vpd_proto_id);
  828. int transport_dump_vpd_assoc(
  829. struct t10_vpd *vpd,
  830. unsigned char *p_buf,
  831. int p_buf_len)
  832. {
  833. unsigned char buf[VPD_TMP_BUF_SIZE];
  834. int ret = 0;
  835. int len;
  836. memset(buf, 0, VPD_TMP_BUF_SIZE);
  837. len = sprintf(buf, "T10 VPD Identifier Association: ");
  838. switch (vpd->association) {
  839. case 0x00:
  840. sprintf(buf+len, "addressed logical unit\n");
  841. break;
  842. case 0x10:
  843. sprintf(buf+len, "target port\n");
  844. break;
  845. case 0x20:
  846. sprintf(buf+len, "SCSI target device\n");
  847. break;
  848. default:
  849. sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
  850. ret = -EINVAL;
  851. break;
  852. }
  853. if (p_buf)
  854. strncpy(p_buf, buf, p_buf_len);
  855. else
  856. pr_debug("%s", buf);
  857. return ret;
  858. }
  859. int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
  860. {
  861. /*
  862. * The VPD identification association..
  863. *
  864. * from spc3r23.pdf Section 7.6.3.1 Table 297
  865. */
  866. vpd->association = (page_83[1] & 0x30);
  867. return transport_dump_vpd_assoc(vpd, NULL, 0);
  868. }
  869. EXPORT_SYMBOL(transport_set_vpd_assoc);
  870. int transport_dump_vpd_ident_type(
  871. struct t10_vpd *vpd,
  872. unsigned char *p_buf,
  873. int p_buf_len)
  874. {
  875. unsigned char buf[VPD_TMP_BUF_SIZE];
  876. int ret = 0;
  877. int len;
  878. memset(buf, 0, VPD_TMP_BUF_SIZE);
  879. len = sprintf(buf, "T10 VPD Identifier Type: ");
  880. switch (vpd->device_identifier_type) {
  881. case 0x00:
  882. sprintf(buf+len, "Vendor specific\n");
  883. break;
  884. case 0x01:
  885. sprintf(buf+len, "T10 Vendor ID based\n");
  886. break;
  887. case 0x02:
  888. sprintf(buf+len, "EUI-64 based\n");
  889. break;
  890. case 0x03:
  891. sprintf(buf+len, "NAA\n");
  892. break;
  893. case 0x04:
  894. sprintf(buf+len, "Relative target port identifier\n");
  895. break;
  896. case 0x08:
  897. sprintf(buf+len, "SCSI name string\n");
  898. break;
  899. default:
  900. sprintf(buf+len, "Unsupported: 0x%02x\n",
  901. vpd->device_identifier_type);
  902. ret = -EINVAL;
  903. break;
  904. }
  905. if (p_buf) {
  906. if (p_buf_len < strlen(buf)+1)
  907. return -EINVAL;
  908. strncpy(p_buf, buf, p_buf_len);
  909. } else {
  910. pr_debug("%s", buf);
  911. }
  912. return ret;
  913. }
  914. int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
  915. {
  916. /*
  917. * The VPD identifier type..
  918. *
  919. * from spc3r23.pdf Section 7.6.3.1 Table 298
  920. */
  921. vpd->device_identifier_type = (page_83[1] & 0x0f);
  922. return transport_dump_vpd_ident_type(vpd, NULL, 0);
  923. }
  924. EXPORT_SYMBOL(transport_set_vpd_ident_type);
  925. int transport_dump_vpd_ident(
  926. struct t10_vpd *vpd,
  927. unsigned char *p_buf,
  928. int p_buf_len)
  929. {
  930. unsigned char buf[VPD_TMP_BUF_SIZE];
  931. int ret = 0;
  932. memset(buf, 0, VPD_TMP_BUF_SIZE);
  933. switch (vpd->device_identifier_code_set) {
  934. case 0x01: /* Binary */
  935. snprintf(buf, sizeof(buf),
  936. "T10 VPD Binary Device Identifier: %s\n",
  937. &vpd->device_identifier[0]);
  938. break;
  939. case 0x02: /* ASCII */
  940. snprintf(buf, sizeof(buf),
  941. "T10 VPD ASCII Device Identifier: %s\n",
  942. &vpd->device_identifier[0]);
  943. break;
  944. case 0x03: /* UTF-8 */
  945. snprintf(buf, sizeof(buf),
  946. "T10 VPD UTF-8 Device Identifier: %s\n",
  947. &vpd->device_identifier[0]);
  948. break;
  949. default:
  950. sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
  951. " 0x%02x", vpd->device_identifier_code_set);
  952. ret = -EINVAL;
  953. break;
  954. }
  955. if (p_buf)
  956. strncpy(p_buf, buf, p_buf_len);
  957. else
  958. pr_debug("%s", buf);
  959. return ret;
  960. }
  961. int
  962. transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
  963. {
  964. static const char hex_str[] = "0123456789abcdef";
  965. int j = 0, i = 4; /* offset to start of the identifier */
  966. /*
  967. * The VPD Code Set (encoding)
  968. *
  969. * from spc3r23.pdf Section 7.6.3.1 Table 296
  970. */
  971. vpd->device_identifier_code_set = (page_83[0] & 0x0f);
  972. switch (vpd->device_identifier_code_set) {
  973. case 0x01: /* Binary */
  974. vpd->device_identifier[j++] =
  975. hex_str[vpd->device_identifier_type];
  976. while (i < (4 + page_83[3])) {
  977. vpd->device_identifier[j++] =
  978. hex_str[(page_83[i] & 0xf0) >> 4];
  979. vpd->device_identifier[j++] =
  980. hex_str[page_83[i] & 0x0f];
  981. i++;
  982. }
  983. break;
  984. case 0x02: /* ASCII */
  985. case 0x03: /* UTF-8 */
  986. while (i < (4 + page_83[3]))
  987. vpd->device_identifier[j++] = page_83[i++];
  988. break;
  989. default:
  990. break;
  991. }
  992. return transport_dump_vpd_ident(vpd, NULL, 0);
  993. }
  994. EXPORT_SYMBOL(transport_set_vpd_ident);
  995. static sense_reason_t
  996. target_check_max_data_sg_nents(struct se_cmd *cmd, struct se_device *dev,
  997. unsigned int size)
  998. {
  999. u32 mtl;
  1000. if (!cmd->se_tfo->max_data_sg_nents)
  1001. return TCM_NO_SENSE;
  1002. /*
  1003. * Check if fabric enforced maximum SGL entries per I/O descriptor
  1004. * exceeds se_cmd->data_length. If true, set SCF_UNDERFLOW_BIT +
  1005. * residual_count and reduce original cmd->data_length to maximum
  1006. * length based on single PAGE_SIZE entry scatter-lists.
  1007. */
  1008. mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE);
  1009. if (cmd->data_length > mtl) {
  1010. /*
  1011. * If an existing CDB overflow is present, calculate new residual
  1012. * based on CDB size minus fabric maximum transfer length.
  1013. *
  1014. * If an existing CDB underflow is present, calculate new residual
  1015. * based on original cmd->data_length minus fabric maximum transfer
  1016. * length.
  1017. *
  1018. * Otherwise, set the underflow residual based on cmd->data_length
  1019. * minus fabric maximum transfer length.
  1020. */
  1021. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  1022. cmd->residual_count = (size - mtl);
  1023. } else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  1024. u32 orig_dl = size + cmd->residual_count;
  1025. cmd->residual_count = (orig_dl - mtl);
  1026. } else {
  1027. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  1028. cmd->residual_count = (cmd->data_length - mtl);
  1029. }
  1030. cmd->data_length = mtl;
  1031. /*
  1032. * Reset sbc_check_prot() calculated protection payload
  1033. * length based upon the new smaller MTL.
  1034. */
  1035. if (cmd->prot_length) {
  1036. u32 sectors = (mtl / dev->dev_attrib.block_size);
  1037. cmd->prot_length = dev->prot_length * sectors;
  1038. }
  1039. }
  1040. return TCM_NO_SENSE;
  1041. }
  1042. sense_reason_t
  1043. target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
  1044. {
  1045. struct se_device *dev = cmd->se_dev;
  1046. if (cmd->unknown_data_length) {
  1047. cmd->data_length = size;
  1048. } else if (size != cmd->data_length) {
  1049. pr_warn_ratelimited("TARGET_CORE[%s]: Expected Transfer Length:"
  1050. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  1051. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  1052. cmd->data_length, size, cmd->t_task_cdb[0]);
  1053. if (cmd->data_direction == DMA_TO_DEVICE) {
  1054. if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
  1055. pr_err_ratelimited("Rejecting underflow/overflow"
  1056. " for WRITE data CDB\n");
  1057. return TCM_INVALID_CDB_FIELD;
  1058. }
  1059. /*
  1060. * Some fabric drivers like iscsi-target still expect to
  1061. * always reject overflow writes. Reject this case until
  1062. * full fabric driver level support for overflow writes
  1063. * is introduced tree-wide.
  1064. */
  1065. if (size > cmd->data_length) {
  1066. pr_err_ratelimited("Rejecting overflow for"
  1067. " WRITE control CDB\n");
  1068. return TCM_INVALID_CDB_FIELD;
  1069. }
  1070. }
  1071. /*
  1072. * Reject READ_* or WRITE_* with overflow/underflow for
  1073. * type SCF_SCSI_DATA_CDB.
  1074. */
  1075. if (dev->dev_attrib.block_size != 512) {
  1076. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  1077. " CDB on non 512-byte sector setup subsystem"
  1078. " plugin: %s\n", dev->transport->name);
  1079. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  1080. return TCM_INVALID_CDB_FIELD;
  1081. }
  1082. /*
  1083. * For the overflow case keep the existing fabric provided
  1084. * ->data_length. Otherwise for the underflow case, reset
  1085. * ->data_length to the smaller SCSI expected data transfer
  1086. * length.
  1087. */
  1088. if (size > cmd->data_length) {
  1089. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  1090. cmd->residual_count = (size - cmd->data_length);
  1091. } else {
  1092. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  1093. cmd->residual_count = (cmd->data_length - size);
  1094. cmd->data_length = size;
  1095. }
  1096. }
  1097. return target_check_max_data_sg_nents(cmd, dev, size);
  1098. }
  1099. /*
  1100. * Used by fabric modules containing a local struct se_cmd within their
  1101. * fabric dependent per I/O descriptor.
  1102. *
  1103. * Preserves the value of @cmd->tag.
  1104. */
  1105. void transport_init_se_cmd(
  1106. struct se_cmd *cmd,
  1107. const struct target_core_fabric_ops *tfo,
  1108. struct se_session *se_sess,
  1109. u32 data_length,
  1110. int data_direction,
  1111. int task_attr,
  1112. unsigned char *sense_buffer)
  1113. {
  1114. INIT_LIST_HEAD(&cmd->se_delayed_node);
  1115. INIT_LIST_HEAD(&cmd->se_qf_node);
  1116. INIT_LIST_HEAD(&cmd->se_cmd_list);
  1117. INIT_LIST_HEAD(&cmd->state_list);
  1118. init_completion(&cmd->t_transport_stop_comp);
  1119. init_completion(&cmd->cmd_wait_comp);
  1120. spin_lock_init(&cmd->t_state_lock);
  1121. INIT_WORK(&cmd->work, NULL);
  1122. kref_init(&cmd->cmd_kref);
  1123. cmd->se_tfo = tfo;
  1124. cmd->se_sess = se_sess;
  1125. cmd->data_length = data_length;
  1126. cmd->data_direction = data_direction;
  1127. cmd->sam_task_attr = task_attr;
  1128. cmd->sense_buffer = sense_buffer;
  1129. cmd->state_active = false;
  1130. }
  1131. EXPORT_SYMBOL(transport_init_se_cmd);
  1132. static sense_reason_t
  1133. transport_check_alloc_task_attr(struct se_cmd *cmd)
  1134. {
  1135. struct se_device *dev = cmd->se_dev;
  1136. /*
  1137. * Check if SAM Task Attribute emulation is enabled for this
  1138. * struct se_device storage object
  1139. */
  1140. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1141. return 0;
  1142. if (cmd->sam_task_attr == TCM_ACA_TAG) {
  1143. pr_debug("SAM Task Attribute ACA"
  1144. " emulation is not supported\n");
  1145. return TCM_INVALID_CDB_FIELD;
  1146. }
  1147. return 0;
  1148. }
  1149. sense_reason_t
  1150. target_setup_cmd_from_cdb(struct se_cmd *cmd, unsigned char *cdb)
  1151. {
  1152. struct se_device *dev = cmd->se_dev;
  1153. sense_reason_t ret;
  1154. /*
  1155. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1156. * for VARIABLE_LENGTH_CMD
  1157. */
  1158. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1159. pr_err("Received SCSI CDB with command_size: %d that"
  1160. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1161. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1162. return TCM_INVALID_CDB_FIELD;
  1163. }
  1164. /*
  1165. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1166. * allocate the additional extended CDB buffer now.. Otherwise
  1167. * setup the pointer from __t_task_cdb to t_task_cdb.
  1168. */
  1169. if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
  1170. cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1171. GFP_KERNEL);
  1172. if (!cmd->t_task_cdb) {
  1173. pr_err("Unable to allocate cmd->t_task_cdb"
  1174. " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
  1175. scsi_command_size(cdb),
  1176. (unsigned long)sizeof(cmd->__t_task_cdb));
  1177. return TCM_OUT_OF_RESOURCES;
  1178. }
  1179. } else
  1180. cmd->t_task_cdb = &cmd->__t_task_cdb[0];
  1181. /*
  1182. * Copy the original CDB into cmd->
  1183. */
  1184. memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
  1185. trace_target_sequencer_start(cmd);
  1186. ret = dev->transport->parse_cdb(cmd);
  1187. if (ret == TCM_UNSUPPORTED_SCSI_OPCODE)
  1188. pr_warn_ratelimited("%s/%s: Unsupported SCSI Opcode 0x%02x, sending CHECK_CONDITION.\n",
  1189. cmd->se_tfo->get_fabric_name(),
  1190. cmd->se_sess->se_node_acl->initiatorname,
  1191. cmd->t_task_cdb[0]);
  1192. if (ret)
  1193. return ret;
  1194. ret = transport_check_alloc_task_attr(cmd);
  1195. if (ret)
  1196. return ret;
  1197. cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1198. atomic_long_inc(&cmd->se_lun->lun_stats.cmd_pdus);
  1199. return 0;
  1200. }
  1201. EXPORT_SYMBOL(target_setup_cmd_from_cdb);
  1202. /*
  1203. * Used by fabric module frontends to queue tasks directly.
  1204. * May only be used from process context.
  1205. */
  1206. int transport_handle_cdb_direct(
  1207. struct se_cmd *cmd)
  1208. {
  1209. sense_reason_t ret;
  1210. if (!cmd->se_lun) {
  1211. dump_stack();
  1212. pr_err("cmd->se_lun is NULL\n");
  1213. return -EINVAL;
  1214. }
  1215. if (in_interrupt()) {
  1216. dump_stack();
  1217. pr_err("transport_generic_handle_cdb cannot be called"
  1218. " from interrupt context\n");
  1219. return -EINVAL;
  1220. }
  1221. /*
  1222. * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
  1223. * outstanding descriptors are handled correctly during shutdown via
  1224. * transport_wait_for_tasks()
  1225. *
  1226. * Also, we don't take cmd->t_state_lock here as we only expect
  1227. * this to be called for initial descriptor submission.
  1228. */
  1229. cmd->t_state = TRANSPORT_NEW_CMD;
  1230. cmd->transport_state |= CMD_T_ACTIVE;
  1231. /*
  1232. * transport_generic_new_cmd() is already handling QUEUE_FULL,
  1233. * so follow TRANSPORT_NEW_CMD processing thread context usage
  1234. * and call transport_generic_request_failure() if necessary..
  1235. */
  1236. ret = transport_generic_new_cmd(cmd);
  1237. if (ret)
  1238. transport_generic_request_failure(cmd, ret);
  1239. return 0;
  1240. }
  1241. EXPORT_SYMBOL(transport_handle_cdb_direct);
  1242. sense_reason_t
  1243. transport_generic_map_mem_to_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
  1244. u32 sgl_count, struct scatterlist *sgl_bidi, u32 sgl_bidi_count)
  1245. {
  1246. if (!sgl || !sgl_count)
  1247. return 0;
  1248. /*
  1249. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  1250. * scatterlists already have been set to follow what the fabric
  1251. * passes for the original expected data transfer length.
  1252. */
  1253. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  1254. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  1255. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  1256. return TCM_INVALID_CDB_FIELD;
  1257. }
  1258. cmd->t_data_sg = sgl;
  1259. cmd->t_data_nents = sgl_count;
  1260. cmd->t_bidi_data_sg = sgl_bidi;
  1261. cmd->t_bidi_data_nents = sgl_bidi_count;
  1262. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  1263. return 0;
  1264. }
  1265. /**
  1266. * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
  1267. * se_cmd + use pre-allocated SGL memory.
  1268. *
  1269. * @se_cmd: command descriptor to submit
  1270. * @se_sess: associated se_sess for endpoint
  1271. * @cdb: pointer to SCSI CDB
  1272. * @sense: pointer to SCSI sense buffer
  1273. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1274. * @data_length: fabric expected data transfer length
  1275. * @task_attr: SAM task attribute
  1276. * @data_dir: DMA data direction
  1277. * @flags: flags for command submission from target_sc_flags_tables
  1278. * @sgl: struct scatterlist memory for unidirectional mapping
  1279. * @sgl_count: scatterlist count for unidirectional mapping
  1280. * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
  1281. * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
  1282. * @sgl_prot: struct scatterlist memory protection information
  1283. * @sgl_prot_count: scatterlist count for protection information
  1284. *
  1285. * Task tags are supported if the caller has set @se_cmd->tag.
  1286. *
  1287. * Returns non zero to signal active I/O shutdown failure. All other
  1288. * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
  1289. * but still return zero here.
  1290. *
  1291. * This may only be called from process context, and also currently
  1292. * assumes internal allocation of fabric payload buffer by target-core.
  1293. */
  1294. int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess,
  1295. unsigned char *cdb, unsigned char *sense, u64 unpacked_lun,
  1296. u32 data_length, int task_attr, int data_dir, int flags,
  1297. struct scatterlist *sgl, u32 sgl_count,
  1298. struct scatterlist *sgl_bidi, u32 sgl_bidi_count,
  1299. struct scatterlist *sgl_prot, u32 sgl_prot_count)
  1300. {
  1301. struct se_portal_group *se_tpg;
  1302. sense_reason_t rc;
  1303. int ret;
  1304. se_tpg = se_sess->se_tpg;
  1305. BUG_ON(!se_tpg);
  1306. BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
  1307. BUG_ON(in_interrupt());
  1308. /*
  1309. * Initialize se_cmd for target operation. From this point
  1310. * exceptions are handled by sending exception status via
  1311. * target_core_fabric_ops->queue_status() callback
  1312. */
  1313. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1314. data_length, data_dir, task_attr, sense);
  1315. if (flags & TARGET_SCF_USE_CPUID)
  1316. se_cmd->se_cmd_flags |= SCF_USE_CPUID;
  1317. else
  1318. se_cmd->cpuid = WORK_CPU_UNBOUND;
  1319. if (flags & TARGET_SCF_UNKNOWN_SIZE)
  1320. se_cmd->unknown_data_length = 1;
  1321. /*
  1322. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1323. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1324. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1325. * kref_put() to happen during fabric packet acknowledgement.
  1326. */
  1327. ret = target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF);
  1328. if (ret)
  1329. return ret;
  1330. /*
  1331. * Signal bidirectional data payloads to target-core
  1332. */
  1333. if (flags & TARGET_SCF_BIDI_OP)
  1334. se_cmd->se_cmd_flags |= SCF_BIDI;
  1335. /*
  1336. * Locate se_lun pointer and attach it to struct se_cmd
  1337. */
  1338. rc = transport_lookup_cmd_lun(se_cmd, unpacked_lun);
  1339. if (rc) {
  1340. transport_send_check_condition_and_sense(se_cmd, rc, 0);
  1341. target_put_sess_cmd(se_cmd);
  1342. return 0;
  1343. }
  1344. rc = target_setup_cmd_from_cdb(se_cmd, cdb);
  1345. if (rc != 0) {
  1346. transport_generic_request_failure(se_cmd, rc);
  1347. return 0;
  1348. }
  1349. /*
  1350. * Save pointers for SGLs containing protection information,
  1351. * if present.
  1352. */
  1353. if (sgl_prot_count) {
  1354. se_cmd->t_prot_sg = sgl_prot;
  1355. se_cmd->t_prot_nents = sgl_prot_count;
  1356. se_cmd->se_cmd_flags |= SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC;
  1357. }
  1358. /*
  1359. * When a non zero sgl_count has been passed perform SGL passthrough
  1360. * mapping for pre-allocated fabric memory instead of having target
  1361. * core perform an internal SGL allocation..
  1362. */
  1363. if (sgl_count != 0) {
  1364. BUG_ON(!sgl);
  1365. /*
  1366. * A work-around for tcm_loop as some userspace code via
  1367. * scsi-generic do not memset their associated read buffers,
  1368. * so go ahead and do that here for type non-data CDBs. Also
  1369. * note that this is currently guaranteed to be a single SGL
  1370. * for this case by target core in target_setup_cmd_from_cdb()
  1371. * -> transport_generic_cmd_sequencer().
  1372. */
  1373. if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
  1374. se_cmd->data_direction == DMA_FROM_DEVICE) {
  1375. unsigned char *buf = NULL;
  1376. if (sgl)
  1377. buf = kmap(sg_page(sgl)) + sgl->offset;
  1378. if (buf) {
  1379. memset(buf, 0, sgl->length);
  1380. kunmap(sg_page(sgl));
  1381. }
  1382. }
  1383. rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count,
  1384. sgl_bidi, sgl_bidi_count);
  1385. if (rc != 0) {
  1386. transport_generic_request_failure(se_cmd, rc);
  1387. return 0;
  1388. }
  1389. }
  1390. /*
  1391. * Check if we need to delay processing because of ALUA
  1392. * Active/NonOptimized primary access state..
  1393. */
  1394. core_alua_check_nonop_delay(se_cmd);
  1395. transport_handle_cdb_direct(se_cmd);
  1396. return 0;
  1397. }
  1398. EXPORT_SYMBOL(target_submit_cmd_map_sgls);
  1399. /**
  1400. * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
  1401. *
  1402. * @se_cmd: command descriptor to submit
  1403. * @se_sess: associated se_sess for endpoint
  1404. * @cdb: pointer to SCSI CDB
  1405. * @sense: pointer to SCSI sense buffer
  1406. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1407. * @data_length: fabric expected data transfer length
  1408. * @task_attr: SAM task attribute
  1409. * @data_dir: DMA data direction
  1410. * @flags: flags for command submission from target_sc_flags_tables
  1411. *
  1412. * Task tags are supported if the caller has set @se_cmd->tag.
  1413. *
  1414. * Returns non zero to signal active I/O shutdown failure. All other
  1415. * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
  1416. * but still return zero here.
  1417. *
  1418. * This may only be called from process context, and also currently
  1419. * assumes internal allocation of fabric payload buffer by target-core.
  1420. *
  1421. * It also assumes interal target core SGL memory allocation.
  1422. */
  1423. int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
  1424. unsigned char *cdb, unsigned char *sense, u64 unpacked_lun,
  1425. u32 data_length, int task_attr, int data_dir, int flags)
  1426. {
  1427. return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense,
  1428. unpacked_lun, data_length, task_attr, data_dir,
  1429. flags, NULL, 0, NULL, 0, NULL, 0);
  1430. }
  1431. EXPORT_SYMBOL(target_submit_cmd);
  1432. static void target_complete_tmr_failure(struct work_struct *work)
  1433. {
  1434. struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
  1435. se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1436. se_cmd->se_tfo->queue_tm_rsp(se_cmd);
  1437. transport_lun_remove_cmd(se_cmd);
  1438. transport_cmd_check_stop_to_fabric(se_cmd);
  1439. }
  1440. static bool target_lookup_lun_from_tag(struct se_session *se_sess, u64 tag,
  1441. u64 *unpacked_lun)
  1442. {
  1443. struct se_cmd *se_cmd;
  1444. unsigned long flags;
  1445. bool ret = false;
  1446. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  1447. list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
  1448. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  1449. continue;
  1450. if (se_cmd->tag == tag) {
  1451. *unpacked_lun = se_cmd->orig_fe_lun;
  1452. ret = true;
  1453. break;
  1454. }
  1455. }
  1456. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  1457. return ret;
  1458. }
  1459. /**
  1460. * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
  1461. * for TMR CDBs
  1462. *
  1463. * @se_cmd: command descriptor to submit
  1464. * @se_sess: associated se_sess for endpoint
  1465. * @sense: pointer to SCSI sense buffer
  1466. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1467. * @fabric_tmr_ptr: fabric context for TMR req
  1468. * @tm_type: Type of TM request
  1469. * @gfp: gfp type for caller
  1470. * @tag: referenced task tag for TMR_ABORT_TASK
  1471. * @flags: submit cmd flags
  1472. *
  1473. * Callable from all contexts.
  1474. **/
  1475. int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
  1476. unsigned char *sense, u64 unpacked_lun,
  1477. void *fabric_tmr_ptr, unsigned char tm_type,
  1478. gfp_t gfp, u64 tag, int flags)
  1479. {
  1480. struct se_portal_group *se_tpg;
  1481. int ret;
  1482. se_tpg = se_sess->se_tpg;
  1483. BUG_ON(!se_tpg);
  1484. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1485. 0, DMA_NONE, TCM_SIMPLE_TAG, sense);
  1486. /*
  1487. * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
  1488. * allocation failure.
  1489. */
  1490. ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
  1491. if (ret < 0)
  1492. return -ENOMEM;
  1493. if (tm_type == TMR_ABORT_TASK)
  1494. se_cmd->se_tmr_req->ref_task_tag = tag;
  1495. /* See target_submit_cmd for commentary */
  1496. ret = target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF);
  1497. if (ret) {
  1498. core_tmr_release_req(se_cmd->se_tmr_req);
  1499. return ret;
  1500. }
  1501. /*
  1502. * If this is ABORT_TASK with no explicit fabric provided LUN,
  1503. * go ahead and search active session tags for a match to figure
  1504. * out unpacked_lun for the original se_cmd.
  1505. */
  1506. if (tm_type == TMR_ABORT_TASK && (flags & TARGET_SCF_LOOKUP_LUN_FROM_TAG)) {
  1507. if (!target_lookup_lun_from_tag(se_sess, tag, &unpacked_lun))
  1508. goto failure;
  1509. }
  1510. ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
  1511. if (ret)
  1512. goto failure;
  1513. transport_generic_handle_tmr(se_cmd);
  1514. return 0;
  1515. /*
  1516. * For callback during failure handling, push this work off
  1517. * to process context with TMR_LUN_DOES_NOT_EXIST status.
  1518. */
  1519. failure:
  1520. INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
  1521. schedule_work(&se_cmd->work);
  1522. return 0;
  1523. }
  1524. EXPORT_SYMBOL(target_submit_tmr);
  1525. /*
  1526. * Handle SAM-esque emulation for generic transport request failures.
  1527. */
  1528. void transport_generic_request_failure(struct se_cmd *cmd,
  1529. sense_reason_t sense_reason)
  1530. {
  1531. int ret = 0, post_ret = 0;
  1532. pr_debug("-----[ Storage Engine Exception; sense_reason %d\n",
  1533. sense_reason);
  1534. target_show_cmd("-----[ ", cmd);
  1535. /*
  1536. * For SAM Task Attribute emulation for failed struct se_cmd
  1537. */
  1538. transport_complete_task_attr(cmd);
  1539. /*
  1540. * Handle special case for COMPARE_AND_WRITE failure, where the
  1541. * callback is expected to drop the per device ->caw_sem.
  1542. */
  1543. if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
  1544. cmd->transport_complete_callback)
  1545. cmd->transport_complete_callback(cmd, false, &post_ret);
  1546. if (transport_check_aborted_status(cmd, 1))
  1547. return;
  1548. switch (sense_reason) {
  1549. case TCM_NON_EXISTENT_LUN:
  1550. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1551. case TCM_INVALID_CDB_FIELD:
  1552. case TCM_INVALID_PARAMETER_LIST:
  1553. case TCM_PARAMETER_LIST_LENGTH_ERROR:
  1554. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1555. case TCM_UNKNOWN_MODE_PAGE:
  1556. case TCM_WRITE_PROTECTED:
  1557. case TCM_ADDRESS_OUT_OF_RANGE:
  1558. case TCM_CHECK_CONDITION_ABORT_CMD:
  1559. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1560. case TCM_CHECK_CONDITION_NOT_READY:
  1561. case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED:
  1562. case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED:
  1563. case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED:
  1564. case TCM_COPY_TARGET_DEVICE_NOT_REACHABLE:
  1565. case TCM_TOO_MANY_TARGET_DESCS:
  1566. case TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE:
  1567. case TCM_TOO_MANY_SEGMENT_DESCS:
  1568. case TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE:
  1569. break;
  1570. case TCM_OUT_OF_RESOURCES:
  1571. cmd->scsi_status = SAM_STAT_TASK_SET_FULL;
  1572. goto queue_status;
  1573. case TCM_LUN_BUSY:
  1574. cmd->scsi_status = SAM_STAT_BUSY;
  1575. goto queue_status;
  1576. case TCM_RESERVATION_CONFLICT:
  1577. /*
  1578. * No SENSE Data payload for this case, set SCSI Status
  1579. * and queue the response to $FABRIC_MOD.
  1580. *
  1581. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1582. */
  1583. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1584. /*
  1585. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1586. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1587. * CONFLICT STATUS.
  1588. *
  1589. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1590. */
  1591. if (cmd->se_sess &&
  1592. cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2) {
  1593. target_ua_allocate_lun(cmd->se_sess->se_node_acl,
  1594. cmd->orig_fe_lun, 0x2C,
  1595. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1596. }
  1597. goto queue_status;
  1598. default:
  1599. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1600. cmd->t_task_cdb[0], sense_reason);
  1601. sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1602. break;
  1603. }
  1604. ret = transport_send_check_condition_and_sense(cmd, sense_reason, 0);
  1605. if (ret)
  1606. goto queue_full;
  1607. check_stop:
  1608. transport_lun_remove_cmd(cmd);
  1609. transport_cmd_check_stop_to_fabric(cmd);
  1610. return;
  1611. queue_status:
  1612. trace_target_cmd_complete(cmd);
  1613. ret = cmd->se_tfo->queue_status(cmd);
  1614. if (!ret)
  1615. goto check_stop;
  1616. queue_full:
  1617. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  1618. }
  1619. EXPORT_SYMBOL(transport_generic_request_failure);
  1620. void __target_execute_cmd(struct se_cmd *cmd, bool do_checks)
  1621. {
  1622. sense_reason_t ret;
  1623. if (!cmd->execute_cmd) {
  1624. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1625. goto err;
  1626. }
  1627. if (do_checks) {
  1628. /*
  1629. * Check for an existing UNIT ATTENTION condition after
  1630. * target_handle_task_attr() has done SAM task attr
  1631. * checking, and possibly have already defered execution
  1632. * out to target_restart_delayed_cmds() context.
  1633. */
  1634. ret = target_scsi3_ua_check(cmd);
  1635. if (ret)
  1636. goto err;
  1637. ret = target_alua_state_check(cmd);
  1638. if (ret)
  1639. goto err;
  1640. ret = target_check_reservation(cmd);
  1641. if (ret) {
  1642. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1643. goto err;
  1644. }
  1645. }
  1646. ret = cmd->execute_cmd(cmd);
  1647. if (!ret)
  1648. return;
  1649. err:
  1650. spin_lock_irq(&cmd->t_state_lock);
  1651. cmd->transport_state &= ~CMD_T_SENT;
  1652. spin_unlock_irq(&cmd->t_state_lock);
  1653. transport_generic_request_failure(cmd, ret);
  1654. }
  1655. static int target_write_prot_action(struct se_cmd *cmd)
  1656. {
  1657. u32 sectors;
  1658. /*
  1659. * Perform WRITE_INSERT of PI using software emulation when backend
  1660. * device has PI enabled, if the transport has not already generated
  1661. * PI using hardware WRITE_INSERT offload.
  1662. */
  1663. switch (cmd->prot_op) {
  1664. case TARGET_PROT_DOUT_INSERT:
  1665. if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_INSERT))
  1666. sbc_dif_generate(cmd);
  1667. break;
  1668. case TARGET_PROT_DOUT_STRIP:
  1669. if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_STRIP)
  1670. break;
  1671. sectors = cmd->data_length >> ilog2(cmd->se_dev->dev_attrib.block_size);
  1672. cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba,
  1673. sectors, 0, cmd->t_prot_sg, 0);
  1674. if (unlikely(cmd->pi_err)) {
  1675. spin_lock_irq(&cmd->t_state_lock);
  1676. cmd->transport_state &= ~CMD_T_SENT;
  1677. spin_unlock_irq(&cmd->t_state_lock);
  1678. transport_generic_request_failure(cmd, cmd->pi_err);
  1679. return -1;
  1680. }
  1681. break;
  1682. default:
  1683. break;
  1684. }
  1685. return 0;
  1686. }
  1687. static bool target_handle_task_attr(struct se_cmd *cmd)
  1688. {
  1689. struct se_device *dev = cmd->se_dev;
  1690. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1691. return false;
  1692. cmd->se_cmd_flags |= SCF_TASK_ATTR_SET;
  1693. /*
  1694. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1695. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1696. */
  1697. switch (cmd->sam_task_attr) {
  1698. case TCM_HEAD_TAG:
  1699. pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x\n",
  1700. cmd->t_task_cdb[0]);
  1701. return false;
  1702. case TCM_ORDERED_TAG:
  1703. atomic_inc_mb(&dev->dev_ordered_sync);
  1704. pr_debug("Added ORDERED for CDB: 0x%02x to ordered list\n",
  1705. cmd->t_task_cdb[0]);
  1706. /*
  1707. * Execute an ORDERED command if no other older commands
  1708. * exist that need to be completed first.
  1709. */
  1710. if (!atomic_read(&dev->simple_cmds))
  1711. return false;
  1712. break;
  1713. default:
  1714. /*
  1715. * For SIMPLE and UNTAGGED Task Attribute commands
  1716. */
  1717. atomic_inc_mb(&dev->simple_cmds);
  1718. break;
  1719. }
  1720. if (atomic_read(&dev->dev_ordered_sync) == 0)
  1721. return false;
  1722. spin_lock(&dev->delayed_cmd_lock);
  1723. list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
  1724. spin_unlock(&dev->delayed_cmd_lock);
  1725. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to delayed CMD listn",
  1726. cmd->t_task_cdb[0], cmd->sam_task_attr);
  1727. return true;
  1728. }
  1729. static int __transport_check_aborted_status(struct se_cmd *, int);
  1730. void target_execute_cmd(struct se_cmd *cmd)
  1731. {
  1732. /*
  1733. * Determine if frontend context caller is requesting the stopping of
  1734. * this command for frontend exceptions.
  1735. *
  1736. * If the received CDB has aleady been aborted stop processing it here.
  1737. */
  1738. spin_lock_irq(&cmd->t_state_lock);
  1739. if (__transport_check_aborted_status(cmd, 1)) {
  1740. spin_unlock_irq(&cmd->t_state_lock);
  1741. return;
  1742. }
  1743. if (cmd->transport_state & CMD_T_STOP) {
  1744. pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n",
  1745. __func__, __LINE__, cmd->tag);
  1746. spin_unlock_irq(&cmd->t_state_lock);
  1747. complete_all(&cmd->t_transport_stop_comp);
  1748. return;
  1749. }
  1750. cmd->t_state = TRANSPORT_PROCESSING;
  1751. cmd->transport_state &= ~CMD_T_PRE_EXECUTE;
  1752. cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT;
  1753. spin_unlock_irq(&cmd->t_state_lock);
  1754. if (target_write_prot_action(cmd))
  1755. return;
  1756. if (target_handle_task_attr(cmd)) {
  1757. spin_lock_irq(&cmd->t_state_lock);
  1758. cmd->transport_state &= ~CMD_T_SENT;
  1759. spin_unlock_irq(&cmd->t_state_lock);
  1760. return;
  1761. }
  1762. __target_execute_cmd(cmd, true);
  1763. }
  1764. EXPORT_SYMBOL(target_execute_cmd);
  1765. /*
  1766. * Process all commands up to the last received ORDERED task attribute which
  1767. * requires another blocking boundary
  1768. */
  1769. static void target_restart_delayed_cmds(struct se_device *dev)
  1770. {
  1771. for (;;) {
  1772. struct se_cmd *cmd;
  1773. spin_lock(&dev->delayed_cmd_lock);
  1774. if (list_empty(&dev->delayed_cmd_list)) {
  1775. spin_unlock(&dev->delayed_cmd_lock);
  1776. break;
  1777. }
  1778. cmd = list_entry(dev->delayed_cmd_list.next,
  1779. struct se_cmd, se_delayed_node);
  1780. list_del(&cmd->se_delayed_node);
  1781. spin_unlock(&dev->delayed_cmd_lock);
  1782. cmd->transport_state |= CMD_T_SENT;
  1783. __target_execute_cmd(cmd, true);
  1784. if (cmd->sam_task_attr == TCM_ORDERED_TAG)
  1785. break;
  1786. }
  1787. }
  1788. /*
  1789. * Called from I/O completion to determine which dormant/delayed
  1790. * and ordered cmds need to have their tasks added to the execution queue.
  1791. */
  1792. static void transport_complete_task_attr(struct se_cmd *cmd)
  1793. {
  1794. struct se_device *dev = cmd->se_dev;
  1795. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1796. return;
  1797. if (!(cmd->se_cmd_flags & SCF_TASK_ATTR_SET))
  1798. goto restart;
  1799. if (cmd->sam_task_attr == TCM_SIMPLE_TAG) {
  1800. atomic_dec_mb(&dev->simple_cmds);
  1801. dev->dev_cur_ordered_id++;
  1802. } else if (cmd->sam_task_attr == TCM_HEAD_TAG) {
  1803. dev->dev_cur_ordered_id++;
  1804. pr_debug("Incremented dev_cur_ordered_id: %u for HEAD_OF_QUEUE\n",
  1805. dev->dev_cur_ordered_id);
  1806. } else if (cmd->sam_task_attr == TCM_ORDERED_TAG) {
  1807. atomic_dec_mb(&dev->dev_ordered_sync);
  1808. dev->dev_cur_ordered_id++;
  1809. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED\n",
  1810. dev->dev_cur_ordered_id);
  1811. }
  1812. cmd->se_cmd_flags &= ~SCF_TASK_ATTR_SET;
  1813. restart:
  1814. target_restart_delayed_cmds(dev);
  1815. }
  1816. static void transport_complete_qf(struct se_cmd *cmd)
  1817. {
  1818. int ret = 0;
  1819. transport_complete_task_attr(cmd);
  1820. /*
  1821. * If a fabric driver ->write_pending() or ->queue_data_in() callback
  1822. * has returned neither -ENOMEM or -EAGAIN, assume it's fatal and
  1823. * the same callbacks should not be retried. Return CHECK_CONDITION
  1824. * if a scsi_status is not already set.
  1825. *
  1826. * If a fabric driver ->queue_status() has returned non zero, always
  1827. * keep retrying no matter what..
  1828. */
  1829. if (cmd->t_state == TRANSPORT_COMPLETE_QF_ERR) {
  1830. if (cmd->scsi_status)
  1831. goto queue_status;
  1832. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  1833. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  1834. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
  1835. translate_sense_reason(cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
  1836. goto queue_status;
  1837. }
  1838. /*
  1839. * Check if we need to send a sense buffer from
  1840. * the struct se_cmd in question. We do NOT want
  1841. * to take this path of the IO has been marked as
  1842. * needing to be treated like a "normal read". This
  1843. * is the case if it's a tape read, and either the
  1844. * FM, EOM, or ILI bits are set, but there is no
  1845. * sense data.
  1846. */
  1847. if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) &&
  1848. cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
  1849. goto queue_status;
  1850. switch (cmd->data_direction) {
  1851. case DMA_FROM_DEVICE:
  1852. /* queue status if not treating this as a normal read */
  1853. if (cmd->scsi_status &&
  1854. !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL))
  1855. goto queue_status;
  1856. trace_target_cmd_complete(cmd);
  1857. ret = cmd->se_tfo->queue_data_in(cmd);
  1858. break;
  1859. case DMA_TO_DEVICE:
  1860. if (cmd->se_cmd_flags & SCF_BIDI) {
  1861. ret = cmd->se_tfo->queue_data_in(cmd);
  1862. break;
  1863. }
  1864. /* fall through */
  1865. case DMA_NONE:
  1866. queue_status:
  1867. trace_target_cmd_complete(cmd);
  1868. ret = cmd->se_tfo->queue_status(cmd);
  1869. break;
  1870. default:
  1871. break;
  1872. }
  1873. if (ret < 0) {
  1874. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  1875. return;
  1876. }
  1877. transport_lun_remove_cmd(cmd);
  1878. transport_cmd_check_stop_to_fabric(cmd);
  1879. }
  1880. static void transport_handle_queue_full(struct se_cmd *cmd, struct se_device *dev,
  1881. int err, bool write_pending)
  1882. {
  1883. /*
  1884. * -EAGAIN or -ENOMEM signals retry of ->write_pending() and/or
  1885. * ->queue_data_in() callbacks from new process context.
  1886. *
  1887. * Otherwise for other errors, transport_complete_qf() will send
  1888. * CHECK_CONDITION via ->queue_status() instead of attempting to
  1889. * retry associated fabric driver data-transfer callbacks.
  1890. */
  1891. if (err == -EAGAIN || err == -ENOMEM) {
  1892. cmd->t_state = (write_pending) ? TRANSPORT_COMPLETE_QF_WP :
  1893. TRANSPORT_COMPLETE_QF_OK;
  1894. } else {
  1895. pr_warn_ratelimited("Got unknown fabric queue status: %d\n", err);
  1896. cmd->t_state = TRANSPORT_COMPLETE_QF_ERR;
  1897. }
  1898. spin_lock_irq(&dev->qf_cmd_lock);
  1899. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  1900. atomic_inc_mb(&dev->dev_qf_count);
  1901. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  1902. schedule_work(&cmd->se_dev->qf_work_queue);
  1903. }
  1904. static bool target_read_prot_action(struct se_cmd *cmd)
  1905. {
  1906. switch (cmd->prot_op) {
  1907. case TARGET_PROT_DIN_STRIP:
  1908. if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_STRIP)) {
  1909. u32 sectors = cmd->data_length >>
  1910. ilog2(cmd->se_dev->dev_attrib.block_size);
  1911. cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba,
  1912. sectors, 0, cmd->t_prot_sg,
  1913. 0);
  1914. if (cmd->pi_err)
  1915. return true;
  1916. }
  1917. break;
  1918. case TARGET_PROT_DIN_INSERT:
  1919. if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_INSERT)
  1920. break;
  1921. sbc_dif_generate(cmd);
  1922. break;
  1923. default:
  1924. break;
  1925. }
  1926. return false;
  1927. }
  1928. static void target_complete_ok_work(struct work_struct *work)
  1929. {
  1930. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  1931. int ret;
  1932. /*
  1933. * Check if we need to move delayed/dormant tasks from cmds on the
  1934. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  1935. * Attribute.
  1936. */
  1937. transport_complete_task_attr(cmd);
  1938. /*
  1939. * Check to schedule QUEUE_FULL work, or execute an existing
  1940. * cmd->transport_qf_callback()
  1941. */
  1942. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  1943. schedule_work(&cmd->se_dev->qf_work_queue);
  1944. /*
  1945. * Check if we need to send a sense buffer from
  1946. * the struct se_cmd in question. We do NOT want
  1947. * to take this path of the IO has been marked as
  1948. * needing to be treated like a "normal read". This
  1949. * is the case if it's a tape read, and either the
  1950. * FM, EOM, or ILI bits are set, but there is no
  1951. * sense data.
  1952. */
  1953. if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) &&
  1954. cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  1955. WARN_ON(!cmd->scsi_status);
  1956. ret = transport_send_check_condition_and_sense(
  1957. cmd, 0, 1);
  1958. if (ret)
  1959. goto queue_full;
  1960. transport_lun_remove_cmd(cmd);
  1961. transport_cmd_check_stop_to_fabric(cmd);
  1962. return;
  1963. }
  1964. /*
  1965. * Check for a callback, used by amongst other things
  1966. * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation.
  1967. */
  1968. if (cmd->transport_complete_callback) {
  1969. sense_reason_t rc;
  1970. bool caw = (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE);
  1971. bool zero_dl = !(cmd->data_length);
  1972. int post_ret = 0;
  1973. rc = cmd->transport_complete_callback(cmd, true, &post_ret);
  1974. if (!rc && !post_ret) {
  1975. if (caw && zero_dl)
  1976. goto queue_rsp;
  1977. return;
  1978. } else if (rc) {
  1979. ret = transport_send_check_condition_and_sense(cmd,
  1980. rc, 0);
  1981. if (ret)
  1982. goto queue_full;
  1983. transport_lun_remove_cmd(cmd);
  1984. transport_cmd_check_stop_to_fabric(cmd);
  1985. return;
  1986. }
  1987. }
  1988. queue_rsp:
  1989. switch (cmd->data_direction) {
  1990. case DMA_FROM_DEVICE:
  1991. /*
  1992. * if this is a READ-type IO, but SCSI status
  1993. * is set, then skip returning data and just
  1994. * return the status -- unless this IO is marked
  1995. * as needing to be treated as a normal read,
  1996. * in which case we want to go ahead and return
  1997. * the data. This happens, for example, for tape
  1998. * reads with the FM, EOM, or ILI bits set, with
  1999. * no sense data.
  2000. */
  2001. if (cmd->scsi_status &&
  2002. !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL))
  2003. goto queue_status;
  2004. atomic_long_add(cmd->data_length,
  2005. &cmd->se_lun->lun_stats.tx_data_octets);
  2006. /*
  2007. * Perform READ_STRIP of PI using software emulation when
  2008. * backend had PI enabled, if the transport will not be
  2009. * performing hardware READ_STRIP offload.
  2010. */
  2011. if (target_read_prot_action(cmd)) {
  2012. ret = transport_send_check_condition_and_sense(cmd,
  2013. cmd->pi_err, 0);
  2014. if (ret)
  2015. goto queue_full;
  2016. transport_lun_remove_cmd(cmd);
  2017. transport_cmd_check_stop_to_fabric(cmd);
  2018. return;
  2019. }
  2020. trace_target_cmd_complete(cmd);
  2021. ret = cmd->se_tfo->queue_data_in(cmd);
  2022. if (ret)
  2023. goto queue_full;
  2024. break;
  2025. case DMA_TO_DEVICE:
  2026. atomic_long_add(cmd->data_length,
  2027. &cmd->se_lun->lun_stats.rx_data_octets);
  2028. /*
  2029. * Check if we need to send READ payload for BIDI-COMMAND
  2030. */
  2031. if (cmd->se_cmd_flags & SCF_BIDI) {
  2032. atomic_long_add(cmd->data_length,
  2033. &cmd->se_lun->lun_stats.tx_data_octets);
  2034. ret = cmd->se_tfo->queue_data_in(cmd);
  2035. if (ret)
  2036. goto queue_full;
  2037. break;
  2038. }
  2039. /* fall through */
  2040. case DMA_NONE:
  2041. queue_status:
  2042. trace_target_cmd_complete(cmd);
  2043. ret = cmd->se_tfo->queue_status(cmd);
  2044. if (ret)
  2045. goto queue_full;
  2046. break;
  2047. default:
  2048. break;
  2049. }
  2050. transport_lun_remove_cmd(cmd);
  2051. transport_cmd_check_stop_to_fabric(cmd);
  2052. return;
  2053. queue_full:
  2054. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  2055. " data_direction: %d\n", cmd, cmd->data_direction);
  2056. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  2057. }
  2058. void target_free_sgl(struct scatterlist *sgl, int nents)
  2059. {
  2060. sgl_free_n_order(sgl, nents, 0);
  2061. }
  2062. EXPORT_SYMBOL(target_free_sgl);
  2063. static inline void transport_reset_sgl_orig(struct se_cmd *cmd)
  2064. {
  2065. /*
  2066. * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
  2067. * emulation, and free + reset pointers if necessary..
  2068. */
  2069. if (!cmd->t_data_sg_orig)
  2070. return;
  2071. kfree(cmd->t_data_sg);
  2072. cmd->t_data_sg = cmd->t_data_sg_orig;
  2073. cmd->t_data_sg_orig = NULL;
  2074. cmd->t_data_nents = cmd->t_data_nents_orig;
  2075. cmd->t_data_nents_orig = 0;
  2076. }
  2077. static inline void transport_free_pages(struct se_cmd *cmd)
  2078. {
  2079. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) {
  2080. target_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents);
  2081. cmd->t_prot_sg = NULL;
  2082. cmd->t_prot_nents = 0;
  2083. }
  2084. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) {
  2085. /*
  2086. * Release special case READ buffer payload required for
  2087. * SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE
  2088. */
  2089. if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) {
  2090. target_free_sgl(cmd->t_bidi_data_sg,
  2091. cmd->t_bidi_data_nents);
  2092. cmd->t_bidi_data_sg = NULL;
  2093. cmd->t_bidi_data_nents = 0;
  2094. }
  2095. transport_reset_sgl_orig(cmd);
  2096. return;
  2097. }
  2098. transport_reset_sgl_orig(cmd);
  2099. target_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  2100. cmd->t_data_sg = NULL;
  2101. cmd->t_data_nents = 0;
  2102. target_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  2103. cmd->t_bidi_data_sg = NULL;
  2104. cmd->t_bidi_data_nents = 0;
  2105. }
  2106. void *transport_kmap_data_sg(struct se_cmd *cmd)
  2107. {
  2108. struct scatterlist *sg = cmd->t_data_sg;
  2109. struct page **pages;
  2110. int i;
  2111. /*
  2112. * We need to take into account a possible offset here for fabrics like
  2113. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  2114. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  2115. */
  2116. if (!cmd->t_data_nents)
  2117. return NULL;
  2118. BUG_ON(!sg);
  2119. if (cmd->t_data_nents == 1)
  2120. return kmap(sg_page(sg)) + sg->offset;
  2121. /* >1 page. use vmap */
  2122. pages = kmalloc_array(cmd->t_data_nents, sizeof(*pages), GFP_KERNEL);
  2123. if (!pages)
  2124. return NULL;
  2125. /* convert sg[] to pages[] */
  2126. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  2127. pages[i] = sg_page(sg);
  2128. }
  2129. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  2130. kfree(pages);
  2131. if (!cmd->t_data_vmap)
  2132. return NULL;
  2133. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  2134. }
  2135. EXPORT_SYMBOL(transport_kmap_data_sg);
  2136. void transport_kunmap_data_sg(struct se_cmd *cmd)
  2137. {
  2138. if (!cmd->t_data_nents) {
  2139. return;
  2140. } else if (cmd->t_data_nents == 1) {
  2141. kunmap(sg_page(cmd->t_data_sg));
  2142. return;
  2143. }
  2144. vunmap(cmd->t_data_vmap);
  2145. cmd->t_data_vmap = NULL;
  2146. }
  2147. EXPORT_SYMBOL(transport_kunmap_data_sg);
  2148. int
  2149. target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length,
  2150. bool zero_page, bool chainable)
  2151. {
  2152. gfp_t gfp = GFP_KERNEL | (zero_page ? __GFP_ZERO : 0);
  2153. *sgl = sgl_alloc_order(length, 0, chainable, gfp, nents);
  2154. return *sgl ? 0 : -ENOMEM;
  2155. }
  2156. EXPORT_SYMBOL(target_alloc_sgl);
  2157. /*
  2158. * Allocate any required resources to execute the command. For writes we
  2159. * might not have the payload yet, so notify the fabric via a call to
  2160. * ->write_pending instead. Otherwise place it on the execution queue.
  2161. */
  2162. sense_reason_t
  2163. transport_generic_new_cmd(struct se_cmd *cmd)
  2164. {
  2165. unsigned long flags;
  2166. int ret = 0;
  2167. bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB);
  2168. if (cmd->prot_op != TARGET_PROT_NORMAL &&
  2169. !(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) {
  2170. ret = target_alloc_sgl(&cmd->t_prot_sg, &cmd->t_prot_nents,
  2171. cmd->prot_length, true, false);
  2172. if (ret < 0)
  2173. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2174. }
  2175. /*
  2176. * Determine is the TCM fabric module has already allocated physical
  2177. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  2178. * beforehand.
  2179. */
  2180. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  2181. cmd->data_length) {
  2182. if ((cmd->se_cmd_flags & SCF_BIDI) ||
  2183. (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) {
  2184. u32 bidi_length;
  2185. if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)
  2186. bidi_length = cmd->t_task_nolb *
  2187. cmd->se_dev->dev_attrib.block_size;
  2188. else
  2189. bidi_length = cmd->data_length;
  2190. ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
  2191. &cmd->t_bidi_data_nents,
  2192. bidi_length, zero_flag, false);
  2193. if (ret < 0)
  2194. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2195. }
  2196. ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
  2197. cmd->data_length, zero_flag, false);
  2198. if (ret < 0)
  2199. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2200. } else if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
  2201. cmd->data_length) {
  2202. /*
  2203. * Special case for COMPARE_AND_WRITE with fabrics
  2204. * using SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC.
  2205. */
  2206. u32 caw_length = cmd->t_task_nolb *
  2207. cmd->se_dev->dev_attrib.block_size;
  2208. ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
  2209. &cmd->t_bidi_data_nents,
  2210. caw_length, zero_flag, false);
  2211. if (ret < 0)
  2212. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2213. }
  2214. /*
  2215. * If this command is not a write we can execute it right here,
  2216. * for write buffers we need to notify the fabric driver first
  2217. * and let it call back once the write buffers are ready.
  2218. */
  2219. target_add_to_state_list(cmd);
  2220. if (cmd->data_direction != DMA_TO_DEVICE || cmd->data_length == 0) {
  2221. target_execute_cmd(cmd);
  2222. return 0;
  2223. }
  2224. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2225. cmd->t_state = TRANSPORT_WRITE_PENDING;
  2226. /*
  2227. * Determine if frontend context caller is requesting the stopping of
  2228. * this command for frontend exceptions.
  2229. */
  2230. if (cmd->transport_state & CMD_T_STOP) {
  2231. pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n",
  2232. __func__, __LINE__, cmd->tag);
  2233. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2234. complete_all(&cmd->t_transport_stop_comp);
  2235. return 0;
  2236. }
  2237. cmd->transport_state &= ~CMD_T_ACTIVE;
  2238. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2239. ret = cmd->se_tfo->write_pending(cmd);
  2240. if (ret)
  2241. goto queue_full;
  2242. return 0;
  2243. queue_full:
  2244. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  2245. transport_handle_queue_full(cmd, cmd->se_dev, ret, true);
  2246. return 0;
  2247. }
  2248. EXPORT_SYMBOL(transport_generic_new_cmd);
  2249. static void transport_write_pending_qf(struct se_cmd *cmd)
  2250. {
  2251. unsigned long flags;
  2252. int ret;
  2253. bool stop;
  2254. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2255. stop = (cmd->transport_state & (CMD_T_STOP | CMD_T_ABORTED));
  2256. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2257. if (stop) {
  2258. pr_debug("%s:%d CMD_T_STOP|CMD_T_ABORTED for ITT: 0x%08llx\n",
  2259. __func__, __LINE__, cmd->tag);
  2260. complete_all(&cmd->t_transport_stop_comp);
  2261. return;
  2262. }
  2263. ret = cmd->se_tfo->write_pending(cmd);
  2264. if (ret) {
  2265. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  2266. cmd);
  2267. transport_handle_queue_full(cmd, cmd->se_dev, ret, true);
  2268. }
  2269. }
  2270. static bool
  2271. __transport_wait_for_tasks(struct se_cmd *, bool, bool *, bool *,
  2272. unsigned long *flags);
  2273. static void target_wait_free_cmd(struct se_cmd *cmd, bool *aborted, bool *tas)
  2274. {
  2275. unsigned long flags;
  2276. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2277. __transport_wait_for_tasks(cmd, true, aborted, tas, &flags);
  2278. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2279. }
  2280. int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  2281. {
  2282. int ret = 0;
  2283. bool aborted = false, tas = false;
  2284. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  2285. if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  2286. target_wait_free_cmd(cmd, &aborted, &tas);
  2287. if (!aborted || tas)
  2288. ret = target_put_sess_cmd(cmd);
  2289. } else {
  2290. if (wait_for_tasks)
  2291. target_wait_free_cmd(cmd, &aborted, &tas);
  2292. /*
  2293. * Handle WRITE failure case where transport_generic_new_cmd()
  2294. * has already added se_cmd to state_list, but fabric has
  2295. * failed command before I/O submission.
  2296. */
  2297. if (cmd->state_active)
  2298. target_remove_from_state_list(cmd);
  2299. if (cmd->se_lun)
  2300. transport_lun_remove_cmd(cmd);
  2301. if (!aborted || tas)
  2302. ret = target_put_sess_cmd(cmd);
  2303. }
  2304. /*
  2305. * If the task has been internally aborted due to TMR ABORT_TASK
  2306. * or LUN_RESET, target_core_tmr.c is responsible for performing
  2307. * the remaining calls to target_put_sess_cmd(), and not the
  2308. * callers of this function.
  2309. */
  2310. if (aborted) {
  2311. pr_debug("Detected CMD_T_ABORTED for ITT: %llu\n", cmd->tag);
  2312. wait_for_completion(&cmd->cmd_wait_comp);
  2313. cmd->se_tfo->release_cmd(cmd);
  2314. ret = 1;
  2315. }
  2316. return ret;
  2317. }
  2318. EXPORT_SYMBOL(transport_generic_free_cmd);
  2319. /**
  2320. * target_get_sess_cmd - Add command to active ->sess_cmd_list
  2321. * @se_cmd: command descriptor to add
  2322. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  2323. */
  2324. int target_get_sess_cmd(struct se_cmd *se_cmd, bool ack_kref)
  2325. {
  2326. struct se_session *se_sess = se_cmd->se_sess;
  2327. unsigned long flags;
  2328. int ret = 0;
  2329. /*
  2330. * Add a second kref if the fabric caller is expecting to handle
  2331. * fabric acknowledgement that requires two target_put_sess_cmd()
  2332. * invocations before se_cmd descriptor release.
  2333. */
  2334. if (ack_kref) {
  2335. if (!kref_get_unless_zero(&se_cmd->cmd_kref))
  2336. return -EINVAL;
  2337. se_cmd->se_cmd_flags |= SCF_ACK_KREF;
  2338. }
  2339. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  2340. if (se_sess->sess_tearing_down) {
  2341. ret = -ESHUTDOWN;
  2342. goto out;
  2343. }
  2344. se_cmd->transport_state |= CMD_T_PRE_EXECUTE;
  2345. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  2346. out:
  2347. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2348. if (ret && ack_kref)
  2349. target_put_sess_cmd(se_cmd);
  2350. return ret;
  2351. }
  2352. EXPORT_SYMBOL(target_get_sess_cmd);
  2353. static void target_free_cmd_mem(struct se_cmd *cmd)
  2354. {
  2355. transport_free_pages(cmd);
  2356. if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  2357. core_tmr_release_req(cmd->se_tmr_req);
  2358. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  2359. kfree(cmd->t_task_cdb);
  2360. }
  2361. static void target_release_cmd_kref(struct kref *kref)
  2362. {
  2363. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  2364. struct se_session *se_sess = se_cmd->se_sess;
  2365. unsigned long flags;
  2366. bool fabric_stop;
  2367. if (se_sess) {
  2368. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  2369. spin_lock(&se_cmd->t_state_lock);
  2370. fabric_stop = (se_cmd->transport_state & CMD_T_FABRIC_STOP) &&
  2371. (se_cmd->transport_state & CMD_T_ABORTED);
  2372. spin_unlock(&se_cmd->t_state_lock);
  2373. if (se_cmd->cmd_wait_set || fabric_stop) {
  2374. list_del_init(&se_cmd->se_cmd_list);
  2375. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2376. target_free_cmd_mem(se_cmd);
  2377. complete(&se_cmd->cmd_wait_comp);
  2378. return;
  2379. }
  2380. list_del_init(&se_cmd->se_cmd_list);
  2381. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2382. }
  2383. target_free_cmd_mem(se_cmd);
  2384. se_cmd->se_tfo->release_cmd(se_cmd);
  2385. }
  2386. /**
  2387. * target_put_sess_cmd - decrease the command reference count
  2388. * @se_cmd: command to drop a reference from
  2389. *
  2390. * Returns 1 if and only if this target_put_sess_cmd() call caused the
  2391. * refcount to drop to zero. Returns zero otherwise.
  2392. */
  2393. int target_put_sess_cmd(struct se_cmd *se_cmd)
  2394. {
  2395. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  2396. }
  2397. EXPORT_SYMBOL(target_put_sess_cmd);
  2398. static const char *data_dir_name(enum dma_data_direction d)
  2399. {
  2400. switch (d) {
  2401. case DMA_BIDIRECTIONAL: return "BIDI";
  2402. case DMA_TO_DEVICE: return "WRITE";
  2403. case DMA_FROM_DEVICE: return "READ";
  2404. case DMA_NONE: return "NONE";
  2405. }
  2406. return "(?)";
  2407. }
  2408. static const char *cmd_state_name(enum transport_state_table t)
  2409. {
  2410. switch (t) {
  2411. case TRANSPORT_NO_STATE: return "NO_STATE";
  2412. case TRANSPORT_NEW_CMD: return "NEW_CMD";
  2413. case TRANSPORT_WRITE_PENDING: return "WRITE_PENDING";
  2414. case TRANSPORT_PROCESSING: return "PROCESSING";
  2415. case TRANSPORT_COMPLETE: return "COMPLETE";
  2416. case TRANSPORT_ISTATE_PROCESSING:
  2417. return "ISTATE_PROCESSING";
  2418. case TRANSPORT_COMPLETE_QF_WP: return "COMPLETE_QF_WP";
  2419. case TRANSPORT_COMPLETE_QF_OK: return "COMPLETE_QF_OK";
  2420. case TRANSPORT_COMPLETE_QF_ERR: return "COMPLETE_QF_ERR";
  2421. }
  2422. return "(?)";
  2423. }
  2424. static void target_append_str(char **str, const char *txt)
  2425. {
  2426. char *prev = *str;
  2427. *str = *str ? kasprintf(GFP_ATOMIC, "%s,%s", *str, txt) :
  2428. kstrdup(txt, GFP_ATOMIC);
  2429. kfree(prev);
  2430. }
  2431. /*
  2432. * Convert a transport state bitmask into a string. The caller is
  2433. * responsible for freeing the returned pointer.
  2434. */
  2435. static char *target_ts_to_str(u32 ts)
  2436. {
  2437. char *str = NULL;
  2438. if (ts & CMD_T_ABORTED)
  2439. target_append_str(&str, "aborted");
  2440. if (ts & CMD_T_ACTIVE)
  2441. target_append_str(&str, "active");
  2442. if (ts & CMD_T_COMPLETE)
  2443. target_append_str(&str, "complete");
  2444. if (ts & CMD_T_SENT)
  2445. target_append_str(&str, "sent");
  2446. if (ts & CMD_T_STOP)
  2447. target_append_str(&str, "stop");
  2448. if (ts & CMD_T_FABRIC_STOP)
  2449. target_append_str(&str, "fabric_stop");
  2450. return str;
  2451. }
  2452. static const char *target_tmf_name(enum tcm_tmreq_table tmf)
  2453. {
  2454. switch (tmf) {
  2455. case TMR_ABORT_TASK: return "ABORT_TASK";
  2456. case TMR_ABORT_TASK_SET: return "ABORT_TASK_SET";
  2457. case TMR_CLEAR_ACA: return "CLEAR_ACA";
  2458. case TMR_CLEAR_TASK_SET: return "CLEAR_TASK_SET";
  2459. case TMR_LUN_RESET: return "LUN_RESET";
  2460. case TMR_TARGET_WARM_RESET: return "TARGET_WARM_RESET";
  2461. case TMR_TARGET_COLD_RESET: return "TARGET_COLD_RESET";
  2462. case TMR_UNKNOWN: break;
  2463. }
  2464. return "(?)";
  2465. }
  2466. void target_show_cmd(const char *pfx, struct se_cmd *cmd)
  2467. {
  2468. char *ts_str = target_ts_to_str(cmd->transport_state);
  2469. const u8 *cdb = cmd->t_task_cdb;
  2470. struct se_tmr_req *tmf = cmd->se_tmr_req;
  2471. if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  2472. pr_debug("%scmd %#02x:%#02x with tag %#llx dir %s i_state %d t_state %s len %d refcnt %d transport_state %s\n",
  2473. pfx, cdb[0], cdb[1], cmd->tag,
  2474. data_dir_name(cmd->data_direction),
  2475. cmd->se_tfo->get_cmd_state(cmd),
  2476. cmd_state_name(cmd->t_state), cmd->data_length,
  2477. kref_read(&cmd->cmd_kref), ts_str);
  2478. } else {
  2479. pr_debug("%stmf %s with tag %#llx ref_task_tag %#llx i_state %d t_state %s refcnt %d transport_state %s\n",
  2480. pfx, target_tmf_name(tmf->function), cmd->tag,
  2481. tmf->ref_task_tag, cmd->se_tfo->get_cmd_state(cmd),
  2482. cmd_state_name(cmd->t_state),
  2483. kref_read(&cmd->cmd_kref), ts_str);
  2484. }
  2485. kfree(ts_str);
  2486. }
  2487. EXPORT_SYMBOL(target_show_cmd);
  2488. /**
  2489. * target_sess_cmd_list_set_waiting - Flag all commands in
  2490. * sess_cmd_list to complete cmd_wait_comp. Set
  2491. * sess_tearing_down so no more commands are queued.
  2492. * @se_sess: session to flag
  2493. */
  2494. void target_sess_cmd_list_set_waiting(struct se_session *se_sess)
  2495. {
  2496. struct se_cmd *se_cmd, *tmp_cmd;
  2497. unsigned long flags;
  2498. int rc;
  2499. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  2500. if (se_sess->sess_tearing_down) {
  2501. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2502. return;
  2503. }
  2504. se_sess->sess_tearing_down = 1;
  2505. list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
  2506. list_for_each_entry_safe(se_cmd, tmp_cmd,
  2507. &se_sess->sess_wait_list, se_cmd_list) {
  2508. rc = kref_get_unless_zero(&se_cmd->cmd_kref);
  2509. if (rc) {
  2510. se_cmd->cmd_wait_set = 1;
  2511. spin_lock(&se_cmd->t_state_lock);
  2512. se_cmd->transport_state |= CMD_T_FABRIC_STOP;
  2513. spin_unlock(&se_cmd->t_state_lock);
  2514. } else
  2515. list_del_init(&se_cmd->se_cmd_list);
  2516. }
  2517. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2518. }
  2519. EXPORT_SYMBOL(target_sess_cmd_list_set_waiting);
  2520. /**
  2521. * target_wait_for_sess_cmds - Wait for outstanding descriptors
  2522. * @se_sess: session to wait for active I/O
  2523. */
  2524. void target_wait_for_sess_cmds(struct se_session *se_sess)
  2525. {
  2526. struct se_cmd *se_cmd, *tmp_cmd;
  2527. unsigned long flags;
  2528. bool tas;
  2529. list_for_each_entry_safe(se_cmd, tmp_cmd,
  2530. &se_sess->sess_wait_list, se_cmd_list) {
  2531. pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
  2532. " %d\n", se_cmd, se_cmd->t_state,
  2533. se_cmd->se_tfo->get_cmd_state(se_cmd));
  2534. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  2535. tas = (se_cmd->transport_state & CMD_T_TAS);
  2536. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  2537. if (!target_put_sess_cmd(se_cmd)) {
  2538. if (tas)
  2539. target_put_sess_cmd(se_cmd);
  2540. }
  2541. wait_for_completion(&se_cmd->cmd_wait_comp);
  2542. pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
  2543. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  2544. se_cmd->se_tfo->get_cmd_state(se_cmd));
  2545. se_cmd->se_tfo->release_cmd(se_cmd);
  2546. }
  2547. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  2548. WARN_ON(!list_empty(&se_sess->sess_cmd_list));
  2549. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2550. }
  2551. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  2552. static void target_lun_confirm(struct percpu_ref *ref)
  2553. {
  2554. struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
  2555. complete(&lun->lun_ref_comp);
  2556. }
  2557. void transport_clear_lun_ref(struct se_lun *lun)
  2558. {
  2559. /*
  2560. * Mark the percpu-ref as DEAD, switch to atomic_t mode, drop
  2561. * the initial reference and schedule confirm kill to be
  2562. * executed after one full RCU grace period has completed.
  2563. */
  2564. percpu_ref_kill_and_confirm(&lun->lun_ref, target_lun_confirm);
  2565. /*
  2566. * The first completion waits for percpu_ref_switch_to_atomic_rcu()
  2567. * to call target_lun_confirm after lun->lun_ref has been marked
  2568. * as __PERCPU_REF_DEAD on all CPUs, and switches to atomic_t
  2569. * mode so that percpu_ref_tryget_live() lookup of lun->lun_ref
  2570. * fails for all new incoming I/O.
  2571. */
  2572. wait_for_completion(&lun->lun_ref_comp);
  2573. /*
  2574. * The second completion waits for percpu_ref_put_many() to
  2575. * invoke ->release() after lun->lun_ref has switched to
  2576. * atomic_t mode, and lun->lun_ref.count has reached zero.
  2577. *
  2578. * At this point all target-core lun->lun_ref references have
  2579. * been dropped via transport_lun_remove_cmd(), and it's safe
  2580. * to proceed with the remaining LUN shutdown.
  2581. */
  2582. wait_for_completion(&lun->lun_shutdown_comp);
  2583. }
  2584. static bool
  2585. __transport_wait_for_tasks(struct se_cmd *cmd, bool fabric_stop,
  2586. bool *aborted, bool *tas, unsigned long *flags)
  2587. __releases(&cmd->t_state_lock)
  2588. __acquires(&cmd->t_state_lock)
  2589. {
  2590. assert_spin_locked(&cmd->t_state_lock);
  2591. WARN_ON_ONCE(!irqs_disabled());
  2592. if (fabric_stop)
  2593. cmd->transport_state |= CMD_T_FABRIC_STOP;
  2594. if (cmd->transport_state & CMD_T_ABORTED)
  2595. *aborted = true;
  2596. if (cmd->transport_state & CMD_T_TAS)
  2597. *tas = true;
  2598. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
  2599. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  2600. return false;
  2601. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
  2602. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  2603. return false;
  2604. if (!(cmd->transport_state & CMD_T_ACTIVE))
  2605. return false;
  2606. if (fabric_stop && *aborted)
  2607. return false;
  2608. cmd->transport_state |= CMD_T_STOP;
  2609. target_show_cmd("wait_for_tasks: Stopping ", cmd);
  2610. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  2611. while (!wait_for_completion_timeout(&cmd->t_transport_stop_comp,
  2612. 180 * HZ))
  2613. target_show_cmd("wait for tasks: ", cmd);
  2614. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  2615. cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
  2616. pr_debug("wait_for_tasks: Stopped wait_for_completion(&cmd->"
  2617. "t_transport_stop_comp) for ITT: 0x%08llx\n", cmd->tag);
  2618. return true;
  2619. }
  2620. /**
  2621. * transport_wait_for_tasks - set CMD_T_STOP and wait for t_transport_stop_comp
  2622. * @cmd: command to wait on
  2623. */
  2624. bool transport_wait_for_tasks(struct se_cmd *cmd)
  2625. {
  2626. unsigned long flags;
  2627. bool ret, aborted = false, tas = false;
  2628. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2629. ret = __transport_wait_for_tasks(cmd, false, &aborted, &tas, &flags);
  2630. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2631. return ret;
  2632. }
  2633. EXPORT_SYMBOL(transport_wait_for_tasks);
  2634. struct sense_info {
  2635. u8 key;
  2636. u8 asc;
  2637. u8 ascq;
  2638. bool add_sector_info;
  2639. };
  2640. static const struct sense_info sense_info_table[] = {
  2641. [TCM_NO_SENSE] = {
  2642. .key = NOT_READY
  2643. },
  2644. [TCM_NON_EXISTENT_LUN] = {
  2645. .key = ILLEGAL_REQUEST,
  2646. .asc = 0x25 /* LOGICAL UNIT NOT SUPPORTED */
  2647. },
  2648. [TCM_UNSUPPORTED_SCSI_OPCODE] = {
  2649. .key = ILLEGAL_REQUEST,
  2650. .asc = 0x20, /* INVALID COMMAND OPERATION CODE */
  2651. },
  2652. [TCM_SECTOR_COUNT_TOO_MANY] = {
  2653. .key = ILLEGAL_REQUEST,
  2654. .asc = 0x20, /* INVALID COMMAND OPERATION CODE */
  2655. },
  2656. [TCM_UNKNOWN_MODE_PAGE] = {
  2657. .key = ILLEGAL_REQUEST,
  2658. .asc = 0x24, /* INVALID FIELD IN CDB */
  2659. },
  2660. [TCM_CHECK_CONDITION_ABORT_CMD] = {
  2661. .key = ABORTED_COMMAND,
  2662. .asc = 0x29, /* BUS DEVICE RESET FUNCTION OCCURRED */
  2663. .ascq = 0x03,
  2664. },
  2665. [TCM_INCORRECT_AMOUNT_OF_DATA] = {
  2666. .key = ABORTED_COMMAND,
  2667. .asc = 0x0c, /* WRITE ERROR */
  2668. .ascq = 0x0d, /* NOT ENOUGH UNSOLICITED DATA */
  2669. },
  2670. [TCM_INVALID_CDB_FIELD] = {
  2671. .key = ILLEGAL_REQUEST,
  2672. .asc = 0x24, /* INVALID FIELD IN CDB */
  2673. },
  2674. [TCM_INVALID_PARAMETER_LIST] = {
  2675. .key = ILLEGAL_REQUEST,
  2676. .asc = 0x26, /* INVALID FIELD IN PARAMETER LIST */
  2677. },
  2678. [TCM_TOO_MANY_TARGET_DESCS] = {
  2679. .key = ILLEGAL_REQUEST,
  2680. .asc = 0x26,
  2681. .ascq = 0x06, /* TOO MANY TARGET DESCRIPTORS */
  2682. },
  2683. [TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE] = {
  2684. .key = ILLEGAL_REQUEST,
  2685. .asc = 0x26,
  2686. .ascq = 0x07, /* UNSUPPORTED TARGET DESCRIPTOR TYPE CODE */
  2687. },
  2688. [TCM_TOO_MANY_SEGMENT_DESCS] = {
  2689. .key = ILLEGAL_REQUEST,
  2690. .asc = 0x26,
  2691. .ascq = 0x08, /* TOO MANY SEGMENT DESCRIPTORS */
  2692. },
  2693. [TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE] = {
  2694. .key = ILLEGAL_REQUEST,
  2695. .asc = 0x26,
  2696. .ascq = 0x09, /* UNSUPPORTED SEGMENT DESCRIPTOR TYPE CODE */
  2697. },
  2698. [TCM_PARAMETER_LIST_LENGTH_ERROR] = {
  2699. .key = ILLEGAL_REQUEST,
  2700. .asc = 0x1a, /* PARAMETER LIST LENGTH ERROR */
  2701. },
  2702. [TCM_UNEXPECTED_UNSOLICITED_DATA] = {
  2703. .key = ILLEGAL_REQUEST,
  2704. .asc = 0x0c, /* WRITE ERROR */
  2705. .ascq = 0x0c, /* UNEXPECTED_UNSOLICITED_DATA */
  2706. },
  2707. [TCM_SERVICE_CRC_ERROR] = {
  2708. .key = ABORTED_COMMAND,
  2709. .asc = 0x47, /* PROTOCOL SERVICE CRC ERROR */
  2710. .ascq = 0x05, /* N/A */
  2711. },
  2712. [TCM_SNACK_REJECTED] = {
  2713. .key = ABORTED_COMMAND,
  2714. .asc = 0x11, /* READ ERROR */
  2715. .ascq = 0x13, /* FAILED RETRANSMISSION REQUEST */
  2716. },
  2717. [TCM_WRITE_PROTECTED] = {
  2718. .key = DATA_PROTECT,
  2719. .asc = 0x27, /* WRITE PROTECTED */
  2720. },
  2721. [TCM_ADDRESS_OUT_OF_RANGE] = {
  2722. .key = ILLEGAL_REQUEST,
  2723. .asc = 0x21, /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
  2724. },
  2725. [TCM_CHECK_CONDITION_UNIT_ATTENTION] = {
  2726. .key = UNIT_ATTENTION,
  2727. },
  2728. [TCM_CHECK_CONDITION_NOT_READY] = {
  2729. .key = NOT_READY,
  2730. },
  2731. [TCM_MISCOMPARE_VERIFY] = {
  2732. .key = MISCOMPARE,
  2733. .asc = 0x1d, /* MISCOMPARE DURING VERIFY OPERATION */
  2734. .ascq = 0x00,
  2735. },
  2736. [TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED] = {
  2737. .key = ABORTED_COMMAND,
  2738. .asc = 0x10,
  2739. .ascq = 0x01, /* LOGICAL BLOCK GUARD CHECK FAILED */
  2740. .add_sector_info = true,
  2741. },
  2742. [TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED] = {
  2743. .key = ABORTED_COMMAND,
  2744. .asc = 0x10,
  2745. .ascq = 0x02, /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */
  2746. .add_sector_info = true,
  2747. },
  2748. [TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED] = {
  2749. .key = ABORTED_COMMAND,
  2750. .asc = 0x10,
  2751. .ascq = 0x03, /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */
  2752. .add_sector_info = true,
  2753. },
  2754. [TCM_COPY_TARGET_DEVICE_NOT_REACHABLE] = {
  2755. .key = COPY_ABORTED,
  2756. .asc = 0x0d,
  2757. .ascq = 0x02, /* COPY TARGET DEVICE NOT REACHABLE */
  2758. },
  2759. [TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE] = {
  2760. /*
  2761. * Returning ILLEGAL REQUEST would cause immediate IO errors on
  2762. * Solaris initiators. Returning NOT READY instead means the
  2763. * operations will be retried a finite number of times and we
  2764. * can survive intermittent errors.
  2765. */
  2766. .key = NOT_READY,
  2767. .asc = 0x08, /* LOGICAL UNIT COMMUNICATION FAILURE */
  2768. },
  2769. [TCM_INSUFFICIENT_REGISTRATION_RESOURCES] = {
  2770. /*
  2771. * From spc4r22 section5.7.7,5.7.8
  2772. * If a PERSISTENT RESERVE OUT command with a REGISTER service action
  2773. * or a REGISTER AND IGNORE EXISTING KEY service action or
  2774. * REGISTER AND MOVE service actionis attempted,
  2775. * but there are insufficient device server resources to complete the
  2776. * operation, then the command shall be terminated with CHECK CONDITION
  2777. * status, with the sense key set to ILLEGAL REQUEST,and the additonal
  2778. * sense code set to INSUFFICIENT REGISTRATION RESOURCES.
  2779. */
  2780. .key = ILLEGAL_REQUEST,
  2781. .asc = 0x55,
  2782. .ascq = 0x04, /* INSUFFICIENT REGISTRATION RESOURCES */
  2783. },
  2784. };
  2785. static int translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason)
  2786. {
  2787. const struct sense_info *si;
  2788. u8 *buffer = cmd->sense_buffer;
  2789. int r = (__force int)reason;
  2790. u8 asc, ascq;
  2791. bool desc_format = target_sense_desc_format(cmd->se_dev);
  2792. if (r < ARRAY_SIZE(sense_info_table) && sense_info_table[r].key)
  2793. si = &sense_info_table[r];
  2794. else
  2795. si = &sense_info_table[(__force int)
  2796. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE];
  2797. if (reason == TCM_CHECK_CONDITION_UNIT_ATTENTION) {
  2798. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  2799. WARN_ON_ONCE(asc == 0);
  2800. } else if (si->asc == 0) {
  2801. WARN_ON_ONCE(cmd->scsi_asc == 0);
  2802. asc = cmd->scsi_asc;
  2803. ascq = cmd->scsi_ascq;
  2804. } else {
  2805. asc = si->asc;
  2806. ascq = si->ascq;
  2807. }
  2808. scsi_build_sense_buffer(desc_format, buffer, si->key, asc, ascq);
  2809. if (si->add_sector_info)
  2810. return scsi_set_sense_information(buffer,
  2811. cmd->scsi_sense_length,
  2812. cmd->bad_sector);
  2813. return 0;
  2814. }
  2815. int
  2816. transport_send_check_condition_and_sense(struct se_cmd *cmd,
  2817. sense_reason_t reason, int from_transport)
  2818. {
  2819. unsigned long flags;
  2820. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2821. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2822. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2823. return 0;
  2824. }
  2825. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  2826. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2827. if (!from_transport) {
  2828. int rc;
  2829. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  2830. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  2831. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
  2832. rc = translate_sense_reason(cmd, reason);
  2833. if (rc)
  2834. return rc;
  2835. }
  2836. trace_target_cmd_complete(cmd);
  2837. return cmd->se_tfo->queue_status(cmd);
  2838. }
  2839. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  2840. static int __transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  2841. __releases(&cmd->t_state_lock)
  2842. __acquires(&cmd->t_state_lock)
  2843. {
  2844. int ret;
  2845. assert_spin_locked(&cmd->t_state_lock);
  2846. WARN_ON_ONCE(!irqs_disabled());
  2847. if (!(cmd->transport_state & CMD_T_ABORTED))
  2848. return 0;
  2849. /*
  2850. * If cmd has been aborted but either no status is to be sent or it has
  2851. * already been sent, just return
  2852. */
  2853. if (!send_status || !(cmd->se_cmd_flags & SCF_SEND_DELAYED_TAS)) {
  2854. if (send_status)
  2855. cmd->se_cmd_flags |= SCF_SEND_DELAYED_TAS;
  2856. return 1;
  2857. }
  2858. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB:"
  2859. " 0x%02x ITT: 0x%08llx\n", cmd->t_task_cdb[0], cmd->tag);
  2860. cmd->se_cmd_flags &= ~SCF_SEND_DELAYED_TAS;
  2861. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  2862. trace_target_cmd_complete(cmd);
  2863. spin_unlock_irq(&cmd->t_state_lock);
  2864. ret = cmd->se_tfo->queue_status(cmd);
  2865. if (ret)
  2866. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  2867. spin_lock_irq(&cmd->t_state_lock);
  2868. return 1;
  2869. }
  2870. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  2871. {
  2872. int ret;
  2873. spin_lock_irq(&cmd->t_state_lock);
  2874. ret = __transport_check_aborted_status(cmd, send_status);
  2875. spin_unlock_irq(&cmd->t_state_lock);
  2876. return ret;
  2877. }
  2878. EXPORT_SYMBOL(transport_check_aborted_status);
  2879. void transport_send_task_abort(struct se_cmd *cmd)
  2880. {
  2881. unsigned long flags;
  2882. int ret;
  2883. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2884. if (cmd->se_cmd_flags & (SCF_SENT_CHECK_CONDITION)) {
  2885. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2886. return;
  2887. }
  2888. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2889. /*
  2890. * If there are still expected incoming fabric WRITEs, we wait
  2891. * until until they have completed before sending a TASK_ABORTED
  2892. * response. This response with TASK_ABORTED status will be
  2893. * queued back to fabric module by transport_check_aborted_status().
  2894. */
  2895. if (cmd->data_direction == DMA_TO_DEVICE) {
  2896. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  2897. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2898. if (cmd->se_cmd_flags & SCF_SEND_DELAYED_TAS) {
  2899. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2900. goto send_abort;
  2901. }
  2902. cmd->se_cmd_flags |= SCF_SEND_DELAYED_TAS;
  2903. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2904. return;
  2905. }
  2906. }
  2907. send_abort:
  2908. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  2909. transport_lun_remove_cmd(cmd);
  2910. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x, ITT: 0x%08llx\n",
  2911. cmd->t_task_cdb[0], cmd->tag);
  2912. trace_target_cmd_complete(cmd);
  2913. ret = cmd->se_tfo->queue_status(cmd);
  2914. if (ret)
  2915. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  2916. }
  2917. static void target_tmr_work(struct work_struct *work)
  2918. {
  2919. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2920. struct se_device *dev = cmd->se_dev;
  2921. struct se_tmr_req *tmr = cmd->se_tmr_req;
  2922. unsigned long flags;
  2923. int ret;
  2924. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2925. if (cmd->transport_state & CMD_T_ABORTED) {
  2926. tmr->response = TMR_FUNCTION_REJECTED;
  2927. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2928. goto check_stop;
  2929. }
  2930. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2931. switch (tmr->function) {
  2932. case TMR_ABORT_TASK:
  2933. core_tmr_abort_task(dev, tmr, cmd->se_sess);
  2934. break;
  2935. case TMR_ABORT_TASK_SET:
  2936. case TMR_CLEAR_ACA:
  2937. case TMR_CLEAR_TASK_SET:
  2938. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  2939. break;
  2940. case TMR_LUN_RESET:
  2941. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  2942. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  2943. TMR_FUNCTION_REJECTED;
  2944. if (tmr->response == TMR_FUNCTION_COMPLETE) {
  2945. target_ua_allocate_lun(cmd->se_sess->se_node_acl,
  2946. cmd->orig_fe_lun, 0x29,
  2947. ASCQ_29H_BUS_DEVICE_RESET_FUNCTION_OCCURRED);
  2948. }
  2949. break;
  2950. case TMR_TARGET_WARM_RESET:
  2951. tmr->response = TMR_FUNCTION_REJECTED;
  2952. break;
  2953. case TMR_TARGET_COLD_RESET:
  2954. tmr->response = TMR_FUNCTION_REJECTED;
  2955. break;
  2956. default:
  2957. pr_err("Unknown TMR function: 0x%02x.\n",
  2958. tmr->function);
  2959. tmr->response = TMR_FUNCTION_REJECTED;
  2960. break;
  2961. }
  2962. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2963. if (cmd->transport_state & CMD_T_ABORTED) {
  2964. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2965. goto check_stop;
  2966. }
  2967. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2968. cmd->se_tfo->queue_tm_rsp(cmd);
  2969. check_stop:
  2970. transport_lun_remove_cmd(cmd);
  2971. transport_cmd_check_stop_to_fabric(cmd);
  2972. }
  2973. int transport_generic_handle_tmr(
  2974. struct se_cmd *cmd)
  2975. {
  2976. unsigned long flags;
  2977. bool aborted = false;
  2978. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2979. if (cmd->transport_state & CMD_T_ABORTED) {
  2980. aborted = true;
  2981. } else {
  2982. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  2983. cmd->transport_state |= CMD_T_ACTIVE;
  2984. }
  2985. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2986. if (aborted) {
  2987. pr_warn_ratelimited("handle_tmr caught CMD_T_ABORTED TMR %d"
  2988. "ref_tag: %llu tag: %llu\n", cmd->se_tmr_req->function,
  2989. cmd->se_tmr_req->ref_task_tag, cmd->tag);
  2990. transport_lun_remove_cmd(cmd);
  2991. transport_cmd_check_stop_to_fabric(cmd);
  2992. return 0;
  2993. }
  2994. INIT_WORK(&cmd->work, target_tmr_work);
  2995. queue_work(cmd->se_dev->tmr_wq, &cmd->work);
  2996. return 0;
  2997. }
  2998. EXPORT_SYMBOL(transport_generic_handle_tmr);
  2999. bool
  3000. target_check_wce(struct se_device *dev)
  3001. {
  3002. bool wce = false;
  3003. if (dev->transport->get_write_cache)
  3004. wce = dev->transport->get_write_cache(dev);
  3005. else if (dev->dev_attrib.emulate_write_cache > 0)
  3006. wce = true;
  3007. return wce;
  3008. }
  3009. bool
  3010. target_check_fua(struct se_device *dev)
  3011. {
  3012. return target_check_wce(dev) && dev->dev_attrib.emulate_fua_write > 0;
  3013. }