target_core_transport.c 129 KB

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