target_core_transport.c 131 KB

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