target_core_transport.c 90 KB

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