target_core_transport.c 78 KB

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