target_core_transport.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286
  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. static int target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
  1179. {
  1180. struct se_device *dev = cmd->se_dev;
  1181. if (cmd->unknown_data_length) {
  1182. cmd->data_length = size;
  1183. } else if (size != cmd->data_length) {
  1184. pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
  1185. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  1186. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  1187. cmd->data_length, size, cmd->t_task_cdb[0]);
  1188. cmd->cmd_spdtl = size;
  1189. if (cmd->data_direction == DMA_TO_DEVICE) {
  1190. pr_err("Rejecting underflow/overflow"
  1191. " WRITE data\n");
  1192. goto out_invalid_cdb_field;
  1193. }
  1194. /*
  1195. * Reject READ_* or WRITE_* with overflow/underflow for
  1196. * type SCF_SCSI_DATA_CDB.
  1197. */
  1198. if (dev->se_sub_dev->se_dev_attrib.block_size != 512) {
  1199. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  1200. " CDB on non 512-byte sector setup subsystem"
  1201. " plugin: %s\n", dev->transport->name);
  1202. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  1203. goto out_invalid_cdb_field;
  1204. }
  1205. if (size > cmd->data_length) {
  1206. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  1207. cmd->residual_count = (size - cmd->data_length);
  1208. } else {
  1209. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  1210. cmd->residual_count = (cmd->data_length - size);
  1211. }
  1212. cmd->data_length = size;
  1213. }
  1214. return 0;
  1215. out_invalid_cdb_field:
  1216. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1217. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1218. return -EINVAL;
  1219. }
  1220. /*
  1221. * Used by fabric modules containing a local struct se_cmd within their
  1222. * fabric dependent per I/O descriptor.
  1223. */
  1224. void transport_init_se_cmd(
  1225. struct se_cmd *cmd,
  1226. struct target_core_fabric_ops *tfo,
  1227. struct se_session *se_sess,
  1228. u32 data_length,
  1229. int data_direction,
  1230. int task_attr,
  1231. unsigned char *sense_buffer)
  1232. {
  1233. INIT_LIST_HEAD(&cmd->se_lun_node);
  1234. INIT_LIST_HEAD(&cmd->se_delayed_node);
  1235. INIT_LIST_HEAD(&cmd->se_qf_node);
  1236. INIT_LIST_HEAD(&cmd->se_queue_node);
  1237. INIT_LIST_HEAD(&cmd->se_cmd_list);
  1238. INIT_LIST_HEAD(&cmd->execute_list);
  1239. INIT_LIST_HEAD(&cmd->state_list);
  1240. init_completion(&cmd->transport_lun_fe_stop_comp);
  1241. init_completion(&cmd->transport_lun_stop_comp);
  1242. init_completion(&cmd->t_transport_stop_comp);
  1243. init_completion(&cmd->cmd_wait_comp);
  1244. init_completion(&cmd->task_stop_comp);
  1245. spin_lock_init(&cmd->t_state_lock);
  1246. cmd->transport_state = CMD_T_DEV_ACTIVE;
  1247. cmd->se_tfo = tfo;
  1248. cmd->se_sess = se_sess;
  1249. cmd->data_length = data_length;
  1250. cmd->data_direction = data_direction;
  1251. cmd->sam_task_attr = task_attr;
  1252. cmd->sense_buffer = sense_buffer;
  1253. cmd->state_active = false;
  1254. }
  1255. EXPORT_SYMBOL(transport_init_se_cmd);
  1256. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1257. {
  1258. /*
  1259. * Check if SAM Task Attribute emulation is enabled for this
  1260. * struct se_device storage object
  1261. */
  1262. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1263. return 0;
  1264. if (cmd->sam_task_attr == MSG_ACA_TAG) {
  1265. pr_debug("SAM Task Attribute ACA"
  1266. " emulation is not supported\n");
  1267. return -EINVAL;
  1268. }
  1269. /*
  1270. * Used to determine when ORDERED commands should go from
  1271. * Dormant to Active status.
  1272. */
  1273. cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
  1274. smp_mb__after_atomic_inc();
  1275. pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1276. cmd->se_ordered_id, cmd->sam_task_attr,
  1277. cmd->se_dev->transport->name);
  1278. return 0;
  1279. }
  1280. /* target_setup_cmd_from_cdb():
  1281. *
  1282. * Called from fabric RX Thread.
  1283. */
  1284. int target_setup_cmd_from_cdb(
  1285. struct se_cmd *cmd,
  1286. unsigned char *cdb)
  1287. {
  1288. struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
  1289. u32 pr_reg_type = 0;
  1290. u8 alua_ascq = 0;
  1291. unsigned long flags;
  1292. int ret;
  1293. transport_generic_prepare_cdb(cdb);
  1294. /*
  1295. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1296. * for VARIABLE_LENGTH_CMD
  1297. */
  1298. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1299. pr_err("Received SCSI CDB with command_size: %d that"
  1300. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1301. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1302. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1303. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1304. return -EINVAL;
  1305. }
  1306. /*
  1307. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1308. * allocate the additional extended CDB buffer now.. Otherwise
  1309. * setup the pointer from __t_task_cdb to t_task_cdb.
  1310. */
  1311. if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
  1312. cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1313. GFP_KERNEL);
  1314. if (!cmd->t_task_cdb) {
  1315. pr_err("Unable to allocate cmd->t_task_cdb"
  1316. " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
  1317. scsi_command_size(cdb),
  1318. (unsigned long)sizeof(cmd->__t_task_cdb));
  1319. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1320. cmd->scsi_sense_reason =
  1321. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1322. return -ENOMEM;
  1323. }
  1324. } else
  1325. cmd->t_task_cdb = &cmd->__t_task_cdb[0];
  1326. /*
  1327. * Copy the original CDB into cmd->
  1328. */
  1329. memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
  1330. /*
  1331. * Check for an existing UNIT ATTENTION condition
  1332. */
  1333. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  1334. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1335. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  1336. return -EINVAL;
  1337. }
  1338. ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
  1339. if (ret != 0) {
  1340. /*
  1341. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  1342. * The ALUA additional sense code qualifier (ASCQ) is determined
  1343. * by the ALUA primary or secondary access state..
  1344. */
  1345. if (ret > 0) {
  1346. pr_debug("[%s]: ALUA TG Port not available, "
  1347. "SenseKey: NOT_READY, ASC/ASCQ: "
  1348. "0x04/0x%02x\n",
  1349. cmd->se_tfo->get_fabric_name(), alua_ascq);
  1350. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  1351. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1352. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  1353. return -EINVAL;
  1354. }
  1355. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1356. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1357. return -EINVAL;
  1358. }
  1359. /*
  1360. * Check status for SPC-3 Persistent Reservations
  1361. */
  1362. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type)) {
  1363. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  1364. cmd, cdb, pr_reg_type) != 0) {
  1365. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1366. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  1367. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1368. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  1369. return -EBUSY;
  1370. }
  1371. /*
  1372. * This means the CDB is allowed for the SCSI Initiator port
  1373. * when said port is *NOT* holding the legacy SPC-2 or
  1374. * SPC-3 Persistent Reservation.
  1375. */
  1376. }
  1377. /*
  1378. * Setup the received CDB based on SCSI defined opcodes and
  1379. * perform unit attention, persistent reservations and ALUA
  1380. * checks for virtual device backends. The cmd->t_task_cdb
  1381. * pointer is expected to be setup before we reach this point.
  1382. */
  1383. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1384. if (ret < 0)
  1385. return ret;
  1386. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1387. cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1388. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1389. /*
  1390. * Check for SAM Task Attribute Emulation
  1391. */
  1392. if (transport_check_alloc_task_attr(cmd) < 0) {
  1393. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1394. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1395. return -EINVAL;
  1396. }
  1397. spin_lock(&cmd->se_lun->lun_sep_lock);
  1398. if (cmd->se_lun->lun_sep)
  1399. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1400. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1401. return 0;
  1402. }
  1403. EXPORT_SYMBOL(target_setup_cmd_from_cdb);
  1404. /*
  1405. * Used by fabric module frontends to queue tasks directly.
  1406. * Many only be used from process context only
  1407. */
  1408. int transport_handle_cdb_direct(
  1409. struct se_cmd *cmd)
  1410. {
  1411. int ret;
  1412. if (!cmd->se_lun) {
  1413. dump_stack();
  1414. pr_err("cmd->se_lun is NULL\n");
  1415. return -EINVAL;
  1416. }
  1417. if (in_interrupt()) {
  1418. dump_stack();
  1419. pr_err("transport_generic_handle_cdb cannot be called"
  1420. " from interrupt context\n");
  1421. return -EINVAL;
  1422. }
  1423. /*
  1424. * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
  1425. * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
  1426. * in existing usage to ensure that outstanding descriptors are handled
  1427. * correctly during shutdown via transport_wait_for_tasks()
  1428. *
  1429. * Also, we don't take cmd->t_state_lock here as we only expect
  1430. * this to be called for initial descriptor submission.
  1431. */
  1432. cmd->t_state = TRANSPORT_NEW_CMD;
  1433. cmd->transport_state |= CMD_T_ACTIVE;
  1434. /*
  1435. * transport_generic_new_cmd() is already handling QUEUE_FULL,
  1436. * so follow TRANSPORT_NEW_CMD processing thread context usage
  1437. * and call transport_generic_request_failure() if necessary..
  1438. */
  1439. ret = transport_generic_new_cmd(cmd);
  1440. if (ret < 0)
  1441. transport_generic_request_failure(cmd);
  1442. return 0;
  1443. }
  1444. EXPORT_SYMBOL(transport_handle_cdb_direct);
  1445. /**
  1446. * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
  1447. *
  1448. * @se_cmd: command descriptor to submit
  1449. * @se_sess: associated se_sess for endpoint
  1450. * @cdb: pointer to SCSI CDB
  1451. * @sense: pointer to SCSI sense buffer
  1452. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1453. * @data_length: fabric expected data transfer length
  1454. * @task_addr: SAM task attribute
  1455. * @data_dir: DMA data direction
  1456. * @flags: flags for command submission from target_sc_flags_tables
  1457. *
  1458. * This may only be called from process context, and also currently
  1459. * assumes internal allocation of fabric payload buffer by target-core.
  1460. **/
  1461. void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
  1462. unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
  1463. u32 data_length, int task_attr, int data_dir, int flags)
  1464. {
  1465. struct se_portal_group *se_tpg;
  1466. int rc;
  1467. se_tpg = se_sess->se_tpg;
  1468. BUG_ON(!se_tpg);
  1469. BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
  1470. BUG_ON(in_interrupt());
  1471. /*
  1472. * Initialize se_cmd for target operation. From this point
  1473. * exceptions are handled by sending exception status via
  1474. * target_core_fabric_ops->queue_status() callback
  1475. */
  1476. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1477. data_length, data_dir, task_attr, sense);
  1478. if (flags & TARGET_SCF_UNKNOWN_SIZE)
  1479. se_cmd->unknown_data_length = 1;
  1480. /*
  1481. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1482. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1483. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1484. * kref_put() to happen during fabric packet acknowledgement.
  1485. */
  1486. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1487. /*
  1488. * Signal bidirectional data payloads to target-core
  1489. */
  1490. if (flags & TARGET_SCF_BIDI_OP)
  1491. se_cmd->se_cmd_flags |= SCF_BIDI;
  1492. /*
  1493. * Locate se_lun pointer and attach it to struct se_cmd
  1494. */
  1495. if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
  1496. transport_send_check_condition_and_sense(se_cmd,
  1497. se_cmd->scsi_sense_reason, 0);
  1498. target_put_sess_cmd(se_sess, se_cmd);
  1499. return;
  1500. }
  1501. /*
  1502. * Sanitize CDBs via transport_generic_cmd_sequencer() and
  1503. * allocate the necessary tasks to complete the received CDB+data
  1504. */
  1505. rc = target_setup_cmd_from_cdb(se_cmd, cdb);
  1506. if (rc != 0) {
  1507. transport_generic_request_failure(se_cmd);
  1508. return;
  1509. }
  1510. /*
  1511. * Check if we need to delay processing because of ALUA
  1512. * Active/NonOptimized primary access state..
  1513. */
  1514. core_alua_check_nonop_delay(se_cmd);
  1515. /*
  1516. * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
  1517. * for immediate execution of READs, otherwise wait for
  1518. * transport_generic_handle_data() to be called for WRITEs
  1519. * when fabric has filled the incoming buffer.
  1520. */
  1521. transport_handle_cdb_direct(se_cmd);
  1522. return;
  1523. }
  1524. EXPORT_SYMBOL(target_submit_cmd);
  1525. static void target_complete_tmr_failure(struct work_struct *work)
  1526. {
  1527. struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
  1528. se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1529. se_cmd->se_tfo->queue_tm_rsp(se_cmd);
  1530. transport_generic_free_cmd(se_cmd, 0);
  1531. }
  1532. /**
  1533. * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
  1534. * for TMR CDBs
  1535. *
  1536. * @se_cmd: command descriptor to submit
  1537. * @se_sess: associated se_sess for endpoint
  1538. * @sense: pointer to SCSI sense buffer
  1539. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1540. * @fabric_context: fabric context for TMR req
  1541. * @tm_type: Type of TM request
  1542. * @gfp: gfp type for caller
  1543. * @tag: referenced task tag for TMR_ABORT_TASK
  1544. * @flags: submit cmd flags
  1545. *
  1546. * Callable from all contexts.
  1547. **/
  1548. int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
  1549. unsigned char *sense, u32 unpacked_lun,
  1550. void *fabric_tmr_ptr, unsigned char tm_type,
  1551. gfp_t gfp, unsigned int tag, int flags)
  1552. {
  1553. struct se_portal_group *se_tpg;
  1554. int ret;
  1555. se_tpg = se_sess->se_tpg;
  1556. BUG_ON(!se_tpg);
  1557. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1558. 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
  1559. /*
  1560. * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
  1561. * allocation failure.
  1562. */
  1563. ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
  1564. if (ret < 0)
  1565. return -ENOMEM;
  1566. if (tm_type == TMR_ABORT_TASK)
  1567. se_cmd->se_tmr_req->ref_task_tag = tag;
  1568. /* See target_submit_cmd for commentary */
  1569. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1570. ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
  1571. if (ret) {
  1572. /*
  1573. * For callback during failure handling, push this work off
  1574. * to process context with TMR_LUN_DOES_NOT_EXIST status.
  1575. */
  1576. INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
  1577. schedule_work(&se_cmd->work);
  1578. return 0;
  1579. }
  1580. transport_generic_handle_tmr(se_cmd);
  1581. return 0;
  1582. }
  1583. EXPORT_SYMBOL(target_submit_tmr);
  1584. /*
  1585. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1586. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1587. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1588. */
  1589. int transport_generic_handle_cdb_map(
  1590. struct se_cmd *cmd)
  1591. {
  1592. if (!cmd->se_lun) {
  1593. dump_stack();
  1594. pr_err("cmd->se_lun is NULL\n");
  1595. return -EINVAL;
  1596. }
  1597. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
  1598. return 0;
  1599. }
  1600. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1601. /* transport_generic_handle_data():
  1602. *
  1603. *
  1604. */
  1605. int transport_generic_handle_data(
  1606. struct se_cmd *cmd)
  1607. {
  1608. /*
  1609. * For the software fabric case, then we assume the nexus is being
  1610. * failed/shutdown when signals are pending from the kthread context
  1611. * caller, so we return a failure. For the HW target mode case running
  1612. * in interrupt code, the signal_pending() check is skipped.
  1613. */
  1614. if (!in_interrupt() && signal_pending(current))
  1615. return -EPERM;
  1616. /*
  1617. * If the received CDB has aleady been ABORTED by the generic
  1618. * target engine, we now call transport_check_aborted_status()
  1619. * to queue any delated TASK_ABORTED status for the received CDB to the
  1620. * fabric module as we are expecting no further incoming DATA OUT
  1621. * sequences at this point.
  1622. */
  1623. if (transport_check_aborted_status(cmd, 1) != 0)
  1624. return 0;
  1625. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
  1626. return 0;
  1627. }
  1628. EXPORT_SYMBOL(transport_generic_handle_data);
  1629. /* transport_generic_handle_tmr():
  1630. *
  1631. *
  1632. */
  1633. int transport_generic_handle_tmr(
  1634. struct se_cmd *cmd)
  1635. {
  1636. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
  1637. return 0;
  1638. }
  1639. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1640. /*
  1641. * If the cmd is active, request it to be stopped and sleep until it
  1642. * has completed.
  1643. */
  1644. bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
  1645. {
  1646. bool was_active = false;
  1647. if (cmd->transport_state & CMD_T_BUSY) {
  1648. cmd->transport_state |= CMD_T_REQUEST_STOP;
  1649. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  1650. pr_debug("cmd %p waiting to complete\n", cmd);
  1651. wait_for_completion(&cmd->task_stop_comp);
  1652. pr_debug("cmd %p stopped successfully\n", cmd);
  1653. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  1654. cmd->transport_state &= ~CMD_T_REQUEST_STOP;
  1655. cmd->transport_state &= ~CMD_T_BUSY;
  1656. was_active = true;
  1657. }
  1658. return was_active;
  1659. }
  1660. /*
  1661. * Handle SAM-esque emulation for generic transport request failures.
  1662. */
  1663. void transport_generic_request_failure(struct se_cmd *cmd)
  1664. {
  1665. int ret = 0;
  1666. pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1667. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1668. cmd->t_task_cdb[0]);
  1669. pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
  1670. cmd->se_tfo->get_cmd_state(cmd),
  1671. cmd->t_state, cmd->scsi_sense_reason);
  1672. pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
  1673. (cmd->transport_state & CMD_T_ACTIVE) != 0,
  1674. (cmd->transport_state & CMD_T_STOP) != 0,
  1675. (cmd->transport_state & CMD_T_SENT) != 0);
  1676. /*
  1677. * For SAM Task Attribute emulation for failed struct se_cmd
  1678. */
  1679. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1680. transport_complete_task_attr(cmd);
  1681. switch (cmd->scsi_sense_reason) {
  1682. case TCM_NON_EXISTENT_LUN:
  1683. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1684. case TCM_INVALID_CDB_FIELD:
  1685. case TCM_INVALID_PARAMETER_LIST:
  1686. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1687. case TCM_UNKNOWN_MODE_PAGE:
  1688. case TCM_WRITE_PROTECTED:
  1689. case TCM_CHECK_CONDITION_ABORT_CMD:
  1690. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1691. case TCM_CHECK_CONDITION_NOT_READY:
  1692. break;
  1693. case TCM_RESERVATION_CONFLICT:
  1694. /*
  1695. * No SENSE Data payload for this case, set SCSI Status
  1696. * and queue the response to $FABRIC_MOD.
  1697. *
  1698. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1699. */
  1700. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1701. /*
  1702. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1703. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1704. * CONFLICT STATUS.
  1705. *
  1706. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1707. */
  1708. if (cmd->se_sess &&
  1709. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1710. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1711. cmd->orig_fe_lun, 0x2C,
  1712. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1713. ret = cmd->se_tfo->queue_status(cmd);
  1714. if (ret == -EAGAIN || ret == -ENOMEM)
  1715. goto queue_full;
  1716. goto check_stop;
  1717. default:
  1718. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1719. cmd->t_task_cdb[0], cmd->scsi_sense_reason);
  1720. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1721. break;
  1722. }
  1723. /*
  1724. * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
  1725. * make the call to transport_send_check_condition_and_sense()
  1726. * directly. Otherwise expect the fabric to make the call to
  1727. * transport_send_check_condition_and_sense() after handling
  1728. * possible unsoliticied write data payloads.
  1729. */
  1730. ret = transport_send_check_condition_and_sense(cmd,
  1731. cmd->scsi_sense_reason, 0);
  1732. if (ret == -EAGAIN || ret == -ENOMEM)
  1733. goto queue_full;
  1734. check_stop:
  1735. transport_lun_remove_cmd(cmd);
  1736. if (!transport_cmd_check_stop_to_fabric(cmd))
  1737. ;
  1738. return;
  1739. queue_full:
  1740. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  1741. transport_handle_queue_full(cmd, cmd->se_dev);
  1742. }
  1743. EXPORT_SYMBOL(transport_generic_request_failure);
  1744. static inline u32 transport_lba_21(unsigned char *cdb)
  1745. {
  1746. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1747. }
  1748. static inline u32 transport_lba_32(unsigned char *cdb)
  1749. {
  1750. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1751. }
  1752. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1753. {
  1754. unsigned int __v1, __v2;
  1755. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1756. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1757. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1758. }
  1759. /*
  1760. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1761. */
  1762. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1763. {
  1764. unsigned int __v1, __v2;
  1765. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1766. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1767. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1768. }
  1769. /*
  1770. * Called from Fabric Module context from transport_execute_tasks()
  1771. *
  1772. * The return of this function determins if the tasks from struct se_cmd
  1773. * get added to the execution queue in transport_execute_tasks(),
  1774. * or are added to the delayed or ordered lists here.
  1775. */
  1776. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  1777. {
  1778. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1779. return 1;
  1780. /*
  1781. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1782. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1783. */
  1784. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  1785. pr_debug("Added HEAD_OF_QUEUE for CDB:"
  1786. " 0x%02x, se_ordered_id: %u\n",
  1787. cmd->t_task_cdb[0],
  1788. cmd->se_ordered_id);
  1789. return 1;
  1790. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  1791. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  1792. smp_mb__after_atomic_inc();
  1793. pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
  1794. " list, se_ordered_id: %u\n",
  1795. cmd->t_task_cdb[0],
  1796. cmd->se_ordered_id);
  1797. /*
  1798. * Add ORDERED command to tail of execution queue if
  1799. * no other older commands exist that need to be
  1800. * completed first.
  1801. */
  1802. if (!atomic_read(&cmd->se_dev->simple_cmds))
  1803. return 1;
  1804. } else {
  1805. /*
  1806. * For SIMPLE and UNTAGGED Task Attribute commands
  1807. */
  1808. atomic_inc(&cmd->se_dev->simple_cmds);
  1809. smp_mb__after_atomic_inc();
  1810. }
  1811. /*
  1812. * Otherwise if one or more outstanding ORDERED task attribute exist,
  1813. * add the dormant task(s) built for the passed struct se_cmd to the
  1814. * execution queue and become in Active state for this struct se_device.
  1815. */
  1816. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  1817. /*
  1818. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  1819. * will be drained upon completion of HEAD_OF_QUEUE task.
  1820. */
  1821. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  1822. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  1823. list_add_tail(&cmd->se_delayed_node,
  1824. &cmd->se_dev->delayed_cmd_list);
  1825. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  1826. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
  1827. " delayed CMD list, se_ordered_id: %u\n",
  1828. cmd->t_task_cdb[0], cmd->sam_task_attr,
  1829. cmd->se_ordered_id);
  1830. /*
  1831. * Return zero to let transport_execute_tasks() know
  1832. * not to add the delayed tasks to the execution list.
  1833. */
  1834. return 0;
  1835. }
  1836. /*
  1837. * Otherwise, no ORDERED task attributes exist..
  1838. */
  1839. return 1;
  1840. }
  1841. /*
  1842. * Called from fabric module context in transport_generic_new_cmd() and
  1843. * transport_generic_process_write()
  1844. */
  1845. static void transport_execute_tasks(struct se_cmd *cmd)
  1846. {
  1847. int add_tasks;
  1848. struct se_device *se_dev = cmd->se_dev;
  1849. /*
  1850. * Call transport_cmd_check_stop() to see if a fabric exception
  1851. * has occurred that prevents execution.
  1852. */
  1853. if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
  1854. /*
  1855. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  1856. * attribute for the tasks of the received struct se_cmd CDB
  1857. */
  1858. add_tasks = transport_execute_task_attr(cmd);
  1859. if (add_tasks) {
  1860. __transport_execute_tasks(se_dev, cmd);
  1861. return;
  1862. }
  1863. }
  1864. __transport_execute_tasks(se_dev, NULL);
  1865. }
  1866. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
  1867. {
  1868. int error;
  1869. struct se_cmd *cmd = NULL;
  1870. unsigned long flags;
  1871. check_depth:
  1872. spin_lock_irq(&dev->execute_task_lock);
  1873. if (new_cmd != NULL)
  1874. __target_add_to_execute_list(new_cmd);
  1875. if (list_empty(&dev->execute_list)) {
  1876. spin_unlock_irq(&dev->execute_task_lock);
  1877. return 0;
  1878. }
  1879. cmd = list_first_entry(&dev->execute_list, struct se_cmd, execute_list);
  1880. __target_remove_from_execute_list(cmd);
  1881. spin_unlock_irq(&dev->execute_task_lock);
  1882. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1883. cmd->transport_state |= CMD_T_BUSY;
  1884. cmd->transport_state |= CMD_T_SENT;
  1885. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1886. if (cmd->execute_cmd)
  1887. error = cmd->execute_cmd(cmd);
  1888. else {
  1889. error = dev->transport->execute_cmd(cmd, cmd->t_data_sg,
  1890. cmd->t_data_nents, cmd->data_direction);
  1891. }
  1892. if (error != 0) {
  1893. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1894. cmd->transport_state &= ~CMD_T_BUSY;
  1895. cmd->transport_state &= ~CMD_T_SENT;
  1896. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1897. transport_generic_request_failure(cmd);
  1898. }
  1899. new_cmd = NULL;
  1900. goto check_depth;
  1901. return 0;
  1902. }
  1903. static inline u32 transport_get_sectors_6(
  1904. unsigned char *cdb,
  1905. struct se_cmd *cmd,
  1906. int *ret)
  1907. {
  1908. struct se_device *dev = cmd->se_dev;
  1909. /*
  1910. * Assume TYPE_DISK for non struct se_device objects.
  1911. * Use 8-bit sector value.
  1912. */
  1913. if (!dev)
  1914. goto type_disk;
  1915. /*
  1916. * Use 24-bit allocation length for TYPE_TAPE.
  1917. */
  1918. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  1919. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  1920. /*
  1921. * Everything else assume TYPE_DISK Sector CDB location.
  1922. * Use 8-bit sector value. SBC-3 says:
  1923. *
  1924. * A TRANSFER LENGTH field set to zero specifies that 256
  1925. * logical blocks shall be written. Any other value
  1926. * specifies the number of logical blocks that shall be
  1927. * written.
  1928. */
  1929. type_disk:
  1930. return cdb[4] ? : 256;
  1931. }
  1932. static inline u32 transport_get_sectors_10(
  1933. unsigned char *cdb,
  1934. struct se_cmd *cmd,
  1935. int *ret)
  1936. {
  1937. struct se_device *dev = cmd->se_dev;
  1938. /*
  1939. * Assume TYPE_DISK for non struct se_device objects.
  1940. * Use 16-bit sector value.
  1941. */
  1942. if (!dev)
  1943. goto type_disk;
  1944. /*
  1945. * XXX_10 is not defined in SSC, throw an exception
  1946. */
  1947. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1948. *ret = -EINVAL;
  1949. return 0;
  1950. }
  1951. /*
  1952. * Everything else assume TYPE_DISK Sector CDB location.
  1953. * Use 16-bit sector value.
  1954. */
  1955. type_disk:
  1956. return (u32)(cdb[7] << 8) + cdb[8];
  1957. }
  1958. static inline u32 transport_get_sectors_12(
  1959. unsigned char *cdb,
  1960. struct se_cmd *cmd,
  1961. int *ret)
  1962. {
  1963. struct se_device *dev = cmd->se_dev;
  1964. /*
  1965. * Assume TYPE_DISK for non struct se_device objects.
  1966. * Use 32-bit sector value.
  1967. */
  1968. if (!dev)
  1969. goto type_disk;
  1970. /*
  1971. * XXX_12 is not defined in SSC, throw an exception
  1972. */
  1973. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1974. *ret = -EINVAL;
  1975. return 0;
  1976. }
  1977. /*
  1978. * Everything else assume TYPE_DISK Sector CDB location.
  1979. * Use 32-bit sector value.
  1980. */
  1981. type_disk:
  1982. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  1983. }
  1984. static inline u32 transport_get_sectors_16(
  1985. unsigned char *cdb,
  1986. struct se_cmd *cmd,
  1987. int *ret)
  1988. {
  1989. struct se_device *dev = cmd->se_dev;
  1990. /*
  1991. * Assume TYPE_DISK for non struct se_device objects.
  1992. * Use 32-bit sector value.
  1993. */
  1994. if (!dev)
  1995. goto type_disk;
  1996. /*
  1997. * Use 24-bit allocation length for TYPE_TAPE.
  1998. */
  1999. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2000. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2001. type_disk:
  2002. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2003. (cdb[12] << 8) + cdb[13];
  2004. }
  2005. /*
  2006. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2007. */
  2008. static inline u32 transport_get_sectors_32(
  2009. unsigned char *cdb,
  2010. struct se_cmd *cmd,
  2011. int *ret)
  2012. {
  2013. /*
  2014. * Assume TYPE_DISK for non struct se_device objects.
  2015. * Use 32-bit sector value.
  2016. */
  2017. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2018. (cdb[30] << 8) + cdb[31];
  2019. }
  2020. static inline u32 transport_get_size(
  2021. u32 sectors,
  2022. unsigned char *cdb,
  2023. struct se_cmd *cmd)
  2024. {
  2025. struct se_device *dev = cmd->se_dev;
  2026. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2027. if (cdb[1] & 1) { /* sectors */
  2028. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2029. } else /* bytes */
  2030. return sectors;
  2031. }
  2032. pr_debug("Returning block_size: %u, sectors: %u == %u for"
  2033. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
  2034. sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  2035. dev->transport->name);
  2036. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2037. }
  2038. static void transport_xor_callback(struct se_cmd *cmd)
  2039. {
  2040. unsigned char *buf, *addr;
  2041. struct scatterlist *sg;
  2042. unsigned int offset;
  2043. int i;
  2044. int count;
  2045. /*
  2046. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2047. *
  2048. * 1) read the specified logical block(s);
  2049. * 2) transfer logical blocks from the data-out buffer;
  2050. * 3) XOR the logical blocks transferred from the data-out buffer with
  2051. * the logical blocks read, storing the resulting XOR data in a buffer;
  2052. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2053. * blocks transferred from the data-out buffer; and
  2054. * 5) transfer the resulting XOR data to the data-in buffer.
  2055. */
  2056. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2057. if (!buf) {
  2058. pr_err("Unable to allocate xor_callback buf\n");
  2059. return;
  2060. }
  2061. /*
  2062. * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
  2063. * into the locally allocated *buf
  2064. */
  2065. sg_copy_to_buffer(cmd->t_data_sg,
  2066. cmd->t_data_nents,
  2067. buf,
  2068. cmd->data_length);
  2069. /*
  2070. * Now perform the XOR against the BIDI read memory located at
  2071. * cmd->t_mem_bidi_list
  2072. */
  2073. offset = 0;
  2074. for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
  2075. addr = kmap_atomic(sg_page(sg));
  2076. if (!addr)
  2077. goto out;
  2078. for (i = 0; i < sg->length; i++)
  2079. *(addr + sg->offset + i) ^= *(buf + offset + i);
  2080. offset += sg->length;
  2081. kunmap_atomic(addr);
  2082. }
  2083. out:
  2084. kfree(buf);
  2085. }
  2086. /*
  2087. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2088. */
  2089. static int transport_get_sense_data(struct se_cmd *cmd)
  2090. {
  2091. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2092. struct se_device *dev = cmd->se_dev;
  2093. unsigned long flags;
  2094. u32 offset = 0;
  2095. WARN_ON(!cmd->se_lun);
  2096. if (!dev)
  2097. return 0;
  2098. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2099. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2100. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2101. return 0;
  2102. }
  2103. if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
  2104. goto out;
  2105. if (!dev->transport->get_sense_buffer) {
  2106. pr_err("dev->transport->get_sense_buffer is NULL\n");
  2107. goto out;
  2108. }
  2109. sense_buffer = dev->transport->get_sense_buffer(cmd);
  2110. if (!sense_buffer) {
  2111. pr_err("ITT 0x%08x cmd %p: Unable to locate"
  2112. " sense buffer for task with sense\n",
  2113. cmd->se_tfo->get_task_tag(cmd), cmd);
  2114. goto out;
  2115. }
  2116. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2117. offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
  2118. memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
  2119. /* Automatically padded */
  2120. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  2121. pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
  2122. dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
  2123. return 0;
  2124. out:
  2125. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2126. return -1;
  2127. }
  2128. static inline long long transport_dev_end_lba(struct se_device *dev)
  2129. {
  2130. return dev->transport->get_blocks(dev) + 1;
  2131. }
  2132. static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
  2133. {
  2134. struct se_device *dev = cmd->se_dev;
  2135. u32 sectors;
  2136. if (dev->transport->get_device_type(dev) != TYPE_DISK)
  2137. return 0;
  2138. sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
  2139. if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
  2140. pr_err("LBA: %llu Sectors: %u exceeds"
  2141. " transport_dev_end_lba(): %llu\n",
  2142. cmd->t_task_lba, sectors,
  2143. transport_dev_end_lba(dev));
  2144. return -EINVAL;
  2145. }
  2146. return 0;
  2147. }
  2148. static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
  2149. {
  2150. /*
  2151. * Determine if the received WRITE_SAME is used to for direct
  2152. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  2153. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  2154. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
  2155. */
  2156. int passthrough = (dev->transport->transport_type ==
  2157. TRANSPORT_PLUGIN_PHBA_PDEV);
  2158. if (!passthrough) {
  2159. if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
  2160. pr_err("WRITE_SAME PBDATA and LBDATA"
  2161. " bits not supported for Block Discard"
  2162. " Emulation\n");
  2163. return -ENOSYS;
  2164. }
  2165. /*
  2166. * Currently for the emulated case we only accept
  2167. * tpws with the UNMAP=1 bit set.
  2168. */
  2169. if (!(flags[0] & 0x08)) {
  2170. pr_err("WRITE_SAME w/o UNMAP bit not"
  2171. " supported for Block Discard Emulation\n");
  2172. return -ENOSYS;
  2173. }
  2174. }
  2175. return 0;
  2176. }
  2177. static int transport_generic_cmd_sequencer(
  2178. struct se_cmd *cmd,
  2179. unsigned char *cdb)
  2180. {
  2181. struct se_device *dev = cmd->se_dev;
  2182. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2183. int sector_ret = 0, passthrough;
  2184. u32 sectors = 0, size = 0;
  2185. u16 service_action;
  2186. int ret;
  2187. /*
  2188. * If we operate in passthrough mode we skip most CDB emulation and
  2189. * instead hand the commands down to the physical SCSI device.
  2190. */
  2191. passthrough =
  2192. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
  2193. switch (cdb[0]) {
  2194. case READ_6:
  2195. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2196. if (sector_ret)
  2197. goto out_unsupported_cdb;
  2198. size = transport_get_size(sectors, cdb, cmd);
  2199. cmd->t_task_lba = transport_lba_21(cdb);
  2200. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2201. break;
  2202. case READ_10:
  2203. sectors = transport_get_sectors_10(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. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2209. break;
  2210. case READ_12:
  2211. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2212. if (sector_ret)
  2213. goto out_unsupported_cdb;
  2214. size = transport_get_size(sectors, cdb, cmd);
  2215. cmd->t_task_lba = transport_lba_32(cdb);
  2216. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2217. break;
  2218. case READ_16:
  2219. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2220. if (sector_ret)
  2221. goto out_unsupported_cdb;
  2222. size = transport_get_size(sectors, cdb, cmd);
  2223. cmd->t_task_lba = transport_lba_64(cdb);
  2224. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2225. break;
  2226. case WRITE_6:
  2227. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2228. if (sector_ret)
  2229. goto out_unsupported_cdb;
  2230. size = transport_get_size(sectors, cdb, cmd);
  2231. cmd->t_task_lba = transport_lba_21(cdb);
  2232. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2233. break;
  2234. case WRITE_10:
  2235. case WRITE_VERIFY:
  2236. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2237. if (sector_ret)
  2238. goto out_unsupported_cdb;
  2239. size = transport_get_size(sectors, cdb, cmd);
  2240. cmd->t_task_lba = transport_lba_32(cdb);
  2241. if (cdb[1] & 0x8)
  2242. cmd->se_cmd_flags |= SCF_FUA;
  2243. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2244. break;
  2245. case WRITE_12:
  2246. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2247. if (sector_ret)
  2248. goto out_unsupported_cdb;
  2249. size = transport_get_size(sectors, cdb, cmd);
  2250. cmd->t_task_lba = transport_lba_32(cdb);
  2251. if (cdb[1] & 0x8)
  2252. cmd->se_cmd_flags |= SCF_FUA;
  2253. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2254. break;
  2255. case WRITE_16:
  2256. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2257. if (sector_ret)
  2258. goto out_unsupported_cdb;
  2259. size = transport_get_size(sectors, cdb, cmd);
  2260. cmd->t_task_lba = transport_lba_64(cdb);
  2261. if (cdb[1] & 0x8)
  2262. cmd->se_cmd_flags |= SCF_FUA;
  2263. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2264. break;
  2265. case XDWRITEREAD_10:
  2266. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2267. !(cmd->se_cmd_flags & SCF_BIDI))
  2268. goto out_invalid_cdb_field;
  2269. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2270. if (sector_ret)
  2271. goto out_unsupported_cdb;
  2272. size = transport_get_size(sectors, cdb, cmd);
  2273. cmd->t_task_lba = transport_lba_32(cdb);
  2274. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2275. /*
  2276. * Do now allow BIDI commands for passthrough mode.
  2277. */
  2278. if (passthrough)
  2279. goto out_unsupported_cdb;
  2280. /*
  2281. * Setup BIDI XOR callback to be run after I/O completion.
  2282. */
  2283. cmd->transport_complete_callback = &transport_xor_callback;
  2284. if (cdb[1] & 0x8)
  2285. cmd->se_cmd_flags |= SCF_FUA;
  2286. break;
  2287. case VARIABLE_LENGTH_CMD:
  2288. service_action = get_unaligned_be16(&cdb[8]);
  2289. switch (service_action) {
  2290. case XDWRITEREAD_32:
  2291. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2292. if (sector_ret)
  2293. goto out_unsupported_cdb;
  2294. size = transport_get_size(sectors, cdb, cmd);
  2295. /*
  2296. * Use WRITE_32 and READ_32 opcodes for the emulated
  2297. * XDWRITE_READ_32 logic.
  2298. */
  2299. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2300. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  2301. /*
  2302. * Do now allow BIDI commands for passthrough mode.
  2303. */
  2304. if (passthrough)
  2305. goto out_unsupported_cdb;
  2306. /*
  2307. * Setup BIDI XOR callback to be run during after I/O
  2308. * completion.
  2309. */
  2310. cmd->transport_complete_callback = &transport_xor_callback;
  2311. if (cdb[1] & 0x8)
  2312. cmd->se_cmd_flags |= SCF_FUA;
  2313. break;
  2314. case WRITE_SAME_32:
  2315. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2316. if (sector_ret)
  2317. goto out_unsupported_cdb;
  2318. if (sectors)
  2319. size = transport_get_size(1, cdb, cmd);
  2320. else {
  2321. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  2322. " supported\n");
  2323. goto out_invalid_cdb_field;
  2324. }
  2325. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2326. if (target_check_write_same_discard(&cdb[10], dev) < 0)
  2327. goto out_unsupported_cdb;
  2328. if (!passthrough)
  2329. cmd->execute_cmd = target_emulate_write_same;
  2330. break;
  2331. default:
  2332. pr_err("VARIABLE_LENGTH_CMD service action"
  2333. " 0x%04x not supported\n", service_action);
  2334. goto out_unsupported_cdb;
  2335. }
  2336. break;
  2337. case MAINTENANCE_IN:
  2338. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2339. /* MAINTENANCE_IN from SCC-2 */
  2340. /*
  2341. * Check for emulated MI_REPORT_TARGET_PGS.
  2342. */
  2343. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS &&
  2344. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2345. cmd->execute_cmd =
  2346. target_emulate_report_target_port_groups;
  2347. }
  2348. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2349. (cdb[8] << 8) | cdb[9];
  2350. } else {
  2351. /* GPCMD_SEND_KEY from multi media commands */
  2352. size = (cdb[8] << 8) + cdb[9];
  2353. }
  2354. break;
  2355. case GPCMD_READ_BUFFER_CAPACITY:
  2356. case GPCMD_SEND_OPC:
  2357. size = (cdb[7] << 8) + cdb[8];
  2358. break;
  2359. case READ_BLOCK_LIMITS:
  2360. size = READ_BLOCK_LEN;
  2361. break;
  2362. case GPCMD_GET_CONFIGURATION:
  2363. case GPCMD_READ_FORMAT_CAPACITIES:
  2364. case GPCMD_READ_DISC_INFO:
  2365. case GPCMD_READ_TRACK_RZONE_INFO:
  2366. size = (cdb[7] << 8) + cdb[8];
  2367. break;
  2368. case GPCMD_MECHANISM_STATUS:
  2369. case GPCMD_READ_DVD_STRUCTURE:
  2370. size = (cdb[8] << 8) + cdb[9];
  2371. break;
  2372. case READ_POSITION:
  2373. size = READ_POSITION_LEN;
  2374. break;
  2375. case MAINTENANCE_OUT:
  2376. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2377. /* MAINTENANCE_OUT from SCC-2
  2378. *
  2379. * Check for emulated MO_SET_TARGET_PGS.
  2380. */
  2381. if (cdb[1] == MO_SET_TARGET_PGS &&
  2382. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2383. cmd->execute_cmd =
  2384. target_emulate_set_target_port_groups;
  2385. }
  2386. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2387. (cdb[8] << 8) | cdb[9];
  2388. } else {
  2389. /* GPCMD_REPORT_KEY from multi media commands */
  2390. size = (cdb[8] << 8) + cdb[9];
  2391. }
  2392. break;
  2393. case READ_BUFFER:
  2394. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2395. break;
  2396. case READ_CAPACITY:
  2397. size = READ_CAP_LEN;
  2398. if (!passthrough)
  2399. cmd->execute_cmd = target_emulate_readcapacity;
  2400. break;
  2401. case READ_MEDIA_SERIAL_NUMBER:
  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. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2420. (cdb[12] << 8) | cdb[13];
  2421. break;
  2422. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2423. #if 0
  2424. case GPCMD_READ_CD:
  2425. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2426. size = (2336 * sectors);
  2427. break;
  2428. #endif
  2429. case READ_TOC:
  2430. size = cdb[8];
  2431. break;
  2432. case READ_ELEMENT_STATUS:
  2433. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2434. break;
  2435. case SYNCHRONIZE_CACHE:
  2436. case SYNCHRONIZE_CACHE_16:
  2437. /*
  2438. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  2439. */
  2440. if (cdb[0] == SYNCHRONIZE_CACHE) {
  2441. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2442. cmd->t_task_lba = transport_lba_32(cdb);
  2443. } else {
  2444. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2445. cmd->t_task_lba = transport_lba_64(cdb);
  2446. }
  2447. if (sector_ret)
  2448. goto out_unsupported_cdb;
  2449. size = transport_get_size(sectors, cdb, cmd);
  2450. if (passthrough)
  2451. break;
  2452. /*
  2453. * Check to ensure that LBA + Range does not exceed past end of
  2454. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  2455. */
  2456. if ((cmd->t_task_lba != 0) || (sectors != 0)) {
  2457. if (transport_cmd_get_valid_sectors(cmd) < 0)
  2458. goto out_invalid_cdb_field;
  2459. }
  2460. cmd->execute_cmd = target_emulate_synchronize_cache;
  2461. break;
  2462. case UNMAP:
  2463. size = get_unaligned_be16(&cdb[7]);
  2464. if (!passthrough)
  2465. cmd->execute_cmd = target_emulate_unmap;
  2466. break;
  2467. case WRITE_SAME_16:
  2468. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2469. if (sector_ret)
  2470. goto out_unsupported_cdb;
  2471. if (sectors)
  2472. size = transport_get_size(1, cdb, cmd);
  2473. else {
  2474. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2475. goto out_invalid_cdb_field;
  2476. }
  2477. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  2478. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2479. goto out_unsupported_cdb;
  2480. if (!passthrough)
  2481. cmd->execute_cmd = target_emulate_write_same;
  2482. break;
  2483. case WRITE_SAME:
  2484. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2485. if (sector_ret)
  2486. goto out_unsupported_cdb;
  2487. if (sectors)
  2488. size = transport_get_size(1, cdb, cmd);
  2489. else {
  2490. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2491. goto out_invalid_cdb_field;
  2492. }
  2493. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  2494. /*
  2495. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  2496. * of byte 1 bit 3 UNMAP instead of original reserved field
  2497. */
  2498. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2499. goto out_unsupported_cdb;
  2500. if (!passthrough)
  2501. cmd->execute_cmd = target_emulate_write_same;
  2502. break;
  2503. case ALLOW_MEDIUM_REMOVAL:
  2504. case ERASE:
  2505. case REZERO_UNIT:
  2506. case SEEK_10:
  2507. case SPACE:
  2508. case START_STOP:
  2509. case VERIFY:
  2510. case WRITE_FILEMARKS:
  2511. if (!passthrough)
  2512. cmd->execute_cmd = target_emulate_noop;
  2513. break;
  2514. case GPCMD_CLOSE_TRACK:
  2515. case INITIALIZE_ELEMENT_STATUS:
  2516. case GPCMD_LOAD_UNLOAD:
  2517. case GPCMD_SET_SPEED:
  2518. case MOVE_MEDIUM:
  2519. break;
  2520. case GET_EVENT_STATUS_NOTIFICATION:
  2521. size = (cdb[7] << 8) | cdb[8];
  2522. break;
  2523. case ATA_16:
  2524. /* Only support ATA passthrough to pSCSI backends.. */
  2525. if (!passthrough)
  2526. goto out_unsupported_cdb;
  2527. /* T_LENGTH */
  2528. switch (cdb[2] & 0x3) {
  2529. case 0x0:
  2530. sectors = 0;
  2531. break;
  2532. case 0x1:
  2533. sectors = (((cdb[1] & 0x1) ? cdb[3] : 0) << 8) | cdb[4];
  2534. break;
  2535. case 0x2:
  2536. sectors = (((cdb[1] & 0x1) ? cdb[5] : 0) << 8) | cdb[6];
  2537. break;
  2538. case 0x3:
  2539. pr_err("T_LENGTH=0x3 not supported for ATA_16\n");
  2540. goto out_invalid_cdb_field;
  2541. }
  2542. /* BYTE_BLOCK */
  2543. if (cdb[2] & 0x4) {
  2544. /* BLOCK T_TYPE: 512 or sector */
  2545. size = sectors * ((cdb[2] & 0x10) ?
  2546. dev->se_sub_dev->se_dev_attrib.block_size : 512);
  2547. } else {
  2548. /* BYTE */
  2549. size = sectors;
  2550. }
  2551. break;
  2552. default:
  2553. ret = spc_parse_cdb(cmd, &size, passthrough);
  2554. if (ret)
  2555. return ret;
  2556. }
  2557. ret = target_cmd_size_check(cmd, size);
  2558. if (ret)
  2559. return ret;
  2560. if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
  2561. if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
  2562. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
  2563. " big sectors %u exceeds fabric_max_sectors:"
  2564. " %u\n", cdb[0], sectors,
  2565. su_dev->se_dev_attrib.fabric_max_sectors);
  2566. goto out_invalid_cdb_field;
  2567. }
  2568. if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
  2569. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
  2570. " big sectors %u exceeds backend hw_max_sectors:"
  2571. " %u\n", cdb[0], sectors,
  2572. su_dev->se_dev_attrib.hw_max_sectors);
  2573. goto out_invalid_cdb_field;
  2574. }
  2575. }
  2576. /* reject any command that we don't have a handler for */
  2577. if (!(passthrough || cmd->execute_cmd ||
  2578. (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB)))
  2579. goto out_unsupported_cdb;
  2580. return 0;
  2581. out_unsupported_cdb:
  2582. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2583. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  2584. return -EINVAL;
  2585. out_invalid_cdb_field:
  2586. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2587. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2588. return -EINVAL;
  2589. }
  2590. /*
  2591. * Called from I/O completion to determine which dormant/delayed
  2592. * and ordered cmds need to have their tasks added to the execution queue.
  2593. */
  2594. static void transport_complete_task_attr(struct se_cmd *cmd)
  2595. {
  2596. struct se_device *dev = cmd->se_dev;
  2597. struct se_cmd *cmd_p, *cmd_tmp;
  2598. int new_active_tasks = 0;
  2599. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  2600. atomic_dec(&dev->simple_cmds);
  2601. smp_mb__after_atomic_dec();
  2602. dev->dev_cur_ordered_id++;
  2603. pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
  2604. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  2605. cmd->se_ordered_id);
  2606. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2607. dev->dev_cur_ordered_id++;
  2608. pr_debug("Incremented dev_cur_ordered_id: %u for"
  2609. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  2610. cmd->se_ordered_id);
  2611. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2612. atomic_dec(&dev->dev_ordered_sync);
  2613. smp_mb__after_atomic_dec();
  2614. dev->dev_cur_ordered_id++;
  2615. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
  2616. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  2617. }
  2618. /*
  2619. * Process all commands up to the last received
  2620. * ORDERED task attribute which requires another blocking
  2621. * boundary
  2622. */
  2623. spin_lock(&dev->delayed_cmd_lock);
  2624. list_for_each_entry_safe(cmd_p, cmd_tmp,
  2625. &dev->delayed_cmd_list, se_delayed_node) {
  2626. list_del(&cmd_p->se_delayed_node);
  2627. spin_unlock(&dev->delayed_cmd_lock);
  2628. pr_debug("Calling add_tasks() for"
  2629. " cmd_p: 0x%02x Task Attr: 0x%02x"
  2630. " Dormant -> Active, se_ordered_id: %u\n",
  2631. cmd_p->t_task_cdb[0],
  2632. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  2633. target_add_to_execute_list(cmd_p);
  2634. new_active_tasks++;
  2635. spin_lock(&dev->delayed_cmd_lock);
  2636. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  2637. break;
  2638. }
  2639. spin_unlock(&dev->delayed_cmd_lock);
  2640. /*
  2641. * If new tasks have become active, wake up the transport thread
  2642. * to do the processing of the Active tasks.
  2643. */
  2644. if (new_active_tasks != 0)
  2645. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2646. }
  2647. static void transport_complete_qf(struct se_cmd *cmd)
  2648. {
  2649. int ret = 0;
  2650. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2651. transport_complete_task_attr(cmd);
  2652. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2653. ret = cmd->se_tfo->queue_status(cmd);
  2654. if (ret)
  2655. goto out;
  2656. }
  2657. switch (cmd->data_direction) {
  2658. case DMA_FROM_DEVICE:
  2659. ret = cmd->se_tfo->queue_data_in(cmd);
  2660. break;
  2661. case DMA_TO_DEVICE:
  2662. if (cmd->t_bidi_data_sg) {
  2663. ret = cmd->se_tfo->queue_data_in(cmd);
  2664. if (ret < 0)
  2665. break;
  2666. }
  2667. /* Fall through for DMA_TO_DEVICE */
  2668. case DMA_NONE:
  2669. ret = cmd->se_tfo->queue_status(cmd);
  2670. break;
  2671. default:
  2672. break;
  2673. }
  2674. out:
  2675. if (ret < 0) {
  2676. transport_handle_queue_full(cmd, cmd->se_dev);
  2677. return;
  2678. }
  2679. transport_lun_remove_cmd(cmd);
  2680. transport_cmd_check_stop_to_fabric(cmd);
  2681. }
  2682. static void transport_handle_queue_full(
  2683. struct se_cmd *cmd,
  2684. struct se_device *dev)
  2685. {
  2686. spin_lock_irq(&dev->qf_cmd_lock);
  2687. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  2688. atomic_inc(&dev->dev_qf_count);
  2689. smp_mb__after_atomic_inc();
  2690. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  2691. schedule_work(&cmd->se_dev->qf_work_queue);
  2692. }
  2693. static void target_complete_ok_work(struct work_struct *work)
  2694. {
  2695. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2696. int reason = 0, ret;
  2697. /*
  2698. * Check if we need to move delayed/dormant tasks from cmds on the
  2699. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  2700. * Attribute.
  2701. */
  2702. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2703. transport_complete_task_attr(cmd);
  2704. /*
  2705. * Check to schedule QUEUE_FULL work, or execute an existing
  2706. * cmd->transport_qf_callback()
  2707. */
  2708. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  2709. schedule_work(&cmd->se_dev->qf_work_queue);
  2710. /*
  2711. * Check if we need to retrieve a sense buffer from
  2712. * the struct se_cmd in question.
  2713. */
  2714. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2715. if (transport_get_sense_data(cmd) < 0)
  2716. reason = TCM_NON_EXISTENT_LUN;
  2717. if (cmd->scsi_status) {
  2718. ret = transport_send_check_condition_and_sense(
  2719. cmd, reason, 1);
  2720. if (ret == -EAGAIN || ret == -ENOMEM)
  2721. goto queue_full;
  2722. transport_lun_remove_cmd(cmd);
  2723. transport_cmd_check_stop_to_fabric(cmd);
  2724. return;
  2725. }
  2726. }
  2727. /*
  2728. * Check for a callback, used by amongst other things
  2729. * XDWRITE_READ_10 emulation.
  2730. */
  2731. if (cmd->transport_complete_callback)
  2732. cmd->transport_complete_callback(cmd);
  2733. switch (cmd->data_direction) {
  2734. case DMA_FROM_DEVICE:
  2735. spin_lock(&cmd->se_lun->lun_sep_lock);
  2736. if (cmd->se_lun->lun_sep) {
  2737. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2738. cmd->data_length;
  2739. }
  2740. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2741. ret = cmd->se_tfo->queue_data_in(cmd);
  2742. if (ret == -EAGAIN || ret == -ENOMEM)
  2743. goto queue_full;
  2744. break;
  2745. case DMA_TO_DEVICE:
  2746. spin_lock(&cmd->se_lun->lun_sep_lock);
  2747. if (cmd->se_lun->lun_sep) {
  2748. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  2749. cmd->data_length;
  2750. }
  2751. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2752. /*
  2753. * Check if we need to send READ payload for BIDI-COMMAND
  2754. */
  2755. if (cmd->t_bidi_data_sg) {
  2756. spin_lock(&cmd->se_lun->lun_sep_lock);
  2757. if (cmd->se_lun->lun_sep) {
  2758. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2759. cmd->data_length;
  2760. }
  2761. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2762. ret = cmd->se_tfo->queue_data_in(cmd);
  2763. if (ret == -EAGAIN || ret == -ENOMEM)
  2764. goto queue_full;
  2765. break;
  2766. }
  2767. /* Fall through for DMA_TO_DEVICE */
  2768. case DMA_NONE:
  2769. ret = cmd->se_tfo->queue_status(cmd);
  2770. if (ret == -EAGAIN || ret == -ENOMEM)
  2771. goto queue_full;
  2772. break;
  2773. default:
  2774. break;
  2775. }
  2776. transport_lun_remove_cmd(cmd);
  2777. transport_cmd_check_stop_to_fabric(cmd);
  2778. return;
  2779. queue_full:
  2780. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  2781. " data_direction: %d\n", cmd, cmd->data_direction);
  2782. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  2783. transport_handle_queue_full(cmd, cmd->se_dev);
  2784. }
  2785. static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
  2786. {
  2787. struct scatterlist *sg;
  2788. int count;
  2789. for_each_sg(sgl, sg, nents, count)
  2790. __free_page(sg_page(sg));
  2791. kfree(sgl);
  2792. }
  2793. static inline void transport_free_pages(struct se_cmd *cmd)
  2794. {
  2795. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  2796. return;
  2797. transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  2798. cmd->t_data_sg = NULL;
  2799. cmd->t_data_nents = 0;
  2800. transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  2801. cmd->t_bidi_data_sg = NULL;
  2802. cmd->t_bidi_data_nents = 0;
  2803. }
  2804. /**
  2805. * transport_release_cmd - free a command
  2806. * @cmd: command to free
  2807. *
  2808. * This routine unconditionally frees a command, and reference counting
  2809. * or list removal must be done in the caller.
  2810. */
  2811. static void transport_release_cmd(struct se_cmd *cmd)
  2812. {
  2813. BUG_ON(!cmd->se_tfo);
  2814. if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  2815. core_tmr_release_req(cmd->se_tmr_req);
  2816. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  2817. kfree(cmd->t_task_cdb);
  2818. /*
  2819. * If this cmd has been setup with target_get_sess_cmd(), drop
  2820. * the kref and call ->release_cmd() in kref callback.
  2821. */
  2822. if (cmd->check_release != 0) {
  2823. target_put_sess_cmd(cmd->se_sess, cmd);
  2824. return;
  2825. }
  2826. cmd->se_tfo->release_cmd(cmd);
  2827. }
  2828. /**
  2829. * transport_put_cmd - release a reference to a command
  2830. * @cmd: command to release
  2831. *
  2832. * This routine releases our reference to the command and frees it if possible.
  2833. */
  2834. static void transport_put_cmd(struct se_cmd *cmd)
  2835. {
  2836. unsigned long flags;
  2837. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2838. if (atomic_read(&cmd->t_fe_count)) {
  2839. if (!atomic_dec_and_test(&cmd->t_fe_count))
  2840. goto out_busy;
  2841. }
  2842. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  2843. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  2844. target_remove_from_state_list(cmd);
  2845. }
  2846. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2847. transport_free_pages(cmd);
  2848. transport_release_cmd(cmd);
  2849. return;
  2850. out_busy:
  2851. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2852. }
  2853. /*
  2854. * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
  2855. * allocating in the core.
  2856. * @cmd: Associated se_cmd descriptor
  2857. * @mem: SGL style memory for TCM WRITE / READ
  2858. * @sg_mem_num: Number of SGL elements
  2859. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  2860. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  2861. *
  2862. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  2863. * of parameters.
  2864. */
  2865. int transport_generic_map_mem_to_cmd(
  2866. struct se_cmd *cmd,
  2867. struct scatterlist *sgl,
  2868. u32 sgl_count,
  2869. struct scatterlist *sgl_bidi,
  2870. u32 sgl_bidi_count)
  2871. {
  2872. if (!sgl || !sgl_count)
  2873. return 0;
  2874. /*
  2875. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  2876. * scatterlists already have been set to follow what the fabric
  2877. * passes for the original expected data transfer length.
  2878. */
  2879. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  2880. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  2881. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  2882. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2883. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2884. return -EINVAL;
  2885. }
  2886. cmd->t_data_sg = sgl;
  2887. cmd->t_data_nents = sgl_count;
  2888. if (sgl_bidi && sgl_bidi_count) {
  2889. cmd->t_bidi_data_sg = sgl_bidi;
  2890. cmd->t_bidi_data_nents = sgl_bidi_count;
  2891. }
  2892. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  2893. return 0;
  2894. }
  2895. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  2896. void *transport_kmap_data_sg(struct se_cmd *cmd)
  2897. {
  2898. struct scatterlist *sg = cmd->t_data_sg;
  2899. struct page **pages;
  2900. int i;
  2901. BUG_ON(!sg);
  2902. /*
  2903. * We need to take into account a possible offset here for fabrics like
  2904. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  2905. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  2906. */
  2907. if (!cmd->t_data_nents)
  2908. return NULL;
  2909. else if (cmd->t_data_nents == 1)
  2910. return kmap(sg_page(sg)) + sg->offset;
  2911. /* >1 page. use vmap */
  2912. pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
  2913. if (!pages)
  2914. return NULL;
  2915. /* convert sg[] to pages[] */
  2916. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  2917. pages[i] = sg_page(sg);
  2918. }
  2919. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  2920. kfree(pages);
  2921. if (!cmd->t_data_vmap)
  2922. return NULL;
  2923. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  2924. }
  2925. EXPORT_SYMBOL(transport_kmap_data_sg);
  2926. void transport_kunmap_data_sg(struct se_cmd *cmd)
  2927. {
  2928. if (!cmd->t_data_nents) {
  2929. return;
  2930. } else if (cmd->t_data_nents == 1) {
  2931. kunmap(sg_page(cmd->t_data_sg));
  2932. return;
  2933. }
  2934. vunmap(cmd->t_data_vmap);
  2935. cmd->t_data_vmap = NULL;
  2936. }
  2937. EXPORT_SYMBOL(transport_kunmap_data_sg);
  2938. static int
  2939. transport_generic_get_mem(struct se_cmd *cmd)
  2940. {
  2941. u32 length = cmd->data_length;
  2942. unsigned int nents;
  2943. struct page *page;
  2944. gfp_t zero_flag;
  2945. int i = 0;
  2946. nents = DIV_ROUND_UP(length, PAGE_SIZE);
  2947. cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
  2948. if (!cmd->t_data_sg)
  2949. return -ENOMEM;
  2950. cmd->t_data_nents = nents;
  2951. sg_init_table(cmd->t_data_sg, nents);
  2952. zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO;
  2953. while (length) {
  2954. u32 page_len = min_t(u32, length, PAGE_SIZE);
  2955. page = alloc_page(GFP_KERNEL | zero_flag);
  2956. if (!page)
  2957. goto out;
  2958. sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
  2959. length -= page_len;
  2960. i++;
  2961. }
  2962. return 0;
  2963. out:
  2964. while (i >= 0) {
  2965. __free_page(sg_page(&cmd->t_data_sg[i]));
  2966. i--;
  2967. }
  2968. kfree(cmd->t_data_sg);
  2969. cmd->t_data_sg = NULL;
  2970. return -ENOMEM;
  2971. }
  2972. /*
  2973. * Allocate any required resources to execute the command. For writes we
  2974. * might not have the payload yet, so notify the fabric via a call to
  2975. * ->write_pending instead. Otherwise place it on the execution queue.
  2976. */
  2977. int transport_generic_new_cmd(struct se_cmd *cmd)
  2978. {
  2979. struct se_device *dev = cmd->se_dev;
  2980. int ret = 0;
  2981. /*
  2982. * Determine is the TCM fabric module has already allocated physical
  2983. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  2984. * beforehand.
  2985. */
  2986. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  2987. cmd->data_length) {
  2988. ret = transport_generic_get_mem(cmd);
  2989. if (ret < 0)
  2990. goto out_fail;
  2991. }
  2992. /* Workaround for handling zero-length control CDBs */
  2993. if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->data_length) {
  2994. spin_lock_irq(&cmd->t_state_lock);
  2995. cmd->t_state = TRANSPORT_COMPLETE;
  2996. cmd->transport_state |= CMD_T_ACTIVE;
  2997. spin_unlock_irq(&cmd->t_state_lock);
  2998. if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
  2999. u8 ua_asc = 0, ua_ascq = 0;
  3000. core_scsi3_ua_clear_for_request_sense(cmd,
  3001. &ua_asc, &ua_ascq);
  3002. }
  3003. INIT_WORK(&cmd->work, target_complete_ok_work);
  3004. queue_work(target_completion_wq, &cmd->work);
  3005. return 0;
  3006. }
  3007. if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
  3008. struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
  3009. if (transport_cmd_get_valid_sectors(cmd) < 0)
  3010. return -EINVAL;
  3011. BUG_ON(cmd->data_length % attr->block_size);
  3012. BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
  3013. attr->hw_max_sectors);
  3014. }
  3015. atomic_inc(&cmd->t_fe_count);
  3016. /*
  3017. * For WRITEs, let the fabric know its buffer is ready.
  3018. *
  3019. * The command will be added to the execution queue after its write
  3020. * data has arrived.
  3021. */
  3022. if (cmd->data_direction == DMA_TO_DEVICE) {
  3023. target_add_to_state_list(cmd);
  3024. return transport_generic_write_pending(cmd);
  3025. }
  3026. /*
  3027. * Everything else but a WRITE, add the command to the execution queue.
  3028. */
  3029. transport_execute_tasks(cmd);
  3030. return 0;
  3031. out_fail:
  3032. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3033. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3034. return -EINVAL;
  3035. }
  3036. EXPORT_SYMBOL(transport_generic_new_cmd);
  3037. /* transport_generic_process_write():
  3038. *
  3039. *
  3040. */
  3041. void transport_generic_process_write(struct se_cmd *cmd)
  3042. {
  3043. transport_execute_tasks(cmd);
  3044. }
  3045. EXPORT_SYMBOL(transport_generic_process_write);
  3046. static void transport_write_pending_qf(struct se_cmd *cmd)
  3047. {
  3048. int ret;
  3049. ret = cmd->se_tfo->write_pending(cmd);
  3050. if (ret == -EAGAIN || ret == -ENOMEM) {
  3051. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  3052. cmd);
  3053. transport_handle_queue_full(cmd, cmd->se_dev);
  3054. }
  3055. }
  3056. static int transport_generic_write_pending(struct se_cmd *cmd)
  3057. {
  3058. unsigned long flags;
  3059. int ret;
  3060. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3061. cmd->t_state = TRANSPORT_WRITE_PENDING;
  3062. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3063. /*
  3064. * Clear the se_cmd for WRITE_PENDING status in order to set
  3065. * CMD_T_ACTIVE so that transport_generic_handle_data can be called
  3066. * from HW target mode interrupt code. This is safe to be called
  3067. * with transport_off=1 before the cmd->se_tfo->write_pending
  3068. * because the se_cmd->se_lun pointer is not being cleared.
  3069. */
  3070. transport_cmd_check_stop(cmd, 1, 0);
  3071. /*
  3072. * Call the fabric write_pending function here to let the
  3073. * frontend know that WRITE buffers are ready.
  3074. */
  3075. ret = cmd->se_tfo->write_pending(cmd);
  3076. if (ret == -EAGAIN || ret == -ENOMEM)
  3077. goto queue_full;
  3078. else if (ret < 0)
  3079. return ret;
  3080. return 1;
  3081. queue_full:
  3082. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  3083. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  3084. transport_handle_queue_full(cmd, cmd->se_dev);
  3085. return 0;
  3086. }
  3087. void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  3088. {
  3089. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  3090. if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  3091. transport_wait_for_tasks(cmd);
  3092. transport_release_cmd(cmd);
  3093. } else {
  3094. if (wait_for_tasks)
  3095. transport_wait_for_tasks(cmd);
  3096. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  3097. if (cmd->se_lun)
  3098. transport_lun_remove_cmd(cmd);
  3099. transport_put_cmd(cmd);
  3100. }
  3101. }
  3102. EXPORT_SYMBOL(transport_generic_free_cmd);
  3103. /* target_get_sess_cmd - Add command to active ->sess_cmd_list
  3104. * @se_sess: session to reference
  3105. * @se_cmd: command descriptor to add
  3106. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  3107. */
  3108. void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
  3109. bool ack_kref)
  3110. {
  3111. unsigned long flags;
  3112. kref_init(&se_cmd->cmd_kref);
  3113. /*
  3114. * Add a second kref if the fabric caller is expecting to handle
  3115. * fabric acknowledgement that requires two target_put_sess_cmd()
  3116. * invocations before se_cmd descriptor release.
  3117. */
  3118. if (ack_kref == true) {
  3119. kref_get(&se_cmd->cmd_kref);
  3120. se_cmd->se_cmd_flags |= SCF_ACK_KREF;
  3121. }
  3122. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3123. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  3124. se_cmd->check_release = 1;
  3125. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3126. }
  3127. EXPORT_SYMBOL(target_get_sess_cmd);
  3128. static void target_release_cmd_kref(struct kref *kref)
  3129. {
  3130. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  3131. struct se_session *se_sess = se_cmd->se_sess;
  3132. unsigned long flags;
  3133. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3134. if (list_empty(&se_cmd->se_cmd_list)) {
  3135. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3136. se_cmd->se_tfo->release_cmd(se_cmd);
  3137. return;
  3138. }
  3139. if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
  3140. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3141. complete(&se_cmd->cmd_wait_comp);
  3142. return;
  3143. }
  3144. list_del(&se_cmd->se_cmd_list);
  3145. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3146. se_cmd->se_tfo->release_cmd(se_cmd);
  3147. }
  3148. /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
  3149. * @se_sess: session to reference
  3150. * @se_cmd: command descriptor to drop
  3151. */
  3152. int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
  3153. {
  3154. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  3155. }
  3156. EXPORT_SYMBOL(target_put_sess_cmd);
  3157. /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
  3158. * @se_sess: session to split
  3159. */
  3160. void target_splice_sess_cmd_list(struct se_session *se_sess)
  3161. {
  3162. struct se_cmd *se_cmd;
  3163. unsigned long flags;
  3164. WARN_ON(!list_empty(&se_sess->sess_wait_list));
  3165. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  3166. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3167. se_sess->sess_tearing_down = 1;
  3168. list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
  3169. list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
  3170. se_cmd->cmd_wait_set = 1;
  3171. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3172. }
  3173. EXPORT_SYMBOL(target_splice_sess_cmd_list);
  3174. /* target_wait_for_sess_cmds - Wait for outstanding descriptors
  3175. * @se_sess: session to wait for active I/O
  3176. * @wait_for_tasks: Make extra transport_wait_for_tasks call
  3177. */
  3178. void target_wait_for_sess_cmds(
  3179. struct se_session *se_sess,
  3180. int wait_for_tasks)
  3181. {
  3182. struct se_cmd *se_cmd, *tmp_cmd;
  3183. bool rc = false;
  3184. list_for_each_entry_safe(se_cmd, tmp_cmd,
  3185. &se_sess->sess_wait_list, se_cmd_list) {
  3186. list_del(&se_cmd->se_cmd_list);
  3187. pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
  3188. " %d\n", se_cmd, se_cmd->t_state,
  3189. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3190. if (wait_for_tasks) {
  3191. pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3192. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3193. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3194. rc = transport_wait_for_tasks(se_cmd);
  3195. pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3196. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3197. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3198. }
  3199. if (!rc) {
  3200. wait_for_completion(&se_cmd->cmd_wait_comp);
  3201. pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
  3202. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3203. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3204. }
  3205. se_cmd->se_tfo->release_cmd(se_cmd);
  3206. }
  3207. }
  3208. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  3209. /* transport_lun_wait_for_tasks():
  3210. *
  3211. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  3212. * an struct se_lun to be successfully shutdown.
  3213. */
  3214. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  3215. {
  3216. unsigned long flags;
  3217. int ret = 0;
  3218. /*
  3219. * If the frontend has already requested this struct se_cmd to
  3220. * be stopped, we can safely ignore this struct se_cmd.
  3221. */
  3222. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3223. if (cmd->transport_state & CMD_T_STOP) {
  3224. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3225. pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
  3226. cmd->se_tfo->get_task_tag(cmd));
  3227. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3228. transport_cmd_check_stop(cmd, 1, 0);
  3229. return -EPERM;
  3230. }
  3231. cmd->transport_state |= CMD_T_LUN_FE_STOP;
  3232. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3233. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3234. // XXX: audit task_flags checks.
  3235. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3236. if ((cmd->transport_state & CMD_T_BUSY) &&
  3237. (cmd->transport_state & CMD_T_SENT)) {
  3238. if (!target_stop_cmd(cmd, &flags))
  3239. ret++;
  3240. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3241. } else {
  3242. spin_unlock_irqrestore(&cmd->t_state_lock,
  3243. flags);
  3244. target_remove_from_execute_list(cmd);
  3245. }
  3246. pr_debug("ConfigFS: cmd: %p stop tasks ret:"
  3247. " %d\n", cmd, ret);
  3248. if (!ret) {
  3249. pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  3250. cmd->se_tfo->get_task_tag(cmd));
  3251. wait_for_completion(&cmd->transport_lun_stop_comp);
  3252. pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  3253. cmd->se_tfo->get_task_tag(cmd));
  3254. }
  3255. transport_remove_cmd_from_queue(cmd);
  3256. return 0;
  3257. }
  3258. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  3259. {
  3260. struct se_cmd *cmd = NULL;
  3261. unsigned long lun_flags, cmd_flags;
  3262. /*
  3263. * Do exception processing and return CHECK_CONDITION status to the
  3264. * Initiator Port.
  3265. */
  3266. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3267. while (!list_empty(&lun->lun_cmd_list)) {
  3268. cmd = list_first_entry(&lun->lun_cmd_list,
  3269. struct se_cmd, se_lun_node);
  3270. list_del_init(&cmd->se_lun_node);
  3271. /*
  3272. * This will notify iscsi_target_transport.c:
  3273. * transport_cmd_check_stop() that a LUN shutdown is in
  3274. * progress for the iscsi_cmd_t.
  3275. */
  3276. spin_lock(&cmd->t_state_lock);
  3277. pr_debug("SE_LUN[%d] - Setting cmd->transport"
  3278. "_lun_stop for ITT: 0x%08x\n",
  3279. cmd->se_lun->unpacked_lun,
  3280. cmd->se_tfo->get_task_tag(cmd));
  3281. cmd->transport_state |= CMD_T_LUN_STOP;
  3282. spin_unlock(&cmd->t_state_lock);
  3283. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3284. if (!cmd->se_lun) {
  3285. pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
  3286. cmd->se_tfo->get_task_tag(cmd),
  3287. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3288. BUG();
  3289. }
  3290. /*
  3291. * If the Storage engine still owns the iscsi_cmd_t, determine
  3292. * and/or stop its context.
  3293. */
  3294. pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
  3295. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  3296. cmd->se_tfo->get_task_tag(cmd));
  3297. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  3298. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3299. continue;
  3300. }
  3301. pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  3302. "_wait_for_tasks(): SUCCESS\n",
  3303. cmd->se_lun->unpacked_lun,
  3304. cmd->se_tfo->get_task_tag(cmd));
  3305. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3306. if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
  3307. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3308. goto check_cond;
  3309. }
  3310. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  3311. target_remove_from_state_list(cmd);
  3312. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3313. /*
  3314. * The Storage engine stopped this struct se_cmd before it was
  3315. * send to the fabric frontend for delivery back to the
  3316. * Initiator Node. Return this SCSI CDB back with an
  3317. * CHECK_CONDITION status.
  3318. */
  3319. check_cond:
  3320. transport_send_check_condition_and_sense(cmd,
  3321. TCM_NON_EXISTENT_LUN, 0);
  3322. /*
  3323. * If the fabric frontend is waiting for this iscsi_cmd_t to
  3324. * be released, notify the waiting thread now that LU has
  3325. * finished accessing it.
  3326. */
  3327. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3328. if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
  3329. pr_debug("SE_LUN[%d] - Detected FE stop for"
  3330. " struct se_cmd: %p ITT: 0x%08x\n",
  3331. lun->unpacked_lun,
  3332. cmd, cmd->se_tfo->get_task_tag(cmd));
  3333. spin_unlock_irqrestore(&cmd->t_state_lock,
  3334. cmd_flags);
  3335. transport_cmd_check_stop(cmd, 1, 0);
  3336. complete(&cmd->transport_lun_fe_stop_comp);
  3337. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3338. continue;
  3339. }
  3340. pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  3341. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  3342. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3343. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3344. }
  3345. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3346. }
  3347. static int transport_clear_lun_thread(void *p)
  3348. {
  3349. struct se_lun *lun = p;
  3350. __transport_clear_lun_from_sessions(lun);
  3351. complete(&lun->lun_shutdown_comp);
  3352. return 0;
  3353. }
  3354. int transport_clear_lun_from_sessions(struct se_lun *lun)
  3355. {
  3356. struct task_struct *kt;
  3357. kt = kthread_run(transport_clear_lun_thread, lun,
  3358. "tcm_cl_%u", lun->unpacked_lun);
  3359. if (IS_ERR(kt)) {
  3360. pr_err("Unable to start clear_lun thread\n");
  3361. return PTR_ERR(kt);
  3362. }
  3363. wait_for_completion(&lun->lun_shutdown_comp);
  3364. return 0;
  3365. }
  3366. /**
  3367. * transport_wait_for_tasks - wait for completion to occur
  3368. * @cmd: command to wait
  3369. *
  3370. * Called from frontend fabric context to wait for storage engine
  3371. * to pause and/or release frontend generated struct se_cmd.
  3372. */
  3373. bool transport_wait_for_tasks(struct se_cmd *cmd)
  3374. {
  3375. unsigned long flags;
  3376. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3377. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
  3378. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3379. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3380. return false;
  3381. }
  3382. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
  3383. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3384. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3385. return false;
  3386. }
  3387. /*
  3388. * If we are already stopped due to an external event (ie: LUN shutdown)
  3389. * sleep until the connection can have the passed struct se_cmd back.
  3390. * The cmd->transport_lun_stopped_sem will be upped by
  3391. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  3392. * has completed its operation on the struct se_cmd.
  3393. */
  3394. if (cmd->transport_state & CMD_T_LUN_STOP) {
  3395. pr_debug("wait_for_tasks: Stopping"
  3396. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  3397. "_stop_comp); for ITT: 0x%08x\n",
  3398. cmd->se_tfo->get_task_tag(cmd));
  3399. /*
  3400. * There is a special case for WRITES where a FE exception +
  3401. * LUN shutdown means ConfigFS context is still sleeping on
  3402. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  3403. * We go ahead and up transport_lun_stop_comp just to be sure
  3404. * here.
  3405. */
  3406. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3407. complete(&cmd->transport_lun_stop_comp);
  3408. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  3409. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3410. target_remove_from_state_list(cmd);
  3411. /*
  3412. * At this point, the frontend who was the originator of this
  3413. * struct se_cmd, now owns the structure and can be released through
  3414. * normal means below.
  3415. */
  3416. pr_debug("wait_for_tasks: Stopped"
  3417. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  3418. "stop_comp); for ITT: 0x%08x\n",
  3419. cmd->se_tfo->get_task_tag(cmd));
  3420. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3421. }
  3422. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  3423. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3424. return false;
  3425. }
  3426. cmd->transport_state |= CMD_T_STOP;
  3427. pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
  3428. " i_state: %d, t_state: %d, CMD_T_STOP\n",
  3429. cmd, cmd->se_tfo->get_task_tag(cmd),
  3430. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3431. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3432. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3433. wait_for_completion(&cmd->t_transport_stop_comp);
  3434. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3435. cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
  3436. pr_debug("wait_for_tasks: Stopped wait_for_compltion("
  3437. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  3438. cmd->se_tfo->get_task_tag(cmd));
  3439. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3440. return true;
  3441. }
  3442. EXPORT_SYMBOL(transport_wait_for_tasks);
  3443. static int transport_get_sense_codes(
  3444. struct se_cmd *cmd,
  3445. u8 *asc,
  3446. u8 *ascq)
  3447. {
  3448. *asc = cmd->scsi_asc;
  3449. *ascq = cmd->scsi_ascq;
  3450. return 0;
  3451. }
  3452. static int transport_set_sense_codes(
  3453. struct se_cmd *cmd,
  3454. u8 asc,
  3455. u8 ascq)
  3456. {
  3457. cmd->scsi_asc = asc;
  3458. cmd->scsi_ascq = ascq;
  3459. return 0;
  3460. }
  3461. int transport_send_check_condition_and_sense(
  3462. struct se_cmd *cmd,
  3463. u8 reason,
  3464. int from_transport)
  3465. {
  3466. unsigned char *buffer = cmd->sense_buffer;
  3467. unsigned long flags;
  3468. int offset;
  3469. u8 asc = 0, ascq = 0;
  3470. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3471. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3472. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3473. return 0;
  3474. }
  3475. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  3476. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3477. if (!reason && from_transport)
  3478. goto after_reason;
  3479. if (!from_transport)
  3480. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  3481. /*
  3482. * Data Segment and SenseLength of the fabric response PDU.
  3483. *
  3484. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  3485. * from include/scsi/scsi_cmnd.h
  3486. */
  3487. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  3488. TRANSPORT_SENSE_BUFFER);
  3489. /*
  3490. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  3491. * SENSE KEY values from include/scsi/scsi.h
  3492. */
  3493. switch (reason) {
  3494. case TCM_NON_EXISTENT_LUN:
  3495. /* CURRENT ERROR */
  3496. buffer[offset] = 0x70;
  3497. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3498. /* ILLEGAL REQUEST */
  3499. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3500. /* LOGICAL UNIT NOT SUPPORTED */
  3501. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
  3502. break;
  3503. case TCM_UNSUPPORTED_SCSI_OPCODE:
  3504. case TCM_SECTOR_COUNT_TOO_MANY:
  3505. /* CURRENT ERROR */
  3506. buffer[offset] = 0x70;
  3507. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3508. /* ILLEGAL REQUEST */
  3509. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3510. /* INVALID COMMAND OPERATION CODE */
  3511. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  3512. break;
  3513. case TCM_UNKNOWN_MODE_PAGE:
  3514. /* CURRENT ERROR */
  3515. buffer[offset] = 0x70;
  3516. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3517. /* ILLEGAL REQUEST */
  3518. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3519. /* INVALID FIELD IN CDB */
  3520. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3521. break;
  3522. case TCM_CHECK_CONDITION_ABORT_CMD:
  3523. /* CURRENT ERROR */
  3524. buffer[offset] = 0x70;
  3525. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3526. /* ABORTED COMMAND */
  3527. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3528. /* BUS DEVICE RESET FUNCTION OCCURRED */
  3529. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  3530. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  3531. break;
  3532. case TCM_INCORRECT_AMOUNT_OF_DATA:
  3533. /* CURRENT ERROR */
  3534. buffer[offset] = 0x70;
  3535. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3536. /* ABORTED COMMAND */
  3537. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3538. /* WRITE ERROR */
  3539. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3540. /* NOT ENOUGH UNSOLICITED DATA */
  3541. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  3542. break;
  3543. case TCM_INVALID_CDB_FIELD:
  3544. /* CURRENT ERROR */
  3545. buffer[offset] = 0x70;
  3546. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3547. /* ILLEGAL REQUEST */
  3548. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3549. /* INVALID FIELD IN CDB */
  3550. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3551. break;
  3552. case TCM_INVALID_PARAMETER_LIST:
  3553. /* CURRENT ERROR */
  3554. buffer[offset] = 0x70;
  3555. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3556. /* ILLEGAL REQUEST */
  3557. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3558. /* INVALID FIELD IN PARAMETER LIST */
  3559. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  3560. break;
  3561. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  3562. /* CURRENT ERROR */
  3563. buffer[offset] = 0x70;
  3564. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3565. /* ABORTED COMMAND */
  3566. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3567. /* WRITE ERROR */
  3568. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3569. /* UNEXPECTED_UNSOLICITED_DATA */
  3570. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  3571. break;
  3572. case TCM_SERVICE_CRC_ERROR:
  3573. /* CURRENT ERROR */
  3574. buffer[offset] = 0x70;
  3575. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3576. /* ABORTED COMMAND */
  3577. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3578. /* PROTOCOL SERVICE CRC ERROR */
  3579. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  3580. /* N/A */
  3581. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  3582. break;
  3583. case TCM_SNACK_REJECTED:
  3584. /* CURRENT ERROR */
  3585. buffer[offset] = 0x70;
  3586. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3587. /* ABORTED COMMAND */
  3588. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3589. /* READ ERROR */
  3590. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  3591. /* FAILED RETRANSMISSION REQUEST */
  3592. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  3593. break;
  3594. case TCM_WRITE_PROTECTED:
  3595. /* CURRENT ERROR */
  3596. buffer[offset] = 0x70;
  3597. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3598. /* DATA PROTECT */
  3599. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  3600. /* WRITE PROTECTED */
  3601. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  3602. break;
  3603. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  3604. /* CURRENT ERROR */
  3605. buffer[offset] = 0x70;
  3606. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3607. /* UNIT ATTENTION */
  3608. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  3609. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  3610. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  3611. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  3612. break;
  3613. case TCM_CHECK_CONDITION_NOT_READY:
  3614. /* CURRENT ERROR */
  3615. buffer[offset] = 0x70;
  3616. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3617. /* Not Ready */
  3618. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  3619. transport_get_sense_codes(cmd, &asc, &ascq);
  3620. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  3621. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  3622. break;
  3623. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  3624. default:
  3625. /* CURRENT ERROR */
  3626. buffer[offset] = 0x70;
  3627. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3628. /* ILLEGAL REQUEST */
  3629. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3630. /* LOGICAL UNIT COMMUNICATION FAILURE */
  3631. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  3632. break;
  3633. }
  3634. /*
  3635. * This code uses linux/include/scsi/scsi.h SAM status codes!
  3636. */
  3637. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  3638. /*
  3639. * Automatically padded, this value is encoded in the fabric's
  3640. * data_length response PDU containing the SCSI defined sense data.
  3641. */
  3642. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  3643. after_reason:
  3644. return cmd->se_tfo->queue_status(cmd);
  3645. }
  3646. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  3647. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  3648. {
  3649. int ret = 0;
  3650. if (cmd->transport_state & CMD_T_ABORTED) {
  3651. if (!send_status ||
  3652. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  3653. return 1;
  3654. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
  3655. " status for CDB: 0x%02x ITT: 0x%08x\n",
  3656. cmd->t_task_cdb[0],
  3657. cmd->se_tfo->get_task_tag(cmd));
  3658. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  3659. cmd->se_tfo->queue_status(cmd);
  3660. ret = 1;
  3661. }
  3662. return ret;
  3663. }
  3664. EXPORT_SYMBOL(transport_check_aborted_status);
  3665. void transport_send_task_abort(struct se_cmd *cmd)
  3666. {
  3667. unsigned long flags;
  3668. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3669. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3670. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3671. return;
  3672. }
  3673. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3674. /*
  3675. * If there are still expected incoming fabric WRITEs, we wait
  3676. * until until they have completed before sending a TASK_ABORTED
  3677. * response. This response with TASK_ABORTED status will be
  3678. * queued back to fabric module by transport_check_aborted_status().
  3679. */
  3680. if (cmd->data_direction == DMA_TO_DEVICE) {
  3681. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  3682. cmd->transport_state |= CMD_T_ABORTED;
  3683. smp_mb__after_atomic_inc();
  3684. }
  3685. }
  3686. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  3687. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  3688. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  3689. cmd->se_tfo->get_task_tag(cmd));
  3690. cmd->se_tfo->queue_status(cmd);
  3691. }
  3692. static int transport_generic_do_tmr(struct se_cmd *cmd)
  3693. {
  3694. struct se_device *dev = cmd->se_dev;
  3695. struct se_tmr_req *tmr = cmd->se_tmr_req;
  3696. int ret;
  3697. switch (tmr->function) {
  3698. case TMR_ABORT_TASK:
  3699. core_tmr_abort_task(dev, tmr, cmd->se_sess);
  3700. break;
  3701. case TMR_ABORT_TASK_SET:
  3702. case TMR_CLEAR_ACA:
  3703. case TMR_CLEAR_TASK_SET:
  3704. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  3705. break;
  3706. case TMR_LUN_RESET:
  3707. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  3708. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  3709. TMR_FUNCTION_REJECTED;
  3710. break;
  3711. case TMR_TARGET_WARM_RESET:
  3712. tmr->response = TMR_FUNCTION_REJECTED;
  3713. break;
  3714. case TMR_TARGET_COLD_RESET:
  3715. tmr->response = TMR_FUNCTION_REJECTED;
  3716. break;
  3717. default:
  3718. pr_err("Uknown TMR function: 0x%02x.\n",
  3719. tmr->function);
  3720. tmr->response = TMR_FUNCTION_REJECTED;
  3721. break;
  3722. }
  3723. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  3724. cmd->se_tfo->queue_tm_rsp(cmd);
  3725. transport_cmd_check_stop_to_fabric(cmd);
  3726. return 0;
  3727. }
  3728. /* transport_processing_thread():
  3729. *
  3730. *
  3731. */
  3732. static int transport_processing_thread(void *param)
  3733. {
  3734. int ret;
  3735. struct se_cmd *cmd;
  3736. struct se_device *dev = param;
  3737. while (!kthread_should_stop()) {
  3738. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  3739. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  3740. kthread_should_stop());
  3741. if (ret < 0)
  3742. goto out;
  3743. get_cmd:
  3744. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  3745. if (!cmd)
  3746. continue;
  3747. switch (cmd->t_state) {
  3748. case TRANSPORT_NEW_CMD:
  3749. BUG();
  3750. break;
  3751. case TRANSPORT_NEW_CMD_MAP:
  3752. if (!cmd->se_tfo->new_cmd_map) {
  3753. pr_err("cmd->se_tfo->new_cmd_map is"
  3754. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  3755. BUG();
  3756. }
  3757. ret = cmd->se_tfo->new_cmd_map(cmd);
  3758. if (ret < 0) {
  3759. transport_generic_request_failure(cmd);
  3760. break;
  3761. }
  3762. ret = transport_generic_new_cmd(cmd);
  3763. if (ret < 0) {
  3764. transport_generic_request_failure(cmd);
  3765. break;
  3766. }
  3767. break;
  3768. case TRANSPORT_PROCESS_WRITE:
  3769. transport_generic_process_write(cmd);
  3770. break;
  3771. case TRANSPORT_PROCESS_TMR:
  3772. transport_generic_do_tmr(cmd);
  3773. break;
  3774. case TRANSPORT_COMPLETE_QF_WP:
  3775. transport_write_pending_qf(cmd);
  3776. break;
  3777. case TRANSPORT_COMPLETE_QF_OK:
  3778. transport_complete_qf(cmd);
  3779. break;
  3780. default:
  3781. pr_err("Unknown t_state: %d for ITT: 0x%08x "
  3782. "i_state: %d on SE LUN: %u\n",
  3783. cmd->t_state,
  3784. cmd->se_tfo->get_task_tag(cmd),
  3785. cmd->se_tfo->get_cmd_state(cmd),
  3786. cmd->se_lun->unpacked_lun);
  3787. BUG();
  3788. }
  3789. goto get_cmd;
  3790. }
  3791. out:
  3792. WARN_ON(!list_empty(&dev->state_list));
  3793. WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
  3794. dev->process_thread = NULL;
  3795. return 0;
  3796. }