target_core_transport.c 95 KB

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