target_core_transport.c 133 KB

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