target_core_transport.c 77 KB

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