target_core_transport.c 134 KB

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