target_core_transport.c 81 KB

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