target_core_transport.c 133 KB

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