target_core_transport.c 126 KB

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