target_core_transport.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477
  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_set_cmd_data_length(struct se_cmd *cmd, int length)
  736. {
  737. if (length < cmd->data_length) {
  738. if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  739. cmd->residual_count += cmd->data_length - length;
  740. } else {
  741. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  742. cmd->residual_count = cmd->data_length - length;
  743. }
  744. cmd->data_length = length;
  745. }
  746. }
  747. EXPORT_SYMBOL(target_set_cmd_data_length);
  748. void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length)
  749. {
  750. if (scsi_status == SAM_STAT_GOOD ||
  751. cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) {
  752. target_set_cmd_data_length(cmd, length);
  753. }
  754. target_complete_cmd(cmd, scsi_status);
  755. }
  756. EXPORT_SYMBOL(target_complete_cmd_with_length);
  757. static void target_add_to_state_list(struct se_cmd *cmd)
  758. {
  759. struct se_device *dev = cmd->se_dev;
  760. unsigned long flags;
  761. spin_lock_irqsave(&dev->execute_task_lock, flags);
  762. if (!cmd->state_active) {
  763. list_add_tail(&cmd->state_list, &dev->state_list);
  764. cmd->state_active = true;
  765. }
  766. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  767. }
  768. /*
  769. * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
  770. */
  771. static void transport_write_pending_qf(struct se_cmd *cmd);
  772. static void transport_complete_qf(struct se_cmd *cmd);
  773. void target_qf_do_work(struct work_struct *work)
  774. {
  775. struct se_device *dev = container_of(work, struct se_device,
  776. qf_work_queue);
  777. LIST_HEAD(qf_cmd_list);
  778. struct se_cmd *cmd, *cmd_tmp;
  779. spin_lock_irq(&dev->qf_cmd_lock);
  780. list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
  781. spin_unlock_irq(&dev->qf_cmd_lock);
  782. list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
  783. list_del(&cmd->se_qf_node);
  784. atomic_dec_mb(&dev->dev_qf_count);
  785. pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
  786. " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
  787. (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
  788. (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
  789. : "UNKNOWN");
  790. if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
  791. transport_write_pending_qf(cmd);
  792. else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK ||
  793. cmd->t_state == TRANSPORT_COMPLETE_QF_ERR)
  794. transport_complete_qf(cmd);
  795. }
  796. }
  797. unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
  798. {
  799. switch (cmd->data_direction) {
  800. case DMA_NONE:
  801. return "NONE";
  802. case DMA_FROM_DEVICE:
  803. return "READ";
  804. case DMA_TO_DEVICE:
  805. return "WRITE";
  806. case DMA_BIDIRECTIONAL:
  807. return "BIDI";
  808. default:
  809. break;
  810. }
  811. return "UNKNOWN";
  812. }
  813. void transport_dump_dev_state(
  814. struct se_device *dev,
  815. char *b,
  816. int *bl)
  817. {
  818. *bl += sprintf(b + *bl, "Status: ");
  819. if (dev->export_count)
  820. *bl += sprintf(b + *bl, "ACTIVATED");
  821. else
  822. *bl += sprintf(b + *bl, "DEACTIVATED");
  823. *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
  824. *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
  825. dev->dev_attrib.block_size,
  826. dev->dev_attrib.hw_max_sectors);
  827. *bl += sprintf(b + *bl, " ");
  828. }
  829. void transport_dump_vpd_proto_id(
  830. struct t10_vpd *vpd,
  831. unsigned char *p_buf,
  832. int p_buf_len)
  833. {
  834. unsigned char buf[VPD_TMP_BUF_SIZE];
  835. int len;
  836. memset(buf, 0, VPD_TMP_BUF_SIZE);
  837. len = sprintf(buf, "T10 VPD Protocol Identifier: ");
  838. switch (vpd->protocol_identifier) {
  839. case 0x00:
  840. sprintf(buf+len, "Fibre Channel\n");
  841. break;
  842. case 0x10:
  843. sprintf(buf+len, "Parallel SCSI\n");
  844. break;
  845. case 0x20:
  846. sprintf(buf+len, "SSA\n");
  847. break;
  848. case 0x30:
  849. sprintf(buf+len, "IEEE 1394\n");
  850. break;
  851. case 0x40:
  852. sprintf(buf+len, "SCSI Remote Direct Memory Access"
  853. " Protocol\n");
  854. break;
  855. case 0x50:
  856. sprintf(buf+len, "Internet SCSI (iSCSI)\n");
  857. break;
  858. case 0x60:
  859. sprintf(buf+len, "SAS Serial SCSI Protocol\n");
  860. break;
  861. case 0x70:
  862. sprintf(buf+len, "Automation/Drive Interface Transport"
  863. " Protocol\n");
  864. break;
  865. case 0x80:
  866. sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
  867. break;
  868. default:
  869. sprintf(buf+len, "Unknown 0x%02x\n",
  870. vpd->protocol_identifier);
  871. break;
  872. }
  873. if (p_buf)
  874. strncpy(p_buf, buf, p_buf_len);
  875. else
  876. pr_debug("%s", buf);
  877. }
  878. void
  879. transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
  880. {
  881. /*
  882. * Check if the Protocol Identifier Valid (PIV) bit is set..
  883. *
  884. * from spc3r23.pdf section 7.5.1
  885. */
  886. if (page_83[1] & 0x80) {
  887. vpd->protocol_identifier = (page_83[0] & 0xf0);
  888. vpd->protocol_identifier_set = 1;
  889. transport_dump_vpd_proto_id(vpd, NULL, 0);
  890. }
  891. }
  892. EXPORT_SYMBOL(transport_set_vpd_proto_id);
  893. int transport_dump_vpd_assoc(
  894. struct t10_vpd *vpd,
  895. unsigned char *p_buf,
  896. int p_buf_len)
  897. {
  898. unsigned char buf[VPD_TMP_BUF_SIZE];
  899. int ret = 0;
  900. int len;
  901. memset(buf, 0, VPD_TMP_BUF_SIZE);
  902. len = sprintf(buf, "T10 VPD Identifier Association: ");
  903. switch (vpd->association) {
  904. case 0x00:
  905. sprintf(buf+len, "addressed logical unit\n");
  906. break;
  907. case 0x10:
  908. sprintf(buf+len, "target port\n");
  909. break;
  910. case 0x20:
  911. sprintf(buf+len, "SCSI target device\n");
  912. break;
  913. default:
  914. sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
  915. ret = -EINVAL;
  916. break;
  917. }
  918. if (p_buf)
  919. strncpy(p_buf, buf, p_buf_len);
  920. else
  921. pr_debug("%s", buf);
  922. return ret;
  923. }
  924. int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
  925. {
  926. /*
  927. * The VPD identification association..
  928. *
  929. * from spc3r23.pdf Section 7.6.3.1 Table 297
  930. */
  931. vpd->association = (page_83[1] & 0x30);
  932. return transport_dump_vpd_assoc(vpd, NULL, 0);
  933. }
  934. EXPORT_SYMBOL(transport_set_vpd_assoc);
  935. int transport_dump_vpd_ident_type(
  936. struct t10_vpd *vpd,
  937. unsigned char *p_buf,
  938. int p_buf_len)
  939. {
  940. unsigned char buf[VPD_TMP_BUF_SIZE];
  941. int ret = 0;
  942. int len;
  943. memset(buf, 0, VPD_TMP_BUF_SIZE);
  944. len = sprintf(buf, "T10 VPD Identifier Type: ");
  945. switch (vpd->device_identifier_type) {
  946. case 0x00:
  947. sprintf(buf+len, "Vendor specific\n");
  948. break;
  949. case 0x01:
  950. sprintf(buf+len, "T10 Vendor ID based\n");
  951. break;
  952. case 0x02:
  953. sprintf(buf+len, "EUI-64 based\n");
  954. break;
  955. case 0x03:
  956. sprintf(buf+len, "NAA\n");
  957. break;
  958. case 0x04:
  959. sprintf(buf+len, "Relative target port identifier\n");
  960. break;
  961. case 0x08:
  962. sprintf(buf+len, "SCSI name string\n");
  963. break;
  964. default:
  965. sprintf(buf+len, "Unsupported: 0x%02x\n",
  966. vpd->device_identifier_type);
  967. ret = -EINVAL;
  968. break;
  969. }
  970. if (p_buf) {
  971. if (p_buf_len < strlen(buf)+1)
  972. return -EINVAL;
  973. strncpy(p_buf, buf, p_buf_len);
  974. } else {
  975. pr_debug("%s", buf);
  976. }
  977. return ret;
  978. }
  979. int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
  980. {
  981. /*
  982. * The VPD identifier type..
  983. *
  984. * from spc3r23.pdf Section 7.6.3.1 Table 298
  985. */
  986. vpd->device_identifier_type = (page_83[1] & 0x0f);
  987. return transport_dump_vpd_ident_type(vpd, NULL, 0);
  988. }
  989. EXPORT_SYMBOL(transport_set_vpd_ident_type);
  990. int transport_dump_vpd_ident(
  991. struct t10_vpd *vpd,
  992. unsigned char *p_buf,
  993. int p_buf_len)
  994. {
  995. unsigned char buf[VPD_TMP_BUF_SIZE];
  996. int ret = 0;
  997. memset(buf, 0, VPD_TMP_BUF_SIZE);
  998. switch (vpd->device_identifier_code_set) {
  999. case 0x01: /* Binary */
  1000. snprintf(buf, sizeof(buf),
  1001. "T10 VPD Binary Device Identifier: %s\n",
  1002. &vpd->device_identifier[0]);
  1003. break;
  1004. case 0x02: /* ASCII */
  1005. snprintf(buf, sizeof(buf),
  1006. "T10 VPD ASCII Device Identifier: %s\n",
  1007. &vpd->device_identifier[0]);
  1008. break;
  1009. case 0x03: /* UTF-8 */
  1010. snprintf(buf, sizeof(buf),
  1011. "T10 VPD UTF-8 Device Identifier: %s\n",
  1012. &vpd->device_identifier[0]);
  1013. break;
  1014. default:
  1015. sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
  1016. " 0x%02x", vpd->device_identifier_code_set);
  1017. ret = -EINVAL;
  1018. break;
  1019. }
  1020. if (p_buf)
  1021. strncpy(p_buf, buf, p_buf_len);
  1022. else
  1023. pr_debug("%s", buf);
  1024. return ret;
  1025. }
  1026. int
  1027. transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
  1028. {
  1029. static const char hex_str[] = "0123456789abcdef";
  1030. int j = 0, i = 4; /* offset to start of the identifier */
  1031. /*
  1032. * The VPD Code Set (encoding)
  1033. *
  1034. * from spc3r23.pdf Section 7.6.3.1 Table 296
  1035. */
  1036. vpd->device_identifier_code_set = (page_83[0] & 0x0f);
  1037. switch (vpd->device_identifier_code_set) {
  1038. case 0x01: /* Binary */
  1039. vpd->device_identifier[j++] =
  1040. hex_str[vpd->device_identifier_type];
  1041. while (i < (4 + page_83[3])) {
  1042. vpd->device_identifier[j++] =
  1043. hex_str[(page_83[i] & 0xf0) >> 4];
  1044. vpd->device_identifier[j++] =
  1045. hex_str[page_83[i] & 0x0f];
  1046. i++;
  1047. }
  1048. break;
  1049. case 0x02: /* ASCII */
  1050. case 0x03: /* UTF-8 */
  1051. while (i < (4 + page_83[3]))
  1052. vpd->device_identifier[j++] = page_83[i++];
  1053. break;
  1054. default:
  1055. break;
  1056. }
  1057. return transport_dump_vpd_ident(vpd, NULL, 0);
  1058. }
  1059. EXPORT_SYMBOL(transport_set_vpd_ident);
  1060. static sense_reason_t
  1061. target_check_max_data_sg_nents(struct se_cmd *cmd, struct se_device *dev,
  1062. unsigned int size)
  1063. {
  1064. u32 mtl;
  1065. if (!cmd->se_tfo->max_data_sg_nents)
  1066. return TCM_NO_SENSE;
  1067. /*
  1068. * Check if fabric enforced maximum SGL entries per I/O descriptor
  1069. * exceeds se_cmd->data_length. If true, set SCF_UNDERFLOW_BIT +
  1070. * residual_count and reduce original cmd->data_length to maximum
  1071. * length based on single PAGE_SIZE entry scatter-lists.
  1072. */
  1073. mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE);
  1074. if (cmd->data_length > mtl) {
  1075. /*
  1076. * If an existing CDB overflow is present, calculate new residual
  1077. * based on CDB size minus fabric maximum transfer length.
  1078. *
  1079. * If an existing CDB underflow is present, calculate new residual
  1080. * based on original cmd->data_length minus fabric maximum transfer
  1081. * length.
  1082. *
  1083. * Otherwise, set the underflow residual based on cmd->data_length
  1084. * minus fabric maximum transfer length.
  1085. */
  1086. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  1087. cmd->residual_count = (size - mtl);
  1088. } else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  1089. u32 orig_dl = size + cmd->residual_count;
  1090. cmd->residual_count = (orig_dl - mtl);
  1091. } else {
  1092. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  1093. cmd->residual_count = (cmd->data_length - mtl);
  1094. }
  1095. cmd->data_length = mtl;
  1096. /*
  1097. * Reset sbc_check_prot() calculated protection payload
  1098. * length based upon the new smaller MTL.
  1099. */
  1100. if (cmd->prot_length) {
  1101. u32 sectors = (mtl / dev->dev_attrib.block_size);
  1102. cmd->prot_length = dev->prot_length * sectors;
  1103. }
  1104. }
  1105. return TCM_NO_SENSE;
  1106. }
  1107. sense_reason_t
  1108. target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
  1109. {
  1110. struct se_device *dev = cmd->se_dev;
  1111. if (cmd->unknown_data_length) {
  1112. cmd->data_length = size;
  1113. } else if (size != cmd->data_length) {
  1114. pr_warn_ratelimited("TARGET_CORE[%s]: Expected Transfer Length:"
  1115. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  1116. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  1117. cmd->data_length, size, cmd->t_task_cdb[0]);
  1118. if (cmd->data_direction == DMA_TO_DEVICE) {
  1119. if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
  1120. pr_err_ratelimited("Rejecting underflow/overflow"
  1121. " for WRITE data CDB\n");
  1122. return TCM_INVALID_CDB_FIELD;
  1123. }
  1124. /*
  1125. * Some fabric drivers like iscsi-target still expect to
  1126. * always reject overflow writes. Reject this case until
  1127. * full fabric driver level support for overflow writes
  1128. * is introduced tree-wide.
  1129. */
  1130. if (size > cmd->data_length) {
  1131. pr_err_ratelimited("Rejecting overflow for"
  1132. " WRITE control CDB\n");
  1133. return TCM_INVALID_CDB_FIELD;
  1134. }
  1135. }
  1136. /*
  1137. * Reject READ_* or WRITE_* with overflow/underflow for
  1138. * type SCF_SCSI_DATA_CDB.
  1139. */
  1140. if (dev->dev_attrib.block_size != 512) {
  1141. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  1142. " CDB on non 512-byte sector setup subsystem"
  1143. " plugin: %s\n", dev->transport->name);
  1144. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  1145. return TCM_INVALID_CDB_FIELD;
  1146. }
  1147. /*
  1148. * For the overflow case keep the existing fabric provided
  1149. * ->data_length. Otherwise for the underflow case, reset
  1150. * ->data_length to the smaller SCSI expected data transfer
  1151. * length.
  1152. */
  1153. if (size > cmd->data_length) {
  1154. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  1155. cmd->residual_count = (size - cmd->data_length);
  1156. } else {
  1157. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  1158. cmd->residual_count = (cmd->data_length - size);
  1159. cmd->data_length = size;
  1160. }
  1161. }
  1162. return target_check_max_data_sg_nents(cmd, dev, size);
  1163. }
  1164. /*
  1165. * Used by fabric modules containing a local struct se_cmd within their
  1166. * fabric dependent per I/O descriptor.
  1167. *
  1168. * Preserves the value of @cmd->tag.
  1169. */
  1170. void transport_init_se_cmd(
  1171. struct se_cmd *cmd,
  1172. const struct target_core_fabric_ops *tfo,
  1173. struct se_session *se_sess,
  1174. u32 data_length,
  1175. int data_direction,
  1176. int task_attr,
  1177. unsigned char *sense_buffer)
  1178. {
  1179. INIT_LIST_HEAD(&cmd->se_delayed_node);
  1180. INIT_LIST_HEAD(&cmd->se_qf_node);
  1181. INIT_LIST_HEAD(&cmd->se_cmd_list);
  1182. INIT_LIST_HEAD(&cmd->state_list);
  1183. init_completion(&cmd->t_transport_stop_comp);
  1184. cmd->compl = NULL;
  1185. spin_lock_init(&cmd->t_state_lock);
  1186. INIT_WORK(&cmd->work, NULL);
  1187. kref_init(&cmd->cmd_kref);
  1188. cmd->se_tfo = tfo;
  1189. cmd->se_sess = se_sess;
  1190. cmd->data_length = data_length;
  1191. cmd->data_direction = data_direction;
  1192. cmd->sam_task_attr = task_attr;
  1193. cmd->sense_buffer = sense_buffer;
  1194. cmd->state_active = false;
  1195. }
  1196. EXPORT_SYMBOL(transport_init_se_cmd);
  1197. static sense_reason_t
  1198. transport_check_alloc_task_attr(struct se_cmd *cmd)
  1199. {
  1200. struct se_device *dev = cmd->se_dev;
  1201. /*
  1202. * Check if SAM Task Attribute emulation is enabled for this
  1203. * struct se_device storage object
  1204. */
  1205. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1206. return 0;
  1207. if (cmd->sam_task_attr == TCM_ACA_TAG) {
  1208. pr_debug("SAM Task Attribute ACA"
  1209. " emulation is not supported\n");
  1210. return TCM_INVALID_CDB_FIELD;
  1211. }
  1212. return 0;
  1213. }
  1214. sense_reason_t
  1215. target_setup_cmd_from_cdb(struct se_cmd *cmd, unsigned char *cdb)
  1216. {
  1217. struct se_device *dev = cmd->se_dev;
  1218. sense_reason_t ret;
  1219. /*
  1220. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1221. * for VARIABLE_LENGTH_CMD
  1222. */
  1223. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1224. pr_err("Received SCSI CDB with command_size: %d that"
  1225. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1226. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1227. return TCM_INVALID_CDB_FIELD;
  1228. }
  1229. /*
  1230. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1231. * allocate the additional extended CDB buffer now.. Otherwise
  1232. * setup the pointer from __t_task_cdb to t_task_cdb.
  1233. */
  1234. if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
  1235. cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1236. GFP_KERNEL);
  1237. if (!cmd->t_task_cdb) {
  1238. pr_err("Unable to allocate cmd->t_task_cdb"
  1239. " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
  1240. scsi_command_size(cdb),
  1241. (unsigned long)sizeof(cmd->__t_task_cdb));
  1242. return TCM_OUT_OF_RESOURCES;
  1243. }
  1244. } else
  1245. cmd->t_task_cdb = &cmd->__t_task_cdb[0];
  1246. /*
  1247. * Copy the original CDB into cmd->
  1248. */
  1249. memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
  1250. trace_target_sequencer_start(cmd);
  1251. ret = dev->transport->parse_cdb(cmd);
  1252. if (ret == TCM_UNSUPPORTED_SCSI_OPCODE)
  1253. pr_warn_ratelimited("%s/%s: Unsupported SCSI Opcode 0x%02x, sending CHECK_CONDITION.\n",
  1254. cmd->se_tfo->get_fabric_name(),
  1255. cmd->se_sess->se_node_acl->initiatorname,
  1256. cmd->t_task_cdb[0]);
  1257. if (ret)
  1258. return ret;
  1259. ret = transport_check_alloc_task_attr(cmd);
  1260. if (ret)
  1261. return ret;
  1262. cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1263. atomic_long_inc(&cmd->se_lun->lun_stats.cmd_pdus);
  1264. return 0;
  1265. }
  1266. EXPORT_SYMBOL(target_setup_cmd_from_cdb);
  1267. /*
  1268. * Used by fabric module frontends to queue tasks directly.
  1269. * May only be used from process context.
  1270. */
  1271. int transport_handle_cdb_direct(
  1272. struct se_cmd *cmd)
  1273. {
  1274. sense_reason_t ret;
  1275. if (!cmd->se_lun) {
  1276. dump_stack();
  1277. pr_err("cmd->se_lun is NULL\n");
  1278. return -EINVAL;
  1279. }
  1280. if (in_interrupt()) {
  1281. dump_stack();
  1282. pr_err("transport_generic_handle_cdb cannot be called"
  1283. " from interrupt context\n");
  1284. return -EINVAL;
  1285. }
  1286. /*
  1287. * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
  1288. * outstanding descriptors are handled correctly during shutdown via
  1289. * transport_wait_for_tasks()
  1290. *
  1291. * Also, we don't take cmd->t_state_lock here as we only expect
  1292. * this to be called for initial descriptor submission.
  1293. */
  1294. cmd->t_state = TRANSPORT_NEW_CMD;
  1295. cmd->transport_state |= CMD_T_ACTIVE;
  1296. /*
  1297. * transport_generic_new_cmd() is already handling QUEUE_FULL,
  1298. * so follow TRANSPORT_NEW_CMD processing thread context usage
  1299. * and call transport_generic_request_failure() if necessary..
  1300. */
  1301. ret = transport_generic_new_cmd(cmd);
  1302. if (ret)
  1303. transport_generic_request_failure(cmd, ret);
  1304. return 0;
  1305. }
  1306. EXPORT_SYMBOL(transport_handle_cdb_direct);
  1307. sense_reason_t
  1308. transport_generic_map_mem_to_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
  1309. u32 sgl_count, struct scatterlist *sgl_bidi, u32 sgl_bidi_count)
  1310. {
  1311. if (!sgl || !sgl_count)
  1312. return 0;
  1313. /*
  1314. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  1315. * scatterlists already have been set to follow what the fabric
  1316. * passes for the original expected data transfer length.
  1317. */
  1318. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  1319. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  1320. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  1321. return TCM_INVALID_CDB_FIELD;
  1322. }
  1323. cmd->t_data_sg = sgl;
  1324. cmd->t_data_nents = sgl_count;
  1325. cmd->t_bidi_data_sg = sgl_bidi;
  1326. cmd->t_bidi_data_nents = sgl_bidi_count;
  1327. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  1328. return 0;
  1329. }
  1330. /**
  1331. * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
  1332. * se_cmd + use pre-allocated SGL memory.
  1333. *
  1334. * @se_cmd: command descriptor to submit
  1335. * @se_sess: associated se_sess for endpoint
  1336. * @cdb: pointer to SCSI CDB
  1337. * @sense: pointer to SCSI sense buffer
  1338. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1339. * @data_length: fabric expected data transfer length
  1340. * @task_attr: SAM task attribute
  1341. * @data_dir: DMA data direction
  1342. * @flags: flags for command submission from target_sc_flags_tables
  1343. * @sgl: struct scatterlist memory for unidirectional mapping
  1344. * @sgl_count: scatterlist count for unidirectional mapping
  1345. * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
  1346. * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
  1347. * @sgl_prot: struct scatterlist memory protection information
  1348. * @sgl_prot_count: scatterlist count for protection information
  1349. *
  1350. * Task tags are supported if the caller has set @se_cmd->tag.
  1351. *
  1352. * Returns non zero to signal active I/O shutdown failure. All other
  1353. * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
  1354. * but still return zero here.
  1355. *
  1356. * This may only be called from process context, and also currently
  1357. * assumes internal allocation of fabric payload buffer by target-core.
  1358. */
  1359. int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess,
  1360. unsigned char *cdb, unsigned char *sense, u64 unpacked_lun,
  1361. u32 data_length, int task_attr, int data_dir, int flags,
  1362. struct scatterlist *sgl, u32 sgl_count,
  1363. struct scatterlist *sgl_bidi, u32 sgl_bidi_count,
  1364. struct scatterlist *sgl_prot, u32 sgl_prot_count)
  1365. {
  1366. struct se_portal_group *se_tpg;
  1367. sense_reason_t rc;
  1368. int ret;
  1369. se_tpg = se_sess->se_tpg;
  1370. BUG_ON(!se_tpg);
  1371. BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
  1372. BUG_ON(in_interrupt());
  1373. /*
  1374. * Initialize se_cmd for target operation. From this point
  1375. * exceptions are handled by sending exception status via
  1376. * target_core_fabric_ops->queue_status() callback
  1377. */
  1378. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1379. data_length, data_dir, task_attr, sense);
  1380. if (flags & TARGET_SCF_USE_CPUID)
  1381. se_cmd->se_cmd_flags |= SCF_USE_CPUID;
  1382. else
  1383. se_cmd->cpuid = WORK_CPU_UNBOUND;
  1384. if (flags & TARGET_SCF_UNKNOWN_SIZE)
  1385. se_cmd->unknown_data_length = 1;
  1386. /*
  1387. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1388. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1389. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1390. * kref_put() to happen during fabric packet acknowledgement.
  1391. */
  1392. ret = target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF);
  1393. if (ret)
  1394. return ret;
  1395. /*
  1396. * Signal bidirectional data payloads to target-core
  1397. */
  1398. if (flags & TARGET_SCF_BIDI_OP)
  1399. se_cmd->se_cmd_flags |= SCF_BIDI;
  1400. /*
  1401. * Locate se_lun pointer and attach it to struct se_cmd
  1402. */
  1403. rc = transport_lookup_cmd_lun(se_cmd, unpacked_lun);
  1404. if (rc) {
  1405. transport_send_check_condition_and_sense(se_cmd, rc, 0);
  1406. target_put_sess_cmd(se_cmd);
  1407. return 0;
  1408. }
  1409. rc = target_setup_cmd_from_cdb(se_cmd, cdb);
  1410. if (rc != 0) {
  1411. transport_generic_request_failure(se_cmd, rc);
  1412. return 0;
  1413. }
  1414. /*
  1415. * Save pointers for SGLs containing protection information,
  1416. * if present.
  1417. */
  1418. if (sgl_prot_count) {
  1419. se_cmd->t_prot_sg = sgl_prot;
  1420. se_cmd->t_prot_nents = sgl_prot_count;
  1421. se_cmd->se_cmd_flags |= SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC;
  1422. }
  1423. /*
  1424. * When a non zero sgl_count has been passed perform SGL passthrough
  1425. * mapping for pre-allocated fabric memory instead of having target
  1426. * core perform an internal SGL allocation..
  1427. */
  1428. if (sgl_count != 0) {
  1429. BUG_ON(!sgl);
  1430. /*
  1431. * A work-around for tcm_loop as some userspace code via
  1432. * scsi-generic do not memset their associated read buffers,
  1433. * so go ahead and do that here for type non-data CDBs. Also
  1434. * note that this is currently guaranteed to be a single SGL
  1435. * for this case by target core in target_setup_cmd_from_cdb()
  1436. * -> transport_generic_cmd_sequencer().
  1437. */
  1438. if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
  1439. se_cmd->data_direction == DMA_FROM_DEVICE) {
  1440. unsigned char *buf = NULL;
  1441. if (sgl)
  1442. buf = kmap(sg_page(sgl)) + sgl->offset;
  1443. if (buf) {
  1444. memset(buf, 0, sgl->length);
  1445. kunmap(sg_page(sgl));
  1446. }
  1447. }
  1448. rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count,
  1449. sgl_bidi, sgl_bidi_count);
  1450. if (rc != 0) {
  1451. transport_generic_request_failure(se_cmd, rc);
  1452. return 0;
  1453. }
  1454. }
  1455. /*
  1456. * Check if we need to delay processing because of ALUA
  1457. * Active/NonOptimized primary access state..
  1458. */
  1459. core_alua_check_nonop_delay(se_cmd);
  1460. transport_handle_cdb_direct(se_cmd);
  1461. return 0;
  1462. }
  1463. EXPORT_SYMBOL(target_submit_cmd_map_sgls);
  1464. /**
  1465. * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
  1466. *
  1467. * @se_cmd: command descriptor to submit
  1468. * @se_sess: associated se_sess for endpoint
  1469. * @cdb: pointer to SCSI CDB
  1470. * @sense: pointer to SCSI sense buffer
  1471. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1472. * @data_length: fabric expected data transfer length
  1473. * @task_attr: SAM task attribute
  1474. * @data_dir: DMA data direction
  1475. * @flags: flags for command submission from target_sc_flags_tables
  1476. *
  1477. * Task tags are supported if the caller has set @se_cmd->tag.
  1478. *
  1479. * Returns non zero to signal active I/O shutdown failure. All other
  1480. * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
  1481. * but still return zero here.
  1482. *
  1483. * This may only be called from process context, and also currently
  1484. * assumes internal allocation of fabric payload buffer by target-core.
  1485. *
  1486. * It also assumes interal target core SGL memory allocation.
  1487. */
  1488. int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
  1489. unsigned char *cdb, unsigned char *sense, u64 unpacked_lun,
  1490. u32 data_length, int task_attr, int data_dir, int flags)
  1491. {
  1492. return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense,
  1493. unpacked_lun, data_length, task_attr, data_dir,
  1494. flags, NULL, 0, NULL, 0, NULL, 0);
  1495. }
  1496. EXPORT_SYMBOL(target_submit_cmd);
  1497. static void target_complete_tmr_failure(struct work_struct *work)
  1498. {
  1499. struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
  1500. se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1501. se_cmd->se_tfo->queue_tm_rsp(se_cmd);
  1502. transport_lun_remove_cmd(se_cmd);
  1503. transport_cmd_check_stop_to_fabric(se_cmd);
  1504. }
  1505. static bool target_lookup_lun_from_tag(struct se_session *se_sess, u64 tag,
  1506. u64 *unpacked_lun)
  1507. {
  1508. struct se_cmd *se_cmd;
  1509. unsigned long flags;
  1510. bool ret = false;
  1511. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  1512. list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
  1513. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  1514. continue;
  1515. if (se_cmd->tag == tag) {
  1516. *unpacked_lun = se_cmd->orig_fe_lun;
  1517. ret = true;
  1518. break;
  1519. }
  1520. }
  1521. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  1522. return ret;
  1523. }
  1524. /**
  1525. * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
  1526. * for TMR CDBs
  1527. *
  1528. * @se_cmd: command descriptor to submit
  1529. * @se_sess: associated se_sess for endpoint
  1530. * @sense: pointer to SCSI sense buffer
  1531. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1532. * @fabric_tmr_ptr: fabric context for TMR req
  1533. * @tm_type: Type of TM request
  1534. * @gfp: gfp type for caller
  1535. * @tag: referenced task tag for TMR_ABORT_TASK
  1536. * @flags: submit cmd flags
  1537. *
  1538. * Callable from all contexts.
  1539. **/
  1540. int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
  1541. unsigned char *sense, u64 unpacked_lun,
  1542. void *fabric_tmr_ptr, unsigned char tm_type,
  1543. gfp_t gfp, u64 tag, int flags)
  1544. {
  1545. struct se_portal_group *se_tpg;
  1546. int ret;
  1547. se_tpg = se_sess->se_tpg;
  1548. BUG_ON(!se_tpg);
  1549. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1550. 0, DMA_NONE, TCM_SIMPLE_TAG, sense);
  1551. /*
  1552. * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
  1553. * allocation failure.
  1554. */
  1555. ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
  1556. if (ret < 0)
  1557. return -ENOMEM;
  1558. if (tm_type == TMR_ABORT_TASK)
  1559. se_cmd->se_tmr_req->ref_task_tag = tag;
  1560. /* See target_submit_cmd for commentary */
  1561. ret = target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF);
  1562. if (ret) {
  1563. core_tmr_release_req(se_cmd->se_tmr_req);
  1564. return ret;
  1565. }
  1566. /*
  1567. * If this is ABORT_TASK with no explicit fabric provided LUN,
  1568. * go ahead and search active session tags for a match to figure
  1569. * out unpacked_lun for the original se_cmd.
  1570. */
  1571. if (tm_type == TMR_ABORT_TASK && (flags & TARGET_SCF_LOOKUP_LUN_FROM_TAG)) {
  1572. if (!target_lookup_lun_from_tag(se_sess, tag, &unpacked_lun))
  1573. goto failure;
  1574. }
  1575. ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
  1576. if (ret)
  1577. goto failure;
  1578. transport_generic_handle_tmr(se_cmd);
  1579. return 0;
  1580. /*
  1581. * For callback during failure handling, push this work off
  1582. * to process context with TMR_LUN_DOES_NOT_EXIST status.
  1583. */
  1584. failure:
  1585. INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
  1586. schedule_work(&se_cmd->work);
  1587. return 0;
  1588. }
  1589. EXPORT_SYMBOL(target_submit_tmr);
  1590. /*
  1591. * Handle SAM-esque emulation for generic transport request failures.
  1592. */
  1593. void transport_generic_request_failure(struct se_cmd *cmd,
  1594. sense_reason_t sense_reason)
  1595. {
  1596. int ret = 0, post_ret = 0;
  1597. pr_debug("-----[ Storage Engine Exception; sense_reason %d\n",
  1598. sense_reason);
  1599. target_show_cmd("-----[ ", cmd);
  1600. /*
  1601. * For SAM Task Attribute emulation for failed struct se_cmd
  1602. */
  1603. transport_complete_task_attr(cmd);
  1604. /*
  1605. * Handle special case for COMPARE_AND_WRITE failure, where the
  1606. * callback is expected to drop the per device ->caw_sem.
  1607. */
  1608. if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
  1609. cmd->transport_complete_callback)
  1610. cmd->transport_complete_callback(cmd, false, &post_ret);
  1611. if (transport_check_aborted_status(cmd, 1))
  1612. return;
  1613. switch (sense_reason) {
  1614. case TCM_NON_EXISTENT_LUN:
  1615. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1616. case TCM_INVALID_CDB_FIELD:
  1617. case TCM_INVALID_PARAMETER_LIST:
  1618. case TCM_PARAMETER_LIST_LENGTH_ERROR:
  1619. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1620. case TCM_UNKNOWN_MODE_PAGE:
  1621. case TCM_WRITE_PROTECTED:
  1622. case TCM_ADDRESS_OUT_OF_RANGE:
  1623. case TCM_CHECK_CONDITION_ABORT_CMD:
  1624. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1625. case TCM_CHECK_CONDITION_NOT_READY:
  1626. case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED:
  1627. case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED:
  1628. case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED:
  1629. case TCM_COPY_TARGET_DEVICE_NOT_REACHABLE:
  1630. case TCM_TOO_MANY_TARGET_DESCS:
  1631. case TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE:
  1632. case TCM_TOO_MANY_SEGMENT_DESCS:
  1633. case TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE:
  1634. break;
  1635. case TCM_OUT_OF_RESOURCES:
  1636. cmd->scsi_status = SAM_STAT_TASK_SET_FULL;
  1637. goto queue_status;
  1638. case TCM_LUN_BUSY:
  1639. cmd->scsi_status = SAM_STAT_BUSY;
  1640. goto queue_status;
  1641. case TCM_RESERVATION_CONFLICT:
  1642. /*
  1643. * No SENSE Data payload for this case, set SCSI Status
  1644. * and queue the response to $FABRIC_MOD.
  1645. *
  1646. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1647. */
  1648. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1649. /*
  1650. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1651. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1652. * CONFLICT STATUS.
  1653. *
  1654. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1655. */
  1656. if (cmd->se_sess &&
  1657. cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2) {
  1658. target_ua_allocate_lun(cmd->se_sess->se_node_acl,
  1659. cmd->orig_fe_lun, 0x2C,
  1660. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1661. }
  1662. goto queue_status;
  1663. default:
  1664. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1665. cmd->t_task_cdb[0], sense_reason);
  1666. sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1667. break;
  1668. }
  1669. ret = transport_send_check_condition_and_sense(cmd, sense_reason, 0);
  1670. if (ret)
  1671. goto queue_full;
  1672. check_stop:
  1673. transport_lun_remove_cmd(cmd);
  1674. transport_cmd_check_stop_to_fabric(cmd);
  1675. return;
  1676. queue_status:
  1677. trace_target_cmd_complete(cmd);
  1678. ret = cmd->se_tfo->queue_status(cmd);
  1679. if (!ret)
  1680. goto check_stop;
  1681. queue_full:
  1682. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  1683. }
  1684. EXPORT_SYMBOL(transport_generic_request_failure);
  1685. void __target_execute_cmd(struct se_cmd *cmd, bool do_checks)
  1686. {
  1687. sense_reason_t ret;
  1688. if (!cmd->execute_cmd) {
  1689. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1690. goto err;
  1691. }
  1692. if (do_checks) {
  1693. /*
  1694. * Check for an existing UNIT ATTENTION condition after
  1695. * target_handle_task_attr() has done SAM task attr
  1696. * checking, and possibly have already defered execution
  1697. * out to target_restart_delayed_cmds() context.
  1698. */
  1699. ret = target_scsi3_ua_check(cmd);
  1700. if (ret)
  1701. goto err;
  1702. ret = target_alua_state_check(cmd);
  1703. if (ret)
  1704. goto err;
  1705. ret = target_check_reservation(cmd);
  1706. if (ret) {
  1707. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1708. goto err;
  1709. }
  1710. }
  1711. ret = cmd->execute_cmd(cmd);
  1712. if (!ret)
  1713. return;
  1714. err:
  1715. spin_lock_irq(&cmd->t_state_lock);
  1716. cmd->transport_state &= ~CMD_T_SENT;
  1717. spin_unlock_irq(&cmd->t_state_lock);
  1718. transport_generic_request_failure(cmd, ret);
  1719. }
  1720. static int target_write_prot_action(struct se_cmd *cmd)
  1721. {
  1722. u32 sectors;
  1723. /*
  1724. * Perform WRITE_INSERT of PI using software emulation when backend
  1725. * device has PI enabled, if the transport has not already generated
  1726. * PI using hardware WRITE_INSERT offload.
  1727. */
  1728. switch (cmd->prot_op) {
  1729. case TARGET_PROT_DOUT_INSERT:
  1730. if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_INSERT))
  1731. sbc_dif_generate(cmd);
  1732. break;
  1733. case TARGET_PROT_DOUT_STRIP:
  1734. if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_STRIP)
  1735. break;
  1736. sectors = cmd->data_length >> ilog2(cmd->se_dev->dev_attrib.block_size);
  1737. cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba,
  1738. sectors, 0, cmd->t_prot_sg, 0);
  1739. if (unlikely(cmd->pi_err)) {
  1740. spin_lock_irq(&cmd->t_state_lock);
  1741. cmd->transport_state &= ~CMD_T_SENT;
  1742. spin_unlock_irq(&cmd->t_state_lock);
  1743. transport_generic_request_failure(cmd, cmd->pi_err);
  1744. return -1;
  1745. }
  1746. break;
  1747. default:
  1748. break;
  1749. }
  1750. return 0;
  1751. }
  1752. static bool target_handle_task_attr(struct se_cmd *cmd)
  1753. {
  1754. struct se_device *dev = cmd->se_dev;
  1755. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1756. return false;
  1757. cmd->se_cmd_flags |= SCF_TASK_ATTR_SET;
  1758. /*
  1759. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1760. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1761. */
  1762. switch (cmd->sam_task_attr) {
  1763. case TCM_HEAD_TAG:
  1764. pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x\n",
  1765. cmd->t_task_cdb[0]);
  1766. return false;
  1767. case TCM_ORDERED_TAG:
  1768. atomic_inc_mb(&dev->dev_ordered_sync);
  1769. pr_debug("Added ORDERED for CDB: 0x%02x to ordered list\n",
  1770. cmd->t_task_cdb[0]);
  1771. /*
  1772. * Execute an ORDERED command if no other older commands
  1773. * exist that need to be completed first.
  1774. */
  1775. if (!atomic_read(&dev->simple_cmds))
  1776. return false;
  1777. break;
  1778. default:
  1779. /*
  1780. * For SIMPLE and UNTAGGED Task Attribute commands
  1781. */
  1782. atomic_inc_mb(&dev->simple_cmds);
  1783. break;
  1784. }
  1785. if (atomic_read(&dev->dev_ordered_sync) == 0)
  1786. return false;
  1787. spin_lock(&dev->delayed_cmd_lock);
  1788. list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
  1789. spin_unlock(&dev->delayed_cmd_lock);
  1790. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to delayed CMD listn",
  1791. cmd->t_task_cdb[0], cmd->sam_task_attr);
  1792. return true;
  1793. }
  1794. static int __transport_check_aborted_status(struct se_cmd *, int);
  1795. void target_execute_cmd(struct se_cmd *cmd)
  1796. {
  1797. /*
  1798. * Determine if frontend context caller is requesting the stopping of
  1799. * this command for frontend exceptions.
  1800. *
  1801. * If the received CDB has aleady been aborted stop processing it here.
  1802. */
  1803. spin_lock_irq(&cmd->t_state_lock);
  1804. if (__transport_check_aborted_status(cmd, 1)) {
  1805. spin_unlock_irq(&cmd->t_state_lock);
  1806. return;
  1807. }
  1808. if (cmd->transport_state & CMD_T_STOP) {
  1809. pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n",
  1810. __func__, __LINE__, cmd->tag);
  1811. spin_unlock_irq(&cmd->t_state_lock);
  1812. complete_all(&cmd->t_transport_stop_comp);
  1813. return;
  1814. }
  1815. cmd->t_state = TRANSPORT_PROCESSING;
  1816. cmd->transport_state &= ~CMD_T_PRE_EXECUTE;
  1817. cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT;
  1818. spin_unlock_irq(&cmd->t_state_lock);
  1819. if (target_write_prot_action(cmd))
  1820. return;
  1821. if (target_handle_task_attr(cmd)) {
  1822. spin_lock_irq(&cmd->t_state_lock);
  1823. cmd->transport_state &= ~CMD_T_SENT;
  1824. spin_unlock_irq(&cmd->t_state_lock);
  1825. return;
  1826. }
  1827. __target_execute_cmd(cmd, true);
  1828. }
  1829. EXPORT_SYMBOL(target_execute_cmd);
  1830. /*
  1831. * Process all commands up to the last received ORDERED task attribute which
  1832. * requires another blocking boundary
  1833. */
  1834. static void target_restart_delayed_cmds(struct se_device *dev)
  1835. {
  1836. for (;;) {
  1837. struct se_cmd *cmd;
  1838. spin_lock(&dev->delayed_cmd_lock);
  1839. if (list_empty(&dev->delayed_cmd_list)) {
  1840. spin_unlock(&dev->delayed_cmd_lock);
  1841. break;
  1842. }
  1843. cmd = list_entry(dev->delayed_cmd_list.next,
  1844. struct se_cmd, se_delayed_node);
  1845. list_del(&cmd->se_delayed_node);
  1846. spin_unlock(&dev->delayed_cmd_lock);
  1847. cmd->transport_state |= CMD_T_SENT;
  1848. __target_execute_cmd(cmd, true);
  1849. if (cmd->sam_task_attr == TCM_ORDERED_TAG)
  1850. break;
  1851. }
  1852. }
  1853. /*
  1854. * Called from I/O completion to determine which dormant/delayed
  1855. * and ordered cmds need to have their tasks added to the execution queue.
  1856. */
  1857. static void transport_complete_task_attr(struct se_cmd *cmd)
  1858. {
  1859. struct se_device *dev = cmd->se_dev;
  1860. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1861. return;
  1862. if (!(cmd->se_cmd_flags & SCF_TASK_ATTR_SET))
  1863. goto restart;
  1864. if (cmd->sam_task_attr == TCM_SIMPLE_TAG) {
  1865. atomic_dec_mb(&dev->simple_cmds);
  1866. dev->dev_cur_ordered_id++;
  1867. } else if (cmd->sam_task_attr == TCM_HEAD_TAG) {
  1868. dev->dev_cur_ordered_id++;
  1869. pr_debug("Incremented dev_cur_ordered_id: %u for HEAD_OF_QUEUE\n",
  1870. dev->dev_cur_ordered_id);
  1871. } else if (cmd->sam_task_attr == TCM_ORDERED_TAG) {
  1872. atomic_dec_mb(&dev->dev_ordered_sync);
  1873. dev->dev_cur_ordered_id++;
  1874. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED\n",
  1875. dev->dev_cur_ordered_id);
  1876. }
  1877. cmd->se_cmd_flags &= ~SCF_TASK_ATTR_SET;
  1878. restart:
  1879. target_restart_delayed_cmds(dev);
  1880. }
  1881. static void transport_complete_qf(struct se_cmd *cmd)
  1882. {
  1883. int ret = 0;
  1884. transport_complete_task_attr(cmd);
  1885. /*
  1886. * If a fabric driver ->write_pending() or ->queue_data_in() callback
  1887. * has returned neither -ENOMEM or -EAGAIN, assume it's fatal and
  1888. * the same callbacks should not be retried. Return CHECK_CONDITION
  1889. * if a scsi_status is not already set.
  1890. *
  1891. * If a fabric driver ->queue_status() has returned non zero, always
  1892. * keep retrying no matter what..
  1893. */
  1894. if (cmd->t_state == TRANSPORT_COMPLETE_QF_ERR) {
  1895. if (cmd->scsi_status)
  1896. goto queue_status;
  1897. translate_sense_reason(cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
  1898. goto queue_status;
  1899. }
  1900. /*
  1901. * Check if we need to send a sense buffer from
  1902. * the struct se_cmd in question. We do NOT want
  1903. * to take this path of the IO has been marked as
  1904. * needing to be treated like a "normal read". This
  1905. * is the case if it's a tape read, and either the
  1906. * FM, EOM, or ILI bits are set, but there is no
  1907. * sense data.
  1908. */
  1909. if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) &&
  1910. cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
  1911. goto queue_status;
  1912. switch (cmd->data_direction) {
  1913. case DMA_FROM_DEVICE:
  1914. /* queue status if not treating this as a normal read */
  1915. if (cmd->scsi_status &&
  1916. !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL))
  1917. goto queue_status;
  1918. trace_target_cmd_complete(cmd);
  1919. ret = cmd->se_tfo->queue_data_in(cmd);
  1920. break;
  1921. case DMA_TO_DEVICE:
  1922. if (cmd->se_cmd_flags & SCF_BIDI) {
  1923. ret = cmd->se_tfo->queue_data_in(cmd);
  1924. break;
  1925. }
  1926. /* fall through */
  1927. case DMA_NONE:
  1928. queue_status:
  1929. trace_target_cmd_complete(cmd);
  1930. ret = cmd->se_tfo->queue_status(cmd);
  1931. break;
  1932. default:
  1933. break;
  1934. }
  1935. if (ret < 0) {
  1936. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  1937. return;
  1938. }
  1939. transport_lun_remove_cmd(cmd);
  1940. transport_cmd_check_stop_to_fabric(cmd);
  1941. }
  1942. static void transport_handle_queue_full(struct se_cmd *cmd, struct se_device *dev,
  1943. int err, bool write_pending)
  1944. {
  1945. /*
  1946. * -EAGAIN or -ENOMEM signals retry of ->write_pending() and/or
  1947. * ->queue_data_in() callbacks from new process context.
  1948. *
  1949. * Otherwise for other errors, transport_complete_qf() will send
  1950. * CHECK_CONDITION via ->queue_status() instead of attempting to
  1951. * retry associated fabric driver data-transfer callbacks.
  1952. */
  1953. if (err == -EAGAIN || err == -ENOMEM) {
  1954. cmd->t_state = (write_pending) ? TRANSPORT_COMPLETE_QF_WP :
  1955. TRANSPORT_COMPLETE_QF_OK;
  1956. } else {
  1957. pr_warn_ratelimited("Got unknown fabric queue status: %d\n", err);
  1958. cmd->t_state = TRANSPORT_COMPLETE_QF_ERR;
  1959. }
  1960. spin_lock_irq(&dev->qf_cmd_lock);
  1961. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  1962. atomic_inc_mb(&dev->dev_qf_count);
  1963. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  1964. schedule_work(&cmd->se_dev->qf_work_queue);
  1965. }
  1966. static bool target_read_prot_action(struct se_cmd *cmd)
  1967. {
  1968. switch (cmd->prot_op) {
  1969. case TARGET_PROT_DIN_STRIP:
  1970. if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_STRIP)) {
  1971. u32 sectors = cmd->data_length >>
  1972. ilog2(cmd->se_dev->dev_attrib.block_size);
  1973. cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba,
  1974. sectors, 0, cmd->t_prot_sg,
  1975. 0);
  1976. if (cmd->pi_err)
  1977. return true;
  1978. }
  1979. break;
  1980. case TARGET_PROT_DIN_INSERT:
  1981. if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_INSERT)
  1982. break;
  1983. sbc_dif_generate(cmd);
  1984. break;
  1985. default:
  1986. break;
  1987. }
  1988. return false;
  1989. }
  1990. static void target_complete_ok_work(struct work_struct *work)
  1991. {
  1992. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  1993. int ret;
  1994. /*
  1995. * Check if we need to move delayed/dormant tasks from cmds on the
  1996. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  1997. * Attribute.
  1998. */
  1999. transport_complete_task_attr(cmd);
  2000. /*
  2001. * Check to schedule QUEUE_FULL work, or execute an existing
  2002. * cmd->transport_qf_callback()
  2003. */
  2004. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  2005. schedule_work(&cmd->se_dev->qf_work_queue);
  2006. /*
  2007. * Check if we need to send a sense buffer from
  2008. * the struct se_cmd in question. We do NOT want
  2009. * to take this path of the IO has been marked as
  2010. * needing to be treated like a "normal read". This
  2011. * is the case if it's a tape read, and either the
  2012. * FM, EOM, or ILI bits are set, but there is no
  2013. * sense data.
  2014. */
  2015. if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) &&
  2016. cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2017. WARN_ON(!cmd->scsi_status);
  2018. ret = transport_send_check_condition_and_sense(
  2019. cmd, 0, 1);
  2020. if (ret)
  2021. goto queue_full;
  2022. transport_lun_remove_cmd(cmd);
  2023. transport_cmd_check_stop_to_fabric(cmd);
  2024. return;
  2025. }
  2026. /*
  2027. * Check for a callback, used by amongst other things
  2028. * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation.
  2029. */
  2030. if (cmd->transport_complete_callback) {
  2031. sense_reason_t rc;
  2032. bool caw = (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE);
  2033. bool zero_dl = !(cmd->data_length);
  2034. int post_ret = 0;
  2035. rc = cmd->transport_complete_callback(cmd, true, &post_ret);
  2036. if (!rc && !post_ret) {
  2037. if (caw && zero_dl)
  2038. goto queue_rsp;
  2039. return;
  2040. } else if (rc) {
  2041. ret = transport_send_check_condition_and_sense(cmd,
  2042. rc, 0);
  2043. if (ret)
  2044. goto queue_full;
  2045. transport_lun_remove_cmd(cmd);
  2046. transport_cmd_check_stop_to_fabric(cmd);
  2047. return;
  2048. }
  2049. }
  2050. queue_rsp:
  2051. switch (cmd->data_direction) {
  2052. case DMA_FROM_DEVICE:
  2053. /*
  2054. * if this is a READ-type IO, but SCSI status
  2055. * is set, then skip returning data and just
  2056. * return the status -- unless this IO is marked
  2057. * as needing to be treated as a normal read,
  2058. * in which case we want to go ahead and return
  2059. * the data. This happens, for example, for tape
  2060. * reads with the FM, EOM, or ILI bits set, with
  2061. * no sense data.
  2062. */
  2063. if (cmd->scsi_status &&
  2064. !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL))
  2065. goto queue_status;
  2066. atomic_long_add(cmd->data_length,
  2067. &cmd->se_lun->lun_stats.tx_data_octets);
  2068. /*
  2069. * Perform READ_STRIP of PI using software emulation when
  2070. * backend had PI enabled, if the transport will not be
  2071. * performing hardware READ_STRIP offload.
  2072. */
  2073. if (target_read_prot_action(cmd)) {
  2074. ret = transport_send_check_condition_and_sense(cmd,
  2075. cmd->pi_err, 0);
  2076. if (ret)
  2077. goto queue_full;
  2078. transport_lun_remove_cmd(cmd);
  2079. transport_cmd_check_stop_to_fabric(cmd);
  2080. return;
  2081. }
  2082. trace_target_cmd_complete(cmd);
  2083. ret = cmd->se_tfo->queue_data_in(cmd);
  2084. if (ret)
  2085. goto queue_full;
  2086. break;
  2087. case DMA_TO_DEVICE:
  2088. atomic_long_add(cmd->data_length,
  2089. &cmd->se_lun->lun_stats.rx_data_octets);
  2090. /*
  2091. * Check if we need to send READ payload for BIDI-COMMAND
  2092. */
  2093. if (cmd->se_cmd_flags & SCF_BIDI) {
  2094. atomic_long_add(cmd->data_length,
  2095. &cmd->se_lun->lun_stats.tx_data_octets);
  2096. ret = cmd->se_tfo->queue_data_in(cmd);
  2097. if (ret)
  2098. goto queue_full;
  2099. break;
  2100. }
  2101. /* fall through */
  2102. case DMA_NONE:
  2103. queue_status:
  2104. trace_target_cmd_complete(cmd);
  2105. ret = cmd->se_tfo->queue_status(cmd);
  2106. if (ret)
  2107. goto queue_full;
  2108. break;
  2109. default:
  2110. break;
  2111. }
  2112. transport_lun_remove_cmd(cmd);
  2113. transport_cmd_check_stop_to_fabric(cmd);
  2114. return;
  2115. queue_full:
  2116. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  2117. " data_direction: %d\n", cmd, cmd->data_direction);
  2118. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  2119. }
  2120. void target_free_sgl(struct scatterlist *sgl, int nents)
  2121. {
  2122. sgl_free_n_order(sgl, nents, 0);
  2123. }
  2124. EXPORT_SYMBOL(target_free_sgl);
  2125. static inline void transport_reset_sgl_orig(struct se_cmd *cmd)
  2126. {
  2127. /*
  2128. * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
  2129. * emulation, and free + reset pointers if necessary..
  2130. */
  2131. if (!cmd->t_data_sg_orig)
  2132. return;
  2133. kfree(cmd->t_data_sg);
  2134. cmd->t_data_sg = cmd->t_data_sg_orig;
  2135. cmd->t_data_sg_orig = NULL;
  2136. cmd->t_data_nents = cmd->t_data_nents_orig;
  2137. cmd->t_data_nents_orig = 0;
  2138. }
  2139. static inline void transport_free_pages(struct se_cmd *cmd)
  2140. {
  2141. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) {
  2142. target_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents);
  2143. cmd->t_prot_sg = NULL;
  2144. cmd->t_prot_nents = 0;
  2145. }
  2146. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) {
  2147. /*
  2148. * Release special case READ buffer payload required for
  2149. * SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE
  2150. */
  2151. if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) {
  2152. target_free_sgl(cmd->t_bidi_data_sg,
  2153. cmd->t_bidi_data_nents);
  2154. cmd->t_bidi_data_sg = NULL;
  2155. cmd->t_bidi_data_nents = 0;
  2156. }
  2157. transport_reset_sgl_orig(cmd);
  2158. return;
  2159. }
  2160. transport_reset_sgl_orig(cmd);
  2161. target_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  2162. cmd->t_data_sg = NULL;
  2163. cmd->t_data_nents = 0;
  2164. target_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  2165. cmd->t_bidi_data_sg = NULL;
  2166. cmd->t_bidi_data_nents = 0;
  2167. }
  2168. void *transport_kmap_data_sg(struct se_cmd *cmd)
  2169. {
  2170. struct scatterlist *sg = cmd->t_data_sg;
  2171. struct page **pages;
  2172. int i;
  2173. /*
  2174. * We need to take into account a possible offset here for fabrics like
  2175. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  2176. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  2177. */
  2178. if (!cmd->t_data_nents)
  2179. return NULL;
  2180. BUG_ON(!sg);
  2181. if (cmd->t_data_nents == 1)
  2182. return kmap(sg_page(sg)) + sg->offset;
  2183. /* >1 page. use vmap */
  2184. pages = kmalloc_array(cmd->t_data_nents, sizeof(*pages), GFP_KERNEL);
  2185. if (!pages)
  2186. return NULL;
  2187. /* convert sg[] to pages[] */
  2188. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  2189. pages[i] = sg_page(sg);
  2190. }
  2191. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  2192. kfree(pages);
  2193. if (!cmd->t_data_vmap)
  2194. return NULL;
  2195. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  2196. }
  2197. EXPORT_SYMBOL(transport_kmap_data_sg);
  2198. void transport_kunmap_data_sg(struct se_cmd *cmd)
  2199. {
  2200. if (!cmd->t_data_nents) {
  2201. return;
  2202. } else if (cmd->t_data_nents == 1) {
  2203. kunmap(sg_page(cmd->t_data_sg));
  2204. return;
  2205. }
  2206. vunmap(cmd->t_data_vmap);
  2207. cmd->t_data_vmap = NULL;
  2208. }
  2209. EXPORT_SYMBOL(transport_kunmap_data_sg);
  2210. int
  2211. target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length,
  2212. bool zero_page, bool chainable)
  2213. {
  2214. gfp_t gfp = GFP_KERNEL | (zero_page ? __GFP_ZERO : 0);
  2215. *sgl = sgl_alloc_order(length, 0, chainable, gfp, nents);
  2216. return *sgl ? 0 : -ENOMEM;
  2217. }
  2218. EXPORT_SYMBOL(target_alloc_sgl);
  2219. /*
  2220. * Allocate any required resources to execute the command. For writes we
  2221. * might not have the payload yet, so notify the fabric via a call to
  2222. * ->write_pending instead. Otherwise place it on the execution queue.
  2223. */
  2224. sense_reason_t
  2225. transport_generic_new_cmd(struct se_cmd *cmd)
  2226. {
  2227. unsigned long flags;
  2228. int ret = 0;
  2229. bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB);
  2230. if (cmd->prot_op != TARGET_PROT_NORMAL &&
  2231. !(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) {
  2232. ret = target_alloc_sgl(&cmd->t_prot_sg, &cmd->t_prot_nents,
  2233. cmd->prot_length, true, false);
  2234. if (ret < 0)
  2235. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2236. }
  2237. /*
  2238. * Determine is the TCM fabric module has already allocated physical
  2239. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  2240. * beforehand.
  2241. */
  2242. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  2243. cmd->data_length) {
  2244. if ((cmd->se_cmd_flags & SCF_BIDI) ||
  2245. (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) {
  2246. u32 bidi_length;
  2247. if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)
  2248. bidi_length = cmd->t_task_nolb *
  2249. cmd->se_dev->dev_attrib.block_size;
  2250. else
  2251. bidi_length = cmd->data_length;
  2252. ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
  2253. &cmd->t_bidi_data_nents,
  2254. bidi_length, zero_flag, false);
  2255. if (ret < 0)
  2256. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2257. }
  2258. ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
  2259. cmd->data_length, zero_flag, false);
  2260. if (ret < 0)
  2261. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2262. } else if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
  2263. cmd->data_length) {
  2264. /*
  2265. * Special case for COMPARE_AND_WRITE with fabrics
  2266. * using SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC.
  2267. */
  2268. u32 caw_length = cmd->t_task_nolb *
  2269. cmd->se_dev->dev_attrib.block_size;
  2270. ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
  2271. &cmd->t_bidi_data_nents,
  2272. caw_length, zero_flag, false);
  2273. if (ret < 0)
  2274. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2275. }
  2276. /*
  2277. * If this command is not a write we can execute it right here,
  2278. * for write buffers we need to notify the fabric driver first
  2279. * and let it call back once the write buffers are ready.
  2280. */
  2281. target_add_to_state_list(cmd);
  2282. if (cmd->data_direction != DMA_TO_DEVICE || cmd->data_length == 0) {
  2283. target_execute_cmd(cmd);
  2284. return 0;
  2285. }
  2286. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2287. cmd->t_state = TRANSPORT_WRITE_PENDING;
  2288. /*
  2289. * Determine if frontend context caller is requesting the stopping of
  2290. * this command for frontend exceptions.
  2291. */
  2292. if (cmd->transport_state & CMD_T_STOP) {
  2293. pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n",
  2294. __func__, __LINE__, cmd->tag);
  2295. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2296. complete_all(&cmd->t_transport_stop_comp);
  2297. return 0;
  2298. }
  2299. cmd->transport_state &= ~CMD_T_ACTIVE;
  2300. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2301. ret = cmd->se_tfo->write_pending(cmd);
  2302. if (ret)
  2303. goto queue_full;
  2304. return 0;
  2305. queue_full:
  2306. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  2307. transport_handle_queue_full(cmd, cmd->se_dev, ret, true);
  2308. return 0;
  2309. }
  2310. EXPORT_SYMBOL(transport_generic_new_cmd);
  2311. static void transport_write_pending_qf(struct se_cmd *cmd)
  2312. {
  2313. unsigned long flags;
  2314. int ret;
  2315. bool stop;
  2316. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2317. stop = (cmd->transport_state & (CMD_T_STOP | CMD_T_ABORTED));
  2318. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2319. if (stop) {
  2320. pr_debug("%s:%d CMD_T_STOP|CMD_T_ABORTED for ITT: 0x%08llx\n",
  2321. __func__, __LINE__, cmd->tag);
  2322. complete_all(&cmd->t_transport_stop_comp);
  2323. return;
  2324. }
  2325. ret = cmd->se_tfo->write_pending(cmd);
  2326. if (ret) {
  2327. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  2328. cmd);
  2329. transport_handle_queue_full(cmd, cmd->se_dev, ret, true);
  2330. }
  2331. }
  2332. static bool
  2333. __transport_wait_for_tasks(struct se_cmd *, bool, bool *, bool *,
  2334. unsigned long *flags);
  2335. static void target_wait_free_cmd(struct se_cmd *cmd, bool *aborted, bool *tas)
  2336. {
  2337. unsigned long flags;
  2338. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2339. __transport_wait_for_tasks(cmd, true, aborted, tas, &flags);
  2340. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2341. }
  2342. /*
  2343. * This function is called by frontend drivers after processing of a command
  2344. * has finished.
  2345. *
  2346. * The protocol for ensuring that either the regular flow or the TMF
  2347. * code drops one reference is as follows:
  2348. * - Calling .queue_data_in(), .queue_status() or queue_tm_rsp() will cause
  2349. * the frontend driver to drop one reference, synchronously or asynchronously.
  2350. * - During regular command processing the target core sets CMD_T_COMPLETE
  2351. * before invoking one of the .queue_*() functions.
  2352. * - The code that aborts commands skips commands and TMFs for which
  2353. * CMD_T_COMPLETE has been set.
  2354. * - CMD_T_ABORTED is set atomically after the CMD_T_COMPLETE check for
  2355. * commands that will be aborted.
  2356. * - If the CMD_T_ABORTED flag is set but CMD_T_TAS has not been set
  2357. * transport_generic_free_cmd() skips its call to target_put_sess_cmd().
  2358. * - For aborted commands for which CMD_T_TAS has been set .queue_status() will
  2359. * be called and will drop a reference.
  2360. * - For aborted commands for which CMD_T_TAS has not been set .aborted_task()
  2361. * will be called. transport_cmd_finish_abort() will drop the final reference.
  2362. */
  2363. int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  2364. {
  2365. DECLARE_COMPLETION_ONSTACK(compl);
  2366. int ret = 0;
  2367. bool aborted = false, tas = false;
  2368. if (wait_for_tasks)
  2369. target_wait_free_cmd(cmd, &aborted, &tas);
  2370. if (cmd->se_cmd_flags & SCF_SE_LUN_CMD) {
  2371. /*
  2372. * Handle WRITE failure case where transport_generic_new_cmd()
  2373. * has already added se_cmd to state_list, but fabric has
  2374. * failed command before I/O submission.
  2375. */
  2376. if (cmd->state_active)
  2377. target_remove_from_state_list(cmd);
  2378. if (cmd->se_lun)
  2379. transport_lun_remove_cmd(cmd);
  2380. }
  2381. if (aborted)
  2382. cmd->compl = &compl;
  2383. if (!aborted || tas)
  2384. ret = target_put_sess_cmd(cmd);
  2385. if (aborted) {
  2386. pr_debug("Detected CMD_T_ABORTED for ITT: %llu\n", cmd->tag);
  2387. wait_for_completion(&compl);
  2388. ret = 1;
  2389. }
  2390. return ret;
  2391. }
  2392. EXPORT_SYMBOL(transport_generic_free_cmd);
  2393. /**
  2394. * target_get_sess_cmd - Add command to active ->sess_cmd_list
  2395. * @se_cmd: command descriptor to add
  2396. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  2397. */
  2398. int target_get_sess_cmd(struct se_cmd *se_cmd, bool ack_kref)
  2399. {
  2400. struct se_session *se_sess = se_cmd->se_sess;
  2401. unsigned long flags;
  2402. int ret = 0;
  2403. /*
  2404. * Add a second kref if the fabric caller is expecting to handle
  2405. * fabric acknowledgement that requires two target_put_sess_cmd()
  2406. * invocations before se_cmd descriptor release.
  2407. */
  2408. if (ack_kref) {
  2409. if (!kref_get_unless_zero(&se_cmd->cmd_kref))
  2410. return -EINVAL;
  2411. se_cmd->se_cmd_flags |= SCF_ACK_KREF;
  2412. }
  2413. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  2414. if (se_sess->sess_tearing_down) {
  2415. ret = -ESHUTDOWN;
  2416. goto out;
  2417. }
  2418. se_cmd->transport_state |= CMD_T_PRE_EXECUTE;
  2419. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  2420. percpu_ref_get(&se_sess->cmd_count);
  2421. out:
  2422. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2423. if (ret && ack_kref)
  2424. target_put_sess_cmd(se_cmd);
  2425. return ret;
  2426. }
  2427. EXPORT_SYMBOL(target_get_sess_cmd);
  2428. static void target_free_cmd_mem(struct se_cmd *cmd)
  2429. {
  2430. transport_free_pages(cmd);
  2431. if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  2432. core_tmr_release_req(cmd->se_tmr_req);
  2433. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  2434. kfree(cmd->t_task_cdb);
  2435. }
  2436. static void target_release_cmd_kref(struct kref *kref)
  2437. {
  2438. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  2439. struct se_session *se_sess = se_cmd->se_sess;
  2440. struct completion *compl = se_cmd->compl;
  2441. unsigned long flags;
  2442. if (se_sess) {
  2443. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  2444. list_del_init(&se_cmd->se_cmd_list);
  2445. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2446. }
  2447. target_free_cmd_mem(se_cmd);
  2448. se_cmd->se_tfo->release_cmd(se_cmd);
  2449. if (compl)
  2450. complete(compl);
  2451. percpu_ref_put(&se_sess->cmd_count);
  2452. }
  2453. /**
  2454. * target_put_sess_cmd - decrease the command reference count
  2455. * @se_cmd: command to drop a reference from
  2456. *
  2457. * Returns 1 if and only if this target_put_sess_cmd() call caused the
  2458. * refcount to drop to zero. Returns zero otherwise.
  2459. */
  2460. int target_put_sess_cmd(struct se_cmd *se_cmd)
  2461. {
  2462. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  2463. }
  2464. EXPORT_SYMBOL(target_put_sess_cmd);
  2465. static const char *data_dir_name(enum dma_data_direction d)
  2466. {
  2467. switch (d) {
  2468. case DMA_BIDIRECTIONAL: return "BIDI";
  2469. case DMA_TO_DEVICE: return "WRITE";
  2470. case DMA_FROM_DEVICE: return "READ";
  2471. case DMA_NONE: return "NONE";
  2472. }
  2473. return "(?)";
  2474. }
  2475. static const char *cmd_state_name(enum transport_state_table t)
  2476. {
  2477. switch (t) {
  2478. case TRANSPORT_NO_STATE: return "NO_STATE";
  2479. case TRANSPORT_NEW_CMD: return "NEW_CMD";
  2480. case TRANSPORT_WRITE_PENDING: return "WRITE_PENDING";
  2481. case TRANSPORT_PROCESSING: return "PROCESSING";
  2482. case TRANSPORT_COMPLETE: return "COMPLETE";
  2483. case TRANSPORT_ISTATE_PROCESSING:
  2484. return "ISTATE_PROCESSING";
  2485. case TRANSPORT_COMPLETE_QF_WP: return "COMPLETE_QF_WP";
  2486. case TRANSPORT_COMPLETE_QF_OK: return "COMPLETE_QF_OK";
  2487. case TRANSPORT_COMPLETE_QF_ERR: return "COMPLETE_QF_ERR";
  2488. }
  2489. return "(?)";
  2490. }
  2491. static void target_append_str(char **str, const char *txt)
  2492. {
  2493. char *prev = *str;
  2494. *str = *str ? kasprintf(GFP_ATOMIC, "%s,%s", *str, txt) :
  2495. kstrdup(txt, GFP_ATOMIC);
  2496. kfree(prev);
  2497. }
  2498. /*
  2499. * Convert a transport state bitmask into a string. The caller is
  2500. * responsible for freeing the returned pointer.
  2501. */
  2502. static char *target_ts_to_str(u32 ts)
  2503. {
  2504. char *str = NULL;
  2505. if (ts & CMD_T_ABORTED)
  2506. target_append_str(&str, "aborted");
  2507. if (ts & CMD_T_ACTIVE)
  2508. target_append_str(&str, "active");
  2509. if (ts & CMD_T_COMPLETE)
  2510. target_append_str(&str, "complete");
  2511. if (ts & CMD_T_SENT)
  2512. target_append_str(&str, "sent");
  2513. if (ts & CMD_T_STOP)
  2514. target_append_str(&str, "stop");
  2515. if (ts & CMD_T_FABRIC_STOP)
  2516. target_append_str(&str, "fabric_stop");
  2517. return str;
  2518. }
  2519. static const char *target_tmf_name(enum tcm_tmreq_table tmf)
  2520. {
  2521. switch (tmf) {
  2522. case TMR_ABORT_TASK: return "ABORT_TASK";
  2523. case TMR_ABORT_TASK_SET: return "ABORT_TASK_SET";
  2524. case TMR_CLEAR_ACA: return "CLEAR_ACA";
  2525. case TMR_CLEAR_TASK_SET: return "CLEAR_TASK_SET";
  2526. case TMR_LUN_RESET: return "LUN_RESET";
  2527. case TMR_TARGET_WARM_RESET: return "TARGET_WARM_RESET";
  2528. case TMR_TARGET_COLD_RESET: return "TARGET_COLD_RESET";
  2529. case TMR_UNKNOWN: break;
  2530. }
  2531. return "(?)";
  2532. }
  2533. void target_show_cmd(const char *pfx, struct se_cmd *cmd)
  2534. {
  2535. char *ts_str = target_ts_to_str(cmd->transport_state);
  2536. const u8 *cdb = cmd->t_task_cdb;
  2537. struct se_tmr_req *tmf = cmd->se_tmr_req;
  2538. if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  2539. pr_debug("%scmd %#02x:%#02x with tag %#llx dir %s i_state %d t_state %s len %d refcnt %d transport_state %s\n",
  2540. pfx, cdb[0], cdb[1], cmd->tag,
  2541. data_dir_name(cmd->data_direction),
  2542. cmd->se_tfo->get_cmd_state(cmd),
  2543. cmd_state_name(cmd->t_state), cmd->data_length,
  2544. kref_read(&cmd->cmd_kref), ts_str);
  2545. } else {
  2546. pr_debug("%stmf %s with tag %#llx ref_task_tag %#llx i_state %d t_state %s refcnt %d transport_state %s\n",
  2547. pfx, target_tmf_name(tmf->function), cmd->tag,
  2548. tmf->ref_task_tag, cmd->se_tfo->get_cmd_state(cmd),
  2549. cmd_state_name(cmd->t_state),
  2550. kref_read(&cmd->cmd_kref), ts_str);
  2551. }
  2552. kfree(ts_str);
  2553. }
  2554. EXPORT_SYMBOL(target_show_cmd);
  2555. /**
  2556. * target_sess_cmd_list_set_waiting - Set sess_tearing_down so no new commands are queued.
  2557. * @se_sess: session to flag
  2558. */
  2559. void target_sess_cmd_list_set_waiting(struct se_session *se_sess)
  2560. {
  2561. unsigned long flags;
  2562. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  2563. se_sess->sess_tearing_down = 1;
  2564. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  2565. percpu_ref_kill(&se_sess->cmd_count);
  2566. }
  2567. EXPORT_SYMBOL(target_sess_cmd_list_set_waiting);
  2568. /**
  2569. * target_wait_for_sess_cmds - Wait for outstanding commands
  2570. * @se_sess: session to wait for active I/O
  2571. */
  2572. void target_wait_for_sess_cmds(struct se_session *se_sess)
  2573. {
  2574. struct se_cmd *cmd;
  2575. int ret;
  2576. WARN_ON_ONCE(!se_sess->sess_tearing_down);
  2577. do {
  2578. ret = wait_event_timeout(se_sess->cmd_list_wq,
  2579. percpu_ref_is_zero(&se_sess->cmd_count),
  2580. 180 * HZ);
  2581. list_for_each_entry(cmd, &se_sess->sess_cmd_list, se_cmd_list)
  2582. target_show_cmd("session shutdown: still waiting for ",
  2583. cmd);
  2584. } while (ret <= 0);
  2585. }
  2586. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  2587. static void target_lun_confirm(struct percpu_ref *ref)
  2588. {
  2589. struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
  2590. complete(&lun->lun_ref_comp);
  2591. }
  2592. void transport_clear_lun_ref(struct se_lun *lun)
  2593. {
  2594. /*
  2595. * Mark the percpu-ref as DEAD, switch to atomic_t mode, drop
  2596. * the initial reference and schedule confirm kill to be
  2597. * executed after one full RCU grace period has completed.
  2598. */
  2599. percpu_ref_kill_and_confirm(&lun->lun_ref, target_lun_confirm);
  2600. /*
  2601. * The first completion waits for percpu_ref_switch_to_atomic_rcu()
  2602. * to call target_lun_confirm after lun->lun_ref has been marked
  2603. * as __PERCPU_REF_DEAD on all CPUs, and switches to atomic_t
  2604. * mode so that percpu_ref_tryget_live() lookup of lun->lun_ref
  2605. * fails for all new incoming I/O.
  2606. */
  2607. wait_for_completion(&lun->lun_ref_comp);
  2608. /*
  2609. * The second completion waits for percpu_ref_put_many() to
  2610. * invoke ->release() after lun->lun_ref has switched to
  2611. * atomic_t mode, and lun->lun_ref.count has reached zero.
  2612. *
  2613. * At this point all target-core lun->lun_ref references have
  2614. * been dropped via transport_lun_remove_cmd(), and it's safe
  2615. * to proceed with the remaining LUN shutdown.
  2616. */
  2617. wait_for_completion(&lun->lun_shutdown_comp);
  2618. }
  2619. static bool
  2620. __transport_wait_for_tasks(struct se_cmd *cmd, bool fabric_stop,
  2621. bool *aborted, bool *tas, unsigned long *flags)
  2622. __releases(&cmd->t_state_lock)
  2623. __acquires(&cmd->t_state_lock)
  2624. {
  2625. assert_spin_locked(&cmd->t_state_lock);
  2626. WARN_ON_ONCE(!irqs_disabled());
  2627. if (fabric_stop)
  2628. cmd->transport_state |= CMD_T_FABRIC_STOP;
  2629. if (cmd->transport_state & CMD_T_ABORTED)
  2630. *aborted = true;
  2631. if (cmd->transport_state & CMD_T_TAS)
  2632. *tas = true;
  2633. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
  2634. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  2635. return false;
  2636. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
  2637. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  2638. return false;
  2639. if (!(cmd->transport_state & CMD_T_ACTIVE))
  2640. return false;
  2641. if (fabric_stop && *aborted)
  2642. return false;
  2643. cmd->transport_state |= CMD_T_STOP;
  2644. target_show_cmd("wait_for_tasks: Stopping ", cmd);
  2645. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  2646. while (!wait_for_completion_timeout(&cmd->t_transport_stop_comp,
  2647. 180 * HZ))
  2648. target_show_cmd("wait for tasks: ", cmd);
  2649. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  2650. cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
  2651. pr_debug("wait_for_tasks: Stopped wait_for_completion(&cmd->"
  2652. "t_transport_stop_comp) for ITT: 0x%08llx\n", cmd->tag);
  2653. return true;
  2654. }
  2655. /**
  2656. * transport_wait_for_tasks - set CMD_T_STOP and wait for t_transport_stop_comp
  2657. * @cmd: command to wait on
  2658. */
  2659. bool transport_wait_for_tasks(struct se_cmd *cmd)
  2660. {
  2661. unsigned long flags;
  2662. bool ret, aborted = false, tas = false;
  2663. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2664. ret = __transport_wait_for_tasks(cmd, false, &aborted, &tas, &flags);
  2665. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2666. return ret;
  2667. }
  2668. EXPORT_SYMBOL(transport_wait_for_tasks);
  2669. struct sense_info {
  2670. u8 key;
  2671. u8 asc;
  2672. u8 ascq;
  2673. bool add_sector_info;
  2674. };
  2675. static const struct sense_info sense_info_table[] = {
  2676. [TCM_NO_SENSE] = {
  2677. .key = NOT_READY
  2678. },
  2679. [TCM_NON_EXISTENT_LUN] = {
  2680. .key = ILLEGAL_REQUEST,
  2681. .asc = 0x25 /* LOGICAL UNIT NOT SUPPORTED */
  2682. },
  2683. [TCM_UNSUPPORTED_SCSI_OPCODE] = {
  2684. .key = ILLEGAL_REQUEST,
  2685. .asc = 0x20, /* INVALID COMMAND OPERATION CODE */
  2686. },
  2687. [TCM_SECTOR_COUNT_TOO_MANY] = {
  2688. .key = ILLEGAL_REQUEST,
  2689. .asc = 0x20, /* INVALID COMMAND OPERATION CODE */
  2690. },
  2691. [TCM_UNKNOWN_MODE_PAGE] = {
  2692. .key = ILLEGAL_REQUEST,
  2693. .asc = 0x24, /* INVALID FIELD IN CDB */
  2694. },
  2695. [TCM_CHECK_CONDITION_ABORT_CMD] = {
  2696. .key = ABORTED_COMMAND,
  2697. .asc = 0x29, /* BUS DEVICE RESET FUNCTION OCCURRED */
  2698. .ascq = 0x03,
  2699. },
  2700. [TCM_INCORRECT_AMOUNT_OF_DATA] = {
  2701. .key = ABORTED_COMMAND,
  2702. .asc = 0x0c, /* WRITE ERROR */
  2703. .ascq = 0x0d, /* NOT ENOUGH UNSOLICITED DATA */
  2704. },
  2705. [TCM_INVALID_CDB_FIELD] = {
  2706. .key = ILLEGAL_REQUEST,
  2707. .asc = 0x24, /* INVALID FIELD IN CDB */
  2708. },
  2709. [TCM_INVALID_PARAMETER_LIST] = {
  2710. .key = ILLEGAL_REQUEST,
  2711. .asc = 0x26, /* INVALID FIELD IN PARAMETER LIST */
  2712. },
  2713. [TCM_TOO_MANY_TARGET_DESCS] = {
  2714. .key = ILLEGAL_REQUEST,
  2715. .asc = 0x26,
  2716. .ascq = 0x06, /* TOO MANY TARGET DESCRIPTORS */
  2717. },
  2718. [TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE] = {
  2719. .key = ILLEGAL_REQUEST,
  2720. .asc = 0x26,
  2721. .ascq = 0x07, /* UNSUPPORTED TARGET DESCRIPTOR TYPE CODE */
  2722. },
  2723. [TCM_TOO_MANY_SEGMENT_DESCS] = {
  2724. .key = ILLEGAL_REQUEST,
  2725. .asc = 0x26,
  2726. .ascq = 0x08, /* TOO MANY SEGMENT DESCRIPTORS */
  2727. },
  2728. [TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE] = {
  2729. .key = ILLEGAL_REQUEST,
  2730. .asc = 0x26,
  2731. .ascq = 0x09, /* UNSUPPORTED SEGMENT DESCRIPTOR TYPE CODE */
  2732. },
  2733. [TCM_PARAMETER_LIST_LENGTH_ERROR] = {
  2734. .key = ILLEGAL_REQUEST,
  2735. .asc = 0x1a, /* PARAMETER LIST LENGTH ERROR */
  2736. },
  2737. [TCM_UNEXPECTED_UNSOLICITED_DATA] = {
  2738. .key = ILLEGAL_REQUEST,
  2739. .asc = 0x0c, /* WRITE ERROR */
  2740. .ascq = 0x0c, /* UNEXPECTED_UNSOLICITED_DATA */
  2741. },
  2742. [TCM_SERVICE_CRC_ERROR] = {
  2743. .key = ABORTED_COMMAND,
  2744. .asc = 0x47, /* PROTOCOL SERVICE CRC ERROR */
  2745. .ascq = 0x05, /* N/A */
  2746. },
  2747. [TCM_SNACK_REJECTED] = {
  2748. .key = ABORTED_COMMAND,
  2749. .asc = 0x11, /* READ ERROR */
  2750. .ascq = 0x13, /* FAILED RETRANSMISSION REQUEST */
  2751. },
  2752. [TCM_WRITE_PROTECTED] = {
  2753. .key = DATA_PROTECT,
  2754. .asc = 0x27, /* WRITE PROTECTED */
  2755. },
  2756. [TCM_ADDRESS_OUT_OF_RANGE] = {
  2757. .key = ILLEGAL_REQUEST,
  2758. .asc = 0x21, /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
  2759. },
  2760. [TCM_CHECK_CONDITION_UNIT_ATTENTION] = {
  2761. .key = UNIT_ATTENTION,
  2762. },
  2763. [TCM_CHECK_CONDITION_NOT_READY] = {
  2764. .key = NOT_READY,
  2765. },
  2766. [TCM_MISCOMPARE_VERIFY] = {
  2767. .key = MISCOMPARE,
  2768. .asc = 0x1d, /* MISCOMPARE DURING VERIFY OPERATION */
  2769. .ascq = 0x00,
  2770. },
  2771. [TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED] = {
  2772. .key = ABORTED_COMMAND,
  2773. .asc = 0x10,
  2774. .ascq = 0x01, /* LOGICAL BLOCK GUARD CHECK FAILED */
  2775. .add_sector_info = true,
  2776. },
  2777. [TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED] = {
  2778. .key = ABORTED_COMMAND,
  2779. .asc = 0x10,
  2780. .ascq = 0x02, /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */
  2781. .add_sector_info = true,
  2782. },
  2783. [TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED] = {
  2784. .key = ABORTED_COMMAND,
  2785. .asc = 0x10,
  2786. .ascq = 0x03, /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */
  2787. .add_sector_info = true,
  2788. },
  2789. [TCM_COPY_TARGET_DEVICE_NOT_REACHABLE] = {
  2790. .key = COPY_ABORTED,
  2791. .asc = 0x0d,
  2792. .ascq = 0x02, /* COPY TARGET DEVICE NOT REACHABLE */
  2793. },
  2794. [TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE] = {
  2795. /*
  2796. * Returning ILLEGAL REQUEST would cause immediate IO errors on
  2797. * Solaris initiators. Returning NOT READY instead means the
  2798. * operations will be retried a finite number of times and we
  2799. * can survive intermittent errors.
  2800. */
  2801. .key = NOT_READY,
  2802. .asc = 0x08, /* LOGICAL UNIT COMMUNICATION FAILURE */
  2803. },
  2804. [TCM_INSUFFICIENT_REGISTRATION_RESOURCES] = {
  2805. /*
  2806. * From spc4r22 section5.7.7,5.7.8
  2807. * If a PERSISTENT RESERVE OUT command with a REGISTER service action
  2808. * or a REGISTER AND IGNORE EXISTING KEY service action or
  2809. * REGISTER AND MOVE service actionis attempted,
  2810. * but there are insufficient device server resources to complete the
  2811. * operation, then the command shall be terminated with CHECK CONDITION
  2812. * status, with the sense key set to ILLEGAL REQUEST,and the additonal
  2813. * sense code set to INSUFFICIENT REGISTRATION RESOURCES.
  2814. */
  2815. .key = ILLEGAL_REQUEST,
  2816. .asc = 0x55,
  2817. .ascq = 0x04, /* INSUFFICIENT REGISTRATION RESOURCES */
  2818. },
  2819. };
  2820. /**
  2821. * translate_sense_reason - translate a sense reason into T10 key, asc and ascq
  2822. * @cmd: SCSI command in which the resulting sense buffer or SCSI status will
  2823. * be stored.
  2824. * @reason: LIO sense reason code. If this argument has the value
  2825. * TCM_CHECK_CONDITION_UNIT_ATTENTION, try to dequeue a unit attention. If
  2826. * dequeuing a unit attention fails due to multiple commands being processed
  2827. * concurrently, set the command status to BUSY.
  2828. *
  2829. * Return: 0 upon success or -EINVAL if the sense buffer is too small.
  2830. */
  2831. static void translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason)
  2832. {
  2833. const struct sense_info *si;
  2834. u8 *buffer = cmd->sense_buffer;
  2835. int r = (__force int)reason;
  2836. u8 key, asc, ascq;
  2837. bool desc_format = target_sense_desc_format(cmd->se_dev);
  2838. if (r < ARRAY_SIZE(sense_info_table) && sense_info_table[r].key)
  2839. si = &sense_info_table[r];
  2840. else
  2841. si = &sense_info_table[(__force int)
  2842. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE];
  2843. key = si->key;
  2844. if (reason == TCM_CHECK_CONDITION_UNIT_ATTENTION) {
  2845. if (!core_scsi3_ua_for_check_condition(cmd, &key, &asc,
  2846. &ascq)) {
  2847. cmd->scsi_status = SAM_STAT_BUSY;
  2848. return;
  2849. }
  2850. } else if (si->asc == 0) {
  2851. WARN_ON_ONCE(cmd->scsi_asc == 0);
  2852. asc = cmd->scsi_asc;
  2853. ascq = cmd->scsi_ascq;
  2854. } else {
  2855. asc = si->asc;
  2856. ascq = si->ascq;
  2857. }
  2858. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  2859. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  2860. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
  2861. scsi_build_sense_buffer(desc_format, buffer, key, asc, ascq);
  2862. if (si->add_sector_info)
  2863. WARN_ON_ONCE(scsi_set_sense_information(buffer,
  2864. cmd->scsi_sense_length,
  2865. cmd->bad_sector) < 0);
  2866. }
  2867. int
  2868. transport_send_check_condition_and_sense(struct se_cmd *cmd,
  2869. sense_reason_t reason, int from_transport)
  2870. {
  2871. unsigned long flags;
  2872. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2873. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2874. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2875. return 0;
  2876. }
  2877. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  2878. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2879. if (!from_transport)
  2880. translate_sense_reason(cmd, reason);
  2881. trace_target_cmd_complete(cmd);
  2882. return cmd->se_tfo->queue_status(cmd);
  2883. }
  2884. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  2885. static int __transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  2886. __releases(&cmd->t_state_lock)
  2887. __acquires(&cmd->t_state_lock)
  2888. {
  2889. int ret;
  2890. assert_spin_locked(&cmd->t_state_lock);
  2891. WARN_ON_ONCE(!irqs_disabled());
  2892. if (!(cmd->transport_state & CMD_T_ABORTED))
  2893. return 0;
  2894. /*
  2895. * If cmd has been aborted but either no status is to be sent or it has
  2896. * already been sent, just return
  2897. */
  2898. if (!send_status || !(cmd->se_cmd_flags & SCF_SEND_DELAYED_TAS)) {
  2899. if (send_status)
  2900. cmd->se_cmd_flags |= SCF_SEND_DELAYED_TAS;
  2901. return 1;
  2902. }
  2903. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB:"
  2904. " 0x%02x ITT: 0x%08llx\n", cmd->t_task_cdb[0], cmd->tag);
  2905. cmd->se_cmd_flags &= ~SCF_SEND_DELAYED_TAS;
  2906. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  2907. trace_target_cmd_complete(cmd);
  2908. spin_unlock_irq(&cmd->t_state_lock);
  2909. ret = cmd->se_tfo->queue_status(cmd);
  2910. if (ret)
  2911. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  2912. spin_lock_irq(&cmd->t_state_lock);
  2913. return 1;
  2914. }
  2915. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  2916. {
  2917. int ret;
  2918. spin_lock_irq(&cmd->t_state_lock);
  2919. ret = __transport_check_aborted_status(cmd, send_status);
  2920. spin_unlock_irq(&cmd->t_state_lock);
  2921. return ret;
  2922. }
  2923. EXPORT_SYMBOL(transport_check_aborted_status);
  2924. void transport_send_task_abort(struct se_cmd *cmd)
  2925. {
  2926. unsigned long flags;
  2927. int ret;
  2928. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2929. if (cmd->se_cmd_flags & (SCF_SENT_CHECK_CONDITION)) {
  2930. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2931. return;
  2932. }
  2933. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2934. /*
  2935. * If there are still expected incoming fabric WRITEs, we wait
  2936. * until until they have completed before sending a TASK_ABORTED
  2937. * response. This response with TASK_ABORTED status will be
  2938. * queued back to fabric module by transport_check_aborted_status().
  2939. */
  2940. if (cmd->data_direction == DMA_TO_DEVICE) {
  2941. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  2942. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2943. if (cmd->se_cmd_flags & SCF_SEND_DELAYED_TAS) {
  2944. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2945. goto send_abort;
  2946. }
  2947. cmd->se_cmd_flags |= SCF_SEND_DELAYED_TAS;
  2948. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2949. return;
  2950. }
  2951. }
  2952. send_abort:
  2953. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  2954. transport_lun_remove_cmd(cmd);
  2955. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x, ITT: 0x%08llx\n",
  2956. cmd->t_task_cdb[0], cmd->tag);
  2957. trace_target_cmd_complete(cmd);
  2958. ret = cmd->se_tfo->queue_status(cmd);
  2959. if (ret)
  2960. transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
  2961. }
  2962. static void target_tmr_work(struct work_struct *work)
  2963. {
  2964. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2965. struct se_device *dev = cmd->se_dev;
  2966. struct se_tmr_req *tmr = cmd->se_tmr_req;
  2967. unsigned long flags;
  2968. int ret;
  2969. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2970. if (cmd->transport_state & CMD_T_ABORTED) {
  2971. tmr->response = TMR_FUNCTION_REJECTED;
  2972. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2973. goto check_stop;
  2974. }
  2975. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2976. switch (tmr->function) {
  2977. case TMR_ABORT_TASK:
  2978. core_tmr_abort_task(dev, tmr, cmd->se_sess);
  2979. break;
  2980. case TMR_ABORT_TASK_SET:
  2981. case TMR_CLEAR_ACA:
  2982. case TMR_CLEAR_TASK_SET:
  2983. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  2984. break;
  2985. case TMR_LUN_RESET:
  2986. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  2987. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  2988. TMR_FUNCTION_REJECTED;
  2989. if (tmr->response == TMR_FUNCTION_COMPLETE) {
  2990. target_ua_allocate_lun(cmd->se_sess->se_node_acl,
  2991. cmd->orig_fe_lun, 0x29,
  2992. ASCQ_29H_BUS_DEVICE_RESET_FUNCTION_OCCURRED);
  2993. }
  2994. break;
  2995. case TMR_TARGET_WARM_RESET:
  2996. tmr->response = TMR_FUNCTION_REJECTED;
  2997. break;
  2998. case TMR_TARGET_COLD_RESET:
  2999. tmr->response = TMR_FUNCTION_REJECTED;
  3000. break;
  3001. default:
  3002. pr_err("Unknown TMR function: 0x%02x.\n",
  3003. tmr->function);
  3004. tmr->response = TMR_FUNCTION_REJECTED;
  3005. break;
  3006. }
  3007. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3008. if (cmd->transport_state & CMD_T_ABORTED) {
  3009. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3010. goto check_stop;
  3011. }
  3012. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3013. cmd->se_tfo->queue_tm_rsp(cmd);
  3014. check_stop:
  3015. transport_lun_remove_cmd(cmd);
  3016. transport_cmd_check_stop_to_fabric(cmd);
  3017. }
  3018. int transport_generic_handle_tmr(
  3019. struct se_cmd *cmd)
  3020. {
  3021. unsigned long flags;
  3022. bool aborted = false;
  3023. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3024. if (cmd->transport_state & CMD_T_ABORTED) {
  3025. aborted = true;
  3026. } else {
  3027. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  3028. cmd->transport_state |= CMD_T_ACTIVE;
  3029. }
  3030. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3031. if (aborted) {
  3032. pr_warn_ratelimited("handle_tmr caught CMD_T_ABORTED TMR %d"
  3033. "ref_tag: %llu tag: %llu\n", cmd->se_tmr_req->function,
  3034. cmd->se_tmr_req->ref_task_tag, cmd->tag);
  3035. transport_lun_remove_cmd(cmd);
  3036. transport_cmd_check_stop_to_fabric(cmd);
  3037. return 0;
  3038. }
  3039. INIT_WORK(&cmd->work, target_tmr_work);
  3040. queue_work(cmd->se_dev->tmr_wq, &cmd->work);
  3041. return 0;
  3042. }
  3043. EXPORT_SYMBOL(transport_generic_handle_tmr);
  3044. bool
  3045. target_check_wce(struct se_device *dev)
  3046. {
  3047. bool wce = false;
  3048. if (dev->transport->get_write_cache)
  3049. wce = dev->transport->get_write_cache(dev);
  3050. else if (dev->dev_attrib.emulate_write_cache > 0)
  3051. wce = true;
  3052. return wce;
  3053. }
  3054. bool
  3055. target_check_fua(struct se_device *dev)
  3056. {
  3057. return target_check_wce(dev) && dev->dev_attrib.emulate_fua_write > 0;
  3058. }