target_core_transport.c 84 KB

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