target_core_transport.c 95 KB

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