target_core_transport.c 79 KB

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