target_core_transport.c 127 KB

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