target_core_transport.c 132 KB

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