target_core_transport.c 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774
  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 release_to_pool, 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, 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, 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 defining a TFO->new_cmd_map() caller
  1643. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1644. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1645. */
  1646. int transport_generic_handle_cdb_map(
  1647. struct se_cmd *cmd)
  1648. {
  1649. if (!cmd->se_lun) {
  1650. dump_stack();
  1651. printk(KERN_ERR "cmd->se_lun is NULL\n");
  1652. return -EINVAL;
  1653. }
  1654. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
  1655. return 0;
  1656. }
  1657. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1658. /* transport_generic_handle_data():
  1659. *
  1660. *
  1661. */
  1662. int transport_generic_handle_data(
  1663. struct se_cmd *cmd)
  1664. {
  1665. /*
  1666. * For the software fabric case, then we assume the nexus is being
  1667. * failed/shutdown when signals are pending from the kthread context
  1668. * caller, so we return a failure. For the HW target mode case running
  1669. * in interrupt code, the signal_pending() check is skipped.
  1670. */
  1671. if (!in_interrupt() && signal_pending(current))
  1672. return -EPERM;
  1673. /*
  1674. * If the received CDB has aleady been ABORTED by the generic
  1675. * target engine, we now call transport_check_aborted_status()
  1676. * to queue any delated TASK_ABORTED status for the received CDB to the
  1677. * fabric module as we are expecting no further incoming DATA OUT
  1678. * sequences at this point.
  1679. */
  1680. if (transport_check_aborted_status(cmd, 1) != 0)
  1681. return 0;
  1682. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
  1683. return 0;
  1684. }
  1685. EXPORT_SYMBOL(transport_generic_handle_data);
  1686. /* transport_generic_handle_tmr():
  1687. *
  1688. *
  1689. */
  1690. int transport_generic_handle_tmr(
  1691. struct se_cmd *cmd)
  1692. {
  1693. /*
  1694. * This is needed for early exceptions.
  1695. */
  1696. cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
  1697. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
  1698. return 0;
  1699. }
  1700. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1701. void transport_generic_free_cmd_intr(
  1702. struct se_cmd *cmd)
  1703. {
  1704. transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR);
  1705. }
  1706. EXPORT_SYMBOL(transport_generic_free_cmd_intr);
  1707. static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
  1708. {
  1709. struct se_task *task, *task_tmp;
  1710. unsigned long flags;
  1711. int ret = 0;
  1712. DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
  1713. cmd->se_tfo->get_task_tag(cmd));
  1714. /*
  1715. * No tasks remain in the execution queue
  1716. */
  1717. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1718. list_for_each_entry_safe(task, task_tmp,
  1719. &cmd->t_task_list, t_list) {
  1720. DEBUG_TS("task_no[%d] - Processing task %p\n",
  1721. task->task_no, task);
  1722. /*
  1723. * If the struct se_task has not been sent and is not active,
  1724. * remove the struct se_task from the execution queue.
  1725. */
  1726. if (!atomic_read(&task->task_sent) &&
  1727. !atomic_read(&task->task_active)) {
  1728. spin_unlock_irqrestore(&cmd->t_state_lock,
  1729. flags);
  1730. transport_remove_task_from_execute_queue(task,
  1731. task->se_dev);
  1732. DEBUG_TS("task_no[%d] - Removed from execute queue\n",
  1733. task->task_no);
  1734. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1735. continue;
  1736. }
  1737. /*
  1738. * If the struct se_task is active, sleep until it is returned
  1739. * from the plugin.
  1740. */
  1741. if (atomic_read(&task->task_active)) {
  1742. atomic_set(&task->task_stop, 1);
  1743. spin_unlock_irqrestore(&cmd->t_state_lock,
  1744. flags);
  1745. DEBUG_TS("task_no[%d] - Waiting to complete\n",
  1746. task->task_no);
  1747. wait_for_completion(&task->task_stop_comp);
  1748. DEBUG_TS("task_no[%d] - Stopped successfully\n",
  1749. task->task_no);
  1750. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1751. atomic_dec(&cmd->t_task_cdbs_left);
  1752. atomic_set(&task->task_active, 0);
  1753. atomic_set(&task->task_stop, 0);
  1754. } else {
  1755. DEBUG_TS("task_no[%d] - Did nothing\n", task->task_no);
  1756. ret++;
  1757. }
  1758. __transport_stop_task_timer(task, &flags);
  1759. }
  1760. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1761. return ret;
  1762. }
  1763. /*
  1764. * Handle SAM-esque emulation for generic transport request failures.
  1765. */
  1766. static void transport_generic_request_failure(
  1767. struct se_cmd *cmd,
  1768. struct se_device *dev,
  1769. int complete,
  1770. int sc)
  1771. {
  1772. DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1773. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1774. cmd->t_task_cdb[0]);
  1775. DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
  1776. " %d/%d transport_error_status: %d\n",
  1777. cmd->se_tfo->get_cmd_state(cmd),
  1778. cmd->t_state, cmd->deferred_t_state,
  1779. cmd->transport_error_status);
  1780. DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
  1781. " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
  1782. " t_transport_active: %d t_transport_stop: %d"
  1783. " t_transport_sent: %d\n", cmd->t_task_cdbs,
  1784. atomic_read(&cmd->t_task_cdbs_left),
  1785. atomic_read(&cmd->t_task_cdbs_sent),
  1786. atomic_read(&cmd->t_task_cdbs_ex_left),
  1787. atomic_read(&cmd->t_transport_active),
  1788. atomic_read(&cmd->t_transport_stop),
  1789. atomic_read(&cmd->t_transport_sent));
  1790. transport_stop_all_task_timers(cmd);
  1791. if (dev)
  1792. atomic_inc(&dev->depth_left);
  1793. /*
  1794. * For SAM Task Attribute emulation for failed struct se_cmd
  1795. */
  1796. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1797. transport_complete_task_attr(cmd);
  1798. if (complete) {
  1799. transport_direct_request_timeout(cmd);
  1800. cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
  1801. }
  1802. switch (cmd->transport_error_status) {
  1803. case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
  1804. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1805. break;
  1806. case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
  1807. cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
  1808. break;
  1809. case PYX_TRANSPORT_INVALID_CDB_FIELD:
  1810. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1811. break;
  1812. case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
  1813. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1814. break;
  1815. case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
  1816. if (!sc)
  1817. transport_new_cmd_failure(cmd);
  1818. /*
  1819. * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
  1820. * we force this session to fall back to session
  1821. * recovery.
  1822. */
  1823. cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
  1824. cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
  1825. goto check_stop;
  1826. case PYX_TRANSPORT_LU_COMM_FAILURE:
  1827. case PYX_TRANSPORT_ILLEGAL_REQUEST:
  1828. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1829. break;
  1830. case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
  1831. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  1832. break;
  1833. case PYX_TRANSPORT_WRITE_PROTECTED:
  1834. cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
  1835. break;
  1836. case PYX_TRANSPORT_RESERVATION_CONFLICT:
  1837. /*
  1838. * No SENSE Data payload for this case, set SCSI Status
  1839. * and queue the response to $FABRIC_MOD.
  1840. *
  1841. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1842. */
  1843. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1844. /*
  1845. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1846. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1847. * CONFLICT STATUS.
  1848. *
  1849. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1850. */
  1851. if (cmd->se_sess &&
  1852. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1853. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1854. cmd->orig_fe_lun, 0x2C,
  1855. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1856. cmd->se_tfo->queue_status(cmd);
  1857. goto check_stop;
  1858. case PYX_TRANSPORT_USE_SENSE_REASON:
  1859. /*
  1860. * struct se_cmd->scsi_sense_reason already set
  1861. */
  1862. break;
  1863. default:
  1864. printk(KERN_ERR "Unknown transport error for CDB 0x%02x: %d\n",
  1865. cmd->t_task_cdb[0],
  1866. cmd->transport_error_status);
  1867. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1868. break;
  1869. }
  1870. if (!sc)
  1871. transport_new_cmd_failure(cmd);
  1872. else
  1873. transport_send_check_condition_and_sense(cmd,
  1874. cmd->scsi_sense_reason, 0);
  1875. check_stop:
  1876. transport_lun_remove_cmd(cmd);
  1877. if (!(transport_cmd_check_stop_to_fabric(cmd)))
  1878. ;
  1879. }
  1880. static void transport_direct_request_timeout(struct se_cmd *cmd)
  1881. {
  1882. unsigned long flags;
  1883. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1884. if (!(atomic_read(&cmd->t_transport_timeout))) {
  1885. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1886. return;
  1887. }
  1888. if (atomic_read(&cmd->t_task_cdbs_timeout_left)) {
  1889. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1890. return;
  1891. }
  1892. atomic_sub(atomic_read(&cmd->t_transport_timeout),
  1893. &cmd->t_se_count);
  1894. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1895. }
  1896. static void transport_generic_request_timeout(struct se_cmd *cmd)
  1897. {
  1898. unsigned long flags;
  1899. /*
  1900. * Reset cmd->t_se_count to allow transport_generic_remove()
  1901. * to allow last call to free memory resources.
  1902. */
  1903. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1904. if (atomic_read(&cmd->t_transport_timeout) > 1) {
  1905. int tmp = (atomic_read(&cmd->t_transport_timeout) - 1);
  1906. atomic_sub(tmp, &cmd->t_se_count);
  1907. }
  1908. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1909. transport_generic_remove(cmd, 0, 0);
  1910. }
  1911. static int
  1912. transport_generic_allocate_buf(struct se_cmd *cmd, u32 data_length)
  1913. {
  1914. unsigned char *buf;
  1915. buf = kzalloc(data_length, GFP_KERNEL);
  1916. if (!(buf)) {
  1917. printk(KERN_ERR "Unable to allocate memory for buffer\n");
  1918. return -ENOMEM;
  1919. }
  1920. cmd->t_tasks_se_num = 0;
  1921. cmd->t_task_buf = buf;
  1922. return 0;
  1923. }
  1924. static inline u32 transport_lba_21(unsigned char *cdb)
  1925. {
  1926. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1927. }
  1928. static inline u32 transport_lba_32(unsigned char *cdb)
  1929. {
  1930. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1931. }
  1932. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1933. {
  1934. unsigned int __v1, __v2;
  1935. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1936. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1937. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1938. }
  1939. /*
  1940. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1941. */
  1942. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1943. {
  1944. unsigned int __v1, __v2;
  1945. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1946. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1947. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1948. }
  1949. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  1950. {
  1951. unsigned long flags;
  1952. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  1953. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1954. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  1955. }
  1956. /*
  1957. * Called from interrupt context.
  1958. */
  1959. static void transport_task_timeout_handler(unsigned long data)
  1960. {
  1961. struct se_task *task = (struct se_task *)data;
  1962. struct se_cmd *cmd = task->task_se_cmd;
  1963. unsigned long flags;
  1964. DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
  1965. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1966. if (task->task_flags & TF_STOP) {
  1967. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1968. return;
  1969. }
  1970. task->task_flags &= ~TF_RUNNING;
  1971. /*
  1972. * Determine if transport_complete_task() has already been called.
  1973. */
  1974. if (!(atomic_read(&task->task_active))) {
  1975. DEBUG_TT("transport task: %p cmd: %p timeout task_active"
  1976. " == 0\n", task, cmd);
  1977. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1978. return;
  1979. }
  1980. atomic_inc(&cmd->t_se_count);
  1981. atomic_inc(&cmd->t_transport_timeout);
  1982. cmd->t_tasks_failed = 1;
  1983. atomic_set(&task->task_timeout, 1);
  1984. task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
  1985. task->task_scsi_status = 1;
  1986. if (atomic_read(&task->task_stop)) {
  1987. DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
  1988. " == 1\n", task, cmd);
  1989. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1990. complete(&task->task_stop_comp);
  1991. return;
  1992. }
  1993. if (!(atomic_dec_and_test(&cmd->t_task_cdbs_left))) {
  1994. DEBUG_TT("transport task: %p cmd: %p timeout non zero"
  1995. " t_task_cdbs_left\n", task, cmd);
  1996. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1997. return;
  1998. }
  1999. DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
  2000. task, cmd);
  2001. cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
  2002. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2003. transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
  2004. }
  2005. /*
  2006. * Called with cmd->t_state_lock held.
  2007. */
  2008. static void transport_start_task_timer(struct se_task *task)
  2009. {
  2010. struct se_device *dev = task->se_dev;
  2011. int timeout;
  2012. if (task->task_flags & TF_RUNNING)
  2013. return;
  2014. /*
  2015. * If the task_timeout is disabled, exit now.
  2016. */
  2017. timeout = dev->se_sub_dev->se_dev_attrib.task_timeout;
  2018. if (!(timeout))
  2019. return;
  2020. init_timer(&task->task_timer);
  2021. task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
  2022. task->task_timer.data = (unsigned long) task;
  2023. task->task_timer.function = transport_task_timeout_handler;
  2024. task->task_flags |= TF_RUNNING;
  2025. add_timer(&task->task_timer);
  2026. #if 0
  2027. printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
  2028. " %d\n", task->task_se_cmd, task, timeout);
  2029. #endif
  2030. }
  2031. /*
  2032. * Called with spin_lock_irq(&cmd->t_state_lock) held.
  2033. */
  2034. void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
  2035. {
  2036. struct se_cmd *cmd = task->task_se_cmd;
  2037. if (!(task->task_flags & TF_RUNNING))
  2038. return;
  2039. task->task_flags |= TF_STOP;
  2040. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  2041. del_timer_sync(&task->task_timer);
  2042. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  2043. task->task_flags &= ~TF_RUNNING;
  2044. task->task_flags &= ~TF_STOP;
  2045. }
  2046. static void transport_stop_all_task_timers(struct se_cmd *cmd)
  2047. {
  2048. struct se_task *task = NULL, *task_tmp;
  2049. unsigned long flags;
  2050. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2051. list_for_each_entry_safe(task, task_tmp,
  2052. &cmd->t_task_list, t_list)
  2053. __transport_stop_task_timer(task, &flags);
  2054. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2055. }
  2056. static inline int transport_tcq_window_closed(struct se_device *dev)
  2057. {
  2058. if (dev->dev_tcq_window_closed++ <
  2059. PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
  2060. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
  2061. } else
  2062. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
  2063. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2064. return 0;
  2065. }
  2066. /*
  2067. * Called from Fabric Module context from transport_execute_tasks()
  2068. *
  2069. * The return of this function determins if the tasks from struct se_cmd
  2070. * get added to the execution queue in transport_execute_tasks(),
  2071. * or are added to the delayed or ordered lists here.
  2072. */
  2073. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  2074. {
  2075. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  2076. return 1;
  2077. /*
  2078. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  2079. * to allow the passed struct se_cmd list of tasks to the front of the list.
  2080. */
  2081. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2082. atomic_inc(&cmd->se_dev->dev_hoq_count);
  2083. smp_mb__after_atomic_inc();
  2084. DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
  2085. " 0x%02x, se_ordered_id: %u\n",
  2086. cmd->_task_cdb[0],
  2087. cmd->se_ordered_id);
  2088. return 1;
  2089. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2090. spin_lock(&cmd->se_dev->ordered_cmd_lock);
  2091. list_add_tail(&cmd->se_ordered_node,
  2092. &cmd->se_dev->ordered_cmd_list);
  2093. spin_unlock(&cmd->se_dev->ordered_cmd_lock);
  2094. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  2095. smp_mb__after_atomic_inc();
  2096. DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
  2097. " list, se_ordered_id: %u\n",
  2098. cmd->t_task_cdb[0],
  2099. cmd->se_ordered_id);
  2100. /*
  2101. * Add ORDERED command to tail of execution queue if
  2102. * no other older commands exist that need to be
  2103. * completed first.
  2104. */
  2105. if (!(atomic_read(&cmd->se_dev->simple_cmds)))
  2106. return 1;
  2107. } else {
  2108. /*
  2109. * For SIMPLE and UNTAGGED Task Attribute commands
  2110. */
  2111. atomic_inc(&cmd->se_dev->simple_cmds);
  2112. smp_mb__after_atomic_inc();
  2113. }
  2114. /*
  2115. * Otherwise if one or more outstanding ORDERED task attribute exist,
  2116. * add the dormant task(s) built for the passed struct se_cmd to the
  2117. * execution queue and become in Active state for this struct se_device.
  2118. */
  2119. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  2120. /*
  2121. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  2122. * will be drained upon completion of HEAD_OF_QUEUE task.
  2123. */
  2124. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  2125. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  2126. list_add_tail(&cmd->se_delayed_node,
  2127. &cmd->se_dev->delayed_cmd_list);
  2128. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  2129. DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
  2130. " delayed CMD list, se_ordered_id: %u\n",
  2131. cmd->t_task_cdb[0], cmd->sam_task_attr,
  2132. cmd->se_ordered_id);
  2133. /*
  2134. * Return zero to let transport_execute_tasks() know
  2135. * not to add the delayed tasks to the execution list.
  2136. */
  2137. return 0;
  2138. }
  2139. /*
  2140. * Otherwise, no ORDERED task attributes exist..
  2141. */
  2142. return 1;
  2143. }
  2144. /*
  2145. * Called from fabric module context in transport_generic_new_cmd() and
  2146. * transport_generic_process_write()
  2147. */
  2148. static int transport_execute_tasks(struct se_cmd *cmd)
  2149. {
  2150. int add_tasks;
  2151. if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
  2152. cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
  2153. transport_generic_request_failure(cmd, NULL, 0, 1);
  2154. return 0;
  2155. }
  2156. /*
  2157. * Call transport_cmd_check_stop() to see if a fabric exception
  2158. * has occurred that prevents execution.
  2159. */
  2160. if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
  2161. /*
  2162. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  2163. * attribute for the tasks of the received struct se_cmd CDB
  2164. */
  2165. add_tasks = transport_execute_task_attr(cmd);
  2166. if (!add_tasks)
  2167. goto execute_tasks;
  2168. /*
  2169. * This calls transport_add_tasks_from_cmd() to handle
  2170. * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
  2171. * (if enabled) in __transport_add_task_to_execute_queue() and
  2172. * transport_add_task_check_sam_attr().
  2173. */
  2174. transport_add_tasks_from_cmd(cmd);
  2175. }
  2176. /*
  2177. * Kick the execution queue for the cmd associated struct se_device
  2178. * storage object.
  2179. */
  2180. execute_tasks:
  2181. __transport_execute_tasks(cmd->se_dev);
  2182. return 0;
  2183. }
  2184. /*
  2185. * Called to check struct se_device tcq depth window, and once open pull struct se_task
  2186. * from struct se_device->execute_task_list and
  2187. *
  2188. * Called from transport_processing_thread()
  2189. */
  2190. static int __transport_execute_tasks(struct se_device *dev)
  2191. {
  2192. int error;
  2193. struct se_cmd *cmd = NULL;
  2194. struct se_task *task = NULL;
  2195. unsigned long flags;
  2196. /*
  2197. * Check if there is enough room in the device and HBA queue to send
  2198. * struct se_tasks to the selected transport.
  2199. */
  2200. check_depth:
  2201. if (!atomic_read(&dev->depth_left))
  2202. return transport_tcq_window_closed(dev);
  2203. dev->dev_tcq_window_closed = 0;
  2204. spin_lock_irq(&dev->execute_task_lock);
  2205. if (list_empty(&dev->execute_task_list)) {
  2206. spin_unlock_irq(&dev->execute_task_lock);
  2207. return 0;
  2208. }
  2209. task = list_first_entry(&dev->execute_task_list,
  2210. struct se_task, t_execute_list);
  2211. list_del(&task->t_execute_list);
  2212. atomic_set(&task->task_execute_queue, 0);
  2213. atomic_dec(&dev->execute_tasks);
  2214. spin_unlock_irq(&dev->execute_task_lock);
  2215. atomic_dec(&dev->depth_left);
  2216. cmd = task->task_se_cmd;
  2217. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2218. atomic_set(&task->task_active, 1);
  2219. atomic_set(&task->task_sent, 1);
  2220. atomic_inc(&cmd->t_task_cdbs_sent);
  2221. if (atomic_read(&cmd->t_task_cdbs_sent) ==
  2222. cmd->t_task_list_num)
  2223. atomic_set(&cmd->transport_sent, 1);
  2224. transport_start_task_timer(task);
  2225. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2226. /*
  2227. * The struct se_cmd->transport_emulate_cdb() function pointer is used
  2228. * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
  2229. * struct se_subsystem_api->do_task() caller below.
  2230. */
  2231. if (cmd->transport_emulate_cdb) {
  2232. error = cmd->transport_emulate_cdb(cmd);
  2233. if (error != 0) {
  2234. cmd->transport_error_status = error;
  2235. atomic_set(&task->task_active, 0);
  2236. atomic_set(&cmd->transport_sent, 0);
  2237. transport_stop_tasks_for_cmd(cmd);
  2238. transport_generic_request_failure(cmd, dev, 0, 1);
  2239. goto check_depth;
  2240. }
  2241. /*
  2242. * Handle the successful completion for transport_emulate_cdb()
  2243. * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
  2244. * Otherwise the caller is expected to complete the task with
  2245. * proper status.
  2246. */
  2247. if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
  2248. cmd->scsi_status = SAM_STAT_GOOD;
  2249. task->task_scsi_status = GOOD;
  2250. transport_complete_task(task, 1);
  2251. }
  2252. } else {
  2253. /*
  2254. * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
  2255. * RAMDISK we use the internal transport_emulate_control_cdb() logic
  2256. * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
  2257. * LUN emulation code.
  2258. *
  2259. * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
  2260. * call ->do_task() directly and let the underlying TCM subsystem plugin
  2261. * code handle the CDB emulation.
  2262. */
  2263. if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
  2264. (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2265. error = transport_emulate_control_cdb(task);
  2266. else
  2267. error = dev->transport->do_task(task);
  2268. if (error != 0) {
  2269. cmd->transport_error_status = error;
  2270. atomic_set(&task->task_active, 0);
  2271. atomic_set(&cmd->transport_sent, 0);
  2272. transport_stop_tasks_for_cmd(cmd);
  2273. transport_generic_request_failure(cmd, dev, 0, 1);
  2274. }
  2275. }
  2276. goto check_depth;
  2277. return 0;
  2278. }
  2279. void transport_new_cmd_failure(struct se_cmd *se_cmd)
  2280. {
  2281. unsigned long flags;
  2282. /*
  2283. * Any unsolicited data will get dumped for failed command inside of
  2284. * the fabric plugin
  2285. */
  2286. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  2287. se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
  2288. se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2289. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  2290. }
  2291. static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
  2292. static inline u32 transport_get_sectors_6(
  2293. unsigned char *cdb,
  2294. struct se_cmd *cmd,
  2295. int *ret)
  2296. {
  2297. struct se_device *dev = cmd->se_dev;
  2298. /*
  2299. * Assume TYPE_DISK for non struct se_device objects.
  2300. * Use 8-bit sector value.
  2301. */
  2302. if (!dev)
  2303. goto type_disk;
  2304. /*
  2305. * Use 24-bit allocation length for TYPE_TAPE.
  2306. */
  2307. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2308. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  2309. /*
  2310. * Everything else assume TYPE_DISK Sector CDB location.
  2311. * Use 8-bit sector value.
  2312. */
  2313. type_disk:
  2314. return (u32)cdb[4];
  2315. }
  2316. static inline u32 transport_get_sectors_10(
  2317. unsigned char *cdb,
  2318. struct se_cmd *cmd,
  2319. int *ret)
  2320. {
  2321. struct se_device *dev = cmd->se_dev;
  2322. /*
  2323. * Assume TYPE_DISK for non struct se_device objects.
  2324. * Use 16-bit sector value.
  2325. */
  2326. if (!dev)
  2327. goto type_disk;
  2328. /*
  2329. * XXX_10 is not defined in SSC, throw an exception
  2330. */
  2331. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2332. *ret = -EINVAL;
  2333. return 0;
  2334. }
  2335. /*
  2336. * Everything else assume TYPE_DISK Sector CDB location.
  2337. * Use 16-bit sector value.
  2338. */
  2339. type_disk:
  2340. return (u32)(cdb[7] << 8) + cdb[8];
  2341. }
  2342. static inline u32 transport_get_sectors_12(
  2343. unsigned char *cdb,
  2344. struct se_cmd *cmd,
  2345. int *ret)
  2346. {
  2347. struct se_device *dev = cmd->se_dev;
  2348. /*
  2349. * Assume TYPE_DISK for non struct se_device objects.
  2350. * Use 32-bit sector value.
  2351. */
  2352. if (!dev)
  2353. goto type_disk;
  2354. /*
  2355. * XXX_12 is not defined in SSC, throw an exception
  2356. */
  2357. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2358. *ret = -EINVAL;
  2359. return 0;
  2360. }
  2361. /*
  2362. * Everything else assume TYPE_DISK Sector CDB location.
  2363. * Use 32-bit sector value.
  2364. */
  2365. type_disk:
  2366. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  2367. }
  2368. static inline u32 transport_get_sectors_16(
  2369. unsigned char *cdb,
  2370. struct se_cmd *cmd,
  2371. int *ret)
  2372. {
  2373. struct se_device *dev = cmd->se_dev;
  2374. /*
  2375. * Assume TYPE_DISK for non struct se_device objects.
  2376. * Use 32-bit sector value.
  2377. */
  2378. if (!dev)
  2379. goto type_disk;
  2380. /*
  2381. * Use 24-bit allocation length for TYPE_TAPE.
  2382. */
  2383. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2384. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2385. type_disk:
  2386. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2387. (cdb[12] << 8) + cdb[13];
  2388. }
  2389. /*
  2390. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2391. */
  2392. static inline u32 transport_get_sectors_32(
  2393. unsigned char *cdb,
  2394. struct se_cmd *cmd,
  2395. int *ret)
  2396. {
  2397. /*
  2398. * Assume TYPE_DISK for non struct se_device objects.
  2399. * Use 32-bit sector value.
  2400. */
  2401. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2402. (cdb[30] << 8) + cdb[31];
  2403. }
  2404. static inline u32 transport_get_size(
  2405. u32 sectors,
  2406. unsigned char *cdb,
  2407. struct se_cmd *cmd)
  2408. {
  2409. struct se_device *dev = cmd->se_dev;
  2410. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2411. if (cdb[1] & 1) { /* sectors */
  2412. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2413. } else /* bytes */
  2414. return sectors;
  2415. }
  2416. #if 0
  2417. printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
  2418. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
  2419. dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  2420. dev->transport->name);
  2421. #endif
  2422. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2423. }
  2424. unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
  2425. {
  2426. unsigned char result = 0;
  2427. /*
  2428. * MSB
  2429. */
  2430. if ((val[0] >= 'a') && (val[0] <= 'f'))
  2431. result = ((val[0] - 'a' + 10) & 0xf) << 4;
  2432. else
  2433. if ((val[0] >= 'A') && (val[0] <= 'F'))
  2434. result = ((val[0] - 'A' + 10) & 0xf) << 4;
  2435. else /* digit */
  2436. result = ((val[0] - '0') & 0xf) << 4;
  2437. /*
  2438. * LSB
  2439. */
  2440. if ((val[1] >= 'a') && (val[1] <= 'f'))
  2441. result |= ((val[1] - 'a' + 10) & 0xf);
  2442. else
  2443. if ((val[1] >= 'A') && (val[1] <= 'F'))
  2444. result |= ((val[1] - 'A' + 10) & 0xf);
  2445. else /* digit */
  2446. result |= ((val[1] - '0') & 0xf);
  2447. return result;
  2448. }
  2449. EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
  2450. static void transport_xor_callback(struct se_cmd *cmd)
  2451. {
  2452. unsigned char *buf, *addr;
  2453. struct se_mem *se_mem;
  2454. unsigned int offset;
  2455. int i;
  2456. /*
  2457. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2458. *
  2459. * 1) read the specified logical block(s);
  2460. * 2) transfer logical blocks from the data-out buffer;
  2461. * 3) XOR the logical blocks transferred from the data-out buffer with
  2462. * the logical blocks read, storing the resulting XOR data in a buffer;
  2463. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2464. * blocks transferred from the data-out buffer; and
  2465. * 5) transfer the resulting XOR data to the data-in buffer.
  2466. */
  2467. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2468. if (!(buf)) {
  2469. printk(KERN_ERR "Unable to allocate xor_callback buf\n");
  2470. return;
  2471. }
  2472. /*
  2473. * Copy the scatterlist WRITE buffer located at cmd->t_mem_list
  2474. * into the locally allocated *buf
  2475. */
  2476. transport_memcpy_se_mem_read_contig(buf, &cmd->t_mem_list,
  2477. cmd->data_length);
  2478. /*
  2479. * Now perform the XOR against the BIDI read memory located at
  2480. * cmd->t_mem_bidi_list
  2481. */
  2482. offset = 0;
  2483. list_for_each_entry(se_mem, &cmd->t_mem_bidi_list, se_list) {
  2484. addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
  2485. if (!(addr))
  2486. goto out;
  2487. for (i = 0; i < se_mem->se_len; i++)
  2488. *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
  2489. offset += se_mem->se_len;
  2490. kunmap_atomic(addr, KM_USER0);
  2491. }
  2492. out:
  2493. kfree(buf);
  2494. }
  2495. /*
  2496. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2497. */
  2498. static int transport_get_sense_data(struct se_cmd *cmd)
  2499. {
  2500. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2501. struct se_device *dev;
  2502. struct se_task *task = NULL, *task_tmp;
  2503. unsigned long flags;
  2504. u32 offset = 0;
  2505. WARN_ON(!cmd->se_lun);
  2506. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2507. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2508. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2509. return 0;
  2510. }
  2511. list_for_each_entry_safe(task, task_tmp,
  2512. &cmd->t_task_list, t_list) {
  2513. if (!task->task_sense)
  2514. continue;
  2515. dev = task->se_dev;
  2516. if (!(dev))
  2517. continue;
  2518. if (!dev->transport->get_sense_buffer) {
  2519. printk(KERN_ERR "dev->transport->get_sense_buffer"
  2520. " is NULL\n");
  2521. continue;
  2522. }
  2523. sense_buffer = dev->transport->get_sense_buffer(task);
  2524. if (!(sense_buffer)) {
  2525. printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
  2526. " sense buffer for task with sense\n",
  2527. cmd->se_tfo->get_task_tag(cmd), task->task_no);
  2528. continue;
  2529. }
  2530. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2531. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  2532. TRANSPORT_SENSE_BUFFER);
  2533. memcpy(&buffer[offset], sense_buffer,
  2534. TRANSPORT_SENSE_BUFFER);
  2535. cmd->scsi_status = task->task_scsi_status;
  2536. /* Automatically padded */
  2537. cmd->scsi_sense_length =
  2538. (TRANSPORT_SENSE_BUFFER + offset);
  2539. printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
  2540. " and sense\n",
  2541. dev->se_hba->hba_id, dev->transport->name,
  2542. cmd->scsi_status);
  2543. return 0;
  2544. }
  2545. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2546. return -1;
  2547. }
  2548. static int transport_allocate_resources(struct se_cmd *cmd)
  2549. {
  2550. u32 length = cmd->data_length;
  2551. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  2552. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB))
  2553. return transport_generic_get_mem(cmd, length);
  2554. else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB)
  2555. return transport_generic_allocate_buf(cmd, length);
  2556. else
  2557. return 0;
  2558. }
  2559. static int
  2560. transport_handle_reservation_conflict(struct se_cmd *cmd)
  2561. {
  2562. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2563. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2564. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2565. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2566. /*
  2567. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  2568. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  2569. * CONFLICT STATUS.
  2570. *
  2571. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  2572. */
  2573. if (cmd->se_sess &&
  2574. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  2575. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  2576. cmd->orig_fe_lun, 0x2C,
  2577. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  2578. return -EINVAL;
  2579. }
  2580. /* transport_generic_cmd_sequencer():
  2581. *
  2582. * Generic Command Sequencer that should work for most DAS transport
  2583. * drivers.
  2584. *
  2585. * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
  2586. * RX Thread.
  2587. *
  2588. * FIXME: Need to support other SCSI OPCODES where as well.
  2589. */
  2590. static int transport_generic_cmd_sequencer(
  2591. struct se_cmd *cmd,
  2592. unsigned char *cdb)
  2593. {
  2594. struct se_device *dev = cmd->se_dev;
  2595. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2596. int ret = 0, sector_ret = 0, passthrough;
  2597. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2598. u16 service_action;
  2599. u8 alua_ascq = 0;
  2600. /*
  2601. * Check for an existing UNIT ATTENTION condition
  2602. */
  2603. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2604. cmd->transport_wait_for_tasks =
  2605. &transport_nop_wait_for_tasks;
  2606. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2607. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2608. return -EINVAL;
  2609. }
  2610. /*
  2611. * Check status of Asymmetric Logical Unit Assignment port
  2612. */
  2613. ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
  2614. if (ret != 0) {
  2615. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2616. /*
  2617. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  2618. * The ALUA additional sense code qualifier (ASCQ) is determined
  2619. * by the ALUA primary or secondary access state..
  2620. */
  2621. if (ret > 0) {
  2622. #if 0
  2623. printk(KERN_INFO "[%s]: ALUA TG Port not available,"
  2624. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2625. cmd->se_tfo->get_fabric_name(), alua_ascq);
  2626. #endif
  2627. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2628. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2629. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2630. return -EINVAL;
  2631. }
  2632. goto out_invalid_cdb_field;
  2633. }
  2634. /*
  2635. * Check status for SPC-3 Persistent Reservations
  2636. */
  2637. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2638. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  2639. cmd, cdb, pr_reg_type) != 0)
  2640. return transport_handle_reservation_conflict(cmd);
  2641. /*
  2642. * This means the CDB is allowed for the SCSI Initiator port
  2643. * when said port is *NOT* holding the legacy SPC-2 or
  2644. * SPC-3 Persistent Reservation.
  2645. */
  2646. }
  2647. switch (cdb[0]) {
  2648. case READ_6:
  2649. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2650. if (sector_ret)
  2651. goto out_unsupported_cdb;
  2652. size = transport_get_size(sectors, cdb, cmd);
  2653. cmd->transport_split_cdb = &split_cdb_XX_6;
  2654. cmd->t_task_lba = transport_lba_21(cdb);
  2655. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2656. break;
  2657. case READ_10:
  2658. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2659. if (sector_ret)
  2660. goto out_unsupported_cdb;
  2661. size = transport_get_size(sectors, cdb, cmd);
  2662. cmd->transport_split_cdb = &split_cdb_XX_10;
  2663. cmd->t_task_lba = transport_lba_32(cdb);
  2664. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2665. break;
  2666. case READ_12:
  2667. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2668. if (sector_ret)
  2669. goto out_unsupported_cdb;
  2670. size = transport_get_size(sectors, cdb, cmd);
  2671. cmd->transport_split_cdb = &split_cdb_XX_12;
  2672. cmd->t_task_lba = transport_lba_32(cdb);
  2673. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2674. break;
  2675. case READ_16:
  2676. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2677. if (sector_ret)
  2678. goto out_unsupported_cdb;
  2679. size = transport_get_size(sectors, cdb, cmd);
  2680. cmd->transport_split_cdb = &split_cdb_XX_16;
  2681. cmd->t_task_lba = transport_lba_64(cdb);
  2682. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2683. break;
  2684. case WRITE_6:
  2685. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2686. if (sector_ret)
  2687. goto out_unsupported_cdb;
  2688. size = transport_get_size(sectors, cdb, cmd);
  2689. cmd->transport_split_cdb = &split_cdb_XX_6;
  2690. cmd->t_task_lba = transport_lba_21(cdb);
  2691. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2692. break;
  2693. case WRITE_10:
  2694. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2695. if (sector_ret)
  2696. goto out_unsupported_cdb;
  2697. size = transport_get_size(sectors, cdb, cmd);
  2698. cmd->transport_split_cdb = &split_cdb_XX_10;
  2699. cmd->t_task_lba = transport_lba_32(cdb);
  2700. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2701. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2702. break;
  2703. case WRITE_12:
  2704. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2705. if (sector_ret)
  2706. goto out_unsupported_cdb;
  2707. size = transport_get_size(sectors, cdb, cmd);
  2708. cmd->transport_split_cdb = &split_cdb_XX_12;
  2709. cmd->t_task_lba = transport_lba_32(cdb);
  2710. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2711. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2712. break;
  2713. case WRITE_16:
  2714. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2715. if (sector_ret)
  2716. goto out_unsupported_cdb;
  2717. size = transport_get_size(sectors, cdb, cmd);
  2718. cmd->transport_split_cdb = &split_cdb_XX_16;
  2719. cmd->t_task_lba = transport_lba_64(cdb);
  2720. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2721. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2722. break;
  2723. case XDWRITEREAD_10:
  2724. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2725. !(cmd->t_tasks_bidi))
  2726. goto out_invalid_cdb_field;
  2727. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2728. if (sector_ret)
  2729. goto out_unsupported_cdb;
  2730. size = transport_get_size(sectors, cdb, cmd);
  2731. cmd->transport_split_cdb = &split_cdb_XX_10;
  2732. cmd->t_task_lba = transport_lba_32(cdb);
  2733. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2734. passthrough = (dev->transport->transport_type ==
  2735. TRANSPORT_PLUGIN_PHBA_PDEV);
  2736. /*
  2737. * Skip the remaining assignments for TCM/PSCSI passthrough
  2738. */
  2739. if (passthrough)
  2740. break;
  2741. /*
  2742. * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
  2743. */
  2744. cmd->transport_complete_callback = &transport_xor_callback;
  2745. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2746. break;
  2747. case VARIABLE_LENGTH_CMD:
  2748. service_action = get_unaligned_be16(&cdb[8]);
  2749. /*
  2750. * Determine if this is TCM/PSCSI device and we should disable
  2751. * internal emulation for this CDB.
  2752. */
  2753. passthrough = (dev->transport->transport_type ==
  2754. TRANSPORT_PLUGIN_PHBA_PDEV);
  2755. switch (service_action) {
  2756. case XDWRITEREAD_32:
  2757. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2758. if (sector_ret)
  2759. goto out_unsupported_cdb;
  2760. size = transport_get_size(sectors, cdb, cmd);
  2761. /*
  2762. * Use WRITE_32 and READ_32 opcodes for the emulated
  2763. * XDWRITE_READ_32 logic.
  2764. */
  2765. cmd->transport_split_cdb = &split_cdb_XX_32;
  2766. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2767. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2768. /*
  2769. * Skip the remaining assignments for TCM/PSCSI passthrough
  2770. */
  2771. if (passthrough)
  2772. break;
  2773. /*
  2774. * Setup BIDI XOR callback to be run during
  2775. * transport_generic_complete_ok()
  2776. */
  2777. cmd->transport_complete_callback = &transport_xor_callback;
  2778. cmd->t_tasks_fua = (cdb[10] & 0x8);
  2779. break;
  2780. case WRITE_SAME_32:
  2781. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2782. if (sector_ret)
  2783. goto out_unsupported_cdb;
  2784. if (sectors != 0)
  2785. size = transport_get_size(sectors, cdb, cmd);
  2786. else
  2787. size = dev->se_sub_dev->se_dev_attrib.block_size;
  2788. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2789. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2790. /*
  2791. * Skip the remaining assignments for TCM/PSCSI passthrough
  2792. */
  2793. if (passthrough)
  2794. break;
  2795. if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
  2796. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  2797. " bits not supported for Block Discard"
  2798. " Emulation\n");
  2799. goto out_invalid_cdb_field;
  2800. }
  2801. /*
  2802. * Currently for the emulated case we only accept
  2803. * tpws with the UNMAP=1 bit set.
  2804. */
  2805. if (!(cdb[10] & 0x08)) {
  2806. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
  2807. " supported for Block Discard Emulation\n");
  2808. goto out_invalid_cdb_field;
  2809. }
  2810. break;
  2811. default:
  2812. printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
  2813. " 0x%04x not supported\n", service_action);
  2814. goto out_unsupported_cdb;
  2815. }
  2816. break;
  2817. case MAINTENANCE_IN:
  2818. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2819. /* MAINTENANCE_IN from SCC-2 */
  2820. /*
  2821. * Check for emulated MI_REPORT_TARGET_PGS.
  2822. */
  2823. if (cdb[1] == MI_REPORT_TARGET_PGS) {
  2824. cmd->transport_emulate_cdb =
  2825. (su_dev->t10_alua.alua_type ==
  2826. SPC3_ALUA_EMULATED) ?
  2827. core_emulate_report_target_port_groups :
  2828. NULL;
  2829. }
  2830. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2831. (cdb[8] << 8) | cdb[9];
  2832. } else {
  2833. /* GPCMD_SEND_KEY from multi media commands */
  2834. size = (cdb[8] << 8) + cdb[9];
  2835. }
  2836. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2837. break;
  2838. case MODE_SELECT:
  2839. size = cdb[4];
  2840. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2841. break;
  2842. case MODE_SELECT_10:
  2843. size = (cdb[7] << 8) + cdb[8];
  2844. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2845. break;
  2846. case MODE_SENSE:
  2847. size = cdb[4];
  2848. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2849. break;
  2850. case MODE_SENSE_10:
  2851. case GPCMD_READ_BUFFER_CAPACITY:
  2852. case GPCMD_SEND_OPC:
  2853. case LOG_SELECT:
  2854. case LOG_SENSE:
  2855. size = (cdb[7] << 8) + cdb[8];
  2856. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2857. break;
  2858. case READ_BLOCK_LIMITS:
  2859. size = READ_BLOCK_LEN;
  2860. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2861. break;
  2862. case GPCMD_GET_CONFIGURATION:
  2863. case GPCMD_READ_FORMAT_CAPACITIES:
  2864. case GPCMD_READ_DISC_INFO:
  2865. case GPCMD_READ_TRACK_RZONE_INFO:
  2866. size = (cdb[7] << 8) + cdb[8];
  2867. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2868. break;
  2869. case PERSISTENT_RESERVE_IN:
  2870. case PERSISTENT_RESERVE_OUT:
  2871. cmd->transport_emulate_cdb =
  2872. (su_dev->t10_pr.res_type ==
  2873. SPC3_PERSISTENT_RESERVATIONS) ?
  2874. core_scsi3_emulate_pr : NULL;
  2875. size = (cdb[7] << 8) + cdb[8];
  2876. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2877. break;
  2878. case GPCMD_MECHANISM_STATUS:
  2879. case GPCMD_READ_DVD_STRUCTURE:
  2880. size = (cdb[8] << 8) + cdb[9];
  2881. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2882. break;
  2883. case READ_POSITION:
  2884. size = READ_POSITION_LEN;
  2885. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2886. break;
  2887. case MAINTENANCE_OUT:
  2888. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2889. /* MAINTENANCE_OUT from SCC-2
  2890. *
  2891. * Check for emulated MO_SET_TARGET_PGS.
  2892. */
  2893. if (cdb[1] == MO_SET_TARGET_PGS) {
  2894. cmd->transport_emulate_cdb =
  2895. (su_dev->t10_alua.alua_type ==
  2896. SPC3_ALUA_EMULATED) ?
  2897. core_emulate_set_target_port_groups :
  2898. NULL;
  2899. }
  2900. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2901. (cdb[8] << 8) | cdb[9];
  2902. } else {
  2903. /* GPCMD_REPORT_KEY from multi media commands */
  2904. size = (cdb[8] << 8) + cdb[9];
  2905. }
  2906. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2907. break;
  2908. case INQUIRY:
  2909. size = (cdb[3] << 8) + cdb[4];
  2910. /*
  2911. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2912. * See spc4r17 section 5.3
  2913. */
  2914. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2915. cmd->sam_task_attr = MSG_HEAD_TAG;
  2916. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2917. break;
  2918. case READ_BUFFER:
  2919. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2920. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2921. break;
  2922. case READ_CAPACITY:
  2923. size = READ_CAP_LEN;
  2924. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2925. break;
  2926. case READ_MEDIA_SERIAL_NUMBER:
  2927. case SECURITY_PROTOCOL_IN:
  2928. case SECURITY_PROTOCOL_OUT:
  2929. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2930. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2931. break;
  2932. case SERVICE_ACTION_IN:
  2933. case ACCESS_CONTROL_IN:
  2934. case ACCESS_CONTROL_OUT:
  2935. case EXTENDED_COPY:
  2936. case READ_ATTRIBUTE:
  2937. case RECEIVE_COPY_RESULTS:
  2938. case WRITE_ATTRIBUTE:
  2939. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2940. (cdb[12] << 8) | cdb[13];
  2941. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2942. break;
  2943. case RECEIVE_DIAGNOSTIC:
  2944. case SEND_DIAGNOSTIC:
  2945. size = (cdb[3] << 8) | cdb[4];
  2946. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2947. break;
  2948. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2949. #if 0
  2950. case GPCMD_READ_CD:
  2951. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2952. size = (2336 * sectors);
  2953. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2954. break;
  2955. #endif
  2956. case READ_TOC:
  2957. size = cdb[8];
  2958. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2959. break;
  2960. case REQUEST_SENSE:
  2961. size = cdb[4];
  2962. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2963. break;
  2964. case READ_ELEMENT_STATUS:
  2965. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2966. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2967. break;
  2968. case WRITE_BUFFER:
  2969. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2970. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2971. break;
  2972. case RESERVE:
  2973. case RESERVE_10:
  2974. /*
  2975. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2976. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2977. */
  2978. if (cdb[0] == RESERVE_10)
  2979. size = (cdb[7] << 8) | cdb[8];
  2980. else
  2981. size = cmd->data_length;
  2982. /*
  2983. * Setup the legacy emulated handler for SPC-2 and
  2984. * >= SPC-3 compatible reservation handling (CRH=1)
  2985. * Otherwise, we assume the underlying SCSI logic is
  2986. * is running in SPC_PASSTHROUGH, and wants reservations
  2987. * emulation disabled.
  2988. */
  2989. cmd->transport_emulate_cdb =
  2990. (su_dev->t10_pr.res_type !=
  2991. SPC_PASSTHROUGH) ?
  2992. core_scsi2_emulate_crh : NULL;
  2993. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2994. break;
  2995. case RELEASE:
  2996. case RELEASE_10:
  2997. /*
  2998. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  2999. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3000. */
  3001. if (cdb[0] == RELEASE_10)
  3002. size = (cdb[7] << 8) | cdb[8];
  3003. else
  3004. size = cmd->data_length;
  3005. cmd->transport_emulate_cdb =
  3006. (su_dev->t10_pr.res_type !=
  3007. SPC_PASSTHROUGH) ?
  3008. core_scsi2_emulate_crh : NULL;
  3009. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3010. break;
  3011. case SYNCHRONIZE_CACHE:
  3012. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  3013. /*
  3014. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  3015. */
  3016. if (cdb[0] == SYNCHRONIZE_CACHE) {
  3017. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  3018. cmd->t_task_lba = transport_lba_32(cdb);
  3019. } else {
  3020. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3021. cmd->t_task_lba = transport_lba_64(cdb);
  3022. }
  3023. if (sector_ret)
  3024. goto out_unsupported_cdb;
  3025. size = transport_get_size(sectors, cdb, cmd);
  3026. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3027. /*
  3028. * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
  3029. */
  3030. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  3031. break;
  3032. /*
  3033. * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
  3034. * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
  3035. */
  3036. cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
  3037. /*
  3038. * Check to ensure that LBA + Range does not exceed past end of
  3039. * device.
  3040. */
  3041. if (!transport_cmd_get_valid_sectors(cmd))
  3042. goto out_invalid_cdb_field;
  3043. break;
  3044. case UNMAP:
  3045. size = get_unaligned_be16(&cdb[7]);
  3046. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3047. break;
  3048. case WRITE_SAME_16:
  3049. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3050. if (sector_ret)
  3051. goto out_unsupported_cdb;
  3052. if (sectors != 0)
  3053. size = transport_get_size(sectors, cdb, cmd);
  3054. else
  3055. size = dev->se_sub_dev->se_dev_attrib.block_size;
  3056. cmd->t_task_lba = get_unaligned_be16(&cdb[2]);
  3057. passthrough = (dev->transport->transport_type ==
  3058. TRANSPORT_PLUGIN_PHBA_PDEV);
  3059. /*
  3060. * Determine if the received WRITE_SAME_16 is used to for direct
  3061. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  3062. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  3063. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
  3064. * TCM/FILEIO subsystem plugin backstores.
  3065. */
  3066. if (!(passthrough)) {
  3067. if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
  3068. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  3069. " bits not supported for Block Discard"
  3070. " Emulation\n");
  3071. goto out_invalid_cdb_field;
  3072. }
  3073. /*
  3074. * Currently for the emulated case we only accept
  3075. * tpws with the UNMAP=1 bit set.
  3076. */
  3077. if (!(cdb[1] & 0x08)) {
  3078. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
  3079. " supported for Block Discard Emulation\n");
  3080. goto out_invalid_cdb_field;
  3081. }
  3082. }
  3083. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3084. break;
  3085. case ALLOW_MEDIUM_REMOVAL:
  3086. case GPCMD_CLOSE_TRACK:
  3087. case ERASE:
  3088. case INITIALIZE_ELEMENT_STATUS:
  3089. case GPCMD_LOAD_UNLOAD:
  3090. case REZERO_UNIT:
  3091. case SEEK_10:
  3092. case GPCMD_SET_SPEED:
  3093. case SPACE:
  3094. case START_STOP:
  3095. case TEST_UNIT_READY:
  3096. case VERIFY:
  3097. case WRITE_FILEMARKS:
  3098. case MOVE_MEDIUM:
  3099. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3100. break;
  3101. case REPORT_LUNS:
  3102. cmd->transport_emulate_cdb =
  3103. transport_core_report_lun_response;
  3104. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  3105. /*
  3106. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  3107. * See spc4r17 section 5.3
  3108. */
  3109. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3110. cmd->sam_task_attr = MSG_HEAD_TAG;
  3111. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3112. break;
  3113. default:
  3114. printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
  3115. " 0x%02x, sending CHECK_CONDITION.\n",
  3116. cmd->se_tfo->get_fabric_name(), cdb[0]);
  3117. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  3118. goto out_unsupported_cdb;
  3119. }
  3120. if (size != cmd->data_length) {
  3121. printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
  3122. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  3123. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  3124. cmd->data_length, size, cdb[0]);
  3125. cmd->cmd_spdtl = size;
  3126. if (cmd->data_direction == DMA_TO_DEVICE) {
  3127. printk(KERN_ERR "Rejecting underflow/overflow"
  3128. " WRITE data\n");
  3129. goto out_invalid_cdb_field;
  3130. }
  3131. /*
  3132. * Reject READ_* or WRITE_* with overflow/underflow for
  3133. * type SCF_SCSI_DATA_SG_IO_CDB.
  3134. */
  3135. if (!(ret) && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
  3136. printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
  3137. " CDB on non 512-byte sector setup subsystem"
  3138. " plugin: %s\n", dev->transport->name);
  3139. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  3140. goto out_invalid_cdb_field;
  3141. }
  3142. if (size > cmd->data_length) {
  3143. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  3144. cmd->residual_count = (size - cmd->data_length);
  3145. } else {
  3146. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  3147. cmd->residual_count = (cmd->data_length - size);
  3148. }
  3149. cmd->data_length = size;
  3150. }
  3151. transport_set_supported_SAM_opcode(cmd);
  3152. return ret;
  3153. out_unsupported_cdb:
  3154. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3155. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  3156. return -EINVAL;
  3157. out_invalid_cdb_field:
  3158. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3159. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3160. return -EINVAL;
  3161. }
  3162. static inline void transport_release_tasks(struct se_cmd *);
  3163. static void transport_memcpy_se_mem_read_contig(
  3164. unsigned char *dst,
  3165. struct list_head *se_mem_list,
  3166. u32 tot_len)
  3167. {
  3168. struct se_mem *se_mem;
  3169. void *src;
  3170. u32 length;
  3171. list_for_each_entry(se_mem, se_mem_list, se_list) {
  3172. length = min_t(u32, se_mem->se_len, tot_len);
  3173. src = page_address(se_mem->se_page) + se_mem->se_off;
  3174. memcpy(dst, src, length);
  3175. tot_len -= length;
  3176. if (!tot_len)
  3177. break;
  3178. dst += length;
  3179. }
  3180. }
  3181. /*
  3182. * Called from transport_generic_complete_ok() and
  3183. * transport_generic_request_failure() to determine which dormant/delayed
  3184. * and ordered cmds need to have their tasks added to the execution queue.
  3185. */
  3186. static void transport_complete_task_attr(struct se_cmd *cmd)
  3187. {
  3188. struct se_device *dev = cmd->se_dev;
  3189. struct se_cmd *cmd_p, *cmd_tmp;
  3190. int new_active_tasks = 0;
  3191. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  3192. atomic_dec(&dev->simple_cmds);
  3193. smp_mb__after_atomic_dec();
  3194. dev->dev_cur_ordered_id++;
  3195. DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
  3196. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  3197. cmd->se_ordered_id);
  3198. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  3199. atomic_dec(&dev->dev_hoq_count);
  3200. smp_mb__after_atomic_dec();
  3201. dev->dev_cur_ordered_id++;
  3202. DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
  3203. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  3204. cmd->se_ordered_id);
  3205. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  3206. spin_lock(&dev->ordered_cmd_lock);
  3207. list_del(&cmd->se_ordered_node);
  3208. atomic_dec(&dev->dev_ordered_sync);
  3209. smp_mb__after_atomic_dec();
  3210. spin_unlock(&dev->ordered_cmd_lock);
  3211. dev->dev_cur_ordered_id++;
  3212. DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
  3213. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  3214. }
  3215. /*
  3216. * Process all commands up to the last received
  3217. * ORDERED task attribute which requires another blocking
  3218. * boundary
  3219. */
  3220. spin_lock(&dev->delayed_cmd_lock);
  3221. list_for_each_entry_safe(cmd_p, cmd_tmp,
  3222. &dev->delayed_cmd_list, se_delayed_node) {
  3223. list_del(&cmd_p->se_delayed_node);
  3224. spin_unlock(&dev->delayed_cmd_lock);
  3225. DEBUG_STA("Calling add_tasks() for"
  3226. " cmd_p: 0x%02x Task Attr: 0x%02x"
  3227. " Dormant -> Active, se_ordered_id: %u\n",
  3228. T_TASK(cmd_p)->t_task_cdb[0],
  3229. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  3230. transport_add_tasks_from_cmd(cmd_p);
  3231. new_active_tasks++;
  3232. spin_lock(&dev->delayed_cmd_lock);
  3233. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  3234. break;
  3235. }
  3236. spin_unlock(&dev->delayed_cmd_lock);
  3237. /*
  3238. * If new tasks have become active, wake up the transport thread
  3239. * to do the processing of the Active tasks.
  3240. */
  3241. if (new_active_tasks != 0)
  3242. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  3243. }
  3244. static void transport_generic_complete_ok(struct se_cmd *cmd)
  3245. {
  3246. int reason = 0;
  3247. /*
  3248. * Check if we need to move delayed/dormant tasks from cmds on the
  3249. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  3250. * Attribute.
  3251. */
  3252. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3253. transport_complete_task_attr(cmd);
  3254. /*
  3255. * Check if we need to retrieve a sense buffer from
  3256. * the struct se_cmd in question.
  3257. */
  3258. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  3259. if (transport_get_sense_data(cmd) < 0)
  3260. reason = TCM_NON_EXISTENT_LUN;
  3261. /*
  3262. * Only set when an struct se_task->task_scsi_status returned
  3263. * a non GOOD status.
  3264. */
  3265. if (cmd->scsi_status) {
  3266. transport_send_check_condition_and_sense(
  3267. cmd, reason, 1);
  3268. transport_lun_remove_cmd(cmd);
  3269. transport_cmd_check_stop_to_fabric(cmd);
  3270. return;
  3271. }
  3272. }
  3273. /*
  3274. * Check for a callback, used by amongst other things
  3275. * XDWRITE_READ_10 emulation.
  3276. */
  3277. if (cmd->transport_complete_callback)
  3278. cmd->transport_complete_callback(cmd);
  3279. switch (cmd->data_direction) {
  3280. case DMA_FROM_DEVICE:
  3281. spin_lock(&cmd->se_lun->lun_sep_lock);
  3282. if (cmd->se_lun->lun_sep) {
  3283. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3284. cmd->data_length;
  3285. }
  3286. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3287. /*
  3288. * If enabled by TCM fabric module pre-registered SGL
  3289. * memory, perform the memcpy() from the TCM internal
  3290. * contiguous buffer back to the original SGL.
  3291. */
  3292. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
  3293. sg_copy_from_buffer(cmd->t_task_pt_sgl,
  3294. cmd->t_task_pt_sgl_num,
  3295. cmd->t_task_buf,
  3296. cmd->data_length);
  3297. cmd->se_tfo->queue_data_in(cmd);
  3298. break;
  3299. case DMA_TO_DEVICE:
  3300. spin_lock(&cmd->se_lun->lun_sep_lock);
  3301. if (cmd->se_lun->lun_sep) {
  3302. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  3303. cmd->data_length;
  3304. }
  3305. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3306. /*
  3307. * Check if we need to send READ payload for BIDI-COMMAND
  3308. */
  3309. if (!list_empty(&cmd->t_mem_bidi_list)) {
  3310. spin_lock(&cmd->se_lun->lun_sep_lock);
  3311. if (cmd->se_lun->lun_sep) {
  3312. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3313. cmd->data_length;
  3314. }
  3315. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3316. cmd->se_tfo->queue_data_in(cmd);
  3317. break;
  3318. }
  3319. /* Fall through for DMA_TO_DEVICE */
  3320. case DMA_NONE:
  3321. cmd->se_tfo->queue_status(cmd);
  3322. break;
  3323. default:
  3324. break;
  3325. }
  3326. transport_lun_remove_cmd(cmd);
  3327. transport_cmd_check_stop_to_fabric(cmd);
  3328. }
  3329. static void transport_free_dev_tasks(struct se_cmd *cmd)
  3330. {
  3331. struct se_task *task, *task_tmp;
  3332. unsigned long flags;
  3333. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3334. list_for_each_entry_safe(task, task_tmp,
  3335. &cmd->t_task_list, t_list) {
  3336. if (atomic_read(&task->task_active))
  3337. continue;
  3338. kfree(task->task_sg_bidi);
  3339. kfree(task->task_sg);
  3340. list_del(&task->t_list);
  3341. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3342. if (task->se_dev)
  3343. task->se_dev->transport->free_task(task);
  3344. else
  3345. printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
  3346. task->task_no);
  3347. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3348. }
  3349. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3350. }
  3351. static inline void transport_free_pages(struct se_cmd *cmd)
  3352. {
  3353. struct se_mem *se_mem, *se_mem_tmp;
  3354. int free_page = 1;
  3355. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3356. free_page = 0;
  3357. if (cmd->se_dev->transport->do_se_mem_map)
  3358. free_page = 0;
  3359. if (cmd->t_task_buf) {
  3360. kfree(cmd->t_task_buf);
  3361. cmd->t_task_buf = NULL;
  3362. return;
  3363. }
  3364. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3365. &cmd->t_mem_list, se_list) {
  3366. /*
  3367. * We only release call __free_page(struct se_mem->se_page) when
  3368. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3369. */
  3370. if (free_page)
  3371. __free_page(se_mem->se_page);
  3372. list_del(&se_mem->se_list);
  3373. kmem_cache_free(se_mem_cache, se_mem);
  3374. }
  3375. cmd->t_tasks_se_num = 0;
  3376. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3377. &cmd->t_mem_bidi_list, se_list) {
  3378. /*
  3379. * We only release call __free_page(struct se_mem->se_page) when
  3380. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3381. */
  3382. if (free_page)
  3383. __free_page(se_mem->se_page);
  3384. list_del(&se_mem->se_list);
  3385. kmem_cache_free(se_mem_cache, se_mem);
  3386. }
  3387. cmd->t_tasks_se_bidi_num = 0;
  3388. }
  3389. static inline void transport_release_tasks(struct se_cmd *cmd)
  3390. {
  3391. transport_free_dev_tasks(cmd);
  3392. }
  3393. static inline int transport_dec_and_check(struct se_cmd *cmd)
  3394. {
  3395. unsigned long flags;
  3396. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3397. if (atomic_read(&cmd->t_fe_count)) {
  3398. if (!(atomic_dec_and_test(&cmd->t_fe_count))) {
  3399. spin_unlock_irqrestore(&cmd->t_state_lock,
  3400. flags);
  3401. return 1;
  3402. }
  3403. }
  3404. if (atomic_read(&cmd->t_se_count)) {
  3405. if (!(atomic_dec_and_test(&cmd->t_se_count))) {
  3406. spin_unlock_irqrestore(&cmd->t_state_lock,
  3407. flags);
  3408. return 1;
  3409. }
  3410. }
  3411. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3412. return 0;
  3413. }
  3414. static void transport_release_fe_cmd(struct se_cmd *cmd)
  3415. {
  3416. unsigned long flags;
  3417. if (transport_dec_and_check(cmd))
  3418. return;
  3419. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3420. if (!(atomic_read(&cmd->transport_dev_active))) {
  3421. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3422. goto free_pages;
  3423. }
  3424. atomic_set(&cmd->transport_dev_active, 0);
  3425. transport_all_task_dev_remove_state(cmd);
  3426. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3427. transport_release_tasks(cmd);
  3428. free_pages:
  3429. transport_free_pages(cmd);
  3430. transport_free_se_cmd(cmd);
  3431. cmd->se_tfo->release_cmd_direct(cmd);
  3432. }
  3433. static int transport_generic_remove(
  3434. struct se_cmd *cmd,
  3435. int release_to_pool,
  3436. int session_reinstatement)
  3437. {
  3438. unsigned long flags;
  3439. if (transport_dec_and_check(cmd)) {
  3440. if (session_reinstatement) {
  3441. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3442. transport_all_task_dev_remove_state(cmd);
  3443. spin_unlock_irqrestore(&cmd->t_state_lock,
  3444. flags);
  3445. }
  3446. return 1;
  3447. }
  3448. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3449. if (!(atomic_read(&cmd->transport_dev_active))) {
  3450. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3451. goto free_pages;
  3452. }
  3453. atomic_set(&cmd->transport_dev_active, 0);
  3454. transport_all_task_dev_remove_state(cmd);
  3455. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3456. transport_release_tasks(cmd);
  3457. free_pages:
  3458. transport_free_pages(cmd);
  3459. if (release_to_pool) {
  3460. transport_release_cmd_to_pool(cmd);
  3461. } else {
  3462. transport_free_se_cmd(cmd);
  3463. cmd->se_tfo->release_cmd_direct(cmd);
  3464. }
  3465. return 0;
  3466. }
  3467. /*
  3468. * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
  3469. * @cmd: Associated se_cmd descriptor
  3470. * @mem: SGL style memory for TCM WRITE / READ
  3471. * @sg_mem_num: Number of SGL elements
  3472. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3473. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3474. *
  3475. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3476. * of parameters.
  3477. */
  3478. int transport_generic_map_mem_to_cmd(
  3479. struct se_cmd *cmd,
  3480. struct scatterlist *sgl,
  3481. u32 sgl_count,
  3482. struct scatterlist *sgl_bidi,
  3483. u32 sgl_bidi_count)
  3484. {
  3485. int ret;
  3486. if (!sgl || !sgl_count)
  3487. return 0;
  3488. /*
  3489. * Convert sgls (sgl, sgl_bidi) to list of se_mems
  3490. */
  3491. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3492. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3493. /*
  3494. * For CDB using TCM struct se_mem linked list scatterlist memory
  3495. * processed into a TCM struct se_subsystem_dev, we do the mapping
  3496. * from the passed physical memory to struct se_mem->se_page here.
  3497. */
  3498. ret = transport_map_sg_to_mem(cmd, &cmd->t_mem_list, sgl);
  3499. if (ret < 0)
  3500. return -ENOMEM;
  3501. cmd->t_tasks_se_num = ret;
  3502. /*
  3503. * Setup BIDI READ list of struct se_mem elements
  3504. */
  3505. if (sgl_bidi && sgl_bidi_count) {
  3506. ret = transport_map_sg_to_mem(cmd, &cmd->t_mem_bidi_list, sgl_bidi);
  3507. if (ret < 0)
  3508. return -ENOMEM;
  3509. cmd->t_tasks_se_bidi_num = ret;
  3510. }
  3511. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3512. } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
  3513. if (sgl_bidi || sgl_bidi_count) {
  3514. printk(KERN_ERR "BIDI-Commands not supported using "
  3515. "SCF_SCSI_CONTROL_NONSG_IO_CDB\n");
  3516. return -ENOSYS;
  3517. }
  3518. /*
  3519. * For incoming CDBs using a contiguous buffer internal with TCM,
  3520. * save the passed struct scatterlist memory. After TCM storage object
  3521. * processing has completed for this struct se_cmd, TCM core will call
  3522. * transport_memcpy_[write,read]_contig() as necessary from
  3523. * transport_generic_complete_ok() and transport_write_pending() in order
  3524. * to copy the TCM buffer to/from the original passed *mem in SGL ->
  3525. * struct scatterlist format.
  3526. */
  3527. cmd->se_cmd_flags |= SCF_PASSTHROUGH_CONTIG_TO_SG;
  3528. cmd->t_task_pt_sgl = sgl;
  3529. cmd->t_task_pt_sgl_num = sgl_count;
  3530. }
  3531. return 0;
  3532. }
  3533. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3534. static inline long long transport_dev_end_lba(struct se_device *dev)
  3535. {
  3536. return dev->transport->get_blocks(dev) + 1;
  3537. }
  3538. static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
  3539. {
  3540. struct se_device *dev = cmd->se_dev;
  3541. u32 sectors;
  3542. if (dev->transport->get_device_type(dev) != TYPE_DISK)
  3543. return 0;
  3544. sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
  3545. if ((cmd->t_task_lba + sectors) >
  3546. transport_dev_end_lba(dev)) {
  3547. printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
  3548. " transport_dev_end_lba(): %llu\n",
  3549. cmd->t_task_lba, sectors,
  3550. transport_dev_end_lba(dev));
  3551. return 0;
  3552. }
  3553. return sectors;
  3554. }
  3555. static int transport_new_cmd_obj(struct se_cmd *cmd)
  3556. {
  3557. struct se_device *dev = cmd->se_dev;
  3558. u32 task_cdbs;
  3559. u32 rc;
  3560. if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
  3561. task_cdbs = 1;
  3562. cmd->t_task_list_num = 1;
  3563. } else {
  3564. int set_counts = 1;
  3565. /*
  3566. * Setup any BIDI READ tasks and memory from
  3567. * cmd->t_mem_bidi_list so the READ struct se_tasks
  3568. * are queued first for the non pSCSI passthrough case.
  3569. */
  3570. if (!list_empty(&cmd->t_mem_bidi_list) &&
  3571. (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3572. rc = transport_allocate_tasks(cmd,
  3573. cmd->t_task_lba,
  3574. transport_cmd_get_valid_sectors(cmd),
  3575. DMA_FROM_DEVICE, &cmd->t_mem_bidi_list,
  3576. set_counts);
  3577. if (!(rc)) {
  3578. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3579. cmd->scsi_sense_reason =
  3580. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3581. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3582. }
  3583. set_counts = 0;
  3584. }
  3585. /*
  3586. * Setup the tasks and memory from cmd->t_mem_list
  3587. * Note for BIDI transfers this will contain the WRITE payload
  3588. */
  3589. task_cdbs = transport_allocate_tasks(cmd,
  3590. cmd->t_task_lba,
  3591. transport_cmd_get_valid_sectors(cmd),
  3592. cmd->data_direction, &cmd->t_mem_list,
  3593. set_counts);
  3594. if (!(task_cdbs)) {
  3595. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3596. cmd->scsi_sense_reason =
  3597. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3598. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3599. }
  3600. cmd->t_task_list_num = task_cdbs;
  3601. #if 0
  3602. printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
  3603. " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
  3604. cmd->t_task_lba, cmd->t_tasks_sectors,
  3605. cmd->t_task_cdbs);
  3606. #endif
  3607. }
  3608. atomic_set(&cmd->t_task_cdbs_left, task_cdbs);
  3609. atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs);
  3610. atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs);
  3611. return 0;
  3612. }
  3613. static int
  3614. transport_generic_get_mem(struct se_cmd *cmd, u32 length)
  3615. {
  3616. struct se_mem *se_mem;
  3617. /*
  3618. * If the device uses memory mapping this is enough.
  3619. */
  3620. if (cmd->se_dev->transport->do_se_mem_map)
  3621. return 0;
  3622. while (length) {
  3623. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  3624. if (!(se_mem)) {
  3625. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  3626. goto out;
  3627. }
  3628. /* #warning FIXME Allocate contigous pages for struct se_mem elements */
  3629. se_mem->se_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
  3630. if (!(se_mem->se_page)) {
  3631. printk(KERN_ERR "alloc_pages() failed\n");
  3632. goto out;
  3633. }
  3634. INIT_LIST_HEAD(&se_mem->se_list);
  3635. se_mem->se_len = min_t(u32, length, PAGE_SIZE);
  3636. list_add_tail(&se_mem->se_list, &cmd->t_mem_list);
  3637. cmd->t_tasks_se_num++;
  3638. DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
  3639. " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
  3640. se_mem->se_off);
  3641. length -= se_mem->se_len;
  3642. }
  3643. DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
  3644. cmd->t_tasks_se_num);
  3645. return 0;
  3646. out:
  3647. if (se_mem)
  3648. __free_pages(se_mem->se_page, 0);
  3649. kmem_cache_free(se_mem_cache, se_mem);
  3650. return -ENOMEM;
  3651. }
  3652. int transport_init_task_sg(
  3653. struct se_task *task,
  3654. struct se_mem *in_se_mem,
  3655. u32 task_offset)
  3656. {
  3657. struct se_cmd *se_cmd = task->task_se_cmd;
  3658. struct se_device *se_dev = se_cmd->se_dev;
  3659. struct se_mem *se_mem = in_se_mem;
  3660. struct target_core_fabric_ops *tfo = se_cmd->se_tfo;
  3661. u32 sg_length, task_size = task->task_size, task_sg_num_padded;
  3662. while (task_size != 0) {
  3663. DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
  3664. " se_mem->se_off(%u) task_offset(%u)\n",
  3665. se_mem->se_page, se_mem->se_len,
  3666. se_mem->se_off, task_offset);
  3667. if (task_offset == 0) {
  3668. if (task_size >= se_mem->se_len) {
  3669. sg_length = se_mem->se_len;
  3670. if (!(list_is_last(&se_mem->se_list,
  3671. &se_cmd->t_mem_list)))
  3672. se_mem = list_entry(se_mem->se_list.next,
  3673. struct se_mem, se_list);
  3674. } else {
  3675. sg_length = task_size;
  3676. task_size -= sg_length;
  3677. goto next;
  3678. }
  3679. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3680. sg_length, task_size);
  3681. } else {
  3682. if ((se_mem->se_len - task_offset) > task_size) {
  3683. sg_length = task_size;
  3684. task_size -= sg_length;
  3685. goto next;
  3686. } else {
  3687. sg_length = (se_mem->se_len - task_offset);
  3688. if (!(list_is_last(&se_mem->se_list,
  3689. &se_cmd->t_mem_list)))
  3690. se_mem = list_entry(se_mem->se_list.next,
  3691. struct se_mem, se_list);
  3692. }
  3693. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3694. sg_length, task_size);
  3695. task_offset = 0;
  3696. }
  3697. task_size -= sg_length;
  3698. next:
  3699. DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
  3700. task->task_no, task_size);
  3701. task->task_sg_num++;
  3702. }
  3703. /*
  3704. * Check if the fabric module driver is requesting that all
  3705. * struct se_task->task_sg[] be chained together.. If so,
  3706. * then allocate an extra padding SG entry for linking and
  3707. * marking the end of the chained SGL.
  3708. */
  3709. if (tfo->task_sg_chaining) {
  3710. task_sg_num_padded = (task->task_sg_num + 1);
  3711. task->task_padded_sg = 1;
  3712. } else
  3713. task_sg_num_padded = task->task_sg_num;
  3714. task->task_sg = kzalloc(task_sg_num_padded *
  3715. sizeof(struct scatterlist), GFP_KERNEL);
  3716. if (!(task->task_sg)) {
  3717. printk(KERN_ERR "Unable to allocate memory for"
  3718. " task->task_sg\n");
  3719. return -ENOMEM;
  3720. }
  3721. sg_init_table(&task->task_sg[0], task_sg_num_padded);
  3722. /*
  3723. * Setup task->task_sg_bidi for SCSI READ payload for
  3724. * TCM/pSCSI passthrough if present for BIDI-COMMAND
  3725. */
  3726. if (!list_empty(&se_cmd->t_mem_bidi_list) &&
  3727. (se_dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3728. task->task_sg_bidi = kzalloc(task_sg_num_padded *
  3729. sizeof(struct scatterlist), GFP_KERNEL);
  3730. if (!(task->task_sg_bidi)) {
  3731. kfree(task->task_sg);
  3732. task->task_sg = NULL;
  3733. printk(KERN_ERR "Unable to allocate memory for"
  3734. " task->task_sg_bidi\n");
  3735. return -ENOMEM;
  3736. }
  3737. sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
  3738. }
  3739. /*
  3740. * For the chaining case, setup the proper end of SGL for the
  3741. * initial submission struct task into struct se_subsystem_api.
  3742. * This will be cleared later by transport_do_task_sg_chain()
  3743. */
  3744. if (task->task_padded_sg) {
  3745. sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
  3746. /*
  3747. * Added the 'if' check before marking end of bi-directional
  3748. * scatterlist (which gets created only in case of request
  3749. * (RD + WR).
  3750. */
  3751. if (task->task_sg_bidi)
  3752. sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
  3753. }
  3754. DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
  3755. " task_sg_num_padded(%u)\n", task->task_sg_num,
  3756. task_sg_num_padded);
  3757. return task->task_sg_num;
  3758. }
  3759. /* Reduce sectors if they are too long for the device */
  3760. static inline sector_t transport_limit_task_sectors(
  3761. struct se_device *dev,
  3762. unsigned long long lba,
  3763. sector_t sectors)
  3764. {
  3765. sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
  3766. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  3767. if ((lba + sectors) > transport_dev_end_lba(dev))
  3768. sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  3769. return sectors;
  3770. }
  3771. /*
  3772. * Convert a sgl into a linked list of se_mems.
  3773. */
  3774. static int transport_map_sg_to_mem(
  3775. struct se_cmd *cmd,
  3776. struct list_head *se_mem_list,
  3777. struct scatterlist *sg)
  3778. {
  3779. struct se_mem *se_mem;
  3780. u32 cmd_size = cmd->data_length;
  3781. int sg_count = 0;
  3782. WARN_ON(!sg);
  3783. while (cmd_size) {
  3784. /*
  3785. * NOTE: it is safe to return -ENOMEM at any time in creating this
  3786. * list because transport_free_pages() will eventually be called, and is
  3787. * smart enough to deallocate all list items for sg and sg_bidi lists.
  3788. */
  3789. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  3790. if (!(se_mem)) {
  3791. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  3792. return -ENOMEM;
  3793. }
  3794. INIT_LIST_HEAD(&se_mem->se_list);
  3795. DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
  3796. " sg_page: %p offset: %d length: %d\n", cmd_size,
  3797. sg_page(sg), sg->offset, sg->length);
  3798. se_mem->se_page = sg_page(sg);
  3799. se_mem->se_off = sg->offset;
  3800. if (cmd_size > sg->length) {
  3801. se_mem->se_len = sg->length;
  3802. sg = sg_next(sg);
  3803. } else
  3804. se_mem->se_len = cmd_size;
  3805. cmd_size -= se_mem->se_len;
  3806. sg_count++;
  3807. DEBUG_MEM("sg_to_mem: sg_count: %u cmd_size: %u\n",
  3808. sg_count, cmd_size);
  3809. DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
  3810. se_mem->se_page, se_mem->se_off, se_mem->se_len);
  3811. list_add_tail(&se_mem->se_list, se_mem_list);
  3812. }
  3813. DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments\n", sg_count);
  3814. return sg_count;
  3815. }
  3816. /* transport_map_mem_to_sg():
  3817. *
  3818. *
  3819. */
  3820. int transport_map_mem_to_sg(
  3821. struct se_task *task,
  3822. struct list_head *se_mem_list,
  3823. struct scatterlist *sg,
  3824. struct se_mem *in_se_mem,
  3825. struct se_mem **out_se_mem,
  3826. u32 *se_mem_cnt,
  3827. u32 *task_offset)
  3828. {
  3829. struct se_cmd *se_cmd = task->task_se_cmd;
  3830. struct se_mem *se_mem = in_se_mem;
  3831. u32 task_size = task->task_size, sg_no = 0;
  3832. if (!sg) {
  3833. printk(KERN_ERR "Unable to locate valid struct"
  3834. " scatterlist pointer\n");
  3835. return -EINVAL;
  3836. }
  3837. while (task_size != 0) {
  3838. /*
  3839. * Setup the contiguous array of scatterlists for
  3840. * this struct se_task.
  3841. */
  3842. sg_assign_page(sg, se_mem->se_page);
  3843. if (*task_offset == 0) {
  3844. sg->offset = se_mem->se_off;
  3845. if (task_size >= se_mem->se_len) {
  3846. sg->length = se_mem->se_len;
  3847. if (!(list_is_last(&se_mem->se_list,
  3848. &se_cmd->t_mem_list))) {
  3849. se_mem = list_entry(se_mem->se_list.next,
  3850. struct se_mem, se_list);
  3851. (*se_mem_cnt)++;
  3852. }
  3853. } else {
  3854. sg->length = task_size;
  3855. /*
  3856. * Determine if we need to calculate an offset
  3857. * into the struct se_mem on the next go around..
  3858. */
  3859. task_size -= sg->length;
  3860. if (!(task_size))
  3861. *task_offset = sg->length;
  3862. goto next;
  3863. }
  3864. } else {
  3865. sg->offset = (*task_offset + se_mem->se_off);
  3866. if ((se_mem->se_len - *task_offset) > task_size) {
  3867. sg->length = task_size;
  3868. /*
  3869. * Determine if we need to calculate an offset
  3870. * into the struct se_mem on the next go around..
  3871. */
  3872. task_size -= sg->length;
  3873. if (!(task_size))
  3874. *task_offset += sg->length;
  3875. goto next;
  3876. } else {
  3877. sg->length = (se_mem->se_len - *task_offset);
  3878. if (!(list_is_last(&se_mem->se_list,
  3879. &se_cmd->t_mem_list))) {
  3880. se_mem = list_entry(se_mem->se_list.next,
  3881. struct se_mem, se_list);
  3882. (*se_mem_cnt)++;
  3883. }
  3884. }
  3885. *task_offset = 0;
  3886. }
  3887. task_size -= sg->length;
  3888. next:
  3889. DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
  3890. " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
  3891. sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
  3892. sg_no++;
  3893. if (!(task_size))
  3894. break;
  3895. sg = sg_next(sg);
  3896. if (task_size > se_cmd->data_length)
  3897. BUG();
  3898. }
  3899. *out_se_mem = se_mem;
  3900. DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
  3901. " SGs\n", task->task_no, *se_mem_cnt, sg_no);
  3902. return 0;
  3903. }
  3904. /*
  3905. * This function can be used by HW target mode drivers to create a linked
  3906. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  3907. * This is intended to be called during the completion path by TCM Core
  3908. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  3909. */
  3910. void transport_do_task_sg_chain(struct se_cmd *cmd)
  3911. {
  3912. struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
  3913. struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
  3914. struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
  3915. struct se_task *task;
  3916. struct target_core_fabric_ops *tfo = cmd->se_tfo;
  3917. u32 task_sg_num = 0, sg_count = 0;
  3918. int i;
  3919. if (tfo->task_sg_chaining == 0) {
  3920. printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
  3921. " %s\n", tfo->get_fabric_name());
  3922. dump_stack();
  3923. return;
  3924. }
  3925. /*
  3926. * Walk the struct se_task list and setup scatterlist chains
  3927. * for each contiguously allocated struct se_task->task_sg[].
  3928. */
  3929. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  3930. if (!(task->task_sg) || !(task->task_padded_sg))
  3931. continue;
  3932. if (sg_head && sg_link) {
  3933. sg_head_cur = &task->task_sg[0];
  3934. sg_link_cur = &task->task_sg[task->task_sg_num];
  3935. /*
  3936. * Either add chain or mark end of scatterlist
  3937. */
  3938. if (!(list_is_last(&task->t_list,
  3939. &cmd->t_task_list))) {
  3940. /*
  3941. * Clear existing SGL termination bit set in
  3942. * transport_init_task_sg(), see sg_mark_end()
  3943. */
  3944. sg_end_cur = &task->task_sg[task->task_sg_num - 1];
  3945. sg_end_cur->page_link &= ~0x02;
  3946. sg_chain(sg_head, task_sg_num, sg_head_cur);
  3947. sg_count += task->task_sg_num;
  3948. task_sg_num = (task->task_sg_num + 1);
  3949. } else {
  3950. sg_chain(sg_head, task_sg_num, sg_head_cur);
  3951. sg_count += task->task_sg_num;
  3952. task_sg_num = task->task_sg_num;
  3953. }
  3954. sg_head = sg_head_cur;
  3955. sg_link = sg_link_cur;
  3956. continue;
  3957. }
  3958. sg_head = sg_first = &task->task_sg[0];
  3959. sg_link = &task->task_sg[task->task_sg_num];
  3960. /*
  3961. * Check for single task..
  3962. */
  3963. if (!(list_is_last(&task->t_list, &cmd->t_task_list))) {
  3964. /*
  3965. * Clear existing SGL termination bit set in
  3966. * transport_init_task_sg(), see sg_mark_end()
  3967. */
  3968. sg_end = &task->task_sg[task->task_sg_num - 1];
  3969. sg_end->page_link &= ~0x02;
  3970. sg_count += task->task_sg_num;
  3971. task_sg_num = (task->task_sg_num + 1);
  3972. } else {
  3973. sg_count += task->task_sg_num;
  3974. task_sg_num = task->task_sg_num;
  3975. }
  3976. }
  3977. /*
  3978. * Setup the starting pointer and total t_tasks_sg_linked_no including
  3979. * padding SGs for linking and to mark the end.
  3980. */
  3981. cmd->t_tasks_sg_chained = sg_first;
  3982. cmd->t_tasks_sg_chained_no = sg_count;
  3983. DEBUG_CMD_M("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
  3984. " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
  3985. cmd->t_tasks_sg_chained_no);
  3986. for_each_sg(cmd->t_tasks_sg_chained, sg,
  3987. cmd->t_tasks_sg_chained_no, i) {
  3988. DEBUG_CMD_M("SG[%d]: %p page: %p length: %d offset: %d\n",
  3989. i, sg, sg_page(sg), sg->length, sg->offset);
  3990. if (sg_is_chain(sg))
  3991. DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
  3992. if (sg_is_last(sg))
  3993. DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
  3994. }
  3995. }
  3996. EXPORT_SYMBOL(transport_do_task_sg_chain);
  3997. static int transport_do_se_mem_map(
  3998. struct se_device *dev,
  3999. struct se_task *task,
  4000. struct list_head *se_mem_list,
  4001. void *in_mem,
  4002. struct se_mem *in_se_mem,
  4003. struct se_mem **out_se_mem,
  4004. u32 *se_mem_cnt,
  4005. u32 *task_offset_in)
  4006. {
  4007. u32 task_offset = *task_offset_in;
  4008. int ret = 0;
  4009. /*
  4010. * se_subsystem_api_t->do_se_mem_map is used when internal allocation
  4011. * has been done by the transport plugin.
  4012. */
  4013. if (dev->transport->do_se_mem_map) {
  4014. ret = dev->transport->do_se_mem_map(task, se_mem_list,
  4015. in_mem, in_se_mem, out_se_mem, se_mem_cnt,
  4016. task_offset_in);
  4017. if (ret == 0)
  4018. task->task_se_cmd->t_tasks_se_num += *se_mem_cnt;
  4019. return ret;
  4020. }
  4021. BUG_ON(list_empty(se_mem_list));
  4022. /*
  4023. * This is the normal path for all normal non BIDI and BIDI-COMMAND
  4024. * WRITE payloads.. If we need to do BIDI READ passthrough for
  4025. * TCM/pSCSI the first call to transport_do_se_mem_map ->
  4026. * transport_init_task_sg() -> transport_map_mem_to_sg() will do the
  4027. * allocation for task->task_sg_bidi, and the subsequent call to
  4028. * transport_do_se_mem_map() from transport_generic_get_cdb_count()
  4029. */
  4030. if (!(task->task_sg_bidi)) {
  4031. /*
  4032. * Assume default that transport plugin speaks preallocated
  4033. * scatterlists.
  4034. */
  4035. ret = transport_init_task_sg(task, in_se_mem, task_offset);
  4036. if (ret <= 0)
  4037. return ret;
  4038. /*
  4039. * struct se_task->task_sg now contains the struct scatterlist array.
  4040. */
  4041. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
  4042. in_se_mem, out_se_mem, se_mem_cnt,
  4043. task_offset_in);
  4044. }
  4045. /*
  4046. * Handle the se_mem_list -> struct task->task_sg_bidi
  4047. * memory map for the extra BIDI READ payload
  4048. */
  4049. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
  4050. in_se_mem, out_se_mem, se_mem_cnt,
  4051. task_offset_in);
  4052. }
  4053. /*
  4054. * Break up cmd into chunks transport can handle
  4055. */
  4056. static u32 transport_allocate_tasks(
  4057. struct se_cmd *cmd,
  4058. unsigned long long lba,
  4059. u32 sectors,
  4060. enum dma_data_direction data_direction,
  4061. struct list_head *mem_list,
  4062. int set_counts)
  4063. {
  4064. unsigned char *cdb = NULL;
  4065. struct se_task *task;
  4066. struct se_mem *se_mem = NULL;
  4067. struct se_mem *se_mem_lout = NULL;
  4068. struct se_mem *se_mem_bidi = NULL;
  4069. struct se_mem *se_mem_bidi_lout = NULL;
  4070. struct se_device *dev = cmd->se_dev;
  4071. int ret;
  4072. u32 task_offset_in = 0;
  4073. u32 se_mem_cnt = 0;
  4074. u32 se_mem_bidi_cnt = 0;
  4075. u32 task_cdbs = 0;
  4076. BUG_ON(!mem_list);
  4077. /*
  4078. * While using RAMDISK_DR backstores is the only case where
  4079. * mem_list will ever be empty at this point.
  4080. */
  4081. if (!(list_empty(mem_list)))
  4082. se_mem = list_first_entry(mem_list, struct se_mem, se_list);
  4083. /*
  4084. * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
  4085. * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
  4086. */
  4087. if (!list_empty(&cmd->t_mem_bidi_list) &&
  4088. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
  4089. se_mem_bidi = list_first_entry(&cmd->t_mem_bidi_list,
  4090. struct se_mem, se_list);
  4091. while (sectors) {
  4092. sector_t limited_sectors;
  4093. DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
  4094. cmd->se_tfo->get_task_tag(cmd), lba, sectors,
  4095. transport_dev_end_lba(dev));
  4096. limited_sectors = transport_limit_task_sectors(dev, lba, sectors);
  4097. if (!limited_sectors)
  4098. break;
  4099. task = transport_generic_get_task(cmd, data_direction);
  4100. if (!task)
  4101. goto out;
  4102. task->task_lba = lba;
  4103. task->task_sectors = limited_sectors;
  4104. lba += task->task_sectors;
  4105. sectors -= task->task_sectors;
  4106. task->task_size = (task->task_sectors *
  4107. dev->se_sub_dev->se_dev_attrib.block_size);
  4108. cdb = dev->transport->get_cdb(task);
  4109. /* Should be part of task, can't fail */
  4110. BUG_ON(!cdb);
  4111. memcpy(cdb, cmd->t_task_cdb,
  4112. scsi_command_size(cmd->t_task_cdb));
  4113. /* Update new cdb with updated lba/sectors */
  4114. cmd->transport_split_cdb(task->task_lba,
  4115. &task->task_sectors, cdb);
  4116. /*
  4117. * Perform the SE OBJ plugin and/or Transport plugin specific
  4118. * mapping for cmd->t_mem_list. And setup the
  4119. * task->task_sg and if necessary task->task_sg_bidi
  4120. */
  4121. ret = transport_do_se_mem_map(dev, task, mem_list,
  4122. NULL, se_mem, &se_mem_lout, &se_mem_cnt,
  4123. &task_offset_in);
  4124. if (ret < 0)
  4125. goto out;
  4126. se_mem = se_mem_lout;
  4127. /*
  4128. * Setup the cmd->t_mem_bidi_list -> task->task_sg_bidi
  4129. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
  4130. *
  4131. * Note that the first call to transport_do_se_mem_map() above will
  4132. * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
  4133. * -> transport_init_task_sg(), and the second here will do the
  4134. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
  4135. */
  4136. if (task->task_sg_bidi != NULL) {
  4137. ret = transport_do_se_mem_map(dev, task,
  4138. &cmd->t_mem_bidi_list, NULL,
  4139. se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
  4140. &task_offset_in);
  4141. if (ret < 0)
  4142. goto out;
  4143. se_mem_bidi = se_mem_bidi_lout;
  4144. }
  4145. task_cdbs++;
  4146. DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
  4147. task_cdbs, task->task_sg_num);
  4148. }
  4149. if (set_counts) {
  4150. atomic_inc(&cmd->t_fe_count);
  4151. atomic_inc(&cmd->t_se_count);
  4152. }
  4153. DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
  4154. cmd->se_tfo->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
  4155. ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
  4156. return task_cdbs;
  4157. out:
  4158. return 0;
  4159. }
  4160. static int
  4161. transport_map_control_cmd_to_task(struct se_cmd *cmd)
  4162. {
  4163. struct se_device *dev = cmd->se_dev;
  4164. unsigned char *cdb;
  4165. struct se_task *task;
  4166. int ret;
  4167. task = transport_generic_get_task(cmd, cmd->data_direction);
  4168. if (!task)
  4169. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4170. cdb = dev->transport->get_cdb(task);
  4171. BUG_ON(!cdb);
  4172. memcpy(cdb, cmd->t_task_cdb,
  4173. scsi_command_size(cmd->t_task_cdb));
  4174. task->task_size = cmd->data_length;
  4175. task->task_sg_num =
  4176. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
  4177. atomic_inc(&cmd->t_fe_count);
  4178. atomic_inc(&cmd->t_se_count);
  4179. if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
  4180. struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
  4181. u32 se_mem_cnt = 0, task_offset = 0;
  4182. if (!list_empty(&cmd->t_mem_list))
  4183. se_mem = list_first_entry(&cmd->t_mem_list,
  4184. struct se_mem, se_list);
  4185. ret = transport_do_se_mem_map(dev, task,
  4186. &cmd->t_mem_list, NULL, se_mem,
  4187. &se_mem_lout, &se_mem_cnt, &task_offset);
  4188. if (ret < 0)
  4189. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4190. if (dev->transport->map_task_SG)
  4191. return dev->transport->map_task_SG(task);
  4192. return 0;
  4193. } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
  4194. if (dev->transport->map_task_non_SG)
  4195. return dev->transport->map_task_non_SG(task);
  4196. return 0;
  4197. } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
  4198. if (dev->transport->cdb_none)
  4199. return dev->transport->cdb_none(task);
  4200. return 0;
  4201. } else {
  4202. BUG();
  4203. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4204. }
  4205. }
  4206. /* transport_generic_new_cmd(): Called from transport_processing_thread()
  4207. *
  4208. * Allocate storage transport resources from a set of values predefined
  4209. * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
  4210. * Any non zero return here is treated as an "out of resource' op here.
  4211. */
  4212. /*
  4213. * Generate struct se_task(s) and/or their payloads for this CDB.
  4214. */
  4215. int transport_generic_new_cmd(struct se_cmd *cmd)
  4216. {
  4217. struct se_task *task;
  4218. struct se_device *dev = cmd->se_dev;
  4219. int ret = 0;
  4220. /*
  4221. * Determine is the TCM fabric module has already allocated physical
  4222. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  4223. * to setup beforehand the linked list of physical memory at
  4224. * cmd->t_mem_list of struct se_mem->se_page
  4225. */
  4226. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
  4227. ret = transport_allocate_resources(cmd);
  4228. if (ret < 0)
  4229. return ret;
  4230. }
  4231. ret = transport_new_cmd_obj(cmd);
  4232. if (ret < 0)
  4233. return ret;
  4234. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  4235. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  4236. if (atomic_read(&task->task_sent))
  4237. continue;
  4238. if (!dev->transport->map_task_SG)
  4239. continue;
  4240. ret = dev->transport->map_task_SG(task);
  4241. if (ret < 0)
  4242. return ret;
  4243. }
  4244. } else {
  4245. ret = transport_map_control_cmd_to_task(cmd);
  4246. if (ret < 0)
  4247. return ret;
  4248. }
  4249. /*
  4250. * For WRITEs, let the fabric know its buffer is ready..
  4251. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  4252. * will be added to the struct se_device execution queue after its WRITE
  4253. * data has arrived. (ie: It gets handled by the transport processing
  4254. * thread a second time)
  4255. */
  4256. if (cmd->data_direction == DMA_TO_DEVICE) {
  4257. transport_add_tasks_to_state_queue(cmd);
  4258. return transport_generic_write_pending(cmd);
  4259. }
  4260. /*
  4261. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  4262. * to the execution queue.
  4263. */
  4264. transport_execute_tasks(cmd);
  4265. return 0;
  4266. }
  4267. EXPORT_SYMBOL(transport_generic_new_cmd);
  4268. /* transport_generic_process_write():
  4269. *
  4270. *
  4271. */
  4272. void transport_generic_process_write(struct se_cmd *cmd)
  4273. {
  4274. #if 0
  4275. /*
  4276. * Copy SCSI Presented DTL sector(s) from received buffers allocated to
  4277. * original EDTL
  4278. */
  4279. if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  4280. if (!cmd->t_tasks_se_num) {
  4281. unsigned char *dst, *buf =
  4282. (unsigned char *)cmd->t_task_buf;
  4283. dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
  4284. if (!(dst)) {
  4285. printk(KERN_ERR "Unable to allocate memory for"
  4286. " WRITE underflow\n");
  4287. transport_generic_request_failure(cmd, NULL,
  4288. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4289. return;
  4290. }
  4291. memcpy(dst, buf, cmd->cmd_spdtl);
  4292. kfree(cmd->t_task_buf);
  4293. cmd->t_task_buf = dst;
  4294. } else {
  4295. struct scatterlist *sg =
  4296. (struct scatterlist *sg)cmd->t_task_buf;
  4297. struct scatterlist *orig_sg;
  4298. orig_sg = kzalloc(sizeof(struct scatterlist) *
  4299. cmd->t_tasks_se_num,
  4300. GFP_KERNEL))) {
  4301. if (!(orig_sg)) {
  4302. printk(KERN_ERR "Unable to allocate memory"
  4303. " for WRITE underflow\n");
  4304. transport_generic_request_failure(cmd, NULL,
  4305. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4306. return;
  4307. }
  4308. memcpy(orig_sg, cmd->t_task_buf,
  4309. sizeof(struct scatterlist) *
  4310. cmd->t_tasks_se_num);
  4311. cmd->data_length = cmd->cmd_spdtl;
  4312. /*
  4313. * FIXME, clear out original struct se_task and state
  4314. * information.
  4315. */
  4316. if (transport_generic_new_cmd(cmd) < 0) {
  4317. transport_generic_request_failure(cmd, NULL,
  4318. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4319. kfree(orig_sg);
  4320. return;
  4321. }
  4322. transport_memcpy_write_sg(cmd, orig_sg);
  4323. }
  4324. }
  4325. #endif
  4326. transport_execute_tasks(cmd);
  4327. }
  4328. EXPORT_SYMBOL(transport_generic_process_write);
  4329. /* transport_generic_write_pending():
  4330. *
  4331. *
  4332. */
  4333. static int transport_generic_write_pending(struct se_cmd *cmd)
  4334. {
  4335. unsigned long flags;
  4336. int ret;
  4337. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4338. cmd->t_state = TRANSPORT_WRITE_PENDING;
  4339. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4340. /*
  4341. * For the TCM control CDBs using a contiguous buffer, do the memcpy
  4342. * from the passed Linux/SCSI struct scatterlist located at
  4343. * se_cmd->t_task_pt_sgl to the contiguous buffer at
  4344. * se_cmd->t_task_buf.
  4345. */
  4346. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
  4347. sg_copy_to_buffer(cmd->t_task_pt_sgl,
  4348. cmd->t_task_pt_sgl_num,
  4349. cmd->t_task_buf,
  4350. cmd->data_length);
  4351. /*
  4352. * Clear the se_cmd for WRITE_PENDING status in order to set
  4353. * cmd->t_transport_active=0 so that transport_generic_handle_data
  4354. * can be called from HW target mode interrupt code. This is safe
  4355. * to be called with transport_off=1 before the cmd->se_tfo->write_pending
  4356. * because the se_cmd->se_lun pointer is not being cleared.
  4357. */
  4358. transport_cmd_check_stop(cmd, 1, 0);
  4359. /*
  4360. * Call the fabric write_pending function here to let the
  4361. * frontend know that WRITE buffers are ready.
  4362. */
  4363. ret = cmd->se_tfo->write_pending(cmd);
  4364. if (ret < 0)
  4365. return ret;
  4366. return PYX_TRANSPORT_WRITE_PENDING;
  4367. }
  4368. /* transport_release_cmd_to_pool():
  4369. *
  4370. *
  4371. */
  4372. void transport_release_cmd_to_pool(struct se_cmd *cmd)
  4373. {
  4374. BUG_ON(!cmd->se_tfo);
  4375. transport_free_se_cmd(cmd);
  4376. cmd->se_tfo->release_cmd_to_pool(cmd);
  4377. }
  4378. EXPORT_SYMBOL(transport_release_cmd_to_pool);
  4379. /* transport_generic_free_cmd():
  4380. *
  4381. * Called from processing frontend to release storage engine resources
  4382. */
  4383. void transport_generic_free_cmd(
  4384. struct se_cmd *cmd,
  4385. int wait_for_tasks,
  4386. int release_to_pool,
  4387. int session_reinstatement)
  4388. {
  4389. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD))
  4390. transport_release_cmd_to_pool(cmd);
  4391. else {
  4392. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  4393. if (cmd->se_lun) {
  4394. #if 0
  4395. printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
  4396. " cmd->se_lun\n", cmd,
  4397. cmd->se_tfo->get_task_tag(cmd));
  4398. #endif
  4399. transport_lun_remove_cmd(cmd);
  4400. }
  4401. if (wait_for_tasks && cmd->transport_wait_for_tasks)
  4402. cmd->transport_wait_for_tasks(cmd, 0, 0);
  4403. transport_free_dev_tasks(cmd);
  4404. transport_generic_remove(cmd, release_to_pool,
  4405. session_reinstatement);
  4406. }
  4407. }
  4408. EXPORT_SYMBOL(transport_generic_free_cmd);
  4409. static void transport_nop_wait_for_tasks(
  4410. struct se_cmd *cmd,
  4411. int remove_cmd,
  4412. int session_reinstatement)
  4413. {
  4414. return;
  4415. }
  4416. /* transport_lun_wait_for_tasks():
  4417. *
  4418. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  4419. * an struct se_lun to be successfully shutdown.
  4420. */
  4421. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  4422. {
  4423. unsigned long flags;
  4424. int ret;
  4425. /*
  4426. * If the frontend has already requested this struct se_cmd to
  4427. * be stopped, we can safely ignore this struct se_cmd.
  4428. */
  4429. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4430. if (atomic_read(&cmd->t_transport_stop)) {
  4431. atomic_set(&cmd->transport_lun_stop, 0);
  4432. DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
  4433. " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
  4434. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4435. transport_cmd_check_stop(cmd, 1, 0);
  4436. return -EPERM;
  4437. }
  4438. atomic_set(&cmd->transport_lun_fe_stop, 1);
  4439. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4440. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  4441. ret = transport_stop_tasks_for_cmd(cmd);
  4442. DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
  4443. " %d\n", cmd, cmd->t_task_cdbs, ret);
  4444. if (!ret) {
  4445. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  4446. cmd->se_tfo->get_task_tag(cmd));
  4447. wait_for_completion(&cmd->transport_lun_stop_comp);
  4448. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  4449. cmd->se_tfo->get_task_tag(cmd));
  4450. }
  4451. transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
  4452. return 0;
  4453. }
  4454. /* #define DEBUG_CLEAR_LUN */
  4455. #ifdef DEBUG_CLEAR_LUN
  4456. #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
  4457. #else
  4458. #define DEBUG_CLEAR_L(x...)
  4459. #endif
  4460. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  4461. {
  4462. struct se_cmd *cmd = NULL;
  4463. unsigned long lun_flags, cmd_flags;
  4464. /*
  4465. * Do exception processing and return CHECK_CONDITION status to the
  4466. * Initiator Port.
  4467. */
  4468. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4469. while (!list_empty(&lun->lun_cmd_list)) {
  4470. cmd = list_first_entry(&lun->lun_cmd_list,
  4471. struct se_cmd, se_lun_node);
  4472. list_del(&cmd->se_lun_node);
  4473. atomic_set(&cmd->transport_lun_active, 0);
  4474. /*
  4475. * This will notify iscsi_target_transport.c:
  4476. * transport_cmd_check_stop() that a LUN shutdown is in
  4477. * progress for the iscsi_cmd_t.
  4478. */
  4479. spin_lock(&cmd->t_state_lock);
  4480. DEBUG_CLEAR_L("SE_LUN[%d] - Setting cmd->transport"
  4481. "_lun_stop for ITT: 0x%08x\n",
  4482. cmd->se_lun->unpacked_lun,
  4483. cmd->se_tfo->get_task_tag(cmd));
  4484. atomic_set(&cmd->transport_lun_stop, 1);
  4485. spin_unlock(&cmd->t_state_lock);
  4486. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4487. if (!(cmd->se_lun)) {
  4488. printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
  4489. cmd->se_tfo->get_task_tag(cmd),
  4490. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  4491. BUG();
  4492. }
  4493. /*
  4494. * If the Storage engine still owns the iscsi_cmd_t, determine
  4495. * and/or stop its context.
  4496. */
  4497. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
  4498. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  4499. cmd->se_tfo->get_task_tag(cmd));
  4500. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  4501. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4502. continue;
  4503. }
  4504. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  4505. "_wait_for_tasks(): SUCCESS\n",
  4506. cmd->se_lun->unpacked_lun,
  4507. cmd->se_tfo->get_task_tag(cmd));
  4508. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  4509. if (!(atomic_read(&cmd->transport_dev_active))) {
  4510. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  4511. goto check_cond;
  4512. }
  4513. atomic_set(&cmd->transport_dev_active, 0);
  4514. transport_all_task_dev_remove_state(cmd);
  4515. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  4516. transport_free_dev_tasks(cmd);
  4517. /*
  4518. * The Storage engine stopped this struct se_cmd before it was
  4519. * send to the fabric frontend for delivery back to the
  4520. * Initiator Node. Return this SCSI CDB back with an
  4521. * CHECK_CONDITION status.
  4522. */
  4523. check_cond:
  4524. transport_send_check_condition_and_sense(cmd,
  4525. TCM_NON_EXISTENT_LUN, 0);
  4526. /*
  4527. * If the fabric frontend is waiting for this iscsi_cmd_t to
  4528. * be released, notify the waiting thread now that LU has
  4529. * finished accessing it.
  4530. */
  4531. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  4532. if (atomic_read(&cmd->transport_lun_fe_stop)) {
  4533. DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
  4534. " struct se_cmd: %p ITT: 0x%08x\n",
  4535. lun->unpacked_lun,
  4536. cmd, cmd->se_tfo->get_task_tag(cmd));
  4537. spin_unlock_irqrestore(&cmd->t_state_lock,
  4538. cmd_flags);
  4539. transport_cmd_check_stop(cmd, 1, 0);
  4540. complete(&cmd->transport_lun_fe_stop_comp);
  4541. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4542. continue;
  4543. }
  4544. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  4545. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  4546. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  4547. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4548. }
  4549. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4550. }
  4551. static int transport_clear_lun_thread(void *p)
  4552. {
  4553. struct se_lun *lun = (struct se_lun *)p;
  4554. __transport_clear_lun_from_sessions(lun);
  4555. complete(&lun->lun_shutdown_comp);
  4556. return 0;
  4557. }
  4558. int transport_clear_lun_from_sessions(struct se_lun *lun)
  4559. {
  4560. struct task_struct *kt;
  4561. kt = kthread_run(transport_clear_lun_thread, lun,
  4562. "tcm_cl_%u", lun->unpacked_lun);
  4563. if (IS_ERR(kt)) {
  4564. printk(KERN_ERR "Unable to start clear_lun thread\n");
  4565. return PTR_ERR(kt);
  4566. }
  4567. wait_for_completion(&lun->lun_shutdown_comp);
  4568. return 0;
  4569. }
  4570. /* transport_generic_wait_for_tasks():
  4571. *
  4572. * Called from frontend or passthrough context to wait for storage engine
  4573. * to pause and/or release frontend generated struct se_cmd.
  4574. */
  4575. static void transport_generic_wait_for_tasks(
  4576. struct se_cmd *cmd,
  4577. int remove_cmd,
  4578. int session_reinstatement)
  4579. {
  4580. unsigned long flags;
  4581. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
  4582. return;
  4583. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4584. /*
  4585. * If we are already stopped due to an external event (ie: LUN shutdown)
  4586. * sleep until the connection can have the passed struct se_cmd back.
  4587. * The cmd->transport_lun_stopped_sem will be upped by
  4588. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  4589. * has completed its operation on the struct se_cmd.
  4590. */
  4591. if (atomic_read(&cmd->transport_lun_stop)) {
  4592. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
  4593. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  4594. "_stop_comp); for ITT: 0x%08x\n",
  4595. cmd->se_tfo->get_task_tag(cmd));
  4596. /*
  4597. * There is a special case for WRITES where a FE exception +
  4598. * LUN shutdown means ConfigFS context is still sleeping on
  4599. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  4600. * We go ahead and up transport_lun_stop_comp just to be sure
  4601. * here.
  4602. */
  4603. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4604. complete(&cmd->transport_lun_stop_comp);
  4605. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  4606. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4607. transport_all_task_dev_remove_state(cmd);
  4608. /*
  4609. * At this point, the frontend who was the originator of this
  4610. * struct se_cmd, now owns the structure and can be released through
  4611. * normal means below.
  4612. */
  4613. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
  4614. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  4615. "stop_comp); for ITT: 0x%08x\n",
  4616. cmd->se_tfo->get_task_tag(cmd));
  4617. atomic_set(&cmd->transport_lun_stop, 0);
  4618. }
  4619. if (!atomic_read(&cmd->t_transport_active) ||
  4620. atomic_read(&cmd->t_transport_aborted))
  4621. goto remove;
  4622. atomic_set(&cmd->t_transport_stop, 1);
  4623. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
  4624. " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
  4625. " = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  4626. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
  4627. cmd->deferred_t_state);
  4628. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4629. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  4630. wait_for_completion(&cmd->t_transport_stop_comp);
  4631. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4632. atomic_set(&cmd->t_transport_active, 0);
  4633. atomic_set(&cmd->t_transport_stop, 0);
  4634. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
  4635. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  4636. cmd->se_tfo->get_task_tag(cmd));
  4637. remove:
  4638. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4639. if (!remove_cmd)
  4640. return;
  4641. transport_generic_free_cmd(cmd, 0, 0, session_reinstatement);
  4642. }
  4643. static int transport_get_sense_codes(
  4644. struct se_cmd *cmd,
  4645. u8 *asc,
  4646. u8 *ascq)
  4647. {
  4648. *asc = cmd->scsi_asc;
  4649. *ascq = cmd->scsi_ascq;
  4650. return 0;
  4651. }
  4652. static int transport_set_sense_codes(
  4653. struct se_cmd *cmd,
  4654. u8 asc,
  4655. u8 ascq)
  4656. {
  4657. cmd->scsi_asc = asc;
  4658. cmd->scsi_ascq = ascq;
  4659. return 0;
  4660. }
  4661. int transport_send_check_condition_and_sense(
  4662. struct se_cmd *cmd,
  4663. u8 reason,
  4664. int from_transport)
  4665. {
  4666. unsigned char *buffer = cmd->sense_buffer;
  4667. unsigned long flags;
  4668. int offset;
  4669. u8 asc = 0, ascq = 0;
  4670. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4671. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4672. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4673. return 0;
  4674. }
  4675. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  4676. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4677. if (!reason && from_transport)
  4678. goto after_reason;
  4679. if (!from_transport)
  4680. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  4681. /*
  4682. * Data Segment and SenseLength of the fabric response PDU.
  4683. *
  4684. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  4685. * from include/scsi/scsi_cmnd.h
  4686. */
  4687. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  4688. TRANSPORT_SENSE_BUFFER);
  4689. /*
  4690. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  4691. * SENSE KEY values from include/scsi/scsi.h
  4692. */
  4693. switch (reason) {
  4694. case TCM_NON_EXISTENT_LUN:
  4695. case TCM_UNSUPPORTED_SCSI_OPCODE:
  4696. case TCM_SECTOR_COUNT_TOO_MANY:
  4697. /* CURRENT ERROR */
  4698. buffer[offset] = 0x70;
  4699. /* ILLEGAL REQUEST */
  4700. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4701. /* INVALID COMMAND OPERATION CODE */
  4702. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  4703. break;
  4704. case TCM_UNKNOWN_MODE_PAGE:
  4705. /* CURRENT ERROR */
  4706. buffer[offset] = 0x70;
  4707. /* ILLEGAL REQUEST */
  4708. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4709. /* INVALID FIELD IN CDB */
  4710. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4711. break;
  4712. case TCM_CHECK_CONDITION_ABORT_CMD:
  4713. /* CURRENT ERROR */
  4714. buffer[offset] = 0x70;
  4715. /* ABORTED COMMAND */
  4716. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4717. /* BUS DEVICE RESET FUNCTION OCCURRED */
  4718. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  4719. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  4720. break;
  4721. case TCM_INCORRECT_AMOUNT_OF_DATA:
  4722. /* CURRENT ERROR */
  4723. buffer[offset] = 0x70;
  4724. /* ABORTED COMMAND */
  4725. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4726. /* WRITE ERROR */
  4727. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4728. /* NOT ENOUGH UNSOLICITED DATA */
  4729. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  4730. break;
  4731. case TCM_INVALID_CDB_FIELD:
  4732. /* CURRENT ERROR */
  4733. buffer[offset] = 0x70;
  4734. /* ABORTED COMMAND */
  4735. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4736. /* INVALID FIELD IN CDB */
  4737. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4738. break;
  4739. case TCM_INVALID_PARAMETER_LIST:
  4740. /* CURRENT ERROR */
  4741. buffer[offset] = 0x70;
  4742. /* ABORTED COMMAND */
  4743. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4744. /* INVALID FIELD IN PARAMETER LIST */
  4745. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  4746. break;
  4747. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  4748. /* CURRENT ERROR */
  4749. buffer[offset] = 0x70;
  4750. /* ABORTED COMMAND */
  4751. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4752. /* WRITE ERROR */
  4753. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4754. /* UNEXPECTED_UNSOLICITED_DATA */
  4755. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  4756. break;
  4757. case TCM_SERVICE_CRC_ERROR:
  4758. /* CURRENT ERROR */
  4759. buffer[offset] = 0x70;
  4760. /* ABORTED COMMAND */
  4761. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4762. /* PROTOCOL SERVICE CRC ERROR */
  4763. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  4764. /* N/A */
  4765. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  4766. break;
  4767. case TCM_SNACK_REJECTED:
  4768. /* CURRENT ERROR */
  4769. buffer[offset] = 0x70;
  4770. /* ABORTED COMMAND */
  4771. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4772. /* READ ERROR */
  4773. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  4774. /* FAILED RETRANSMISSION REQUEST */
  4775. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  4776. break;
  4777. case TCM_WRITE_PROTECTED:
  4778. /* CURRENT ERROR */
  4779. buffer[offset] = 0x70;
  4780. /* DATA PROTECT */
  4781. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  4782. /* WRITE PROTECTED */
  4783. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  4784. break;
  4785. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  4786. /* CURRENT ERROR */
  4787. buffer[offset] = 0x70;
  4788. /* UNIT ATTENTION */
  4789. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  4790. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  4791. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4792. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4793. break;
  4794. case TCM_CHECK_CONDITION_NOT_READY:
  4795. /* CURRENT ERROR */
  4796. buffer[offset] = 0x70;
  4797. /* Not Ready */
  4798. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  4799. transport_get_sense_codes(cmd, &asc, &ascq);
  4800. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4801. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4802. break;
  4803. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  4804. default:
  4805. /* CURRENT ERROR */
  4806. buffer[offset] = 0x70;
  4807. /* ILLEGAL REQUEST */
  4808. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4809. /* LOGICAL UNIT COMMUNICATION FAILURE */
  4810. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  4811. break;
  4812. }
  4813. /*
  4814. * This code uses linux/include/scsi/scsi.h SAM status codes!
  4815. */
  4816. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  4817. /*
  4818. * Automatically padded, this value is encoded in the fabric's
  4819. * data_length response PDU containing the SCSI defined sense data.
  4820. */
  4821. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  4822. after_reason:
  4823. cmd->se_tfo->queue_status(cmd);
  4824. return 0;
  4825. }
  4826. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  4827. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  4828. {
  4829. int ret = 0;
  4830. if (atomic_read(&cmd->t_transport_aborted) != 0) {
  4831. if (!(send_status) ||
  4832. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  4833. return 1;
  4834. #if 0
  4835. printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
  4836. " status for CDB: 0x%02x ITT: 0x%08x\n",
  4837. cmd->t_task_cdb[0],
  4838. cmd->se_tfo->get_task_tag(cmd));
  4839. #endif
  4840. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  4841. cmd->se_tfo->queue_status(cmd);
  4842. ret = 1;
  4843. }
  4844. return ret;
  4845. }
  4846. EXPORT_SYMBOL(transport_check_aborted_status);
  4847. void transport_send_task_abort(struct se_cmd *cmd)
  4848. {
  4849. /*
  4850. * If there are still expected incoming fabric WRITEs, we wait
  4851. * until until they have completed before sending a TASK_ABORTED
  4852. * response. This response with TASK_ABORTED status will be
  4853. * queued back to fabric module by transport_check_aborted_status().
  4854. */
  4855. if (cmd->data_direction == DMA_TO_DEVICE) {
  4856. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  4857. atomic_inc(&cmd->t_transport_aborted);
  4858. smp_mb__after_atomic_inc();
  4859. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4860. transport_new_cmd_failure(cmd);
  4861. return;
  4862. }
  4863. }
  4864. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4865. #if 0
  4866. printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  4867. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  4868. cmd->se_tfo->get_task_tag(cmd));
  4869. #endif
  4870. cmd->se_tfo->queue_status(cmd);
  4871. }
  4872. /* transport_generic_do_tmr():
  4873. *
  4874. *
  4875. */
  4876. int transport_generic_do_tmr(struct se_cmd *cmd)
  4877. {
  4878. struct se_device *dev = cmd->se_dev;
  4879. struct se_tmr_req *tmr = cmd->se_tmr_req;
  4880. int ret;
  4881. switch (tmr->function) {
  4882. case TMR_ABORT_TASK:
  4883. tmr->response = TMR_FUNCTION_REJECTED;
  4884. break;
  4885. case TMR_ABORT_TASK_SET:
  4886. case TMR_CLEAR_ACA:
  4887. case TMR_CLEAR_TASK_SET:
  4888. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  4889. break;
  4890. case TMR_LUN_RESET:
  4891. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  4892. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  4893. TMR_FUNCTION_REJECTED;
  4894. break;
  4895. case TMR_TARGET_WARM_RESET:
  4896. tmr->response = TMR_FUNCTION_REJECTED;
  4897. break;
  4898. case TMR_TARGET_COLD_RESET:
  4899. tmr->response = TMR_FUNCTION_REJECTED;
  4900. break;
  4901. default:
  4902. printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
  4903. tmr->function);
  4904. tmr->response = TMR_FUNCTION_REJECTED;
  4905. break;
  4906. }
  4907. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  4908. cmd->se_tfo->queue_tm_rsp(cmd);
  4909. transport_cmd_check_stop(cmd, 2, 0);
  4910. return 0;
  4911. }
  4912. /*
  4913. * Called with spin_lock_irq(&dev->execute_task_lock); held
  4914. *
  4915. */
  4916. static struct se_task *
  4917. transport_get_task_from_state_list(struct se_device *dev)
  4918. {
  4919. struct se_task *task;
  4920. if (list_empty(&dev->state_task_list))
  4921. return NULL;
  4922. list_for_each_entry(task, &dev->state_task_list, t_state_list)
  4923. break;
  4924. list_del(&task->t_state_list);
  4925. atomic_set(&task->task_state_active, 0);
  4926. return task;
  4927. }
  4928. static void transport_processing_shutdown(struct se_device *dev)
  4929. {
  4930. struct se_cmd *cmd;
  4931. struct se_task *task;
  4932. unsigned long flags;
  4933. /*
  4934. * Empty the struct se_device's struct se_task state list.
  4935. */
  4936. spin_lock_irqsave(&dev->execute_task_lock, flags);
  4937. while ((task = transport_get_task_from_state_list(dev))) {
  4938. if (!task->task_se_cmd) {
  4939. printk(KERN_ERR "task->task_se_cmd is NULL!\n");
  4940. continue;
  4941. }
  4942. cmd = task->task_se_cmd;
  4943. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  4944. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4945. DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
  4946. " i_state/def_i_state: %d/%d, t_state/def_t_state:"
  4947. " %d/%d cdb: 0x%02x\n", cmd, task,
  4948. cmd->se_tfo->get_task_tag(cmd), cmd->cmd_sn,
  4949. cmd->se_tfo->get_cmd_state(cmd), cmd->deferred_i_state,
  4950. cmd->t_state, cmd->deferred_t_state,
  4951. cmd->t_task_cdb[0]);
  4952. DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
  4953. " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
  4954. " t_transport_stop: %d t_transport_sent: %d\n",
  4955. cmd->se_tfo->get_task_tag(cmd),
  4956. cmd->t_task_cdbs,
  4957. atomic_read(&cmd->t_task_cdbs_left),
  4958. atomic_read(&cmd->t_task_cdbs_sent),
  4959. atomic_read(&cmd->t_transport_active),
  4960. atomic_read(&cmd->t_transport_stop),
  4961. atomic_read(&cmd->t_transport_sent));
  4962. if (atomic_read(&task->task_active)) {
  4963. atomic_set(&task->task_stop, 1);
  4964. spin_unlock_irqrestore(
  4965. &cmd->t_state_lock, flags);
  4966. DEBUG_DO("Waiting for task: %p to shutdown for dev:"
  4967. " %p\n", task, dev);
  4968. wait_for_completion(&task->task_stop_comp);
  4969. DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
  4970. task, dev);
  4971. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4972. atomic_dec(&cmd->t_task_cdbs_left);
  4973. atomic_set(&task->task_active, 0);
  4974. atomic_set(&task->task_stop, 0);
  4975. } else {
  4976. if (atomic_read(&task->task_execute_queue) != 0)
  4977. transport_remove_task_from_execute_queue(task, dev);
  4978. }
  4979. __transport_stop_task_timer(task, &flags);
  4980. if (!(atomic_dec_and_test(&cmd->t_task_cdbs_ex_left))) {
  4981. spin_unlock_irqrestore(
  4982. &cmd->t_state_lock, flags);
  4983. DEBUG_DO("Skipping task: %p, dev: %p for"
  4984. " t_task_cdbs_ex_left: %d\n", task, dev,
  4985. atomic_read(&cmd->t_task_cdbs_ex_left));
  4986. spin_lock_irqsave(&dev->execute_task_lock, flags);
  4987. continue;
  4988. }
  4989. if (atomic_read(&cmd->t_transport_active)) {
  4990. DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
  4991. " %p\n", task, dev);
  4992. if (atomic_read(&cmd->t_fe_count)) {
  4993. spin_unlock_irqrestore(
  4994. &cmd->t_state_lock, flags);
  4995. transport_send_check_condition_and_sense(
  4996. cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
  4997. 0);
  4998. transport_remove_cmd_from_queue(cmd,
  4999. &cmd->se_dev->dev_queue_obj);
  5000. transport_lun_remove_cmd(cmd);
  5001. transport_cmd_check_stop(cmd, 1, 0);
  5002. } else {
  5003. spin_unlock_irqrestore(
  5004. &cmd->t_state_lock, flags);
  5005. transport_remove_cmd_from_queue(cmd,
  5006. &cmd->se_dev->dev_queue_obj);
  5007. transport_lun_remove_cmd(cmd);
  5008. if (transport_cmd_check_stop(cmd, 1, 0))
  5009. transport_generic_remove(cmd, 0, 0);
  5010. }
  5011. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5012. continue;
  5013. }
  5014. DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
  5015. task, dev);
  5016. if (atomic_read(&cmd->t_fe_count)) {
  5017. spin_unlock_irqrestore(
  5018. &cmd->t_state_lock, flags);
  5019. transport_send_check_condition_and_sense(cmd,
  5020. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5021. transport_remove_cmd_from_queue(cmd,
  5022. &cmd->se_dev->dev_queue_obj);
  5023. transport_lun_remove_cmd(cmd);
  5024. transport_cmd_check_stop(cmd, 1, 0);
  5025. } else {
  5026. spin_unlock_irqrestore(
  5027. &cmd->t_state_lock, flags);
  5028. transport_remove_cmd_from_queue(cmd,
  5029. &cmd->se_dev->dev_queue_obj);
  5030. transport_lun_remove_cmd(cmd);
  5031. if (transport_cmd_check_stop(cmd, 1, 0))
  5032. transport_generic_remove(cmd, 0, 0);
  5033. }
  5034. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5035. }
  5036. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5037. /*
  5038. * Empty the struct se_device's struct se_cmd list.
  5039. */
  5040. while ((cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj))) {
  5041. DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
  5042. cmd, cmd->t_state);
  5043. if (atomic_read(&cmd->t_fe_count)) {
  5044. transport_send_check_condition_and_sense(cmd,
  5045. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5046. transport_lun_remove_cmd(cmd);
  5047. transport_cmd_check_stop(cmd, 1, 0);
  5048. } else {
  5049. transport_lun_remove_cmd(cmd);
  5050. if (transport_cmd_check_stop(cmd, 1, 0))
  5051. transport_generic_remove(cmd, 0, 0);
  5052. }
  5053. }
  5054. }
  5055. /* transport_processing_thread():
  5056. *
  5057. *
  5058. */
  5059. static int transport_processing_thread(void *param)
  5060. {
  5061. int ret;
  5062. struct se_cmd *cmd;
  5063. struct se_device *dev = (struct se_device *) param;
  5064. set_user_nice(current, -20);
  5065. while (!kthread_should_stop()) {
  5066. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  5067. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  5068. kthread_should_stop());
  5069. if (ret < 0)
  5070. goto out;
  5071. spin_lock_irq(&dev->dev_status_lock);
  5072. if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
  5073. spin_unlock_irq(&dev->dev_status_lock);
  5074. transport_processing_shutdown(dev);
  5075. continue;
  5076. }
  5077. spin_unlock_irq(&dev->dev_status_lock);
  5078. get_cmd:
  5079. __transport_execute_tasks(dev);
  5080. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  5081. if (!cmd)
  5082. continue;
  5083. switch (cmd->t_state) {
  5084. case TRANSPORT_NEW_CMD_MAP:
  5085. if (!(cmd->se_tfo->new_cmd_map)) {
  5086. printk(KERN_ERR "cmd->se_tfo->new_cmd_map is"
  5087. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  5088. BUG();
  5089. }
  5090. ret = cmd->se_tfo->new_cmd_map(cmd);
  5091. if (ret < 0) {
  5092. cmd->transport_error_status = ret;
  5093. transport_generic_request_failure(cmd, NULL,
  5094. 0, (cmd->data_direction !=
  5095. DMA_TO_DEVICE));
  5096. break;
  5097. }
  5098. /* Fall through */
  5099. case TRANSPORT_NEW_CMD:
  5100. ret = transport_generic_new_cmd(cmd);
  5101. if (ret < 0) {
  5102. cmd->transport_error_status = ret;
  5103. transport_generic_request_failure(cmd, NULL,
  5104. 0, (cmd->data_direction !=
  5105. DMA_TO_DEVICE));
  5106. }
  5107. break;
  5108. case TRANSPORT_PROCESS_WRITE:
  5109. transport_generic_process_write(cmd);
  5110. break;
  5111. case TRANSPORT_COMPLETE_OK:
  5112. transport_stop_all_task_timers(cmd);
  5113. transport_generic_complete_ok(cmd);
  5114. break;
  5115. case TRANSPORT_REMOVE:
  5116. transport_generic_remove(cmd, 1, 0);
  5117. break;
  5118. case TRANSPORT_FREE_CMD_INTR:
  5119. transport_generic_free_cmd(cmd, 0, 1, 0);
  5120. break;
  5121. case TRANSPORT_PROCESS_TMR:
  5122. transport_generic_do_tmr(cmd);
  5123. break;
  5124. case TRANSPORT_COMPLETE_FAILURE:
  5125. transport_generic_request_failure(cmd, NULL, 1, 1);
  5126. break;
  5127. case TRANSPORT_COMPLETE_TIMEOUT:
  5128. transport_stop_all_task_timers(cmd);
  5129. transport_generic_request_timeout(cmd);
  5130. break;
  5131. default:
  5132. printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
  5133. " %d for ITT: 0x%08x i_state: %d on SE LUN:"
  5134. " %u\n", cmd->t_state, cmd->deferred_t_state,
  5135. cmd->se_tfo->get_task_tag(cmd),
  5136. cmd->se_tfo->get_cmd_state(cmd),
  5137. cmd->se_lun->unpacked_lun);
  5138. BUG();
  5139. }
  5140. goto get_cmd;
  5141. }
  5142. out:
  5143. transport_release_all_cmds(dev);
  5144. dev->process_thread = NULL;
  5145. return 0;
  5146. }