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