target_core_transport.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376
  1. /*******************************************************************************
  2. * Filename: target_core_transport.c
  3. *
  4. * This file contains the Generic Target Engine Core.
  5. *
  6. * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
  7. * Copyright (c) 2005, 2006, 2007 SBE, Inc.
  8. * Copyright (c) 2007-2010 Rising Tide Systems
  9. * Copyright (c) 2008-2010 Linux-iSCSI.org
  10. *
  11. * Nicholas A. Bellinger <nab@kernel.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. *
  27. ******************************************************************************/
  28. #include <linux/net.h>
  29. #include <linux/delay.h>
  30. #include <linux/string.h>
  31. #include <linux/timer.h>
  32. #include <linux/slab.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/kthread.h>
  36. #include <linux/in.h>
  37. #include <linux/cdrom.h>
  38. #include <linux/module.h>
  39. #include <linux/ratelimit.h>
  40. #include <asm/unaligned.h>
  41. #include <net/sock.h>
  42. #include <net/tcp.h>
  43. #include <scsi/scsi.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include <target/target_core_base.h>
  47. #include <target/target_core_backend.h>
  48. #include <target/target_core_fabric.h>
  49. #include <target/target_core_configfs.h>
  50. #include "target_core_internal.h"
  51. #include "target_core_alua.h"
  52. #include "target_core_pr.h"
  53. #include "target_core_ua.h"
  54. static int sub_api_initialized;
  55. static struct workqueue_struct *target_completion_wq;
  56. static struct kmem_cache *se_sess_cache;
  57. struct kmem_cache *se_ua_cache;
  58. struct kmem_cache *t10_pr_reg_cache;
  59. struct kmem_cache *t10_alua_lu_gp_cache;
  60. struct kmem_cache *t10_alua_lu_gp_mem_cache;
  61. struct kmem_cache *t10_alua_tg_pt_gp_cache;
  62. struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
  63. static int transport_generic_write_pending(struct se_cmd *);
  64. static int transport_processing_thread(void *param);
  65. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
  66. static void transport_complete_task_attr(struct se_cmd *cmd);
  67. static void transport_handle_queue_full(struct se_cmd *cmd,
  68. struct se_device *dev);
  69. static int transport_generic_get_mem(struct se_cmd *cmd);
  70. static void transport_put_cmd(struct se_cmd *cmd);
  71. static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
  72. static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
  73. static void target_complete_ok_work(struct work_struct *work);
  74. int init_se_kmem_caches(void)
  75. {
  76. se_sess_cache = kmem_cache_create("se_sess_cache",
  77. sizeof(struct se_session), __alignof__(struct se_session),
  78. 0, NULL);
  79. if (!se_sess_cache) {
  80. pr_err("kmem_cache_create() for struct se_session"
  81. " failed\n");
  82. goto out;
  83. }
  84. se_ua_cache = kmem_cache_create("se_ua_cache",
  85. sizeof(struct se_ua), __alignof__(struct se_ua),
  86. 0, NULL);
  87. if (!se_ua_cache) {
  88. pr_err("kmem_cache_create() for struct se_ua failed\n");
  89. goto out_free_sess_cache;
  90. }
  91. t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
  92. sizeof(struct t10_pr_registration),
  93. __alignof__(struct t10_pr_registration), 0, NULL);
  94. if (!t10_pr_reg_cache) {
  95. pr_err("kmem_cache_create() for struct t10_pr_registration"
  96. " failed\n");
  97. goto out_free_ua_cache;
  98. }
  99. t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
  100. sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
  101. 0, NULL);
  102. if (!t10_alua_lu_gp_cache) {
  103. pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
  104. " failed\n");
  105. goto out_free_pr_reg_cache;
  106. }
  107. t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
  108. sizeof(struct t10_alua_lu_gp_member),
  109. __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
  110. if (!t10_alua_lu_gp_mem_cache) {
  111. pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
  112. "cache failed\n");
  113. goto out_free_lu_gp_cache;
  114. }
  115. t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
  116. sizeof(struct t10_alua_tg_pt_gp),
  117. __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
  118. if (!t10_alua_tg_pt_gp_cache) {
  119. pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
  120. "cache failed\n");
  121. goto out_free_lu_gp_mem_cache;
  122. }
  123. t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
  124. "t10_alua_tg_pt_gp_mem_cache",
  125. sizeof(struct t10_alua_tg_pt_gp_member),
  126. __alignof__(struct t10_alua_tg_pt_gp_member),
  127. 0, NULL);
  128. if (!t10_alua_tg_pt_gp_mem_cache) {
  129. pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
  130. "mem_t failed\n");
  131. goto out_free_tg_pt_gp_cache;
  132. }
  133. target_completion_wq = alloc_workqueue("target_completion",
  134. WQ_MEM_RECLAIM, 0);
  135. if (!target_completion_wq)
  136. goto out_free_tg_pt_gp_mem_cache;
  137. return 0;
  138. out_free_tg_pt_gp_mem_cache:
  139. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  140. out_free_tg_pt_gp_cache:
  141. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  142. out_free_lu_gp_mem_cache:
  143. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  144. out_free_lu_gp_cache:
  145. kmem_cache_destroy(t10_alua_lu_gp_cache);
  146. out_free_pr_reg_cache:
  147. kmem_cache_destroy(t10_pr_reg_cache);
  148. out_free_ua_cache:
  149. kmem_cache_destroy(se_ua_cache);
  150. out_free_sess_cache:
  151. kmem_cache_destroy(se_sess_cache);
  152. out:
  153. return -ENOMEM;
  154. }
  155. void release_se_kmem_caches(void)
  156. {
  157. destroy_workqueue(target_completion_wq);
  158. kmem_cache_destroy(se_sess_cache);
  159. kmem_cache_destroy(se_ua_cache);
  160. kmem_cache_destroy(t10_pr_reg_cache);
  161. kmem_cache_destroy(t10_alua_lu_gp_cache);
  162. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  163. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  164. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  165. }
  166. /* This code ensures unique mib indexes are handed out. */
  167. static DEFINE_SPINLOCK(scsi_mib_index_lock);
  168. static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
  169. /*
  170. * Allocate a new row index for the entry type specified
  171. */
  172. u32 scsi_get_new_index(scsi_index_t type)
  173. {
  174. u32 new_index;
  175. BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
  176. spin_lock(&scsi_mib_index_lock);
  177. new_index = ++scsi_mib_index[type];
  178. spin_unlock(&scsi_mib_index_lock);
  179. return new_index;
  180. }
  181. static void transport_init_queue_obj(struct se_queue_obj *qobj)
  182. {
  183. atomic_set(&qobj->queue_cnt, 0);
  184. INIT_LIST_HEAD(&qobj->qobj_list);
  185. init_waitqueue_head(&qobj->thread_wq);
  186. spin_lock_init(&qobj->cmd_queue_lock);
  187. }
  188. void transport_subsystem_check_init(void)
  189. {
  190. int ret;
  191. if (sub_api_initialized)
  192. return;
  193. ret = request_module("target_core_iblock");
  194. if (ret != 0)
  195. pr_err("Unable to load target_core_iblock\n");
  196. ret = request_module("target_core_file");
  197. if (ret != 0)
  198. pr_err("Unable to load target_core_file\n");
  199. ret = request_module("target_core_pscsi");
  200. if (ret != 0)
  201. pr_err("Unable to load target_core_pscsi\n");
  202. ret = request_module("target_core_stgt");
  203. if (ret != 0)
  204. pr_err("Unable to load target_core_stgt\n");
  205. sub_api_initialized = 1;
  206. return;
  207. }
  208. struct se_session *transport_init_session(void)
  209. {
  210. struct se_session *se_sess;
  211. se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
  212. if (!se_sess) {
  213. pr_err("Unable to allocate struct se_session from"
  214. " se_sess_cache\n");
  215. return ERR_PTR(-ENOMEM);
  216. }
  217. INIT_LIST_HEAD(&se_sess->sess_list);
  218. INIT_LIST_HEAD(&se_sess->sess_acl_list);
  219. INIT_LIST_HEAD(&se_sess->sess_cmd_list);
  220. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  221. spin_lock_init(&se_sess->sess_cmd_lock);
  222. kref_init(&se_sess->sess_kref);
  223. return se_sess;
  224. }
  225. EXPORT_SYMBOL(transport_init_session);
  226. /*
  227. * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
  228. */
  229. void __transport_register_session(
  230. struct se_portal_group *se_tpg,
  231. struct se_node_acl *se_nacl,
  232. struct se_session *se_sess,
  233. void *fabric_sess_ptr)
  234. {
  235. unsigned char buf[PR_REG_ISID_LEN];
  236. se_sess->se_tpg = se_tpg;
  237. se_sess->fabric_sess_ptr = fabric_sess_ptr;
  238. /*
  239. * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
  240. *
  241. * Only set for struct se_session's that will actually be moving I/O.
  242. * eg: *NOT* discovery sessions.
  243. */
  244. if (se_nacl) {
  245. /*
  246. * If the fabric module supports an ISID based TransportID,
  247. * save this value in binary from the fabric I_T Nexus now.
  248. */
  249. if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
  250. memset(&buf[0], 0, PR_REG_ISID_LEN);
  251. se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
  252. &buf[0], PR_REG_ISID_LEN);
  253. se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
  254. }
  255. kref_get(&se_nacl->acl_kref);
  256. spin_lock_irq(&se_nacl->nacl_sess_lock);
  257. /*
  258. * The se_nacl->nacl_sess pointer will be set to the
  259. * last active I_T Nexus for each struct se_node_acl.
  260. */
  261. se_nacl->nacl_sess = se_sess;
  262. list_add_tail(&se_sess->sess_acl_list,
  263. &se_nacl->acl_sess_list);
  264. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  265. }
  266. list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
  267. pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
  268. se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
  269. }
  270. EXPORT_SYMBOL(__transport_register_session);
  271. void transport_register_session(
  272. struct se_portal_group *se_tpg,
  273. struct se_node_acl *se_nacl,
  274. struct se_session *se_sess,
  275. void *fabric_sess_ptr)
  276. {
  277. unsigned long flags;
  278. spin_lock_irqsave(&se_tpg->session_lock, flags);
  279. __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
  280. spin_unlock_irqrestore(&se_tpg->session_lock, flags);
  281. }
  282. EXPORT_SYMBOL(transport_register_session);
  283. void target_release_session(struct kref *kref)
  284. {
  285. struct se_session *se_sess = container_of(kref,
  286. struct se_session, sess_kref);
  287. struct se_portal_group *se_tpg = se_sess->se_tpg;
  288. se_tpg->se_tpg_tfo->close_session(se_sess);
  289. }
  290. void target_get_session(struct se_session *se_sess)
  291. {
  292. kref_get(&se_sess->sess_kref);
  293. }
  294. EXPORT_SYMBOL(target_get_session);
  295. void target_put_session(struct se_session *se_sess)
  296. {
  297. struct se_portal_group *tpg = se_sess->se_tpg;
  298. if (tpg->se_tpg_tfo->put_session != NULL) {
  299. tpg->se_tpg_tfo->put_session(se_sess);
  300. return;
  301. }
  302. kref_put(&se_sess->sess_kref, target_release_session);
  303. }
  304. EXPORT_SYMBOL(target_put_session);
  305. static void target_complete_nacl(struct kref *kref)
  306. {
  307. struct se_node_acl *nacl = container_of(kref,
  308. struct se_node_acl, acl_kref);
  309. complete(&nacl->acl_free_comp);
  310. }
  311. void target_put_nacl(struct se_node_acl *nacl)
  312. {
  313. kref_put(&nacl->acl_kref, target_complete_nacl);
  314. }
  315. void transport_deregister_session_configfs(struct se_session *se_sess)
  316. {
  317. struct se_node_acl *se_nacl;
  318. unsigned long flags;
  319. /*
  320. * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
  321. */
  322. se_nacl = se_sess->se_node_acl;
  323. if (se_nacl) {
  324. spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
  325. if (se_nacl->acl_stop == 0)
  326. list_del(&se_sess->sess_acl_list);
  327. /*
  328. * If the session list is empty, then clear the pointer.
  329. * Otherwise, set the struct se_session pointer from the tail
  330. * element of the per struct se_node_acl active session list.
  331. */
  332. if (list_empty(&se_nacl->acl_sess_list))
  333. se_nacl->nacl_sess = NULL;
  334. else {
  335. se_nacl->nacl_sess = container_of(
  336. se_nacl->acl_sess_list.prev,
  337. struct se_session, sess_acl_list);
  338. }
  339. spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
  340. }
  341. }
  342. EXPORT_SYMBOL(transport_deregister_session_configfs);
  343. void transport_free_session(struct se_session *se_sess)
  344. {
  345. kmem_cache_free(se_sess_cache, se_sess);
  346. }
  347. EXPORT_SYMBOL(transport_free_session);
  348. void transport_deregister_session(struct se_session *se_sess)
  349. {
  350. struct se_portal_group *se_tpg = se_sess->se_tpg;
  351. struct target_core_fabric_ops *se_tfo;
  352. struct se_node_acl *se_nacl;
  353. unsigned long flags;
  354. bool comp_nacl = true;
  355. if (!se_tpg) {
  356. transport_free_session(se_sess);
  357. return;
  358. }
  359. se_tfo = se_tpg->se_tpg_tfo;
  360. spin_lock_irqsave(&se_tpg->session_lock, flags);
  361. list_del(&se_sess->sess_list);
  362. se_sess->se_tpg = NULL;
  363. se_sess->fabric_sess_ptr = NULL;
  364. spin_unlock_irqrestore(&se_tpg->session_lock, flags);
  365. /*
  366. * Determine if we need to do extra work for this initiator node's
  367. * struct se_node_acl if it had been previously dynamically generated.
  368. */
  369. se_nacl = se_sess->se_node_acl;
  370. spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
  371. if (se_nacl && se_nacl->dynamic_node_acl) {
  372. if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
  373. list_del(&se_nacl->acl_list);
  374. se_tpg->num_node_acls--;
  375. spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
  376. core_tpg_wait_for_nacl_pr_ref(se_nacl);
  377. core_free_device_list_for_node(se_nacl, se_tpg);
  378. se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
  379. comp_nacl = false;
  380. spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
  381. }
  382. }
  383. spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
  384. pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
  385. se_tpg->se_tpg_tfo->get_fabric_name());
  386. /*
  387. * If last kref is dropping now for an explict NodeACL, awake sleeping
  388. * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
  389. * removal context.
  390. */
  391. if (se_nacl && comp_nacl == true)
  392. target_put_nacl(se_nacl);
  393. transport_free_session(se_sess);
  394. }
  395. EXPORT_SYMBOL(transport_deregister_session);
  396. /*
  397. * Called with cmd->t_state_lock held.
  398. */
  399. static void target_remove_from_state_list(struct se_cmd *cmd)
  400. {
  401. struct se_device *dev = cmd->se_dev;
  402. unsigned long flags;
  403. if (!dev)
  404. return;
  405. if (cmd->transport_state & CMD_T_BUSY)
  406. return;
  407. spin_lock_irqsave(&dev->execute_task_lock, flags);
  408. if (cmd->state_active) {
  409. list_del(&cmd->state_list);
  410. cmd->state_active = false;
  411. }
  412. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  413. }
  414. /* transport_cmd_check_stop():
  415. *
  416. * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
  417. * 'transport_off = 2' determines if task_dev_state should be removed.
  418. *
  419. * A non-zero u8 t_state sets cmd->t_state.
  420. * Returns 1 when command is stopped, else 0.
  421. */
  422. static int transport_cmd_check_stop(
  423. struct se_cmd *cmd,
  424. int transport_off,
  425. u8 t_state)
  426. {
  427. unsigned long flags;
  428. spin_lock_irqsave(&cmd->t_state_lock, flags);
  429. /*
  430. * Determine if IOCTL context caller in requesting the stopping of this
  431. * command for LUN shutdown purposes.
  432. */
  433. if (cmd->transport_state & CMD_T_LUN_STOP) {
  434. pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
  435. __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
  436. cmd->transport_state &= ~CMD_T_ACTIVE;
  437. if (transport_off == 2)
  438. target_remove_from_state_list(cmd);
  439. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  440. complete(&cmd->transport_lun_stop_comp);
  441. return 1;
  442. }
  443. /*
  444. * Determine if frontend context caller is requesting the stopping of
  445. * this command for frontend exceptions.
  446. */
  447. if (cmd->transport_state & CMD_T_STOP) {
  448. pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
  449. __func__, __LINE__,
  450. cmd->se_tfo->get_task_tag(cmd));
  451. if (transport_off == 2)
  452. target_remove_from_state_list(cmd);
  453. /*
  454. * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
  455. * to FE.
  456. */
  457. if (transport_off == 2)
  458. cmd->se_lun = NULL;
  459. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  460. complete(&cmd->t_transport_stop_comp);
  461. return 1;
  462. }
  463. if (transport_off) {
  464. cmd->transport_state &= ~CMD_T_ACTIVE;
  465. if (transport_off == 2) {
  466. target_remove_from_state_list(cmd);
  467. /*
  468. * Clear struct se_cmd->se_lun before the transport_off == 2
  469. * handoff to fabric module.
  470. */
  471. cmd->se_lun = NULL;
  472. /*
  473. * Some fabric modules like tcm_loop can release
  474. * their internally allocated I/O reference now and
  475. * struct se_cmd now.
  476. *
  477. * Fabric modules are expected to return '1' here if the
  478. * se_cmd being passed is released at this point,
  479. * or zero if not being released.
  480. */
  481. if (cmd->se_tfo->check_stop_free != NULL) {
  482. spin_unlock_irqrestore(
  483. &cmd->t_state_lock, flags);
  484. return cmd->se_tfo->check_stop_free(cmd);
  485. }
  486. }
  487. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  488. return 0;
  489. } else if (t_state)
  490. cmd->t_state = t_state;
  491. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  492. return 0;
  493. }
  494. static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
  495. {
  496. return transport_cmd_check_stop(cmd, 2, 0);
  497. }
  498. static void transport_lun_remove_cmd(struct se_cmd *cmd)
  499. {
  500. struct se_lun *lun = cmd->se_lun;
  501. unsigned long flags;
  502. if (!lun)
  503. return;
  504. spin_lock_irqsave(&cmd->t_state_lock, flags);
  505. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  506. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  507. target_remove_from_state_list(cmd);
  508. }
  509. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  510. spin_lock_irqsave(&lun->lun_cmd_lock, flags);
  511. if (!list_empty(&cmd->se_lun_node))
  512. list_del_init(&cmd->se_lun_node);
  513. spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
  514. }
  515. void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
  516. {
  517. if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  518. transport_lun_remove_cmd(cmd);
  519. if (transport_cmd_check_stop_to_fabric(cmd))
  520. return;
  521. if (remove) {
  522. transport_remove_cmd_from_queue(cmd);
  523. transport_put_cmd(cmd);
  524. }
  525. }
  526. static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
  527. bool at_head)
  528. {
  529. struct se_device *dev = cmd->se_dev;
  530. struct se_queue_obj *qobj = &dev->dev_queue_obj;
  531. unsigned long flags;
  532. if (t_state) {
  533. spin_lock_irqsave(&cmd->t_state_lock, flags);
  534. cmd->t_state = t_state;
  535. cmd->transport_state |= CMD_T_ACTIVE;
  536. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  537. }
  538. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  539. /* If the cmd is already on the list, remove it before we add it */
  540. if (!list_empty(&cmd->se_queue_node))
  541. list_del(&cmd->se_queue_node);
  542. else
  543. atomic_inc(&qobj->queue_cnt);
  544. if (at_head)
  545. list_add(&cmd->se_queue_node, &qobj->qobj_list);
  546. else
  547. list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
  548. cmd->transport_state |= CMD_T_QUEUED;
  549. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  550. wake_up_interruptible(&qobj->thread_wq);
  551. }
  552. static struct se_cmd *
  553. transport_get_cmd_from_queue(struct se_queue_obj *qobj)
  554. {
  555. struct se_cmd *cmd;
  556. unsigned long flags;
  557. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  558. if (list_empty(&qobj->qobj_list)) {
  559. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  560. return NULL;
  561. }
  562. cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
  563. cmd->transport_state &= ~CMD_T_QUEUED;
  564. list_del_init(&cmd->se_queue_node);
  565. atomic_dec(&qobj->queue_cnt);
  566. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  567. return cmd;
  568. }
  569. static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
  570. {
  571. struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
  572. unsigned long flags;
  573. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  574. if (!(cmd->transport_state & CMD_T_QUEUED)) {
  575. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  576. return;
  577. }
  578. cmd->transport_state &= ~CMD_T_QUEUED;
  579. atomic_dec(&qobj->queue_cnt);
  580. list_del_init(&cmd->se_queue_node);
  581. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  582. }
  583. static void target_complete_failure_work(struct work_struct *work)
  584. {
  585. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  586. transport_generic_request_failure(cmd);
  587. }
  588. void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
  589. {
  590. struct se_device *dev = cmd->se_dev;
  591. int success = scsi_status == GOOD;
  592. unsigned long flags;
  593. cmd->scsi_status = scsi_status;
  594. spin_lock_irqsave(&cmd->t_state_lock, flags);
  595. cmd->transport_state &= ~CMD_T_BUSY;
  596. if (dev && dev->transport->transport_complete) {
  597. if (dev->transport->transport_complete(cmd,
  598. cmd->t_data_sg) != 0) {
  599. cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
  600. success = 1;
  601. }
  602. }
  603. /*
  604. * See if we are waiting to complete for an exception condition.
  605. */
  606. if (cmd->transport_state & CMD_T_REQUEST_STOP) {
  607. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  608. complete(&cmd->task_stop_comp);
  609. return;
  610. }
  611. if (!success)
  612. cmd->transport_state |= CMD_T_FAILED;
  613. /*
  614. * Check for case where an explict ABORT_TASK has been received
  615. * and transport_wait_for_tasks() will be waiting for completion..
  616. */
  617. if (cmd->transport_state & CMD_T_ABORTED &&
  618. cmd->transport_state & CMD_T_STOP) {
  619. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  620. complete(&cmd->t_transport_stop_comp);
  621. return;
  622. } else if (cmd->transport_state & CMD_T_FAILED) {
  623. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  624. INIT_WORK(&cmd->work, target_complete_failure_work);
  625. } else {
  626. INIT_WORK(&cmd->work, target_complete_ok_work);
  627. }
  628. cmd->t_state = TRANSPORT_COMPLETE;
  629. cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
  630. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  631. queue_work(target_completion_wq, &cmd->work);
  632. }
  633. EXPORT_SYMBOL(target_complete_cmd);
  634. static void target_add_to_state_list(struct se_cmd *cmd)
  635. {
  636. struct se_device *dev = cmd->se_dev;
  637. unsigned long flags;
  638. spin_lock_irqsave(&dev->execute_task_lock, flags);
  639. if (!cmd->state_active) {
  640. list_add_tail(&cmd->state_list, &dev->state_list);
  641. cmd->state_active = true;
  642. }
  643. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  644. }
  645. static void __target_add_to_execute_list(struct se_cmd *cmd)
  646. {
  647. struct se_device *dev = cmd->se_dev;
  648. bool head_of_queue = false;
  649. if (!list_empty(&cmd->execute_list))
  650. return;
  651. if (dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED &&
  652. cmd->sam_task_attr == MSG_HEAD_TAG)
  653. head_of_queue = true;
  654. if (head_of_queue)
  655. list_add(&cmd->execute_list, &dev->execute_list);
  656. else
  657. list_add_tail(&cmd->execute_list, &dev->execute_list);
  658. atomic_inc(&dev->execute_tasks);
  659. if (cmd->state_active)
  660. return;
  661. if (head_of_queue)
  662. list_add(&cmd->state_list, &dev->state_list);
  663. else
  664. list_add_tail(&cmd->state_list, &dev->state_list);
  665. cmd->state_active = true;
  666. }
  667. static void target_add_to_execute_list(struct se_cmd *cmd)
  668. {
  669. unsigned long flags;
  670. struct se_device *dev = cmd->se_dev;
  671. spin_lock_irqsave(&dev->execute_task_lock, flags);
  672. __target_add_to_execute_list(cmd);
  673. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  674. }
  675. void __target_remove_from_execute_list(struct se_cmd *cmd)
  676. {
  677. list_del_init(&cmd->execute_list);
  678. atomic_dec(&cmd->se_dev->execute_tasks);
  679. }
  680. static void target_remove_from_execute_list(struct se_cmd *cmd)
  681. {
  682. struct se_device *dev = cmd->se_dev;
  683. unsigned long flags;
  684. if (WARN_ON(list_empty(&cmd->execute_list)))
  685. return;
  686. spin_lock_irqsave(&dev->execute_task_lock, flags);
  687. __target_remove_from_execute_list(cmd);
  688. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  689. }
  690. /*
  691. * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
  692. */
  693. static void target_qf_do_work(struct work_struct *work)
  694. {
  695. struct se_device *dev = container_of(work, struct se_device,
  696. qf_work_queue);
  697. LIST_HEAD(qf_cmd_list);
  698. struct se_cmd *cmd, *cmd_tmp;
  699. spin_lock_irq(&dev->qf_cmd_lock);
  700. list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
  701. spin_unlock_irq(&dev->qf_cmd_lock);
  702. list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
  703. list_del(&cmd->se_qf_node);
  704. atomic_dec(&dev->dev_qf_count);
  705. smp_mb__after_atomic_dec();
  706. pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
  707. " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
  708. (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
  709. (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
  710. : "UNKNOWN");
  711. transport_add_cmd_to_queue(cmd, cmd->t_state, true);
  712. }
  713. }
  714. unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
  715. {
  716. switch (cmd->data_direction) {
  717. case DMA_NONE:
  718. return "NONE";
  719. case DMA_FROM_DEVICE:
  720. return "READ";
  721. case DMA_TO_DEVICE:
  722. return "WRITE";
  723. case DMA_BIDIRECTIONAL:
  724. return "BIDI";
  725. default:
  726. break;
  727. }
  728. return "UNKNOWN";
  729. }
  730. void transport_dump_dev_state(
  731. struct se_device *dev,
  732. char *b,
  733. int *bl)
  734. {
  735. *bl += sprintf(b + *bl, "Status: ");
  736. switch (dev->dev_status) {
  737. case TRANSPORT_DEVICE_ACTIVATED:
  738. *bl += sprintf(b + *bl, "ACTIVATED");
  739. break;
  740. case TRANSPORT_DEVICE_DEACTIVATED:
  741. *bl += sprintf(b + *bl, "DEACTIVATED");
  742. break;
  743. case TRANSPORT_DEVICE_SHUTDOWN:
  744. *bl += sprintf(b + *bl, "SHUTDOWN");
  745. break;
  746. case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
  747. case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
  748. *bl += sprintf(b + *bl, "OFFLINE");
  749. break;
  750. default:
  751. *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
  752. break;
  753. }
  754. *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
  755. atomic_read(&dev->execute_tasks), dev->queue_depth);
  756. *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
  757. dev->se_sub_dev->se_dev_attrib.block_size,
  758. dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
  759. *bl += sprintf(b + *bl, " ");
  760. }
  761. void transport_dump_vpd_proto_id(
  762. struct t10_vpd *vpd,
  763. unsigned char *p_buf,
  764. int p_buf_len)
  765. {
  766. unsigned char buf[VPD_TMP_BUF_SIZE];
  767. int len;
  768. memset(buf, 0, VPD_TMP_BUF_SIZE);
  769. len = sprintf(buf, "T10 VPD Protocol Identifier: ");
  770. switch (vpd->protocol_identifier) {
  771. case 0x00:
  772. sprintf(buf+len, "Fibre Channel\n");
  773. break;
  774. case 0x10:
  775. sprintf(buf+len, "Parallel SCSI\n");
  776. break;
  777. case 0x20:
  778. sprintf(buf+len, "SSA\n");
  779. break;
  780. case 0x30:
  781. sprintf(buf+len, "IEEE 1394\n");
  782. break;
  783. case 0x40:
  784. sprintf(buf+len, "SCSI Remote Direct Memory Access"
  785. " Protocol\n");
  786. break;
  787. case 0x50:
  788. sprintf(buf+len, "Internet SCSI (iSCSI)\n");
  789. break;
  790. case 0x60:
  791. sprintf(buf+len, "SAS Serial SCSI Protocol\n");
  792. break;
  793. case 0x70:
  794. sprintf(buf+len, "Automation/Drive Interface Transport"
  795. " Protocol\n");
  796. break;
  797. case 0x80:
  798. sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
  799. break;
  800. default:
  801. sprintf(buf+len, "Unknown 0x%02x\n",
  802. vpd->protocol_identifier);
  803. break;
  804. }
  805. if (p_buf)
  806. strncpy(p_buf, buf, p_buf_len);
  807. else
  808. pr_debug("%s", buf);
  809. }
  810. void
  811. transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
  812. {
  813. /*
  814. * Check if the Protocol Identifier Valid (PIV) bit is set..
  815. *
  816. * from spc3r23.pdf section 7.5.1
  817. */
  818. if (page_83[1] & 0x80) {
  819. vpd->protocol_identifier = (page_83[0] & 0xf0);
  820. vpd->protocol_identifier_set = 1;
  821. transport_dump_vpd_proto_id(vpd, NULL, 0);
  822. }
  823. }
  824. EXPORT_SYMBOL(transport_set_vpd_proto_id);
  825. int transport_dump_vpd_assoc(
  826. struct t10_vpd *vpd,
  827. unsigned char *p_buf,
  828. int p_buf_len)
  829. {
  830. unsigned char buf[VPD_TMP_BUF_SIZE];
  831. int ret = 0;
  832. int len;
  833. memset(buf, 0, VPD_TMP_BUF_SIZE);
  834. len = sprintf(buf, "T10 VPD Identifier Association: ");
  835. switch (vpd->association) {
  836. case 0x00:
  837. sprintf(buf+len, "addressed logical unit\n");
  838. break;
  839. case 0x10:
  840. sprintf(buf+len, "target port\n");
  841. break;
  842. case 0x20:
  843. sprintf(buf+len, "SCSI target device\n");
  844. break;
  845. default:
  846. sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
  847. ret = -EINVAL;
  848. break;
  849. }
  850. if (p_buf)
  851. strncpy(p_buf, buf, p_buf_len);
  852. else
  853. pr_debug("%s", buf);
  854. return ret;
  855. }
  856. int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
  857. {
  858. /*
  859. * The VPD identification association..
  860. *
  861. * from spc3r23.pdf Section 7.6.3.1 Table 297
  862. */
  863. vpd->association = (page_83[1] & 0x30);
  864. return transport_dump_vpd_assoc(vpd, NULL, 0);
  865. }
  866. EXPORT_SYMBOL(transport_set_vpd_assoc);
  867. int transport_dump_vpd_ident_type(
  868. struct t10_vpd *vpd,
  869. unsigned char *p_buf,
  870. int p_buf_len)
  871. {
  872. unsigned char buf[VPD_TMP_BUF_SIZE];
  873. int ret = 0;
  874. int len;
  875. memset(buf, 0, VPD_TMP_BUF_SIZE);
  876. len = sprintf(buf, "T10 VPD Identifier Type: ");
  877. switch (vpd->device_identifier_type) {
  878. case 0x00:
  879. sprintf(buf+len, "Vendor specific\n");
  880. break;
  881. case 0x01:
  882. sprintf(buf+len, "T10 Vendor ID based\n");
  883. break;
  884. case 0x02:
  885. sprintf(buf+len, "EUI-64 based\n");
  886. break;
  887. case 0x03:
  888. sprintf(buf+len, "NAA\n");
  889. break;
  890. case 0x04:
  891. sprintf(buf+len, "Relative target port identifier\n");
  892. break;
  893. case 0x08:
  894. sprintf(buf+len, "SCSI name string\n");
  895. break;
  896. default:
  897. sprintf(buf+len, "Unsupported: 0x%02x\n",
  898. vpd->device_identifier_type);
  899. ret = -EINVAL;
  900. break;
  901. }
  902. if (p_buf) {
  903. if (p_buf_len < strlen(buf)+1)
  904. return -EINVAL;
  905. strncpy(p_buf, buf, p_buf_len);
  906. } else {
  907. pr_debug("%s", buf);
  908. }
  909. return ret;
  910. }
  911. int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
  912. {
  913. /*
  914. * The VPD identifier type..
  915. *
  916. * from spc3r23.pdf Section 7.6.3.1 Table 298
  917. */
  918. vpd->device_identifier_type = (page_83[1] & 0x0f);
  919. return transport_dump_vpd_ident_type(vpd, NULL, 0);
  920. }
  921. EXPORT_SYMBOL(transport_set_vpd_ident_type);
  922. int transport_dump_vpd_ident(
  923. struct t10_vpd *vpd,
  924. unsigned char *p_buf,
  925. int p_buf_len)
  926. {
  927. unsigned char buf[VPD_TMP_BUF_SIZE];
  928. int ret = 0;
  929. memset(buf, 0, VPD_TMP_BUF_SIZE);
  930. switch (vpd->device_identifier_code_set) {
  931. case 0x01: /* Binary */
  932. sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
  933. &vpd->device_identifier[0]);
  934. break;
  935. case 0x02: /* ASCII */
  936. sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
  937. &vpd->device_identifier[0]);
  938. break;
  939. case 0x03: /* UTF-8 */
  940. sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
  941. &vpd->device_identifier[0]);
  942. break;
  943. default:
  944. sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
  945. " 0x%02x", vpd->device_identifier_code_set);
  946. ret = -EINVAL;
  947. break;
  948. }
  949. if (p_buf)
  950. strncpy(p_buf, buf, p_buf_len);
  951. else
  952. pr_debug("%s", buf);
  953. return ret;
  954. }
  955. int
  956. transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
  957. {
  958. static const char hex_str[] = "0123456789abcdef";
  959. int j = 0, i = 4; /* offset to start of the identifer */
  960. /*
  961. * The VPD Code Set (encoding)
  962. *
  963. * from spc3r23.pdf Section 7.6.3.1 Table 296
  964. */
  965. vpd->device_identifier_code_set = (page_83[0] & 0x0f);
  966. switch (vpd->device_identifier_code_set) {
  967. case 0x01: /* Binary */
  968. vpd->device_identifier[j++] =
  969. hex_str[vpd->device_identifier_type];
  970. while (i < (4 + page_83[3])) {
  971. vpd->device_identifier[j++] =
  972. hex_str[(page_83[i] & 0xf0) >> 4];
  973. vpd->device_identifier[j++] =
  974. hex_str[page_83[i] & 0x0f];
  975. i++;
  976. }
  977. break;
  978. case 0x02: /* ASCII */
  979. case 0x03: /* UTF-8 */
  980. while (i < (4 + page_83[3]))
  981. vpd->device_identifier[j++] = page_83[i++];
  982. break;
  983. default:
  984. break;
  985. }
  986. return transport_dump_vpd_ident(vpd, NULL, 0);
  987. }
  988. EXPORT_SYMBOL(transport_set_vpd_ident);
  989. static void core_setup_task_attr_emulation(struct se_device *dev)
  990. {
  991. /*
  992. * If this device is from Target_Core_Mod/pSCSI, disable the
  993. * SAM Task Attribute emulation.
  994. *
  995. * This is currently not available in upsream Linux/SCSI Target
  996. * mode code, and is assumed to be disabled while using TCM/pSCSI.
  997. */
  998. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  999. dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
  1000. return;
  1001. }
  1002. dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
  1003. pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
  1004. " device\n", dev->transport->name,
  1005. dev->transport->get_device_rev(dev));
  1006. }
  1007. static void scsi_dump_inquiry(struct se_device *dev)
  1008. {
  1009. struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
  1010. char buf[17];
  1011. int i, device_type;
  1012. /*
  1013. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1014. */
  1015. for (i = 0; i < 8; i++)
  1016. if (wwn->vendor[i] >= 0x20)
  1017. buf[i] = wwn->vendor[i];
  1018. else
  1019. buf[i] = ' ';
  1020. buf[i] = '\0';
  1021. pr_debug(" Vendor: %s\n", buf);
  1022. for (i = 0; i < 16; i++)
  1023. if (wwn->model[i] >= 0x20)
  1024. buf[i] = wwn->model[i];
  1025. else
  1026. buf[i] = ' ';
  1027. buf[i] = '\0';
  1028. pr_debug(" Model: %s\n", buf);
  1029. for (i = 0; i < 4; i++)
  1030. if (wwn->revision[i] >= 0x20)
  1031. buf[i] = wwn->revision[i];
  1032. else
  1033. buf[i] = ' ';
  1034. buf[i] = '\0';
  1035. pr_debug(" Revision: %s\n", buf);
  1036. device_type = dev->transport->get_device_type(dev);
  1037. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1038. pr_debug(" ANSI SCSI revision: %02x\n",
  1039. dev->transport->get_device_rev(dev));
  1040. }
  1041. struct se_device *transport_add_device_to_core_hba(
  1042. struct se_hba *hba,
  1043. struct se_subsystem_api *transport,
  1044. struct se_subsystem_dev *se_dev,
  1045. u32 device_flags,
  1046. void *transport_dev,
  1047. struct se_dev_limits *dev_limits,
  1048. const char *inquiry_prod,
  1049. const char *inquiry_rev)
  1050. {
  1051. int force_pt;
  1052. struct se_device *dev;
  1053. dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
  1054. if (!dev) {
  1055. pr_err("Unable to allocate memory for se_dev_t\n");
  1056. return NULL;
  1057. }
  1058. transport_init_queue_obj(&dev->dev_queue_obj);
  1059. dev->dev_flags = device_flags;
  1060. dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
  1061. dev->dev_ptr = transport_dev;
  1062. dev->se_hba = hba;
  1063. dev->se_sub_dev = se_dev;
  1064. dev->transport = transport;
  1065. INIT_LIST_HEAD(&dev->dev_list);
  1066. INIT_LIST_HEAD(&dev->dev_sep_list);
  1067. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1068. INIT_LIST_HEAD(&dev->execute_list);
  1069. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1070. INIT_LIST_HEAD(&dev->state_list);
  1071. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1072. spin_lock_init(&dev->execute_task_lock);
  1073. spin_lock_init(&dev->delayed_cmd_lock);
  1074. spin_lock_init(&dev->dev_reservation_lock);
  1075. spin_lock_init(&dev->dev_status_lock);
  1076. spin_lock_init(&dev->se_port_lock);
  1077. spin_lock_init(&dev->se_tmr_lock);
  1078. spin_lock_init(&dev->qf_cmd_lock);
  1079. atomic_set(&dev->dev_ordered_id, 0);
  1080. se_dev_set_default_attribs(dev, dev_limits);
  1081. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1082. dev->creation_time = get_jiffies_64();
  1083. spin_lock_init(&dev->stats_lock);
  1084. spin_lock(&hba->device_lock);
  1085. list_add_tail(&dev->dev_list, &hba->hba_dev_list);
  1086. hba->dev_count++;
  1087. spin_unlock(&hba->device_lock);
  1088. /*
  1089. * Setup the SAM Task Attribute emulation for struct se_device
  1090. */
  1091. core_setup_task_attr_emulation(dev);
  1092. /*
  1093. * Force PR and ALUA passthrough emulation with internal object use.
  1094. */
  1095. force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
  1096. /*
  1097. * Setup the Reservations infrastructure for struct se_device
  1098. */
  1099. core_setup_reservations(dev, force_pt);
  1100. /*
  1101. * Setup the Asymmetric Logical Unit Assignment for struct se_device
  1102. */
  1103. if (core_setup_alua(dev, force_pt) < 0)
  1104. goto out;
  1105. /*
  1106. * Startup the struct se_device processing thread
  1107. */
  1108. dev->process_thread = kthread_run(transport_processing_thread, dev,
  1109. "LIO_%s", dev->transport->name);
  1110. if (IS_ERR(dev->process_thread)) {
  1111. pr_err("Unable to create kthread: LIO_%s\n",
  1112. dev->transport->name);
  1113. goto out;
  1114. }
  1115. /*
  1116. * Setup work_queue for QUEUE_FULL
  1117. */
  1118. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1119. /*
  1120. * Preload the initial INQUIRY const values if we are doing
  1121. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1122. * passthrough because this is being provided by the backend LLD.
  1123. * This is required so that transport_get_inquiry() copies these
  1124. * originals once back into DEV_T10_WWN(dev) for the virtual device
  1125. * setup.
  1126. */
  1127. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1128. if (!inquiry_prod || !inquiry_rev) {
  1129. pr_err("All non TCM/pSCSI plugins require"
  1130. " INQUIRY consts\n");
  1131. goto out;
  1132. }
  1133. strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1134. strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
  1135. strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
  1136. }
  1137. scsi_dump_inquiry(dev);
  1138. return dev;
  1139. out:
  1140. kthread_stop(dev->process_thread);
  1141. spin_lock(&hba->device_lock);
  1142. list_del(&dev->dev_list);
  1143. hba->dev_count--;
  1144. spin_unlock(&hba->device_lock);
  1145. se_release_vpd_for_dev(dev);
  1146. kfree(dev);
  1147. return NULL;
  1148. }
  1149. EXPORT_SYMBOL(transport_add_device_to_core_hba);
  1150. /* transport_generic_prepare_cdb():
  1151. *
  1152. * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
  1153. * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
  1154. * The point of this is since we are mapping iSCSI LUNs to
  1155. * SCSI Target IDs having a non-zero LUN in the CDB will throw the
  1156. * devices and HBAs for a loop.
  1157. */
  1158. static inline void transport_generic_prepare_cdb(
  1159. unsigned char *cdb)
  1160. {
  1161. switch (cdb[0]) {
  1162. case READ_10: /* SBC - RDProtect */
  1163. case READ_12: /* SBC - RDProtect */
  1164. case READ_16: /* SBC - RDProtect */
  1165. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  1166. case VERIFY: /* SBC - VRProtect */
  1167. case VERIFY_16: /* SBC - VRProtect */
  1168. case WRITE_VERIFY: /* SBC - VRProtect */
  1169. case WRITE_VERIFY_12: /* SBC - VRProtect */
  1170. case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
  1171. break;
  1172. default:
  1173. cdb[1] &= 0x1f; /* clear logical unit number */
  1174. break;
  1175. }
  1176. }
  1177. static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
  1178. /*
  1179. * Used by fabric modules containing a local struct se_cmd within their
  1180. * fabric dependent per I/O descriptor.
  1181. */
  1182. void transport_init_se_cmd(
  1183. struct se_cmd *cmd,
  1184. struct target_core_fabric_ops *tfo,
  1185. struct se_session *se_sess,
  1186. u32 data_length,
  1187. int data_direction,
  1188. int task_attr,
  1189. unsigned char *sense_buffer)
  1190. {
  1191. INIT_LIST_HEAD(&cmd->se_lun_node);
  1192. INIT_LIST_HEAD(&cmd->se_delayed_node);
  1193. INIT_LIST_HEAD(&cmd->se_qf_node);
  1194. INIT_LIST_HEAD(&cmd->se_queue_node);
  1195. INIT_LIST_HEAD(&cmd->se_cmd_list);
  1196. INIT_LIST_HEAD(&cmd->execute_list);
  1197. INIT_LIST_HEAD(&cmd->state_list);
  1198. init_completion(&cmd->transport_lun_fe_stop_comp);
  1199. init_completion(&cmd->transport_lun_stop_comp);
  1200. init_completion(&cmd->t_transport_stop_comp);
  1201. init_completion(&cmd->cmd_wait_comp);
  1202. init_completion(&cmd->task_stop_comp);
  1203. spin_lock_init(&cmd->t_state_lock);
  1204. cmd->transport_state = CMD_T_DEV_ACTIVE;
  1205. cmd->se_tfo = tfo;
  1206. cmd->se_sess = se_sess;
  1207. cmd->data_length = data_length;
  1208. cmd->data_direction = data_direction;
  1209. cmd->sam_task_attr = task_attr;
  1210. cmd->sense_buffer = sense_buffer;
  1211. cmd->state_active = false;
  1212. }
  1213. EXPORT_SYMBOL(transport_init_se_cmd);
  1214. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1215. {
  1216. /*
  1217. * Check if SAM Task Attribute emulation is enabled for this
  1218. * struct se_device storage object
  1219. */
  1220. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1221. return 0;
  1222. if (cmd->sam_task_attr == MSG_ACA_TAG) {
  1223. pr_debug("SAM Task Attribute ACA"
  1224. " emulation is not supported\n");
  1225. return -EINVAL;
  1226. }
  1227. /*
  1228. * Used to determine when ORDERED commands should go from
  1229. * Dormant to Active status.
  1230. */
  1231. cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
  1232. smp_mb__after_atomic_inc();
  1233. pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1234. cmd->se_ordered_id, cmd->sam_task_attr,
  1235. cmd->se_dev->transport->name);
  1236. return 0;
  1237. }
  1238. /* target_setup_cmd_from_cdb():
  1239. *
  1240. * Called from fabric RX Thread.
  1241. */
  1242. int target_setup_cmd_from_cdb(
  1243. struct se_cmd *cmd,
  1244. unsigned char *cdb)
  1245. {
  1246. struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
  1247. u32 pr_reg_type = 0;
  1248. u8 alua_ascq = 0;
  1249. unsigned long flags;
  1250. int ret;
  1251. transport_generic_prepare_cdb(cdb);
  1252. /*
  1253. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1254. * for VARIABLE_LENGTH_CMD
  1255. */
  1256. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1257. pr_err("Received SCSI CDB with command_size: %d that"
  1258. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1259. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1260. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1261. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1262. return -EINVAL;
  1263. }
  1264. /*
  1265. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1266. * allocate the additional extended CDB buffer now.. Otherwise
  1267. * setup the pointer from __t_task_cdb to t_task_cdb.
  1268. */
  1269. if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
  1270. cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1271. GFP_KERNEL);
  1272. if (!cmd->t_task_cdb) {
  1273. pr_err("Unable to allocate cmd->t_task_cdb"
  1274. " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
  1275. scsi_command_size(cdb),
  1276. (unsigned long)sizeof(cmd->__t_task_cdb));
  1277. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1278. cmd->scsi_sense_reason =
  1279. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1280. return -ENOMEM;
  1281. }
  1282. } else
  1283. cmd->t_task_cdb = &cmd->__t_task_cdb[0];
  1284. /*
  1285. * Copy the original CDB into cmd->
  1286. */
  1287. memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
  1288. /*
  1289. * Check for an existing UNIT ATTENTION condition
  1290. */
  1291. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  1292. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1293. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  1294. return -EINVAL;
  1295. }
  1296. ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
  1297. if (ret != 0) {
  1298. /*
  1299. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  1300. * The ALUA additional sense code qualifier (ASCQ) is determined
  1301. * by the ALUA primary or secondary access state..
  1302. */
  1303. if (ret > 0) {
  1304. pr_debug("[%s]: ALUA TG Port not available, "
  1305. "SenseKey: NOT_READY, ASC/ASCQ: "
  1306. "0x04/0x%02x\n",
  1307. cmd->se_tfo->get_fabric_name(), alua_ascq);
  1308. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  1309. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1310. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  1311. return -EINVAL;
  1312. }
  1313. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1314. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1315. return -EINVAL;
  1316. }
  1317. /*
  1318. * Check status for SPC-3 Persistent Reservations
  1319. */
  1320. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type)) {
  1321. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  1322. cmd, cdb, pr_reg_type) != 0) {
  1323. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1324. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  1325. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1326. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  1327. return -EBUSY;
  1328. }
  1329. /*
  1330. * This means the CDB is allowed for the SCSI Initiator port
  1331. * when said port is *NOT* holding the legacy SPC-2 or
  1332. * SPC-3 Persistent Reservation.
  1333. */
  1334. }
  1335. /*
  1336. * Setup the received CDB based on SCSI defined opcodes and
  1337. * perform unit attention, persistent reservations and ALUA
  1338. * checks for virtual device backends. The cmd->t_task_cdb
  1339. * pointer is expected to be setup before we reach this point.
  1340. */
  1341. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1342. if (ret < 0)
  1343. return ret;
  1344. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1345. cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1346. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1347. /*
  1348. * Check for SAM Task Attribute Emulation
  1349. */
  1350. if (transport_check_alloc_task_attr(cmd) < 0) {
  1351. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1352. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1353. return -EINVAL;
  1354. }
  1355. spin_lock(&cmd->se_lun->lun_sep_lock);
  1356. if (cmd->se_lun->lun_sep)
  1357. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1358. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1359. return 0;
  1360. }
  1361. EXPORT_SYMBOL(target_setup_cmd_from_cdb);
  1362. /*
  1363. * Used by fabric module frontends to queue tasks directly.
  1364. * Many only be used from process context only
  1365. */
  1366. int transport_handle_cdb_direct(
  1367. struct se_cmd *cmd)
  1368. {
  1369. int ret;
  1370. if (!cmd->se_lun) {
  1371. dump_stack();
  1372. pr_err("cmd->se_lun is NULL\n");
  1373. return -EINVAL;
  1374. }
  1375. if (in_interrupt()) {
  1376. dump_stack();
  1377. pr_err("transport_generic_handle_cdb cannot be called"
  1378. " from interrupt context\n");
  1379. return -EINVAL;
  1380. }
  1381. /*
  1382. * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
  1383. * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
  1384. * in existing usage to ensure that outstanding descriptors are handled
  1385. * correctly during shutdown via transport_wait_for_tasks()
  1386. *
  1387. * Also, we don't take cmd->t_state_lock here as we only expect
  1388. * this to be called for initial descriptor submission.
  1389. */
  1390. cmd->t_state = TRANSPORT_NEW_CMD;
  1391. cmd->transport_state |= CMD_T_ACTIVE;
  1392. /*
  1393. * transport_generic_new_cmd() is already handling QUEUE_FULL,
  1394. * so follow TRANSPORT_NEW_CMD processing thread context usage
  1395. * and call transport_generic_request_failure() if necessary..
  1396. */
  1397. ret = transport_generic_new_cmd(cmd);
  1398. if (ret < 0)
  1399. transport_generic_request_failure(cmd);
  1400. return 0;
  1401. }
  1402. EXPORT_SYMBOL(transport_handle_cdb_direct);
  1403. /**
  1404. * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
  1405. *
  1406. * @se_cmd: command descriptor to submit
  1407. * @se_sess: associated se_sess for endpoint
  1408. * @cdb: pointer to SCSI CDB
  1409. * @sense: pointer to SCSI sense buffer
  1410. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1411. * @data_length: fabric expected data transfer length
  1412. * @task_addr: SAM task attribute
  1413. * @data_dir: DMA data direction
  1414. * @flags: flags for command submission from target_sc_flags_tables
  1415. *
  1416. * This may only be called from process context, and also currently
  1417. * assumes internal allocation of fabric payload buffer by target-core.
  1418. **/
  1419. void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
  1420. unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
  1421. u32 data_length, int task_attr, int data_dir, int flags)
  1422. {
  1423. struct se_portal_group *se_tpg;
  1424. int rc;
  1425. se_tpg = se_sess->se_tpg;
  1426. BUG_ON(!se_tpg);
  1427. BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
  1428. BUG_ON(in_interrupt());
  1429. /*
  1430. * Initialize se_cmd for target operation. From this point
  1431. * exceptions are handled by sending exception status via
  1432. * target_core_fabric_ops->queue_status() callback
  1433. */
  1434. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1435. data_length, data_dir, task_attr, sense);
  1436. if (flags & TARGET_SCF_UNKNOWN_SIZE)
  1437. se_cmd->unknown_data_length = 1;
  1438. /*
  1439. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1440. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1441. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1442. * kref_put() to happen during fabric packet acknowledgement.
  1443. */
  1444. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1445. /*
  1446. * Signal bidirectional data payloads to target-core
  1447. */
  1448. if (flags & TARGET_SCF_BIDI_OP)
  1449. se_cmd->se_cmd_flags |= SCF_BIDI;
  1450. /*
  1451. * Locate se_lun pointer and attach it to struct se_cmd
  1452. */
  1453. if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
  1454. transport_send_check_condition_and_sense(se_cmd,
  1455. se_cmd->scsi_sense_reason, 0);
  1456. target_put_sess_cmd(se_sess, se_cmd);
  1457. return;
  1458. }
  1459. /*
  1460. * Sanitize CDBs via transport_generic_cmd_sequencer() and
  1461. * allocate the necessary tasks to complete the received CDB+data
  1462. */
  1463. rc = target_setup_cmd_from_cdb(se_cmd, cdb);
  1464. if (rc != 0) {
  1465. transport_generic_request_failure(se_cmd);
  1466. return;
  1467. }
  1468. /*
  1469. * Check if we need to delay processing because of ALUA
  1470. * Active/NonOptimized primary access state..
  1471. */
  1472. core_alua_check_nonop_delay(se_cmd);
  1473. /*
  1474. * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
  1475. * for immediate execution of READs, otherwise wait for
  1476. * transport_generic_handle_data() to be called for WRITEs
  1477. * when fabric has filled the incoming buffer.
  1478. */
  1479. transport_handle_cdb_direct(se_cmd);
  1480. return;
  1481. }
  1482. EXPORT_SYMBOL(target_submit_cmd);
  1483. static void target_complete_tmr_failure(struct work_struct *work)
  1484. {
  1485. struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
  1486. se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1487. se_cmd->se_tfo->queue_tm_rsp(se_cmd);
  1488. transport_generic_free_cmd(se_cmd, 0);
  1489. }
  1490. /**
  1491. * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
  1492. * for TMR CDBs
  1493. *
  1494. * @se_cmd: command descriptor to submit
  1495. * @se_sess: associated se_sess for endpoint
  1496. * @sense: pointer to SCSI sense buffer
  1497. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1498. * @fabric_context: fabric context for TMR req
  1499. * @tm_type: Type of TM request
  1500. * @gfp: gfp type for caller
  1501. * @tag: referenced task tag for TMR_ABORT_TASK
  1502. * @flags: submit cmd flags
  1503. *
  1504. * Callable from all contexts.
  1505. **/
  1506. int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
  1507. unsigned char *sense, u32 unpacked_lun,
  1508. void *fabric_tmr_ptr, unsigned char tm_type,
  1509. gfp_t gfp, unsigned int tag, int flags)
  1510. {
  1511. struct se_portal_group *se_tpg;
  1512. int ret;
  1513. se_tpg = se_sess->se_tpg;
  1514. BUG_ON(!se_tpg);
  1515. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1516. 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
  1517. /*
  1518. * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
  1519. * allocation failure.
  1520. */
  1521. ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
  1522. if (ret < 0)
  1523. return -ENOMEM;
  1524. if (tm_type == TMR_ABORT_TASK)
  1525. se_cmd->se_tmr_req->ref_task_tag = tag;
  1526. /* See target_submit_cmd for commentary */
  1527. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1528. ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
  1529. if (ret) {
  1530. /*
  1531. * For callback during failure handling, push this work off
  1532. * to process context with TMR_LUN_DOES_NOT_EXIST status.
  1533. */
  1534. INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
  1535. schedule_work(&se_cmd->work);
  1536. return 0;
  1537. }
  1538. transport_generic_handle_tmr(se_cmd);
  1539. return 0;
  1540. }
  1541. EXPORT_SYMBOL(target_submit_tmr);
  1542. /*
  1543. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1544. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1545. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1546. */
  1547. int transport_generic_handle_cdb_map(
  1548. struct se_cmd *cmd)
  1549. {
  1550. if (!cmd->se_lun) {
  1551. dump_stack();
  1552. pr_err("cmd->se_lun is NULL\n");
  1553. return -EINVAL;
  1554. }
  1555. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
  1556. return 0;
  1557. }
  1558. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1559. /* transport_generic_handle_data():
  1560. *
  1561. *
  1562. */
  1563. int transport_generic_handle_data(
  1564. struct se_cmd *cmd)
  1565. {
  1566. /*
  1567. * For the software fabric case, then we assume the nexus is being
  1568. * failed/shutdown when signals are pending from the kthread context
  1569. * caller, so we return a failure. For the HW target mode case running
  1570. * in interrupt code, the signal_pending() check is skipped.
  1571. */
  1572. if (!in_interrupt() && signal_pending(current))
  1573. return -EPERM;
  1574. /*
  1575. * If the received CDB has aleady been ABORTED by the generic
  1576. * target engine, we now call transport_check_aborted_status()
  1577. * to queue any delated TASK_ABORTED status for the received CDB to the
  1578. * fabric module as we are expecting no further incoming DATA OUT
  1579. * sequences at this point.
  1580. */
  1581. if (transport_check_aborted_status(cmd, 1) != 0)
  1582. return 0;
  1583. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
  1584. return 0;
  1585. }
  1586. EXPORT_SYMBOL(transport_generic_handle_data);
  1587. /* transport_generic_handle_tmr():
  1588. *
  1589. *
  1590. */
  1591. int transport_generic_handle_tmr(
  1592. struct se_cmd *cmd)
  1593. {
  1594. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
  1595. return 0;
  1596. }
  1597. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1598. /*
  1599. * If the cmd is active, request it to be stopped and sleep until it
  1600. * has completed.
  1601. */
  1602. bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
  1603. {
  1604. bool was_active = false;
  1605. if (cmd->transport_state & CMD_T_BUSY) {
  1606. cmd->transport_state |= CMD_T_REQUEST_STOP;
  1607. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  1608. pr_debug("cmd %p waiting to complete\n", cmd);
  1609. wait_for_completion(&cmd->task_stop_comp);
  1610. pr_debug("cmd %p stopped successfully\n", cmd);
  1611. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  1612. cmd->transport_state &= ~CMD_T_REQUEST_STOP;
  1613. cmd->transport_state &= ~CMD_T_BUSY;
  1614. was_active = true;
  1615. }
  1616. return was_active;
  1617. }
  1618. /*
  1619. * Handle SAM-esque emulation for generic transport request failures.
  1620. */
  1621. void transport_generic_request_failure(struct se_cmd *cmd)
  1622. {
  1623. int ret = 0;
  1624. pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1625. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1626. cmd->t_task_cdb[0]);
  1627. pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
  1628. cmd->se_tfo->get_cmd_state(cmd),
  1629. cmd->t_state, cmd->scsi_sense_reason);
  1630. pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
  1631. (cmd->transport_state & CMD_T_ACTIVE) != 0,
  1632. (cmd->transport_state & CMD_T_STOP) != 0,
  1633. (cmd->transport_state & CMD_T_SENT) != 0);
  1634. /*
  1635. * For SAM Task Attribute emulation for failed struct se_cmd
  1636. */
  1637. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1638. transport_complete_task_attr(cmd);
  1639. switch (cmd->scsi_sense_reason) {
  1640. case TCM_NON_EXISTENT_LUN:
  1641. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1642. case TCM_INVALID_CDB_FIELD:
  1643. case TCM_INVALID_PARAMETER_LIST:
  1644. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1645. case TCM_UNKNOWN_MODE_PAGE:
  1646. case TCM_WRITE_PROTECTED:
  1647. case TCM_CHECK_CONDITION_ABORT_CMD:
  1648. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1649. case TCM_CHECK_CONDITION_NOT_READY:
  1650. break;
  1651. case TCM_RESERVATION_CONFLICT:
  1652. /*
  1653. * No SENSE Data payload for this case, set SCSI Status
  1654. * and queue the response to $FABRIC_MOD.
  1655. *
  1656. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1657. */
  1658. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1659. /*
  1660. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1661. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1662. * CONFLICT STATUS.
  1663. *
  1664. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1665. */
  1666. if (cmd->se_sess &&
  1667. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1668. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1669. cmd->orig_fe_lun, 0x2C,
  1670. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1671. ret = cmd->se_tfo->queue_status(cmd);
  1672. if (ret == -EAGAIN || ret == -ENOMEM)
  1673. goto queue_full;
  1674. goto check_stop;
  1675. default:
  1676. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1677. cmd->t_task_cdb[0], cmd->scsi_sense_reason);
  1678. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1679. break;
  1680. }
  1681. /*
  1682. * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
  1683. * make the call to transport_send_check_condition_and_sense()
  1684. * directly. Otherwise expect the fabric to make the call to
  1685. * transport_send_check_condition_and_sense() after handling
  1686. * possible unsoliticied write data payloads.
  1687. */
  1688. ret = transport_send_check_condition_and_sense(cmd,
  1689. cmd->scsi_sense_reason, 0);
  1690. if (ret == -EAGAIN || ret == -ENOMEM)
  1691. goto queue_full;
  1692. check_stop:
  1693. transport_lun_remove_cmd(cmd);
  1694. if (!transport_cmd_check_stop_to_fabric(cmd))
  1695. ;
  1696. return;
  1697. queue_full:
  1698. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  1699. transport_handle_queue_full(cmd, cmd->se_dev);
  1700. }
  1701. EXPORT_SYMBOL(transport_generic_request_failure);
  1702. static inline u32 transport_lba_21(unsigned char *cdb)
  1703. {
  1704. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1705. }
  1706. static inline u32 transport_lba_32(unsigned char *cdb)
  1707. {
  1708. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1709. }
  1710. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1711. {
  1712. unsigned int __v1, __v2;
  1713. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1714. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1715. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1716. }
  1717. /*
  1718. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1719. */
  1720. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1721. {
  1722. unsigned int __v1, __v2;
  1723. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1724. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1725. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1726. }
  1727. /*
  1728. * Called from Fabric Module context from transport_execute_tasks()
  1729. *
  1730. * The return of this function determins if the tasks from struct se_cmd
  1731. * get added to the execution queue in transport_execute_tasks(),
  1732. * or are added to the delayed or ordered lists here.
  1733. */
  1734. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  1735. {
  1736. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1737. return 1;
  1738. /*
  1739. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1740. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1741. */
  1742. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  1743. pr_debug("Added HEAD_OF_QUEUE for CDB:"
  1744. " 0x%02x, se_ordered_id: %u\n",
  1745. cmd->t_task_cdb[0],
  1746. cmd->se_ordered_id);
  1747. return 1;
  1748. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  1749. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  1750. smp_mb__after_atomic_inc();
  1751. pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
  1752. " list, se_ordered_id: %u\n",
  1753. cmd->t_task_cdb[0],
  1754. cmd->se_ordered_id);
  1755. /*
  1756. * Add ORDERED command to tail of execution queue if
  1757. * no other older commands exist that need to be
  1758. * completed first.
  1759. */
  1760. if (!atomic_read(&cmd->se_dev->simple_cmds))
  1761. return 1;
  1762. } else {
  1763. /*
  1764. * For SIMPLE and UNTAGGED Task Attribute commands
  1765. */
  1766. atomic_inc(&cmd->se_dev->simple_cmds);
  1767. smp_mb__after_atomic_inc();
  1768. }
  1769. /*
  1770. * Otherwise if one or more outstanding ORDERED task attribute exist,
  1771. * add the dormant task(s) built for the passed struct se_cmd to the
  1772. * execution queue and become in Active state for this struct se_device.
  1773. */
  1774. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  1775. /*
  1776. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  1777. * will be drained upon completion of HEAD_OF_QUEUE task.
  1778. */
  1779. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  1780. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  1781. list_add_tail(&cmd->se_delayed_node,
  1782. &cmd->se_dev->delayed_cmd_list);
  1783. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  1784. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
  1785. " delayed CMD list, se_ordered_id: %u\n",
  1786. cmd->t_task_cdb[0], cmd->sam_task_attr,
  1787. cmd->se_ordered_id);
  1788. /*
  1789. * Return zero to let transport_execute_tasks() know
  1790. * not to add the delayed tasks to the execution list.
  1791. */
  1792. return 0;
  1793. }
  1794. /*
  1795. * Otherwise, no ORDERED task attributes exist..
  1796. */
  1797. return 1;
  1798. }
  1799. /*
  1800. * Called from fabric module context in transport_generic_new_cmd() and
  1801. * transport_generic_process_write()
  1802. */
  1803. static void transport_execute_tasks(struct se_cmd *cmd)
  1804. {
  1805. int add_tasks;
  1806. struct se_device *se_dev = cmd->se_dev;
  1807. /*
  1808. * Call transport_cmd_check_stop() to see if a fabric exception
  1809. * has occurred that prevents execution.
  1810. */
  1811. if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
  1812. /*
  1813. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  1814. * attribute for the tasks of the received struct se_cmd CDB
  1815. */
  1816. add_tasks = transport_execute_task_attr(cmd);
  1817. if (add_tasks) {
  1818. __transport_execute_tasks(se_dev, cmd);
  1819. return;
  1820. }
  1821. }
  1822. __transport_execute_tasks(se_dev, NULL);
  1823. }
  1824. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
  1825. {
  1826. int error;
  1827. struct se_cmd *cmd = NULL;
  1828. unsigned long flags;
  1829. check_depth:
  1830. spin_lock_irq(&dev->execute_task_lock);
  1831. if (new_cmd != NULL)
  1832. __target_add_to_execute_list(new_cmd);
  1833. if (list_empty(&dev->execute_list)) {
  1834. spin_unlock_irq(&dev->execute_task_lock);
  1835. return 0;
  1836. }
  1837. cmd = list_first_entry(&dev->execute_list, struct se_cmd, execute_list);
  1838. __target_remove_from_execute_list(cmd);
  1839. spin_unlock_irq(&dev->execute_task_lock);
  1840. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1841. cmd->transport_state |= CMD_T_BUSY;
  1842. cmd->transport_state |= CMD_T_SENT;
  1843. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1844. if (cmd->execute_cmd)
  1845. error = cmd->execute_cmd(cmd);
  1846. else {
  1847. error = dev->transport->execute_cmd(cmd, cmd->t_data_sg,
  1848. cmd->t_data_nents, cmd->data_direction);
  1849. }
  1850. if (error != 0) {
  1851. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1852. cmd->transport_state &= ~CMD_T_BUSY;
  1853. cmd->transport_state &= ~CMD_T_SENT;
  1854. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1855. transport_generic_request_failure(cmd);
  1856. }
  1857. new_cmd = NULL;
  1858. goto check_depth;
  1859. return 0;
  1860. }
  1861. static inline u32 transport_get_sectors_6(
  1862. unsigned char *cdb,
  1863. struct se_cmd *cmd,
  1864. int *ret)
  1865. {
  1866. struct se_device *dev = cmd->se_dev;
  1867. /*
  1868. * Assume TYPE_DISK for non struct se_device objects.
  1869. * Use 8-bit sector value.
  1870. */
  1871. if (!dev)
  1872. goto type_disk;
  1873. /*
  1874. * Use 24-bit allocation length for TYPE_TAPE.
  1875. */
  1876. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  1877. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  1878. /*
  1879. * Everything else assume TYPE_DISK Sector CDB location.
  1880. * Use 8-bit sector value. SBC-3 says:
  1881. *
  1882. * A TRANSFER LENGTH field set to zero specifies that 256
  1883. * logical blocks shall be written. Any other value
  1884. * specifies the number of logical blocks that shall be
  1885. * written.
  1886. */
  1887. type_disk:
  1888. return cdb[4] ? : 256;
  1889. }
  1890. static inline u32 transport_get_sectors_10(
  1891. unsigned char *cdb,
  1892. struct se_cmd *cmd,
  1893. int *ret)
  1894. {
  1895. struct se_device *dev = cmd->se_dev;
  1896. /*
  1897. * Assume TYPE_DISK for non struct se_device objects.
  1898. * Use 16-bit sector value.
  1899. */
  1900. if (!dev)
  1901. goto type_disk;
  1902. /*
  1903. * XXX_10 is not defined in SSC, throw an exception
  1904. */
  1905. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1906. *ret = -EINVAL;
  1907. return 0;
  1908. }
  1909. /*
  1910. * Everything else assume TYPE_DISK Sector CDB location.
  1911. * Use 16-bit sector value.
  1912. */
  1913. type_disk:
  1914. return (u32)(cdb[7] << 8) + cdb[8];
  1915. }
  1916. static inline u32 transport_get_sectors_12(
  1917. unsigned char *cdb,
  1918. struct se_cmd *cmd,
  1919. int *ret)
  1920. {
  1921. struct se_device *dev = cmd->se_dev;
  1922. /*
  1923. * Assume TYPE_DISK for non struct se_device objects.
  1924. * Use 32-bit sector value.
  1925. */
  1926. if (!dev)
  1927. goto type_disk;
  1928. /*
  1929. * XXX_12 is not defined in SSC, throw an exception
  1930. */
  1931. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1932. *ret = -EINVAL;
  1933. return 0;
  1934. }
  1935. /*
  1936. * Everything else assume TYPE_DISK Sector CDB location.
  1937. * Use 32-bit sector value.
  1938. */
  1939. type_disk:
  1940. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  1941. }
  1942. static inline u32 transport_get_sectors_16(
  1943. unsigned char *cdb,
  1944. struct se_cmd *cmd,
  1945. int *ret)
  1946. {
  1947. struct se_device *dev = cmd->se_dev;
  1948. /*
  1949. * Assume TYPE_DISK for non struct se_device objects.
  1950. * Use 32-bit sector value.
  1951. */
  1952. if (!dev)
  1953. goto type_disk;
  1954. /*
  1955. * Use 24-bit allocation length for TYPE_TAPE.
  1956. */
  1957. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  1958. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  1959. type_disk:
  1960. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  1961. (cdb[12] << 8) + cdb[13];
  1962. }
  1963. /*
  1964. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  1965. */
  1966. static inline u32 transport_get_sectors_32(
  1967. unsigned char *cdb,
  1968. struct se_cmd *cmd,
  1969. int *ret)
  1970. {
  1971. /*
  1972. * Assume TYPE_DISK for non struct se_device objects.
  1973. * Use 32-bit sector value.
  1974. */
  1975. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  1976. (cdb[30] << 8) + cdb[31];
  1977. }
  1978. static inline u32 transport_get_size(
  1979. u32 sectors,
  1980. unsigned char *cdb,
  1981. struct se_cmd *cmd)
  1982. {
  1983. struct se_device *dev = cmd->se_dev;
  1984. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1985. if (cdb[1] & 1) { /* sectors */
  1986. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  1987. } else /* bytes */
  1988. return sectors;
  1989. }
  1990. pr_debug("Returning block_size: %u, sectors: %u == %u for"
  1991. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
  1992. sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  1993. dev->transport->name);
  1994. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  1995. }
  1996. static void transport_xor_callback(struct se_cmd *cmd)
  1997. {
  1998. unsigned char *buf, *addr;
  1999. struct scatterlist *sg;
  2000. unsigned int offset;
  2001. int i;
  2002. int count;
  2003. /*
  2004. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2005. *
  2006. * 1) read the specified logical block(s);
  2007. * 2) transfer logical blocks from the data-out buffer;
  2008. * 3) XOR the logical blocks transferred from the data-out buffer with
  2009. * the logical blocks read, storing the resulting XOR data in a buffer;
  2010. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2011. * blocks transferred from the data-out buffer; and
  2012. * 5) transfer the resulting XOR data to the data-in buffer.
  2013. */
  2014. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2015. if (!buf) {
  2016. pr_err("Unable to allocate xor_callback buf\n");
  2017. return;
  2018. }
  2019. /*
  2020. * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
  2021. * into the locally allocated *buf
  2022. */
  2023. sg_copy_to_buffer(cmd->t_data_sg,
  2024. cmd->t_data_nents,
  2025. buf,
  2026. cmd->data_length);
  2027. /*
  2028. * Now perform the XOR against the BIDI read memory located at
  2029. * cmd->t_mem_bidi_list
  2030. */
  2031. offset = 0;
  2032. for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
  2033. addr = kmap_atomic(sg_page(sg));
  2034. if (!addr)
  2035. goto out;
  2036. for (i = 0; i < sg->length; i++)
  2037. *(addr + sg->offset + i) ^= *(buf + offset + i);
  2038. offset += sg->length;
  2039. kunmap_atomic(addr);
  2040. }
  2041. out:
  2042. kfree(buf);
  2043. }
  2044. /*
  2045. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2046. */
  2047. static int transport_get_sense_data(struct se_cmd *cmd)
  2048. {
  2049. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2050. struct se_device *dev = cmd->se_dev;
  2051. unsigned long flags;
  2052. u32 offset = 0;
  2053. WARN_ON(!cmd->se_lun);
  2054. if (!dev)
  2055. return 0;
  2056. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2057. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2058. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2059. return 0;
  2060. }
  2061. if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
  2062. goto out;
  2063. if (!dev->transport->get_sense_buffer) {
  2064. pr_err("dev->transport->get_sense_buffer is NULL\n");
  2065. goto out;
  2066. }
  2067. sense_buffer = dev->transport->get_sense_buffer(cmd);
  2068. if (!sense_buffer) {
  2069. pr_err("ITT 0x%08x cmd %p: Unable to locate"
  2070. " sense buffer for task with sense\n",
  2071. cmd->se_tfo->get_task_tag(cmd), cmd);
  2072. goto out;
  2073. }
  2074. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2075. offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
  2076. memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
  2077. /* Automatically padded */
  2078. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  2079. pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
  2080. dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
  2081. return 0;
  2082. out:
  2083. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2084. return -1;
  2085. }
  2086. static inline long long transport_dev_end_lba(struct se_device *dev)
  2087. {
  2088. return dev->transport->get_blocks(dev) + 1;
  2089. }
  2090. static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
  2091. {
  2092. struct se_device *dev = cmd->se_dev;
  2093. u32 sectors;
  2094. if (dev->transport->get_device_type(dev) != TYPE_DISK)
  2095. return 0;
  2096. sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
  2097. if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
  2098. pr_err("LBA: %llu Sectors: %u exceeds"
  2099. " transport_dev_end_lba(): %llu\n",
  2100. cmd->t_task_lba, sectors,
  2101. transport_dev_end_lba(dev));
  2102. return -EINVAL;
  2103. }
  2104. return 0;
  2105. }
  2106. static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
  2107. {
  2108. /*
  2109. * Determine if the received WRITE_SAME is used to for direct
  2110. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  2111. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  2112. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
  2113. */
  2114. int passthrough = (dev->transport->transport_type ==
  2115. TRANSPORT_PLUGIN_PHBA_PDEV);
  2116. if (!passthrough) {
  2117. if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
  2118. pr_err("WRITE_SAME PBDATA and LBDATA"
  2119. " bits not supported for Block Discard"
  2120. " Emulation\n");
  2121. return -ENOSYS;
  2122. }
  2123. /*
  2124. * Currently for the emulated case we only accept
  2125. * tpws with the UNMAP=1 bit set.
  2126. */
  2127. if (!(flags[0] & 0x08)) {
  2128. pr_err("WRITE_SAME w/o UNMAP bit not"
  2129. " supported for Block Discard Emulation\n");
  2130. return -ENOSYS;
  2131. }
  2132. }
  2133. return 0;
  2134. }
  2135. static int transport_generic_cmd_sequencer(
  2136. struct se_cmd *cmd,
  2137. unsigned char *cdb)
  2138. {
  2139. struct se_device *dev = cmd->se_dev;
  2140. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2141. int sector_ret = 0, passthrough;
  2142. u32 sectors = 0, size = 0;
  2143. u16 service_action;
  2144. /*
  2145. * If we operate in passthrough mode we skip most CDB emulation and
  2146. * instead hand the commands down to the physical SCSI device.
  2147. */
  2148. passthrough =
  2149. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
  2150. switch (cdb[0]) {
  2151. case READ_6:
  2152. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2153. if (sector_ret)
  2154. goto out_unsupported_cdb;
  2155. size = transport_get_size(sectors, cdb, cmd);
  2156. cmd->t_task_lba = transport_lba_21(cdb);
  2157. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2158. break;
  2159. case READ_10:
  2160. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2161. if (sector_ret)
  2162. goto out_unsupported_cdb;
  2163. size = transport_get_size(sectors, cdb, cmd);
  2164. cmd->t_task_lba = transport_lba_32(cdb);
  2165. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2166. break;
  2167. case READ_12:
  2168. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2169. if (sector_ret)
  2170. goto out_unsupported_cdb;
  2171. size = transport_get_size(sectors, cdb, cmd);
  2172. cmd->t_task_lba = transport_lba_32(cdb);
  2173. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2174. break;
  2175. case READ_16:
  2176. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2177. if (sector_ret)
  2178. goto out_unsupported_cdb;
  2179. size = transport_get_size(sectors, cdb, cmd);
  2180. cmd->t_task_lba = transport_lba_64(cdb);
  2181. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2182. break;
  2183. case WRITE_6:
  2184. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2185. if (sector_ret)
  2186. goto out_unsupported_cdb;
  2187. size = transport_get_size(sectors, cdb, cmd);
  2188. cmd->t_task_lba = transport_lba_21(cdb);
  2189. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2190. break;
  2191. case WRITE_10:
  2192. case WRITE_VERIFY:
  2193. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2194. if (sector_ret)
  2195. goto out_unsupported_cdb;
  2196. size = transport_get_size(sectors, cdb, cmd);
  2197. cmd->t_task_lba = transport_lba_32(cdb);
  2198. if (cdb[1] & 0x8)
  2199. cmd->se_cmd_flags |= SCF_FUA;
  2200. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2201. break;
  2202. case WRITE_12:
  2203. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2204. if (sector_ret)
  2205. goto out_unsupported_cdb;
  2206. size = transport_get_size(sectors, cdb, cmd);
  2207. cmd->t_task_lba = transport_lba_32(cdb);
  2208. if (cdb[1] & 0x8)
  2209. cmd->se_cmd_flags |= SCF_FUA;
  2210. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2211. break;
  2212. case WRITE_16:
  2213. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2214. if (sector_ret)
  2215. goto out_unsupported_cdb;
  2216. size = transport_get_size(sectors, cdb, cmd);
  2217. cmd->t_task_lba = transport_lba_64(cdb);
  2218. if (cdb[1] & 0x8)
  2219. cmd->se_cmd_flags |= SCF_FUA;
  2220. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2221. break;
  2222. case XDWRITEREAD_10:
  2223. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2224. !(cmd->se_cmd_flags & SCF_BIDI))
  2225. goto out_invalid_cdb_field;
  2226. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2227. if (sector_ret)
  2228. goto out_unsupported_cdb;
  2229. size = transport_get_size(sectors, cdb, cmd);
  2230. cmd->t_task_lba = transport_lba_32(cdb);
  2231. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2232. /*
  2233. * Do now allow BIDI commands for passthrough mode.
  2234. */
  2235. if (passthrough)
  2236. goto out_unsupported_cdb;
  2237. /*
  2238. * Setup BIDI XOR callback to be run after I/O completion.
  2239. */
  2240. cmd->transport_complete_callback = &transport_xor_callback;
  2241. if (cdb[1] & 0x8)
  2242. cmd->se_cmd_flags |= SCF_FUA;
  2243. break;
  2244. case VARIABLE_LENGTH_CMD:
  2245. service_action = get_unaligned_be16(&cdb[8]);
  2246. switch (service_action) {
  2247. case XDWRITEREAD_32:
  2248. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2249. if (sector_ret)
  2250. goto out_unsupported_cdb;
  2251. size = transport_get_size(sectors, cdb, cmd);
  2252. /*
  2253. * Use WRITE_32 and READ_32 opcodes for the emulated
  2254. * XDWRITE_READ_32 logic.
  2255. */
  2256. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2257. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2258. /*
  2259. * Do now allow BIDI commands for passthrough mode.
  2260. */
  2261. if (passthrough)
  2262. goto out_unsupported_cdb;
  2263. /*
  2264. * Setup BIDI XOR callback to be run during after I/O
  2265. * completion.
  2266. */
  2267. cmd->transport_complete_callback = &transport_xor_callback;
  2268. if (cdb[1] & 0x8)
  2269. cmd->se_cmd_flags |= SCF_FUA;
  2270. break;
  2271. case WRITE_SAME_32:
  2272. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2273. if (sector_ret)
  2274. goto out_unsupported_cdb;
  2275. if (sectors)
  2276. size = transport_get_size(1, cdb, cmd);
  2277. else {
  2278. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  2279. " supported\n");
  2280. goto out_invalid_cdb_field;
  2281. }
  2282. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2283. if (target_check_write_same_discard(&cdb[10], dev) < 0)
  2284. goto out_unsupported_cdb;
  2285. if (!passthrough)
  2286. cmd->execute_cmd = target_emulate_write_same;
  2287. break;
  2288. default:
  2289. pr_err("VARIABLE_LENGTH_CMD service action"
  2290. " 0x%04x not supported\n", service_action);
  2291. goto out_unsupported_cdb;
  2292. }
  2293. break;
  2294. case MAINTENANCE_IN:
  2295. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2296. /* MAINTENANCE_IN from SCC-2 */
  2297. /*
  2298. * Check for emulated MI_REPORT_TARGET_PGS.
  2299. */
  2300. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS &&
  2301. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2302. cmd->execute_cmd =
  2303. target_emulate_report_target_port_groups;
  2304. }
  2305. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2306. (cdb[8] << 8) | cdb[9];
  2307. } else {
  2308. /* GPCMD_SEND_KEY from multi media commands */
  2309. size = (cdb[8] << 8) + cdb[9];
  2310. }
  2311. break;
  2312. case MODE_SELECT:
  2313. size = cdb[4];
  2314. break;
  2315. case MODE_SELECT_10:
  2316. size = (cdb[7] << 8) + cdb[8];
  2317. break;
  2318. case MODE_SENSE:
  2319. size = cdb[4];
  2320. if (!passthrough)
  2321. cmd->execute_cmd = target_emulate_modesense;
  2322. break;
  2323. case MODE_SENSE_10:
  2324. size = (cdb[7] << 8) + cdb[8];
  2325. if (!passthrough)
  2326. cmd->execute_cmd = target_emulate_modesense;
  2327. break;
  2328. case GPCMD_READ_BUFFER_CAPACITY:
  2329. case GPCMD_SEND_OPC:
  2330. case LOG_SELECT:
  2331. case LOG_SENSE:
  2332. size = (cdb[7] << 8) + cdb[8];
  2333. break;
  2334. case READ_BLOCK_LIMITS:
  2335. size = READ_BLOCK_LEN;
  2336. break;
  2337. case GPCMD_GET_CONFIGURATION:
  2338. case GPCMD_READ_FORMAT_CAPACITIES:
  2339. case GPCMD_READ_DISC_INFO:
  2340. case GPCMD_READ_TRACK_RZONE_INFO:
  2341. size = (cdb[7] << 8) + cdb[8];
  2342. break;
  2343. case PERSISTENT_RESERVE_IN:
  2344. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2345. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  2346. size = (cdb[7] << 8) + cdb[8];
  2347. break;
  2348. case PERSISTENT_RESERVE_OUT:
  2349. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2350. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  2351. size = (cdb[7] << 8) + cdb[8];
  2352. break;
  2353. case GPCMD_MECHANISM_STATUS:
  2354. case GPCMD_READ_DVD_STRUCTURE:
  2355. size = (cdb[8] << 8) + cdb[9];
  2356. break;
  2357. case READ_POSITION:
  2358. size = READ_POSITION_LEN;
  2359. break;
  2360. case MAINTENANCE_OUT:
  2361. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2362. /* MAINTENANCE_OUT from SCC-2
  2363. *
  2364. * Check for emulated MO_SET_TARGET_PGS.
  2365. */
  2366. if (cdb[1] == MO_SET_TARGET_PGS &&
  2367. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2368. cmd->execute_cmd =
  2369. target_emulate_set_target_port_groups;
  2370. }
  2371. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2372. (cdb[8] << 8) | cdb[9];
  2373. } else {
  2374. /* GPCMD_REPORT_KEY from multi media commands */
  2375. size = (cdb[8] << 8) + cdb[9];
  2376. }
  2377. break;
  2378. case INQUIRY:
  2379. size = (cdb[3] << 8) + cdb[4];
  2380. /*
  2381. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2382. * See spc4r17 section 5.3
  2383. */
  2384. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2385. cmd->sam_task_attr = MSG_HEAD_TAG;
  2386. if (!passthrough)
  2387. cmd->execute_cmd = target_emulate_inquiry;
  2388. break;
  2389. case READ_BUFFER:
  2390. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2391. break;
  2392. case READ_CAPACITY:
  2393. size = READ_CAP_LEN;
  2394. if (!passthrough)
  2395. cmd->execute_cmd = target_emulate_readcapacity;
  2396. break;
  2397. case READ_MEDIA_SERIAL_NUMBER:
  2398. case SECURITY_PROTOCOL_IN:
  2399. case SECURITY_PROTOCOL_OUT:
  2400. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2401. break;
  2402. case SERVICE_ACTION_IN:
  2403. switch (cmd->t_task_cdb[1] & 0x1f) {
  2404. case SAI_READ_CAPACITY_16:
  2405. if (!passthrough)
  2406. cmd->execute_cmd =
  2407. target_emulate_readcapacity_16;
  2408. break;
  2409. default:
  2410. if (passthrough)
  2411. break;
  2412. pr_err("Unsupported SA: 0x%02x\n",
  2413. cmd->t_task_cdb[1] & 0x1f);
  2414. goto out_invalid_cdb_field;
  2415. }
  2416. /*FALLTHROUGH*/
  2417. case ACCESS_CONTROL_IN:
  2418. case ACCESS_CONTROL_OUT:
  2419. case EXTENDED_COPY:
  2420. case READ_ATTRIBUTE:
  2421. case RECEIVE_COPY_RESULTS:
  2422. case WRITE_ATTRIBUTE:
  2423. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2424. (cdb[12] << 8) | cdb[13];
  2425. break;
  2426. case RECEIVE_DIAGNOSTIC:
  2427. case SEND_DIAGNOSTIC:
  2428. size = (cdb[3] << 8) | cdb[4];
  2429. break;
  2430. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2431. #if 0
  2432. case GPCMD_READ_CD:
  2433. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2434. size = (2336 * sectors);
  2435. break;
  2436. #endif
  2437. case READ_TOC:
  2438. size = cdb[8];
  2439. break;
  2440. case REQUEST_SENSE:
  2441. size = cdb[4];
  2442. if (!passthrough)
  2443. cmd->execute_cmd = target_emulate_request_sense;
  2444. break;
  2445. case READ_ELEMENT_STATUS:
  2446. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2447. break;
  2448. case WRITE_BUFFER:
  2449. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2450. break;
  2451. case RESERVE:
  2452. case RESERVE_10:
  2453. /*
  2454. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2455. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2456. */
  2457. if (cdb[0] == RESERVE_10)
  2458. size = (cdb[7] << 8) | cdb[8];
  2459. else
  2460. size = cmd->data_length;
  2461. /*
  2462. * Setup the legacy emulated handler for SPC-2 and
  2463. * >= SPC-3 compatible reservation handling (CRH=1)
  2464. * Otherwise, we assume the underlying SCSI logic is
  2465. * is running in SPC_PASSTHROUGH, and wants reservations
  2466. * emulation disabled.
  2467. */
  2468. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2469. cmd->execute_cmd = target_scsi2_reservation_reserve;
  2470. break;
  2471. case RELEASE:
  2472. case RELEASE_10:
  2473. /*
  2474. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  2475. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2476. */
  2477. if (cdb[0] == RELEASE_10)
  2478. size = (cdb[7] << 8) | cdb[8];
  2479. else
  2480. size = cmd->data_length;
  2481. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2482. cmd->execute_cmd = target_scsi2_reservation_release;
  2483. break;
  2484. case SYNCHRONIZE_CACHE:
  2485. case SYNCHRONIZE_CACHE_16:
  2486. /*
  2487. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  2488. */
  2489. if (cdb[0] == SYNCHRONIZE_CACHE) {
  2490. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2491. cmd->t_task_lba = transport_lba_32(cdb);
  2492. } else {
  2493. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2494. cmd->t_task_lba = transport_lba_64(cdb);
  2495. }
  2496. if (sector_ret)
  2497. goto out_unsupported_cdb;
  2498. size = transport_get_size(sectors, cdb, cmd);
  2499. if (passthrough)
  2500. break;
  2501. /*
  2502. * Check to ensure that LBA + Range does not exceed past end of
  2503. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  2504. */
  2505. if ((cmd->t_task_lba != 0) || (sectors != 0)) {
  2506. if (transport_cmd_get_valid_sectors(cmd) < 0)
  2507. goto out_invalid_cdb_field;
  2508. }
  2509. cmd->execute_cmd = target_emulate_synchronize_cache;
  2510. break;
  2511. case UNMAP:
  2512. size = get_unaligned_be16(&cdb[7]);
  2513. if (!passthrough)
  2514. cmd->execute_cmd = target_emulate_unmap;
  2515. break;
  2516. case WRITE_SAME_16:
  2517. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2518. if (sector_ret)
  2519. goto out_unsupported_cdb;
  2520. if (sectors)
  2521. size = transport_get_size(1, cdb, cmd);
  2522. else {
  2523. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2524. goto out_invalid_cdb_field;
  2525. }
  2526. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  2527. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2528. goto out_unsupported_cdb;
  2529. if (!passthrough)
  2530. cmd->execute_cmd = target_emulate_write_same;
  2531. break;
  2532. case WRITE_SAME:
  2533. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2534. if (sector_ret)
  2535. goto out_unsupported_cdb;
  2536. if (sectors)
  2537. size = transport_get_size(1, cdb, cmd);
  2538. else {
  2539. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2540. goto out_invalid_cdb_field;
  2541. }
  2542. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  2543. /*
  2544. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  2545. * of byte 1 bit 3 UNMAP instead of original reserved field
  2546. */
  2547. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2548. goto out_unsupported_cdb;
  2549. if (!passthrough)
  2550. cmd->execute_cmd = target_emulate_write_same;
  2551. break;
  2552. case ALLOW_MEDIUM_REMOVAL:
  2553. case ERASE:
  2554. case REZERO_UNIT:
  2555. case SEEK_10:
  2556. case SPACE:
  2557. case START_STOP:
  2558. case TEST_UNIT_READY:
  2559. case VERIFY:
  2560. case WRITE_FILEMARKS:
  2561. if (!passthrough)
  2562. cmd->execute_cmd = target_emulate_noop;
  2563. break;
  2564. case GPCMD_CLOSE_TRACK:
  2565. case INITIALIZE_ELEMENT_STATUS:
  2566. case GPCMD_LOAD_UNLOAD:
  2567. case GPCMD_SET_SPEED:
  2568. case MOVE_MEDIUM:
  2569. break;
  2570. case REPORT_LUNS:
  2571. cmd->execute_cmd = target_report_luns;
  2572. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2573. /*
  2574. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  2575. * See spc4r17 section 5.3
  2576. */
  2577. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2578. cmd->sam_task_attr = MSG_HEAD_TAG;
  2579. break;
  2580. case GET_EVENT_STATUS_NOTIFICATION:
  2581. size = (cdb[7] << 8) | cdb[8];
  2582. break;
  2583. case ATA_16:
  2584. /* Only support ATA passthrough to pSCSI backends.. */
  2585. if (!passthrough)
  2586. goto out_unsupported_cdb;
  2587. /* T_LENGTH */
  2588. switch (cdb[2] & 0x3) {
  2589. case 0x0:
  2590. sectors = 0;
  2591. break;
  2592. case 0x1:
  2593. sectors = (((cdb[1] & 0x1) ? cdb[3] : 0) << 8) | cdb[4];
  2594. break;
  2595. case 0x2:
  2596. sectors = (((cdb[1] & 0x1) ? cdb[5] : 0) << 8) | cdb[6];
  2597. break;
  2598. case 0x3:
  2599. pr_err("T_LENGTH=0x3 not supported for ATA_16\n");
  2600. goto out_invalid_cdb_field;
  2601. }
  2602. /* BYTE_BLOCK */
  2603. if (cdb[2] & 0x4) {
  2604. /* BLOCK T_TYPE: 512 or sector */
  2605. size = sectors * ((cdb[2] & 0x10) ?
  2606. dev->se_sub_dev->se_dev_attrib.block_size : 512);
  2607. } else {
  2608. /* BYTE */
  2609. size = sectors;
  2610. }
  2611. break;
  2612. default:
  2613. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  2614. " 0x%02x, sending CHECK_CONDITION.\n",
  2615. cmd->se_tfo->get_fabric_name(), cdb[0]);
  2616. goto out_unsupported_cdb;
  2617. }
  2618. if (cmd->unknown_data_length)
  2619. cmd->data_length = size;
  2620. if (size != cmd->data_length) {
  2621. pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
  2622. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  2623. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  2624. cmd->data_length, size, cdb[0]);
  2625. cmd->cmd_spdtl = size;
  2626. if (cmd->data_direction == DMA_TO_DEVICE) {
  2627. pr_err("Rejecting underflow/overflow"
  2628. " WRITE data\n");
  2629. goto out_invalid_cdb_field;
  2630. }
  2631. /*
  2632. * Reject READ_* or WRITE_* with overflow/underflow for
  2633. * type SCF_SCSI_DATA_CDB.
  2634. */
  2635. if (dev->se_sub_dev->se_dev_attrib.block_size != 512) {
  2636. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  2637. " CDB on non 512-byte sector setup subsystem"
  2638. " plugin: %s\n", dev->transport->name);
  2639. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  2640. goto out_invalid_cdb_field;
  2641. }
  2642. if (size > cmd->data_length) {
  2643. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  2644. cmd->residual_count = (size - cmd->data_length);
  2645. } else {
  2646. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  2647. cmd->residual_count = (cmd->data_length - size);
  2648. }
  2649. cmd->data_length = size;
  2650. }
  2651. if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
  2652. if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
  2653. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
  2654. " big sectors %u exceeds fabric_max_sectors:"
  2655. " %u\n", cdb[0], sectors,
  2656. su_dev->se_dev_attrib.fabric_max_sectors);
  2657. goto out_invalid_cdb_field;
  2658. }
  2659. if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
  2660. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
  2661. " big sectors %u exceeds backend hw_max_sectors:"
  2662. " %u\n", cdb[0], sectors,
  2663. su_dev->se_dev_attrib.hw_max_sectors);
  2664. goto out_invalid_cdb_field;
  2665. }
  2666. }
  2667. /* reject any command that we don't have a handler for */
  2668. if (!(passthrough || cmd->execute_cmd ||
  2669. (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB)))
  2670. goto out_unsupported_cdb;
  2671. return 0;
  2672. out_unsupported_cdb:
  2673. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2674. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  2675. return -EINVAL;
  2676. out_invalid_cdb_field:
  2677. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2678. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2679. return -EINVAL;
  2680. }
  2681. /*
  2682. * Called from I/O completion to determine which dormant/delayed
  2683. * and ordered cmds need to have their tasks added to the execution queue.
  2684. */
  2685. static void transport_complete_task_attr(struct se_cmd *cmd)
  2686. {
  2687. struct se_device *dev = cmd->se_dev;
  2688. struct se_cmd *cmd_p, *cmd_tmp;
  2689. int new_active_tasks = 0;
  2690. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  2691. atomic_dec(&dev->simple_cmds);
  2692. smp_mb__after_atomic_dec();
  2693. dev->dev_cur_ordered_id++;
  2694. pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
  2695. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  2696. cmd->se_ordered_id);
  2697. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2698. dev->dev_cur_ordered_id++;
  2699. pr_debug("Incremented dev_cur_ordered_id: %u for"
  2700. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  2701. cmd->se_ordered_id);
  2702. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2703. atomic_dec(&dev->dev_ordered_sync);
  2704. smp_mb__after_atomic_dec();
  2705. dev->dev_cur_ordered_id++;
  2706. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
  2707. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  2708. }
  2709. /*
  2710. * Process all commands up to the last received
  2711. * ORDERED task attribute which requires another blocking
  2712. * boundary
  2713. */
  2714. spin_lock(&dev->delayed_cmd_lock);
  2715. list_for_each_entry_safe(cmd_p, cmd_tmp,
  2716. &dev->delayed_cmd_list, se_delayed_node) {
  2717. list_del(&cmd_p->se_delayed_node);
  2718. spin_unlock(&dev->delayed_cmd_lock);
  2719. pr_debug("Calling add_tasks() for"
  2720. " cmd_p: 0x%02x Task Attr: 0x%02x"
  2721. " Dormant -> Active, se_ordered_id: %u\n",
  2722. cmd_p->t_task_cdb[0],
  2723. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  2724. target_add_to_execute_list(cmd_p);
  2725. new_active_tasks++;
  2726. spin_lock(&dev->delayed_cmd_lock);
  2727. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  2728. break;
  2729. }
  2730. spin_unlock(&dev->delayed_cmd_lock);
  2731. /*
  2732. * If new tasks have become active, wake up the transport thread
  2733. * to do the processing of the Active tasks.
  2734. */
  2735. if (new_active_tasks != 0)
  2736. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2737. }
  2738. static void transport_complete_qf(struct se_cmd *cmd)
  2739. {
  2740. int ret = 0;
  2741. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2742. transport_complete_task_attr(cmd);
  2743. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2744. ret = cmd->se_tfo->queue_status(cmd);
  2745. if (ret)
  2746. goto out;
  2747. }
  2748. switch (cmd->data_direction) {
  2749. case DMA_FROM_DEVICE:
  2750. ret = cmd->se_tfo->queue_data_in(cmd);
  2751. break;
  2752. case DMA_TO_DEVICE:
  2753. if (cmd->t_bidi_data_sg) {
  2754. ret = cmd->se_tfo->queue_data_in(cmd);
  2755. if (ret < 0)
  2756. break;
  2757. }
  2758. /* Fall through for DMA_TO_DEVICE */
  2759. case DMA_NONE:
  2760. ret = cmd->se_tfo->queue_status(cmd);
  2761. break;
  2762. default:
  2763. break;
  2764. }
  2765. out:
  2766. if (ret < 0) {
  2767. transport_handle_queue_full(cmd, cmd->se_dev);
  2768. return;
  2769. }
  2770. transport_lun_remove_cmd(cmd);
  2771. transport_cmd_check_stop_to_fabric(cmd);
  2772. }
  2773. static void transport_handle_queue_full(
  2774. struct se_cmd *cmd,
  2775. struct se_device *dev)
  2776. {
  2777. spin_lock_irq(&dev->qf_cmd_lock);
  2778. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  2779. atomic_inc(&dev->dev_qf_count);
  2780. smp_mb__after_atomic_inc();
  2781. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  2782. schedule_work(&cmd->se_dev->qf_work_queue);
  2783. }
  2784. static void target_complete_ok_work(struct work_struct *work)
  2785. {
  2786. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2787. int reason = 0, ret;
  2788. /*
  2789. * Check if we need to move delayed/dormant tasks from cmds on the
  2790. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  2791. * Attribute.
  2792. */
  2793. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2794. transport_complete_task_attr(cmd);
  2795. /*
  2796. * Check to schedule QUEUE_FULL work, or execute an existing
  2797. * cmd->transport_qf_callback()
  2798. */
  2799. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  2800. schedule_work(&cmd->se_dev->qf_work_queue);
  2801. /*
  2802. * Check if we need to retrieve a sense buffer from
  2803. * the struct se_cmd in question.
  2804. */
  2805. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2806. if (transport_get_sense_data(cmd) < 0)
  2807. reason = TCM_NON_EXISTENT_LUN;
  2808. if (cmd->scsi_status) {
  2809. ret = transport_send_check_condition_and_sense(
  2810. cmd, reason, 1);
  2811. if (ret == -EAGAIN || ret == -ENOMEM)
  2812. goto queue_full;
  2813. transport_lun_remove_cmd(cmd);
  2814. transport_cmd_check_stop_to_fabric(cmd);
  2815. return;
  2816. }
  2817. }
  2818. /*
  2819. * Check for a callback, used by amongst other things
  2820. * XDWRITE_READ_10 emulation.
  2821. */
  2822. if (cmd->transport_complete_callback)
  2823. cmd->transport_complete_callback(cmd);
  2824. switch (cmd->data_direction) {
  2825. case DMA_FROM_DEVICE:
  2826. spin_lock(&cmd->se_lun->lun_sep_lock);
  2827. if (cmd->se_lun->lun_sep) {
  2828. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2829. cmd->data_length;
  2830. }
  2831. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2832. ret = cmd->se_tfo->queue_data_in(cmd);
  2833. if (ret == -EAGAIN || ret == -ENOMEM)
  2834. goto queue_full;
  2835. break;
  2836. case DMA_TO_DEVICE:
  2837. spin_lock(&cmd->se_lun->lun_sep_lock);
  2838. if (cmd->se_lun->lun_sep) {
  2839. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  2840. cmd->data_length;
  2841. }
  2842. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2843. /*
  2844. * Check if we need to send READ payload for BIDI-COMMAND
  2845. */
  2846. if (cmd->t_bidi_data_sg) {
  2847. spin_lock(&cmd->se_lun->lun_sep_lock);
  2848. if (cmd->se_lun->lun_sep) {
  2849. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2850. cmd->data_length;
  2851. }
  2852. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2853. ret = cmd->se_tfo->queue_data_in(cmd);
  2854. if (ret == -EAGAIN || ret == -ENOMEM)
  2855. goto queue_full;
  2856. break;
  2857. }
  2858. /* Fall through for DMA_TO_DEVICE */
  2859. case DMA_NONE:
  2860. ret = cmd->se_tfo->queue_status(cmd);
  2861. if (ret == -EAGAIN || ret == -ENOMEM)
  2862. goto queue_full;
  2863. break;
  2864. default:
  2865. break;
  2866. }
  2867. transport_lun_remove_cmd(cmd);
  2868. transport_cmd_check_stop_to_fabric(cmd);
  2869. return;
  2870. queue_full:
  2871. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  2872. " data_direction: %d\n", cmd, cmd->data_direction);
  2873. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  2874. transport_handle_queue_full(cmd, cmd->se_dev);
  2875. }
  2876. static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
  2877. {
  2878. struct scatterlist *sg;
  2879. int count;
  2880. for_each_sg(sgl, sg, nents, count)
  2881. __free_page(sg_page(sg));
  2882. kfree(sgl);
  2883. }
  2884. static inline void transport_free_pages(struct se_cmd *cmd)
  2885. {
  2886. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  2887. return;
  2888. transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  2889. cmd->t_data_sg = NULL;
  2890. cmd->t_data_nents = 0;
  2891. transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  2892. cmd->t_bidi_data_sg = NULL;
  2893. cmd->t_bidi_data_nents = 0;
  2894. }
  2895. /**
  2896. * transport_release_cmd - free a command
  2897. * @cmd: command to free
  2898. *
  2899. * This routine unconditionally frees a command, and reference counting
  2900. * or list removal must be done in the caller.
  2901. */
  2902. static void transport_release_cmd(struct se_cmd *cmd)
  2903. {
  2904. BUG_ON(!cmd->se_tfo);
  2905. if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  2906. core_tmr_release_req(cmd->se_tmr_req);
  2907. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  2908. kfree(cmd->t_task_cdb);
  2909. /*
  2910. * If this cmd has been setup with target_get_sess_cmd(), drop
  2911. * the kref and call ->release_cmd() in kref callback.
  2912. */
  2913. if (cmd->check_release != 0) {
  2914. target_put_sess_cmd(cmd->se_sess, cmd);
  2915. return;
  2916. }
  2917. cmd->se_tfo->release_cmd(cmd);
  2918. }
  2919. /**
  2920. * transport_put_cmd - release a reference to a command
  2921. * @cmd: command to release
  2922. *
  2923. * This routine releases our reference to the command and frees it if possible.
  2924. */
  2925. static void transport_put_cmd(struct se_cmd *cmd)
  2926. {
  2927. unsigned long flags;
  2928. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2929. if (atomic_read(&cmd->t_fe_count)) {
  2930. if (!atomic_dec_and_test(&cmd->t_fe_count))
  2931. goto out_busy;
  2932. }
  2933. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  2934. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  2935. target_remove_from_state_list(cmd);
  2936. }
  2937. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2938. transport_free_pages(cmd);
  2939. transport_release_cmd(cmd);
  2940. return;
  2941. out_busy:
  2942. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2943. }
  2944. /*
  2945. * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
  2946. * allocating in the core.
  2947. * @cmd: Associated se_cmd descriptor
  2948. * @mem: SGL style memory for TCM WRITE / READ
  2949. * @sg_mem_num: Number of SGL elements
  2950. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  2951. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  2952. *
  2953. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  2954. * of parameters.
  2955. */
  2956. int transport_generic_map_mem_to_cmd(
  2957. struct se_cmd *cmd,
  2958. struct scatterlist *sgl,
  2959. u32 sgl_count,
  2960. struct scatterlist *sgl_bidi,
  2961. u32 sgl_bidi_count)
  2962. {
  2963. if (!sgl || !sgl_count)
  2964. return 0;
  2965. /*
  2966. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  2967. * scatterlists already have been set to follow what the fabric
  2968. * passes for the original expected data transfer length.
  2969. */
  2970. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  2971. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  2972. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  2973. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2974. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2975. return -EINVAL;
  2976. }
  2977. cmd->t_data_sg = sgl;
  2978. cmd->t_data_nents = sgl_count;
  2979. if (sgl_bidi && sgl_bidi_count) {
  2980. cmd->t_bidi_data_sg = sgl_bidi;
  2981. cmd->t_bidi_data_nents = sgl_bidi_count;
  2982. }
  2983. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  2984. return 0;
  2985. }
  2986. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  2987. void *transport_kmap_data_sg(struct se_cmd *cmd)
  2988. {
  2989. struct scatterlist *sg = cmd->t_data_sg;
  2990. struct page **pages;
  2991. int i;
  2992. BUG_ON(!sg);
  2993. /*
  2994. * We need to take into account a possible offset here for fabrics like
  2995. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  2996. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  2997. */
  2998. if (!cmd->t_data_nents)
  2999. return NULL;
  3000. else if (cmd->t_data_nents == 1)
  3001. return kmap(sg_page(sg)) + sg->offset;
  3002. /* >1 page. use vmap */
  3003. pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
  3004. if (!pages)
  3005. return NULL;
  3006. /* convert sg[] to pages[] */
  3007. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  3008. pages[i] = sg_page(sg);
  3009. }
  3010. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  3011. kfree(pages);
  3012. if (!cmd->t_data_vmap)
  3013. return NULL;
  3014. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  3015. }
  3016. EXPORT_SYMBOL(transport_kmap_data_sg);
  3017. void transport_kunmap_data_sg(struct se_cmd *cmd)
  3018. {
  3019. if (!cmd->t_data_nents) {
  3020. return;
  3021. } else if (cmd->t_data_nents == 1) {
  3022. kunmap(sg_page(cmd->t_data_sg));
  3023. return;
  3024. }
  3025. vunmap(cmd->t_data_vmap);
  3026. cmd->t_data_vmap = NULL;
  3027. }
  3028. EXPORT_SYMBOL(transport_kunmap_data_sg);
  3029. static int
  3030. transport_generic_get_mem(struct se_cmd *cmd)
  3031. {
  3032. u32 length = cmd->data_length;
  3033. unsigned int nents;
  3034. struct page *page;
  3035. gfp_t zero_flag;
  3036. int i = 0;
  3037. nents = DIV_ROUND_UP(length, PAGE_SIZE);
  3038. cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
  3039. if (!cmd->t_data_sg)
  3040. return -ENOMEM;
  3041. cmd->t_data_nents = nents;
  3042. sg_init_table(cmd->t_data_sg, nents);
  3043. zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO;
  3044. while (length) {
  3045. u32 page_len = min_t(u32, length, PAGE_SIZE);
  3046. page = alloc_page(GFP_KERNEL | zero_flag);
  3047. if (!page)
  3048. goto out;
  3049. sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
  3050. length -= page_len;
  3051. i++;
  3052. }
  3053. return 0;
  3054. out:
  3055. while (i >= 0) {
  3056. __free_page(sg_page(&cmd->t_data_sg[i]));
  3057. i--;
  3058. }
  3059. kfree(cmd->t_data_sg);
  3060. cmd->t_data_sg = NULL;
  3061. return -ENOMEM;
  3062. }
  3063. /*
  3064. * Allocate any required resources to execute the command. For writes we
  3065. * might not have the payload yet, so notify the fabric via a call to
  3066. * ->write_pending instead. Otherwise place it on the execution queue.
  3067. */
  3068. int transport_generic_new_cmd(struct se_cmd *cmd)
  3069. {
  3070. struct se_device *dev = cmd->se_dev;
  3071. int ret = 0;
  3072. /*
  3073. * Determine is the TCM fabric module has already allocated physical
  3074. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  3075. * beforehand.
  3076. */
  3077. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  3078. cmd->data_length) {
  3079. ret = transport_generic_get_mem(cmd);
  3080. if (ret < 0)
  3081. goto out_fail;
  3082. }
  3083. /* Workaround for handling zero-length control CDBs */
  3084. if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->data_length) {
  3085. spin_lock_irq(&cmd->t_state_lock);
  3086. cmd->t_state = TRANSPORT_COMPLETE;
  3087. cmd->transport_state |= CMD_T_ACTIVE;
  3088. spin_unlock_irq(&cmd->t_state_lock);
  3089. if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
  3090. u8 ua_asc = 0, ua_ascq = 0;
  3091. core_scsi3_ua_clear_for_request_sense(cmd,
  3092. &ua_asc, &ua_ascq);
  3093. }
  3094. INIT_WORK(&cmd->work, target_complete_ok_work);
  3095. queue_work(target_completion_wq, &cmd->work);
  3096. return 0;
  3097. }
  3098. if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
  3099. struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
  3100. if (transport_cmd_get_valid_sectors(cmd) < 0)
  3101. return -EINVAL;
  3102. BUG_ON(cmd->data_length % attr->block_size);
  3103. BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
  3104. attr->hw_max_sectors);
  3105. }
  3106. atomic_inc(&cmd->t_fe_count);
  3107. /*
  3108. * For WRITEs, let the fabric know its buffer is ready.
  3109. *
  3110. * The command will be added to the execution queue after its write
  3111. * data has arrived.
  3112. */
  3113. if (cmd->data_direction == DMA_TO_DEVICE) {
  3114. target_add_to_state_list(cmd);
  3115. return transport_generic_write_pending(cmd);
  3116. }
  3117. /*
  3118. * Everything else but a WRITE, add the command to the execution queue.
  3119. */
  3120. transport_execute_tasks(cmd);
  3121. return 0;
  3122. out_fail:
  3123. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3124. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3125. return -EINVAL;
  3126. }
  3127. EXPORT_SYMBOL(transport_generic_new_cmd);
  3128. /* transport_generic_process_write():
  3129. *
  3130. *
  3131. */
  3132. void transport_generic_process_write(struct se_cmd *cmd)
  3133. {
  3134. transport_execute_tasks(cmd);
  3135. }
  3136. EXPORT_SYMBOL(transport_generic_process_write);
  3137. static void transport_write_pending_qf(struct se_cmd *cmd)
  3138. {
  3139. int ret;
  3140. ret = cmd->se_tfo->write_pending(cmd);
  3141. if (ret == -EAGAIN || ret == -ENOMEM) {
  3142. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  3143. cmd);
  3144. transport_handle_queue_full(cmd, cmd->se_dev);
  3145. }
  3146. }
  3147. static int transport_generic_write_pending(struct se_cmd *cmd)
  3148. {
  3149. unsigned long flags;
  3150. int ret;
  3151. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3152. cmd->t_state = TRANSPORT_WRITE_PENDING;
  3153. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3154. /*
  3155. * Clear the se_cmd for WRITE_PENDING status in order to set
  3156. * CMD_T_ACTIVE so that transport_generic_handle_data can be called
  3157. * from HW target mode interrupt code. This is safe to be called
  3158. * with transport_off=1 before the cmd->se_tfo->write_pending
  3159. * because the se_cmd->se_lun pointer is not being cleared.
  3160. */
  3161. transport_cmd_check_stop(cmd, 1, 0);
  3162. /*
  3163. * Call the fabric write_pending function here to let the
  3164. * frontend know that WRITE buffers are ready.
  3165. */
  3166. ret = cmd->se_tfo->write_pending(cmd);
  3167. if (ret == -EAGAIN || ret == -ENOMEM)
  3168. goto queue_full;
  3169. else if (ret < 0)
  3170. return ret;
  3171. return 1;
  3172. queue_full:
  3173. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  3174. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  3175. transport_handle_queue_full(cmd, cmd->se_dev);
  3176. return 0;
  3177. }
  3178. void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  3179. {
  3180. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  3181. if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  3182. transport_wait_for_tasks(cmd);
  3183. transport_release_cmd(cmd);
  3184. } else {
  3185. if (wait_for_tasks)
  3186. transport_wait_for_tasks(cmd);
  3187. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  3188. if (cmd->se_lun)
  3189. transport_lun_remove_cmd(cmd);
  3190. transport_put_cmd(cmd);
  3191. }
  3192. }
  3193. EXPORT_SYMBOL(transport_generic_free_cmd);
  3194. /* target_get_sess_cmd - Add command to active ->sess_cmd_list
  3195. * @se_sess: session to reference
  3196. * @se_cmd: command descriptor to add
  3197. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  3198. */
  3199. void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
  3200. bool ack_kref)
  3201. {
  3202. unsigned long flags;
  3203. kref_init(&se_cmd->cmd_kref);
  3204. /*
  3205. * Add a second kref if the fabric caller is expecting to handle
  3206. * fabric acknowledgement that requires two target_put_sess_cmd()
  3207. * invocations before se_cmd descriptor release.
  3208. */
  3209. if (ack_kref == true) {
  3210. kref_get(&se_cmd->cmd_kref);
  3211. se_cmd->se_cmd_flags |= SCF_ACK_KREF;
  3212. }
  3213. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3214. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  3215. se_cmd->check_release = 1;
  3216. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3217. }
  3218. EXPORT_SYMBOL(target_get_sess_cmd);
  3219. static void target_release_cmd_kref(struct kref *kref)
  3220. {
  3221. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  3222. struct se_session *se_sess = se_cmd->se_sess;
  3223. unsigned long flags;
  3224. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3225. if (list_empty(&se_cmd->se_cmd_list)) {
  3226. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3227. se_cmd->se_tfo->release_cmd(se_cmd);
  3228. return;
  3229. }
  3230. if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
  3231. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3232. complete(&se_cmd->cmd_wait_comp);
  3233. return;
  3234. }
  3235. list_del(&se_cmd->se_cmd_list);
  3236. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3237. se_cmd->se_tfo->release_cmd(se_cmd);
  3238. }
  3239. /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
  3240. * @se_sess: session to reference
  3241. * @se_cmd: command descriptor to drop
  3242. */
  3243. int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
  3244. {
  3245. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  3246. }
  3247. EXPORT_SYMBOL(target_put_sess_cmd);
  3248. /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
  3249. * @se_sess: session to split
  3250. */
  3251. void target_splice_sess_cmd_list(struct se_session *se_sess)
  3252. {
  3253. struct se_cmd *se_cmd;
  3254. unsigned long flags;
  3255. WARN_ON(!list_empty(&se_sess->sess_wait_list));
  3256. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  3257. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3258. se_sess->sess_tearing_down = 1;
  3259. list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
  3260. list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
  3261. se_cmd->cmd_wait_set = 1;
  3262. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3263. }
  3264. EXPORT_SYMBOL(target_splice_sess_cmd_list);
  3265. /* target_wait_for_sess_cmds - Wait for outstanding descriptors
  3266. * @se_sess: session to wait for active I/O
  3267. * @wait_for_tasks: Make extra transport_wait_for_tasks call
  3268. */
  3269. void target_wait_for_sess_cmds(
  3270. struct se_session *se_sess,
  3271. int wait_for_tasks)
  3272. {
  3273. struct se_cmd *se_cmd, *tmp_cmd;
  3274. bool rc = false;
  3275. list_for_each_entry_safe(se_cmd, tmp_cmd,
  3276. &se_sess->sess_wait_list, se_cmd_list) {
  3277. list_del(&se_cmd->se_cmd_list);
  3278. pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
  3279. " %d\n", se_cmd, se_cmd->t_state,
  3280. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3281. if (wait_for_tasks) {
  3282. pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3283. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3284. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3285. rc = transport_wait_for_tasks(se_cmd);
  3286. pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3287. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3288. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3289. }
  3290. if (!rc) {
  3291. wait_for_completion(&se_cmd->cmd_wait_comp);
  3292. pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
  3293. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3294. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3295. }
  3296. se_cmd->se_tfo->release_cmd(se_cmd);
  3297. }
  3298. }
  3299. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  3300. /* transport_lun_wait_for_tasks():
  3301. *
  3302. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  3303. * an struct se_lun to be successfully shutdown.
  3304. */
  3305. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  3306. {
  3307. unsigned long flags;
  3308. int ret = 0;
  3309. /*
  3310. * If the frontend has already requested this struct se_cmd to
  3311. * be stopped, we can safely ignore this struct se_cmd.
  3312. */
  3313. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3314. if (cmd->transport_state & CMD_T_STOP) {
  3315. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3316. pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
  3317. cmd->se_tfo->get_task_tag(cmd));
  3318. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3319. transport_cmd_check_stop(cmd, 1, 0);
  3320. return -EPERM;
  3321. }
  3322. cmd->transport_state |= CMD_T_LUN_FE_STOP;
  3323. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3324. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3325. // XXX: audit task_flags checks.
  3326. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3327. if ((cmd->transport_state & CMD_T_BUSY) &&
  3328. (cmd->transport_state & CMD_T_SENT)) {
  3329. if (!target_stop_cmd(cmd, &flags))
  3330. ret++;
  3331. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3332. } else {
  3333. spin_unlock_irqrestore(&cmd->t_state_lock,
  3334. flags);
  3335. target_remove_from_execute_list(cmd);
  3336. }
  3337. pr_debug("ConfigFS: cmd: %p stop tasks ret:"
  3338. " %d\n", cmd, ret);
  3339. if (!ret) {
  3340. pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  3341. cmd->se_tfo->get_task_tag(cmd));
  3342. wait_for_completion(&cmd->transport_lun_stop_comp);
  3343. pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  3344. cmd->se_tfo->get_task_tag(cmd));
  3345. }
  3346. transport_remove_cmd_from_queue(cmd);
  3347. return 0;
  3348. }
  3349. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  3350. {
  3351. struct se_cmd *cmd = NULL;
  3352. unsigned long lun_flags, cmd_flags;
  3353. /*
  3354. * Do exception processing and return CHECK_CONDITION status to the
  3355. * Initiator Port.
  3356. */
  3357. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3358. while (!list_empty(&lun->lun_cmd_list)) {
  3359. cmd = list_first_entry(&lun->lun_cmd_list,
  3360. struct se_cmd, se_lun_node);
  3361. list_del_init(&cmd->se_lun_node);
  3362. /*
  3363. * This will notify iscsi_target_transport.c:
  3364. * transport_cmd_check_stop() that a LUN shutdown is in
  3365. * progress for the iscsi_cmd_t.
  3366. */
  3367. spin_lock(&cmd->t_state_lock);
  3368. pr_debug("SE_LUN[%d] - Setting cmd->transport"
  3369. "_lun_stop for ITT: 0x%08x\n",
  3370. cmd->se_lun->unpacked_lun,
  3371. cmd->se_tfo->get_task_tag(cmd));
  3372. cmd->transport_state |= CMD_T_LUN_STOP;
  3373. spin_unlock(&cmd->t_state_lock);
  3374. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3375. if (!cmd->se_lun) {
  3376. pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
  3377. cmd->se_tfo->get_task_tag(cmd),
  3378. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3379. BUG();
  3380. }
  3381. /*
  3382. * If the Storage engine still owns the iscsi_cmd_t, determine
  3383. * and/or stop its context.
  3384. */
  3385. pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
  3386. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  3387. cmd->se_tfo->get_task_tag(cmd));
  3388. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  3389. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3390. continue;
  3391. }
  3392. pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  3393. "_wait_for_tasks(): SUCCESS\n",
  3394. cmd->se_lun->unpacked_lun,
  3395. cmd->se_tfo->get_task_tag(cmd));
  3396. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3397. if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
  3398. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3399. goto check_cond;
  3400. }
  3401. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  3402. target_remove_from_state_list(cmd);
  3403. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3404. /*
  3405. * The Storage engine stopped this struct se_cmd before it was
  3406. * send to the fabric frontend for delivery back to the
  3407. * Initiator Node. Return this SCSI CDB back with an
  3408. * CHECK_CONDITION status.
  3409. */
  3410. check_cond:
  3411. transport_send_check_condition_and_sense(cmd,
  3412. TCM_NON_EXISTENT_LUN, 0);
  3413. /*
  3414. * If the fabric frontend is waiting for this iscsi_cmd_t to
  3415. * be released, notify the waiting thread now that LU has
  3416. * finished accessing it.
  3417. */
  3418. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3419. if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
  3420. pr_debug("SE_LUN[%d] - Detected FE stop for"
  3421. " struct se_cmd: %p ITT: 0x%08x\n",
  3422. lun->unpacked_lun,
  3423. cmd, cmd->se_tfo->get_task_tag(cmd));
  3424. spin_unlock_irqrestore(&cmd->t_state_lock,
  3425. cmd_flags);
  3426. transport_cmd_check_stop(cmd, 1, 0);
  3427. complete(&cmd->transport_lun_fe_stop_comp);
  3428. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3429. continue;
  3430. }
  3431. pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  3432. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  3433. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3434. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3435. }
  3436. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3437. }
  3438. static int transport_clear_lun_thread(void *p)
  3439. {
  3440. struct se_lun *lun = p;
  3441. __transport_clear_lun_from_sessions(lun);
  3442. complete(&lun->lun_shutdown_comp);
  3443. return 0;
  3444. }
  3445. int transport_clear_lun_from_sessions(struct se_lun *lun)
  3446. {
  3447. struct task_struct *kt;
  3448. kt = kthread_run(transport_clear_lun_thread, lun,
  3449. "tcm_cl_%u", lun->unpacked_lun);
  3450. if (IS_ERR(kt)) {
  3451. pr_err("Unable to start clear_lun thread\n");
  3452. return PTR_ERR(kt);
  3453. }
  3454. wait_for_completion(&lun->lun_shutdown_comp);
  3455. return 0;
  3456. }
  3457. /**
  3458. * transport_wait_for_tasks - wait for completion to occur
  3459. * @cmd: command to wait
  3460. *
  3461. * Called from frontend fabric context to wait for storage engine
  3462. * to pause and/or release frontend generated struct se_cmd.
  3463. */
  3464. bool transport_wait_for_tasks(struct se_cmd *cmd)
  3465. {
  3466. unsigned long flags;
  3467. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3468. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
  3469. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3470. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3471. return false;
  3472. }
  3473. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
  3474. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3475. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3476. return false;
  3477. }
  3478. /*
  3479. * If we are already stopped due to an external event (ie: LUN shutdown)
  3480. * sleep until the connection can have the passed struct se_cmd back.
  3481. * The cmd->transport_lun_stopped_sem will be upped by
  3482. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  3483. * has completed its operation on the struct se_cmd.
  3484. */
  3485. if (cmd->transport_state & CMD_T_LUN_STOP) {
  3486. pr_debug("wait_for_tasks: Stopping"
  3487. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  3488. "_stop_comp); for ITT: 0x%08x\n",
  3489. cmd->se_tfo->get_task_tag(cmd));
  3490. /*
  3491. * There is a special case for WRITES where a FE exception +
  3492. * LUN shutdown means ConfigFS context is still sleeping on
  3493. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  3494. * We go ahead and up transport_lun_stop_comp just to be sure
  3495. * here.
  3496. */
  3497. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3498. complete(&cmd->transport_lun_stop_comp);
  3499. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  3500. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3501. target_remove_from_state_list(cmd);
  3502. /*
  3503. * At this point, the frontend who was the originator of this
  3504. * struct se_cmd, now owns the structure and can be released through
  3505. * normal means below.
  3506. */
  3507. pr_debug("wait_for_tasks: Stopped"
  3508. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  3509. "stop_comp); for ITT: 0x%08x\n",
  3510. cmd->se_tfo->get_task_tag(cmd));
  3511. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3512. }
  3513. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  3514. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3515. return false;
  3516. }
  3517. cmd->transport_state |= CMD_T_STOP;
  3518. pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
  3519. " i_state: %d, t_state: %d, CMD_T_STOP\n",
  3520. cmd, cmd->se_tfo->get_task_tag(cmd),
  3521. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3522. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3523. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3524. wait_for_completion(&cmd->t_transport_stop_comp);
  3525. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3526. cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
  3527. pr_debug("wait_for_tasks: Stopped wait_for_compltion("
  3528. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  3529. cmd->se_tfo->get_task_tag(cmd));
  3530. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3531. return true;
  3532. }
  3533. EXPORT_SYMBOL(transport_wait_for_tasks);
  3534. static int transport_get_sense_codes(
  3535. struct se_cmd *cmd,
  3536. u8 *asc,
  3537. u8 *ascq)
  3538. {
  3539. *asc = cmd->scsi_asc;
  3540. *ascq = cmd->scsi_ascq;
  3541. return 0;
  3542. }
  3543. static int transport_set_sense_codes(
  3544. struct se_cmd *cmd,
  3545. u8 asc,
  3546. u8 ascq)
  3547. {
  3548. cmd->scsi_asc = asc;
  3549. cmd->scsi_ascq = ascq;
  3550. return 0;
  3551. }
  3552. int transport_send_check_condition_and_sense(
  3553. struct se_cmd *cmd,
  3554. u8 reason,
  3555. int from_transport)
  3556. {
  3557. unsigned char *buffer = cmd->sense_buffer;
  3558. unsigned long flags;
  3559. int offset;
  3560. u8 asc = 0, ascq = 0;
  3561. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3562. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3563. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3564. return 0;
  3565. }
  3566. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  3567. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3568. if (!reason && from_transport)
  3569. goto after_reason;
  3570. if (!from_transport)
  3571. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  3572. /*
  3573. * Data Segment and SenseLength of the fabric response PDU.
  3574. *
  3575. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  3576. * from include/scsi/scsi_cmnd.h
  3577. */
  3578. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  3579. TRANSPORT_SENSE_BUFFER);
  3580. /*
  3581. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  3582. * SENSE KEY values from include/scsi/scsi.h
  3583. */
  3584. switch (reason) {
  3585. case TCM_NON_EXISTENT_LUN:
  3586. /* CURRENT ERROR */
  3587. buffer[offset] = 0x70;
  3588. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3589. /* ILLEGAL REQUEST */
  3590. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3591. /* LOGICAL UNIT NOT SUPPORTED */
  3592. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
  3593. break;
  3594. case TCM_UNSUPPORTED_SCSI_OPCODE:
  3595. case TCM_SECTOR_COUNT_TOO_MANY:
  3596. /* CURRENT ERROR */
  3597. buffer[offset] = 0x70;
  3598. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3599. /* ILLEGAL REQUEST */
  3600. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3601. /* INVALID COMMAND OPERATION CODE */
  3602. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  3603. break;
  3604. case TCM_UNKNOWN_MODE_PAGE:
  3605. /* CURRENT ERROR */
  3606. buffer[offset] = 0x70;
  3607. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3608. /* ILLEGAL REQUEST */
  3609. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3610. /* INVALID FIELD IN CDB */
  3611. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3612. break;
  3613. case TCM_CHECK_CONDITION_ABORT_CMD:
  3614. /* CURRENT ERROR */
  3615. buffer[offset] = 0x70;
  3616. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3617. /* ABORTED COMMAND */
  3618. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3619. /* BUS DEVICE RESET FUNCTION OCCURRED */
  3620. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  3621. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  3622. break;
  3623. case TCM_INCORRECT_AMOUNT_OF_DATA:
  3624. /* CURRENT ERROR */
  3625. buffer[offset] = 0x70;
  3626. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3627. /* ABORTED COMMAND */
  3628. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3629. /* WRITE ERROR */
  3630. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3631. /* NOT ENOUGH UNSOLICITED DATA */
  3632. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  3633. break;
  3634. case TCM_INVALID_CDB_FIELD:
  3635. /* CURRENT ERROR */
  3636. buffer[offset] = 0x70;
  3637. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3638. /* ILLEGAL REQUEST */
  3639. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3640. /* INVALID FIELD IN CDB */
  3641. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3642. break;
  3643. case TCM_INVALID_PARAMETER_LIST:
  3644. /* CURRENT ERROR */
  3645. buffer[offset] = 0x70;
  3646. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3647. /* ILLEGAL REQUEST */
  3648. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3649. /* INVALID FIELD IN PARAMETER LIST */
  3650. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  3651. break;
  3652. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  3653. /* CURRENT ERROR */
  3654. buffer[offset] = 0x70;
  3655. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3656. /* ABORTED COMMAND */
  3657. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3658. /* WRITE ERROR */
  3659. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3660. /* UNEXPECTED_UNSOLICITED_DATA */
  3661. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  3662. break;
  3663. case TCM_SERVICE_CRC_ERROR:
  3664. /* CURRENT ERROR */
  3665. buffer[offset] = 0x70;
  3666. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3667. /* ABORTED COMMAND */
  3668. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3669. /* PROTOCOL SERVICE CRC ERROR */
  3670. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  3671. /* N/A */
  3672. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  3673. break;
  3674. case TCM_SNACK_REJECTED:
  3675. /* CURRENT ERROR */
  3676. buffer[offset] = 0x70;
  3677. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3678. /* ABORTED COMMAND */
  3679. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3680. /* READ ERROR */
  3681. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  3682. /* FAILED RETRANSMISSION REQUEST */
  3683. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  3684. break;
  3685. case TCM_WRITE_PROTECTED:
  3686. /* CURRENT ERROR */
  3687. buffer[offset] = 0x70;
  3688. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3689. /* DATA PROTECT */
  3690. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  3691. /* WRITE PROTECTED */
  3692. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  3693. break;
  3694. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  3695. /* CURRENT ERROR */
  3696. buffer[offset] = 0x70;
  3697. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3698. /* UNIT ATTENTION */
  3699. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  3700. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  3701. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  3702. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  3703. break;
  3704. case TCM_CHECK_CONDITION_NOT_READY:
  3705. /* CURRENT ERROR */
  3706. buffer[offset] = 0x70;
  3707. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3708. /* Not Ready */
  3709. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  3710. transport_get_sense_codes(cmd, &asc, &ascq);
  3711. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  3712. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  3713. break;
  3714. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  3715. default:
  3716. /* CURRENT ERROR */
  3717. buffer[offset] = 0x70;
  3718. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3719. /* ILLEGAL REQUEST */
  3720. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3721. /* LOGICAL UNIT COMMUNICATION FAILURE */
  3722. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  3723. break;
  3724. }
  3725. /*
  3726. * This code uses linux/include/scsi/scsi.h SAM status codes!
  3727. */
  3728. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  3729. /*
  3730. * Automatically padded, this value is encoded in the fabric's
  3731. * data_length response PDU containing the SCSI defined sense data.
  3732. */
  3733. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  3734. after_reason:
  3735. return cmd->se_tfo->queue_status(cmd);
  3736. }
  3737. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  3738. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  3739. {
  3740. int ret = 0;
  3741. if (cmd->transport_state & CMD_T_ABORTED) {
  3742. if (!send_status ||
  3743. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  3744. return 1;
  3745. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
  3746. " status for CDB: 0x%02x ITT: 0x%08x\n",
  3747. cmd->t_task_cdb[0],
  3748. cmd->se_tfo->get_task_tag(cmd));
  3749. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  3750. cmd->se_tfo->queue_status(cmd);
  3751. ret = 1;
  3752. }
  3753. return ret;
  3754. }
  3755. EXPORT_SYMBOL(transport_check_aborted_status);
  3756. void transport_send_task_abort(struct se_cmd *cmd)
  3757. {
  3758. unsigned long flags;
  3759. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3760. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3761. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3762. return;
  3763. }
  3764. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3765. /*
  3766. * If there are still expected incoming fabric WRITEs, we wait
  3767. * until until they have completed before sending a TASK_ABORTED
  3768. * response. This response with TASK_ABORTED status will be
  3769. * queued back to fabric module by transport_check_aborted_status().
  3770. */
  3771. if (cmd->data_direction == DMA_TO_DEVICE) {
  3772. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  3773. cmd->transport_state |= CMD_T_ABORTED;
  3774. smp_mb__after_atomic_inc();
  3775. }
  3776. }
  3777. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  3778. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  3779. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  3780. cmd->se_tfo->get_task_tag(cmd));
  3781. cmd->se_tfo->queue_status(cmd);
  3782. }
  3783. static int transport_generic_do_tmr(struct se_cmd *cmd)
  3784. {
  3785. struct se_device *dev = cmd->se_dev;
  3786. struct se_tmr_req *tmr = cmd->se_tmr_req;
  3787. int ret;
  3788. switch (tmr->function) {
  3789. case TMR_ABORT_TASK:
  3790. core_tmr_abort_task(dev, tmr, cmd->se_sess);
  3791. break;
  3792. case TMR_ABORT_TASK_SET:
  3793. case TMR_CLEAR_ACA:
  3794. case TMR_CLEAR_TASK_SET:
  3795. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  3796. break;
  3797. case TMR_LUN_RESET:
  3798. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  3799. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  3800. TMR_FUNCTION_REJECTED;
  3801. break;
  3802. case TMR_TARGET_WARM_RESET:
  3803. tmr->response = TMR_FUNCTION_REJECTED;
  3804. break;
  3805. case TMR_TARGET_COLD_RESET:
  3806. tmr->response = TMR_FUNCTION_REJECTED;
  3807. break;
  3808. default:
  3809. pr_err("Uknown TMR function: 0x%02x.\n",
  3810. tmr->function);
  3811. tmr->response = TMR_FUNCTION_REJECTED;
  3812. break;
  3813. }
  3814. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  3815. cmd->se_tfo->queue_tm_rsp(cmd);
  3816. transport_cmd_check_stop_to_fabric(cmd);
  3817. return 0;
  3818. }
  3819. /* transport_processing_thread():
  3820. *
  3821. *
  3822. */
  3823. static int transport_processing_thread(void *param)
  3824. {
  3825. int ret;
  3826. struct se_cmd *cmd;
  3827. struct se_device *dev = param;
  3828. while (!kthread_should_stop()) {
  3829. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  3830. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  3831. kthread_should_stop());
  3832. if (ret < 0)
  3833. goto out;
  3834. get_cmd:
  3835. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  3836. if (!cmd)
  3837. continue;
  3838. switch (cmd->t_state) {
  3839. case TRANSPORT_NEW_CMD:
  3840. BUG();
  3841. break;
  3842. case TRANSPORT_NEW_CMD_MAP:
  3843. if (!cmd->se_tfo->new_cmd_map) {
  3844. pr_err("cmd->se_tfo->new_cmd_map is"
  3845. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  3846. BUG();
  3847. }
  3848. ret = cmd->se_tfo->new_cmd_map(cmd);
  3849. if (ret < 0) {
  3850. transport_generic_request_failure(cmd);
  3851. break;
  3852. }
  3853. ret = transport_generic_new_cmd(cmd);
  3854. if (ret < 0) {
  3855. transport_generic_request_failure(cmd);
  3856. break;
  3857. }
  3858. break;
  3859. case TRANSPORT_PROCESS_WRITE:
  3860. transport_generic_process_write(cmd);
  3861. break;
  3862. case TRANSPORT_PROCESS_TMR:
  3863. transport_generic_do_tmr(cmd);
  3864. break;
  3865. case TRANSPORT_COMPLETE_QF_WP:
  3866. transport_write_pending_qf(cmd);
  3867. break;
  3868. case TRANSPORT_COMPLETE_QF_OK:
  3869. transport_complete_qf(cmd);
  3870. break;
  3871. default:
  3872. pr_err("Unknown t_state: %d for ITT: 0x%08x "
  3873. "i_state: %d on SE LUN: %u\n",
  3874. cmd->t_state,
  3875. cmd->se_tfo->get_task_tag(cmd),
  3876. cmd->se_tfo->get_cmd_state(cmd),
  3877. cmd->se_lun->unpacked_lun);
  3878. BUG();
  3879. }
  3880. goto get_cmd;
  3881. }
  3882. out:
  3883. WARN_ON(!list_empty(&dev->state_list));
  3884. WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
  3885. dev->process_thread = NULL;
  3886. return 0;
  3887. }