target_core_transport.c 90 KB

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