target_core_transport.c 127 KB

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