target_core_transport.c 131 KB

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