target_core_transport.c 130 KB

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