target_core_transport.c 91 KB

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