target_core_transport.c 86 KB

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