target_core_transport.c 132 KB

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