target_core_transport.c 88 KB

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