target_core_transport.c 80 KB

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