target_core_transport.c 127 KB

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