target_core_transport.c 84 KB

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