target_core_transport.c 77 KB

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