target_core_transport.c 133 KB

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