target_core_transport.c 87 KB

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