target_core_transport.c 86 KB

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