target_core_transport.c 85 KB

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