target_core_transport.c 156 KB

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