target_core_transport.c 133 KB

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