target_core_transport.c 129 KB

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