target_core_transport.c 126 KB

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