scsi_debug.c 175 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004
  1. /*
  2. * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  3. * Copyright (C) 1992 Eric Youngdale
  4. * Simulate a host adapter with 2 disks attached. Do a lot of checking
  5. * to make sure that we are not getting blocks mixed up, and PANIC if
  6. * anything out of the ordinary is seen.
  7. * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. *
  9. * Copyright (C) 2001 - 2018 Douglas Gilbert
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * For documentation see http://sg.danny.cz/sg/sdebug26.html
  17. *
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/slab.h>
  25. #include <linux/types.h>
  26. #include <linux/string.h>
  27. #include <linux/genhd.h>
  28. #include <linux/fs.h>
  29. #include <linux/init.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/scatterlist.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/crc-t10dif.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/atomic.h>
  39. #include <linux/hrtimer.h>
  40. #include <linux/uuid.h>
  41. #include <linux/t10-pi.h>
  42. #include <net/checksum.h>
  43. #include <asm/unaligned.h>
  44. #include <scsi/scsi.h>
  45. #include <scsi/scsi_cmnd.h>
  46. #include <scsi/scsi_device.h>
  47. #include <scsi/scsi_host.h>
  48. #include <scsi/scsicam.h>
  49. #include <scsi/scsi_eh.h>
  50. #include <scsi/scsi_tcq.h>
  51. #include <scsi/scsi_dbg.h>
  52. #include "sd.h"
  53. #include "scsi_logging.h"
  54. /* make sure inq_product_rev string corresponds to this version */
  55. #define SDEBUG_VERSION "0188" /* format to fit INQUIRY revision field */
  56. static const char *sdebug_version_date = "20190125";
  57. #define MY_NAME "scsi_debug"
  58. /* Additional Sense Code (ASC) */
  59. #define NO_ADDITIONAL_SENSE 0x0
  60. #define LOGICAL_UNIT_NOT_READY 0x4
  61. #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
  62. #define UNRECOVERED_READ_ERR 0x11
  63. #define PARAMETER_LIST_LENGTH_ERR 0x1a
  64. #define INVALID_OPCODE 0x20
  65. #define LBA_OUT_OF_RANGE 0x21
  66. #define INVALID_FIELD_IN_CDB 0x24
  67. #define INVALID_FIELD_IN_PARAM_LIST 0x26
  68. #define UA_RESET_ASC 0x29
  69. #define UA_CHANGED_ASC 0x2a
  70. #define TARGET_CHANGED_ASC 0x3f
  71. #define LUNS_CHANGED_ASCQ 0x0e
  72. #define INSUFF_RES_ASC 0x55
  73. #define INSUFF_RES_ASCQ 0x3
  74. #define POWER_ON_RESET_ASCQ 0x0
  75. #define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */
  76. #define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */
  77. #define CAPACITY_CHANGED_ASCQ 0x9
  78. #define SAVING_PARAMS_UNSUP 0x39
  79. #define TRANSPORT_PROBLEM 0x4b
  80. #define THRESHOLD_EXCEEDED 0x5d
  81. #define LOW_POWER_COND_ON 0x5e
  82. #define MISCOMPARE_VERIFY_ASC 0x1d
  83. #define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */
  84. #define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16
  85. #define WRITE_ERROR_ASC 0xc
  86. /* Additional Sense Code Qualifier (ASCQ) */
  87. #define ACK_NAK_TO 0x3
  88. /* Default values for driver parameters */
  89. #define DEF_NUM_HOST 1
  90. #define DEF_NUM_TGTS 1
  91. #define DEF_MAX_LUNS 1
  92. /* With these defaults, this driver will make 1 host with 1 target
  93. * (id 0) containing 1 logical unit (lun 0). That is 1 device.
  94. */
  95. #define DEF_ATO 1
  96. #define DEF_CDB_LEN 10
  97. #define DEF_JDELAY 1 /* if > 0 unit is a jiffy */
  98. #define DEF_DEV_SIZE_MB 8
  99. #define DEF_DIF 0
  100. #define DEF_DIX 0
  101. #define DEF_D_SENSE 0
  102. #define DEF_EVERY_NTH 0
  103. #define DEF_FAKE_RW 0
  104. #define DEF_GUARD 0
  105. #define DEF_HOST_LOCK 0
  106. #define DEF_LBPU 0
  107. #define DEF_LBPWS 0
  108. #define DEF_LBPWS10 0
  109. #define DEF_LBPRZ 1
  110. #define DEF_LOWEST_ALIGNED 0
  111. #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */
  112. #define DEF_NO_LUN_0 0
  113. #define DEF_NUM_PARTS 0
  114. #define DEF_OPTS 0
  115. #define DEF_OPT_BLKS 1024
  116. #define DEF_PHYSBLK_EXP 0
  117. #define DEF_OPT_XFERLEN_EXP 0
  118. #define DEF_PTYPE TYPE_DISK
  119. #define DEF_REMOVABLE false
  120. #define DEF_SCSI_LEVEL 7 /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */
  121. #define DEF_SECTOR_SIZE 512
  122. #define DEF_UNMAP_ALIGNMENT 0
  123. #define DEF_UNMAP_GRANULARITY 1
  124. #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
  125. #define DEF_UNMAP_MAX_DESC 256
  126. #define DEF_VIRTUAL_GB 0
  127. #define DEF_VPD_USE_HOSTNO 1
  128. #define DEF_WRITESAME_LENGTH 0xFFFF
  129. #define DEF_STRICT 0
  130. #define DEF_STATISTICS false
  131. #define DEF_SUBMIT_QUEUES 1
  132. #define DEF_UUID_CTL 0
  133. #define JDELAY_OVERRIDDEN -9999
  134. #define SDEBUG_LUN_0_VAL 0
  135. /* bit mask values for sdebug_opts */
  136. #define SDEBUG_OPT_NOISE 1
  137. #define SDEBUG_OPT_MEDIUM_ERR 2
  138. #define SDEBUG_OPT_TIMEOUT 4
  139. #define SDEBUG_OPT_RECOVERED_ERR 8
  140. #define SDEBUG_OPT_TRANSPORT_ERR 16
  141. #define SDEBUG_OPT_DIF_ERR 32
  142. #define SDEBUG_OPT_DIX_ERR 64
  143. #define SDEBUG_OPT_MAC_TIMEOUT 128
  144. #define SDEBUG_OPT_SHORT_TRANSFER 0x100
  145. #define SDEBUG_OPT_Q_NOISE 0x200
  146. #define SDEBUG_OPT_ALL_TSF 0x400
  147. #define SDEBUG_OPT_RARE_TSF 0x800
  148. #define SDEBUG_OPT_N_WCE 0x1000
  149. #define SDEBUG_OPT_RESET_NOISE 0x2000
  150. #define SDEBUG_OPT_NO_CDB_NOISE 0x4000
  151. #define SDEBUG_OPT_HOST_BUSY 0x8000
  152. #define SDEBUG_OPT_CMD_ABORT 0x10000
  153. #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \
  154. SDEBUG_OPT_RESET_NOISE)
  155. #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \
  156. SDEBUG_OPT_TRANSPORT_ERR | \
  157. SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \
  158. SDEBUG_OPT_SHORT_TRANSFER | \
  159. SDEBUG_OPT_HOST_BUSY | \
  160. SDEBUG_OPT_CMD_ABORT)
  161. /* When "every_nth" > 0 then modulo "every_nth" commands:
  162. * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set
  163. * - a RECOVERED_ERROR is simulated on successful read and write
  164. * commands if SDEBUG_OPT_RECOVERED_ERR is set.
  165. * - a TRANSPORT_ERROR is simulated on successful read and write
  166. * commands if SDEBUG_OPT_TRANSPORT_ERR is set.
  167. * - similarly for DIF_ERR, DIX_ERR, SHORT_TRANSFER, HOST_BUSY and
  168. * CMD_ABORT
  169. *
  170. * When "every_nth" < 0 then after "- every_nth" commands the selected
  171. * error will be injected. The error will be injected on every subsequent
  172. * command until some other action occurs; for example, the user writing
  173. * a new value (other than -1 or 1) to every_nth:
  174. * echo 0 > /sys/bus/pseudo/drivers/scsi_debug/every_nth
  175. */
  176. /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in
  177. * priority order. In the subset implemented here lower numbers have higher
  178. * priority. The UA numbers should be a sequence starting from 0 with
  179. * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
  180. #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */
  181. #define SDEBUG_UA_BUS_RESET 1
  182. #define SDEBUG_UA_MODE_CHANGED 2
  183. #define SDEBUG_UA_CAPACITY_CHANGED 3
  184. #define SDEBUG_UA_LUNS_CHANGED 4
  185. #define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */
  186. #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6
  187. #define SDEBUG_NUM_UAS 7
  188. /* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
  189. * sector on read commands: */
  190. #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
  191. #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */
  192. /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
  193. * or "peripheral device" addressing (value 0) */
  194. #define SAM2_LUN_ADDRESS_METHOD 0
  195. /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued
  196. * (for response) per submit queue at one time. Can be reduced by max_queue
  197. * option. Command responses are not queued when jdelay=0 and ndelay=0. The
  198. * per-device DEF_CMD_PER_LUN can be changed via sysfs:
  199. * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth
  200. * but cannot exceed SDEBUG_CANQUEUE .
  201. */
  202. #define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */
  203. #define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG)
  204. #define DEF_CMD_PER_LUN 255
  205. #define F_D_IN 1
  206. #define F_D_OUT 2
  207. #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */
  208. #define F_D_UNKN 8
  209. #define F_RL_WLUN_OK 0x10
  210. #define F_SKIP_UA 0x20
  211. #define F_DELAY_OVERR 0x40
  212. #define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */
  213. #define F_SA_HIGH 0x100 /* as used by variable length cdbs */
  214. #define F_INV_OP 0x200
  215. #define F_FAKE_RW 0x400
  216. #define F_M_ACCESS 0x800 /* media access */
  217. #define F_SSU_DELAY 0x1000
  218. #define F_SYNC_DELAY 0x2000
  219. #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
  220. #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW)
  221. #define FF_SA (F_SA_HIGH | F_SA_LOW)
  222. #define F_LONG_DELAY (F_SSU_DELAY | F_SYNC_DELAY)
  223. #define SDEBUG_MAX_PARTS 4
  224. #define SDEBUG_MAX_CMD_LEN 32
  225. struct sdebug_dev_info {
  226. struct list_head dev_list;
  227. unsigned int channel;
  228. unsigned int target;
  229. u64 lun;
  230. uuid_t lu_name;
  231. struct sdebug_host_info *sdbg_host;
  232. unsigned long uas_bm[1];
  233. atomic_t num_in_q;
  234. atomic_t stopped;
  235. bool used;
  236. };
  237. struct sdebug_host_info {
  238. struct list_head host_list;
  239. struct Scsi_Host *shost;
  240. struct device dev;
  241. struct list_head dev_info_list;
  242. };
  243. #define to_sdebug_host(d) \
  244. container_of(d, struct sdebug_host_info, dev)
  245. enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1,
  246. SDEB_DEFER_WQ = 2};
  247. struct sdebug_defer {
  248. struct hrtimer hrt;
  249. struct execute_work ew;
  250. int sqa_idx; /* index of sdebug_queue array */
  251. int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */
  252. int issuing_cpu;
  253. bool init_hrt;
  254. bool init_wq;
  255. bool aborted; /* true when blk_abort_request() already called */
  256. enum sdeb_defer_type defer_t;
  257. };
  258. struct sdebug_queued_cmd {
  259. /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue
  260. * instance indicates this slot is in use.
  261. */
  262. struct sdebug_defer *sd_dp;
  263. struct scsi_cmnd *a_cmnd;
  264. unsigned int inj_recovered:1;
  265. unsigned int inj_transport:1;
  266. unsigned int inj_dif:1;
  267. unsigned int inj_dix:1;
  268. unsigned int inj_short:1;
  269. unsigned int inj_host_busy:1;
  270. unsigned int inj_cmd_abort:1;
  271. };
  272. struct sdebug_queue {
  273. struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE];
  274. unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS];
  275. spinlock_t qc_lock;
  276. atomic_t blocked; /* to temporarily stop more being queued */
  277. };
  278. static atomic_t sdebug_cmnd_count; /* number of incoming commands */
  279. static atomic_t sdebug_completions; /* count of deferred completions */
  280. static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */
  281. static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */
  282. struct opcode_info_t {
  283. u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */
  284. /* for terminating element */
  285. u8 opcode; /* if num_attached > 0, preferred */
  286. u16 sa; /* service action */
  287. u32 flags; /* OR-ed set of SDEB_F_* */
  288. int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
  289. const struct opcode_info_t *arrp; /* num_attached elements or NULL */
  290. u8 len_mask[16]; /* len_mask[0]-->cdb_len, then mask for cdb */
  291. /* 1 to min(cdb_len, 15); ignore cdb[15...] */
  292. };
  293. /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */
  294. enum sdeb_opcode_index {
  295. SDEB_I_INVALID_OPCODE = 0,
  296. SDEB_I_INQUIRY = 1,
  297. SDEB_I_REPORT_LUNS = 2,
  298. SDEB_I_REQUEST_SENSE = 3,
  299. SDEB_I_TEST_UNIT_READY = 4,
  300. SDEB_I_MODE_SENSE = 5, /* 6, 10 */
  301. SDEB_I_MODE_SELECT = 6, /* 6, 10 */
  302. SDEB_I_LOG_SENSE = 7,
  303. SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */
  304. SDEB_I_READ = 9, /* 6, 10, 12, 16 */
  305. SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */
  306. SDEB_I_START_STOP = 11,
  307. SDEB_I_SERV_ACT_IN_16 = 12, /* add ...SERV_ACT_IN_12 if needed */
  308. SDEB_I_SERV_ACT_OUT_16 = 13, /* add ...SERV_ACT_OUT_12 if needed */
  309. SDEB_I_MAINT_IN = 14,
  310. SDEB_I_MAINT_OUT = 15,
  311. SDEB_I_VERIFY = 16, /* 10 only */
  312. SDEB_I_VARIABLE_LEN = 17, /* READ(32), WRITE(32), WR_SCAT(32) */
  313. SDEB_I_RESERVE = 18, /* 6, 10 */
  314. SDEB_I_RELEASE = 19, /* 6, 10 */
  315. SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */
  316. SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */
  317. SDEB_I_ATA_PT = 22, /* 12, 16 */
  318. SDEB_I_SEND_DIAG = 23,
  319. SDEB_I_UNMAP = 24,
  320. SDEB_I_XDWRITEREAD = 25, /* 10 only */
  321. SDEB_I_WRITE_BUFFER = 26,
  322. SDEB_I_WRITE_SAME = 27, /* 10, 16 */
  323. SDEB_I_SYNC_CACHE = 28, /* 10, 16 */
  324. SDEB_I_COMP_WRITE = 29,
  325. SDEB_I_LAST_ELEMENT = 30, /* keep this last (previous + 1) */
  326. };
  327. static const unsigned char opcode_ind_arr[256] = {
  328. /* 0x0; 0x0->0x1f: 6 byte cdbs */
  329. SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
  330. 0, 0, 0, 0,
  331. SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
  332. 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
  333. SDEB_I_RELEASE,
  334. 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
  335. SDEB_I_ALLOW_REMOVAL, 0,
  336. /* 0x20; 0x20->0x3f: 10 byte cdbs */
  337. 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
  338. SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY,
  339. 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0,
  340. 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
  341. /* 0x40; 0x40->0x5f: 10 byte cdbs */
  342. 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
  343. 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
  344. 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
  345. SDEB_I_RELEASE,
  346. 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
  347. /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */
  348. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  349. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  350. 0, SDEB_I_VARIABLE_LEN,
  351. /* 0x80; 0x80->0x9f: 16 byte cdbs */
  352. 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
  353. SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0,
  354. 0, SDEB_I_SYNC_CACHE, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0,
  355. 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN_16, SDEB_I_SERV_ACT_OUT_16,
  356. /* 0xa0; 0xa0->0xbf: 12 byte cdbs */
  357. SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
  358. SDEB_I_MAINT_OUT, 0, 0, 0,
  359. SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE,
  360. 0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0,
  361. 0, 0, 0, 0, 0, 0, 0, 0,
  362. 0, 0, 0, 0, 0, 0, 0, 0,
  363. /* 0xc0; 0xc0->0xff: vendor specific */
  364. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  365. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  366. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  367. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  368. };
  369. /*
  370. * The following "response" functions return the SCSI mid-level's 4 byte
  371. * tuple-in-an-int. To handle commands with an IMMED bit, for a faster
  372. * command completion, they can mask their return value with
  373. * SDEG_RES_IMMED_MASK .
  374. */
  375. #define SDEG_RES_IMMED_MASK 0x40000000
  376. static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
  377. static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
  378. static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
  379. static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
  380. static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
  381. static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
  382. static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
  383. static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
  384. static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
  385. static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *);
  386. static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
  387. static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
  388. static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
  389. static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
  390. static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
  391. static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *);
  392. static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *);
  393. static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
  394. static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
  395. static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *);
  396. static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *);
  397. static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *);
  398. static int resp_sync_cache(struct scsi_cmnd *, struct sdebug_dev_info *);
  399. /*
  400. * The following are overflow arrays for cdbs that "hit" the same index in
  401. * the opcode_info_arr array. The most time sensitive (or commonly used) cdb
  402. * should be placed in opcode_info_arr[], the others should be placed here.
  403. */
  404. static const struct opcode_info_t msense_iarr[] = {
  405. {0, 0x1a, 0, F_D_IN, NULL, NULL,
  406. {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  407. };
  408. static const struct opcode_info_t mselect_iarr[] = {
  409. {0, 0x15, 0, F_D_OUT, NULL, NULL,
  410. {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  411. };
  412. static const struct opcode_info_t read_iarr[] = {
  413. {0, 0x28, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(10) */
  414. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
  415. 0, 0, 0, 0} },
  416. {0, 0x8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL, /* READ(6) */
  417. {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  418. {0, 0xa8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(12) */
  419. {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf,
  420. 0xc7, 0, 0, 0, 0} },
  421. };
  422. static const struct opcode_info_t write_iarr[] = {
  423. {0, 0x2a, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(10) */
  424. NULL, {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7,
  425. 0, 0, 0, 0, 0, 0} },
  426. {0, 0xa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) */
  427. NULL, {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0,
  428. 0, 0, 0} },
  429. {0, 0xaa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(12) */
  430. NULL, {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  431. 0xbf, 0xc7, 0, 0, 0, 0} },
  432. };
  433. static const struct opcode_info_t sa_in_16_iarr[] = {
  434. {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
  435. {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  436. 0xff, 0xff, 0xff, 0, 0xc7} }, /* GET LBA STATUS(16) */
  437. };
  438. static const struct opcode_info_t vl_iarr[] = { /* VARIABLE LENGTH */
  439. {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_dt0,
  440. NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa,
  441. 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */
  442. {0, 0x7f, 0x11, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
  443. NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8,
  444. 0, 0xff, 0xff, 0x0, 0x0} }, /* WRITE SCATTERED(32) */
  445. };
  446. static const struct opcode_info_t maint_in_iarr[] = { /* MAINT IN */
  447. {0, 0xa3, 0xc, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL,
  448. {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
  449. 0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */
  450. {0, 0xa3, 0xd, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL,
  451. {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
  452. 0, 0} }, /* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */
  453. };
  454. static const struct opcode_info_t write_same_iarr[] = {
  455. {0, 0x93, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_write_same_16, NULL,
  456. {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  457. 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* WRITE SAME(16) */
  458. };
  459. static const struct opcode_info_t reserve_iarr[] = {
  460. {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */
  461. {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  462. };
  463. static const struct opcode_info_t release_iarr[] = {
  464. {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */
  465. {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  466. };
  467. static const struct opcode_info_t sync_cache_iarr[] = {
  468. {0, 0x91, 0, F_SYNC_DELAY | F_M_ACCESS, resp_sync_cache, NULL,
  469. {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  470. 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* SYNC_CACHE (16) */
  471. };
  472. /* This array is accessed via SDEB_I_* values. Make sure all are mapped,
  473. * plus the terminating elements for logic that scans this table such as
  474. * REPORT SUPPORTED OPERATION CODES. */
  475. static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = {
  476. /* 0 */
  477. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* unknown opcodes */
  478. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  479. {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */
  480. {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  481. {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
  482. {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
  483. 0, 0} }, /* REPORT LUNS */
  484. {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL,
  485. {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  486. {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
  487. {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  488. /* 5 */
  489. {ARRAY_SIZE(msense_iarr), 0x5a, 0, F_D_IN, /* MODE SENSE(10) */
  490. resp_mode_sense, msense_iarr, {10, 0xf8, 0xff, 0xff, 0, 0, 0,
  491. 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  492. {ARRAY_SIZE(mselect_iarr), 0x55, 0, F_D_OUT, /* MODE SELECT(10) */
  493. resp_mode_select, mselect_iarr, {10, 0xf1, 0, 0, 0, 0, 0, 0xff,
  494. 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  495. {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, /* LOG SENSE */
  496. {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
  497. 0, 0, 0} },
  498. {0, 0x25, 0, F_D_IN, resp_readcap, NULL, /* READ CAPACITY(10) */
  499. {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
  500. 0, 0} },
  501. {ARRAY_SIZE(read_iarr), 0x88, 0, F_D_IN | FF_MEDIA_IO, /* READ(16) */
  502. resp_read_dt0, read_iarr, {16, 0xfe, 0xff, 0xff, 0xff, 0xff,
  503. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
  504. /* 10 */
  505. {ARRAY_SIZE(write_iarr), 0x8a, 0, F_D_OUT | FF_MEDIA_IO,
  506. resp_write_dt0, write_iarr, /* WRITE(16) */
  507. {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  508. 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
  509. {0, 0x1b, 0, F_SSU_DELAY, resp_start_stop, NULL,/* START STOP UNIT */
  510. {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  511. {ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, F_SA_LOW | F_D_IN,
  512. resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */
  513. {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  514. 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} },
  515. {0, 0x9f, 0x12, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
  516. NULL, {16, 0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff,
  517. 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* SA_OUT(16), WRITE SCAT(16) */
  518. {ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, F_SA_LOW | F_D_IN,
  519. resp_report_tgtpgs, /* MAINT IN, REPORT TARGET PORT GROUPS */
  520. maint_in_iarr, {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff,
  521. 0xff, 0, 0xc7, 0, 0, 0, 0} },
  522. /* 15 */
  523. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
  524. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  525. {0, 0x2f, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, NULL, NULL, /* VERIFY(10) */
  526. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7,
  527. 0, 0, 0, 0, 0, 0} },
  528. {ARRAY_SIZE(vl_iarr), 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_MEDIA_IO,
  529. resp_read_dt0, vl_iarr, /* VARIABLE LENGTH, READ(32) */
  530. {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff,
  531. 0xff, 0xff} },
  532. {ARRAY_SIZE(reserve_iarr), 0x56, 0, F_D_OUT,
  533. NULL, reserve_iarr, /* RESERVE(10) <no response function> */
  534. {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
  535. 0} },
  536. {ARRAY_SIZE(release_iarr), 0x57, 0, F_D_OUT,
  537. NULL, release_iarr, /* RELEASE(10) <no response function> */
  538. {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
  539. 0} },
  540. /* 20 */
  541. {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */
  542. {6, 0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  543. {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */
  544. {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  545. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
  546. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  547. {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */
  548. {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  549. {0, 0x42, 0, F_D_OUT | FF_MEDIA_IO, resp_unmap, NULL, /* UNMAP */
  550. {10, 0x1, 0, 0, 0, 0, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  551. /* 25 */
  552. {0, 0x53, 0, F_D_IN | F_D_OUT | FF_MEDIA_IO, resp_xdwriteread_10,
  553. NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7,
  554. 0, 0, 0, 0, 0, 0} }, /* XDWRITEREAD(10) */
  555. {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL,
  556. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0,
  557. 0, 0, 0, 0} }, /* WRITE_BUFFER */
  558. {ARRAY_SIZE(write_same_iarr), 0x41, 0, F_D_OUT_MAYBE | FF_MEDIA_IO,
  559. resp_write_same_10, write_same_iarr, /* WRITE SAME(10) */
  560. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0,
  561. 0, 0, 0, 0, 0} },
  562. {ARRAY_SIZE(sync_cache_iarr), 0x35, 0, F_SYNC_DELAY | F_M_ACCESS,
  563. resp_sync_cache, sync_cache_iarr,
  564. {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
  565. 0, 0, 0, 0} }, /* SYNC_CACHE (10) */
  566. {0, 0x89, 0, F_D_OUT | FF_MEDIA_IO, resp_comp_write, NULL,
  567. {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
  568. 0, 0xff, 0x3f, 0xc7} }, /* COMPARE AND WRITE */
  569. /* 30 */
  570. {0xff, 0, 0, 0, NULL, NULL, /* terminating element */
  571. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  572. };
  573. static int sdebug_add_host = DEF_NUM_HOST;
  574. static int sdebug_ato = DEF_ATO;
  575. static int sdebug_cdb_len = DEF_CDB_LEN;
  576. static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */
  577. static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB;
  578. static int sdebug_dif = DEF_DIF;
  579. static int sdebug_dix = DEF_DIX;
  580. static int sdebug_dsense = DEF_D_SENSE;
  581. static int sdebug_every_nth = DEF_EVERY_NTH;
  582. static int sdebug_fake_rw = DEF_FAKE_RW;
  583. static unsigned int sdebug_guard = DEF_GUARD;
  584. static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED;
  585. static int sdebug_max_luns = DEF_MAX_LUNS;
  586. static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */
  587. static unsigned int sdebug_medium_error_start = OPT_MEDIUM_ERR_ADDR;
  588. static int sdebug_medium_error_count = OPT_MEDIUM_ERR_NUM;
  589. static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */
  590. static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */
  591. static int sdebug_no_lun_0 = DEF_NO_LUN_0;
  592. static int sdebug_no_uld;
  593. static int sdebug_num_parts = DEF_NUM_PARTS;
  594. static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */
  595. static int sdebug_opt_blks = DEF_OPT_BLKS;
  596. static int sdebug_opts = DEF_OPTS;
  597. static int sdebug_physblk_exp = DEF_PHYSBLK_EXP;
  598. static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP;
  599. static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */
  600. static int sdebug_scsi_level = DEF_SCSI_LEVEL;
  601. static int sdebug_sector_size = DEF_SECTOR_SIZE;
  602. static int sdebug_virtual_gb = DEF_VIRTUAL_GB;
  603. static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
  604. static unsigned int sdebug_lbpu = DEF_LBPU;
  605. static unsigned int sdebug_lbpws = DEF_LBPWS;
  606. static unsigned int sdebug_lbpws10 = DEF_LBPWS10;
  607. static unsigned int sdebug_lbprz = DEF_LBPRZ;
  608. static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
  609. static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY;
  610. static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
  611. static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
  612. static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH;
  613. static int sdebug_uuid_ctl = DEF_UUID_CTL;
  614. static bool sdebug_removable = DEF_REMOVABLE;
  615. static bool sdebug_clustering;
  616. static bool sdebug_host_lock = DEF_HOST_LOCK;
  617. static bool sdebug_strict = DEF_STRICT;
  618. static bool sdebug_any_injecting_opt;
  619. static bool sdebug_verbose;
  620. static bool have_dif_prot;
  621. static bool write_since_sync;
  622. static bool sdebug_statistics = DEF_STATISTICS;
  623. static unsigned int sdebug_store_sectors;
  624. static sector_t sdebug_capacity; /* in sectors */
  625. /* old BIOS stuff, kernel may get rid of them but some mode sense pages
  626. may still need them */
  627. static int sdebug_heads; /* heads per disk */
  628. static int sdebug_cylinders_per; /* cylinders per surface */
  629. static int sdebug_sectors_per; /* sectors per cylinder */
  630. static LIST_HEAD(sdebug_host_list);
  631. static DEFINE_SPINLOCK(sdebug_host_list_lock);
  632. static unsigned char *fake_storep; /* ramdisk storage */
  633. static struct t10_pi_tuple *dif_storep; /* protection info */
  634. static void *map_storep; /* provisioning map */
  635. static unsigned long map_size;
  636. static int num_aborts;
  637. static int num_dev_resets;
  638. static int num_target_resets;
  639. static int num_bus_resets;
  640. static int num_host_resets;
  641. static int dix_writes;
  642. static int dix_reads;
  643. static int dif_errors;
  644. static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */
  645. static struct sdebug_queue *sdebug_q_arr; /* ptr to array of submit queues */
  646. static DEFINE_RWLOCK(atomic_rw);
  647. static char sdebug_proc_name[] = MY_NAME;
  648. static const char *my_name = MY_NAME;
  649. static struct bus_type pseudo_lld_bus;
  650. static struct device_driver sdebug_driverfs_driver = {
  651. .name = sdebug_proc_name,
  652. .bus = &pseudo_lld_bus,
  653. };
  654. static const int check_condition_result =
  655. (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  656. static const int illegal_condition_result =
  657. (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
  658. static const int device_qfull_result =
  659. (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL;
  660. /* Only do the extra work involved in logical block provisioning if one or
  661. * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing
  662. * real reads and writes (i.e. not skipping them for speed).
  663. */
  664. static inline bool scsi_debug_lbp(void)
  665. {
  666. return 0 == sdebug_fake_rw &&
  667. (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10);
  668. }
  669. static void *lba2fake_store(unsigned long long lba)
  670. {
  671. lba = do_div(lba, sdebug_store_sectors);
  672. return fake_storep + lba * sdebug_sector_size;
  673. }
  674. static struct t10_pi_tuple *dif_store(sector_t sector)
  675. {
  676. sector = sector_div(sector, sdebug_store_sectors);
  677. return dif_storep + sector;
  678. }
  679. static void sdebug_max_tgts_luns(void)
  680. {
  681. struct sdebug_host_info *sdbg_host;
  682. struct Scsi_Host *hpnt;
  683. spin_lock(&sdebug_host_list_lock);
  684. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  685. hpnt = sdbg_host->shost;
  686. if ((hpnt->this_id >= 0) &&
  687. (sdebug_num_tgts > hpnt->this_id))
  688. hpnt->max_id = sdebug_num_tgts + 1;
  689. else
  690. hpnt->max_id = sdebug_num_tgts;
  691. /* sdebug_max_luns; */
  692. hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
  693. }
  694. spin_unlock(&sdebug_host_list_lock);
  695. }
  696. enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1};
  697. /* Set in_bit to -1 to indicate no bit position of invalid field */
  698. static void mk_sense_invalid_fld(struct scsi_cmnd *scp,
  699. enum sdeb_cmd_data c_d,
  700. int in_byte, int in_bit)
  701. {
  702. unsigned char *sbuff;
  703. u8 sks[4];
  704. int sl, asc;
  705. sbuff = scp->sense_buffer;
  706. if (!sbuff) {
  707. sdev_printk(KERN_ERR, scp->device,
  708. "%s: sense_buffer is NULL\n", __func__);
  709. return;
  710. }
  711. asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST;
  712. memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
  713. scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0);
  714. memset(sks, 0, sizeof(sks));
  715. sks[0] = 0x80;
  716. if (c_d)
  717. sks[0] |= 0x40;
  718. if (in_bit >= 0) {
  719. sks[0] |= 0x8;
  720. sks[0] |= 0x7 & in_bit;
  721. }
  722. put_unaligned_be16(in_byte, sks + 1);
  723. if (sdebug_dsense) {
  724. sl = sbuff[7] + 8;
  725. sbuff[7] = sl;
  726. sbuff[sl] = 0x2;
  727. sbuff[sl + 1] = 0x6;
  728. memcpy(sbuff + sl + 4, sks, 3);
  729. } else
  730. memcpy(sbuff + 15, sks, 3);
  731. if (sdebug_verbose)
  732. sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq"
  733. "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
  734. my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit);
  735. }
  736. static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq)
  737. {
  738. unsigned char *sbuff;
  739. sbuff = scp->sense_buffer;
  740. if (!sbuff) {
  741. sdev_printk(KERN_ERR, scp->device,
  742. "%s: sense_buffer is NULL\n", __func__);
  743. return;
  744. }
  745. memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
  746. scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq);
  747. if (sdebug_verbose)
  748. sdev_printk(KERN_INFO, scp->device,
  749. "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n",
  750. my_name, key, asc, asq);
  751. }
  752. static void mk_sense_invalid_opcode(struct scsi_cmnd *scp)
  753. {
  754. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
  755. }
  756. static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  757. {
  758. if (sdebug_verbose) {
  759. if (0x1261 == cmd)
  760. sdev_printk(KERN_INFO, dev,
  761. "%s: BLKFLSBUF [0x1261]\n", __func__);
  762. else if (0x5331 == cmd)
  763. sdev_printk(KERN_INFO, dev,
  764. "%s: CDROM_GET_CAPABILITY [0x5331]\n",
  765. __func__);
  766. else
  767. sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n",
  768. __func__, cmd);
  769. }
  770. return -EINVAL;
  771. /* return -ENOTTY; // correct return but upsets fdisk */
  772. }
  773. static void config_cdb_len(struct scsi_device *sdev)
  774. {
  775. switch (sdebug_cdb_len) {
  776. case 6: /* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */
  777. sdev->use_10_for_rw = false;
  778. sdev->use_16_for_rw = false;
  779. sdev->use_10_for_ms = false;
  780. break;
  781. case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */
  782. sdev->use_10_for_rw = true;
  783. sdev->use_16_for_rw = false;
  784. sdev->use_10_for_ms = false;
  785. break;
  786. case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */
  787. sdev->use_10_for_rw = true;
  788. sdev->use_16_for_rw = false;
  789. sdev->use_10_for_ms = true;
  790. break;
  791. case 16:
  792. sdev->use_10_for_rw = false;
  793. sdev->use_16_for_rw = true;
  794. sdev->use_10_for_ms = true;
  795. break;
  796. case 32: /* No knobs to suggest this so same as 16 for now */
  797. sdev->use_10_for_rw = false;
  798. sdev->use_16_for_rw = true;
  799. sdev->use_10_for_ms = true;
  800. break;
  801. default:
  802. pr_warn("unexpected cdb_len=%d, force to 10\n",
  803. sdebug_cdb_len);
  804. sdev->use_10_for_rw = true;
  805. sdev->use_16_for_rw = false;
  806. sdev->use_10_for_ms = false;
  807. sdebug_cdb_len = 10;
  808. break;
  809. }
  810. }
  811. static void all_config_cdb_len(void)
  812. {
  813. struct sdebug_host_info *sdbg_host;
  814. struct Scsi_Host *shost;
  815. struct scsi_device *sdev;
  816. spin_lock(&sdebug_host_list_lock);
  817. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  818. shost = sdbg_host->shost;
  819. shost_for_each_device(sdev, shost) {
  820. config_cdb_len(sdev);
  821. }
  822. }
  823. spin_unlock(&sdebug_host_list_lock);
  824. }
  825. static void clear_luns_changed_on_target(struct sdebug_dev_info *devip)
  826. {
  827. struct sdebug_host_info *sdhp;
  828. struct sdebug_dev_info *dp;
  829. spin_lock(&sdebug_host_list_lock);
  830. list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
  831. list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
  832. if ((devip->sdbg_host == dp->sdbg_host) &&
  833. (devip->target == dp->target))
  834. clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
  835. }
  836. }
  837. spin_unlock(&sdebug_host_list_lock);
  838. }
  839. static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  840. {
  841. int k;
  842. k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
  843. if (k != SDEBUG_NUM_UAS) {
  844. const char *cp = NULL;
  845. switch (k) {
  846. case SDEBUG_UA_POR:
  847. mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
  848. POWER_ON_RESET_ASCQ);
  849. if (sdebug_verbose)
  850. cp = "power on reset";
  851. break;
  852. case SDEBUG_UA_BUS_RESET:
  853. mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
  854. BUS_RESET_ASCQ);
  855. if (sdebug_verbose)
  856. cp = "bus reset";
  857. break;
  858. case SDEBUG_UA_MODE_CHANGED:
  859. mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
  860. MODE_CHANGED_ASCQ);
  861. if (sdebug_verbose)
  862. cp = "mode parameters changed";
  863. break;
  864. case SDEBUG_UA_CAPACITY_CHANGED:
  865. mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
  866. CAPACITY_CHANGED_ASCQ);
  867. if (sdebug_verbose)
  868. cp = "capacity data changed";
  869. break;
  870. case SDEBUG_UA_MICROCODE_CHANGED:
  871. mk_sense_buffer(scp, UNIT_ATTENTION,
  872. TARGET_CHANGED_ASC,
  873. MICROCODE_CHANGED_ASCQ);
  874. if (sdebug_verbose)
  875. cp = "microcode has been changed";
  876. break;
  877. case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET:
  878. mk_sense_buffer(scp, UNIT_ATTENTION,
  879. TARGET_CHANGED_ASC,
  880. MICROCODE_CHANGED_WO_RESET_ASCQ);
  881. if (sdebug_verbose)
  882. cp = "microcode has been changed without reset";
  883. break;
  884. case SDEBUG_UA_LUNS_CHANGED:
  885. /*
  886. * SPC-3 behavior is to report a UNIT ATTENTION with
  887. * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN
  888. * on the target, until a REPORT LUNS command is
  889. * received. SPC-4 behavior is to report it only once.
  890. * NOTE: sdebug_scsi_level does not use the same
  891. * values as struct scsi_device->scsi_level.
  892. */
  893. if (sdebug_scsi_level >= 6) /* SPC-4 and above */
  894. clear_luns_changed_on_target(devip);
  895. mk_sense_buffer(scp, UNIT_ATTENTION,
  896. TARGET_CHANGED_ASC,
  897. LUNS_CHANGED_ASCQ);
  898. if (sdebug_verbose)
  899. cp = "reported luns data has changed";
  900. break;
  901. default:
  902. pr_warn("unexpected unit attention code=%d\n", k);
  903. if (sdebug_verbose)
  904. cp = "unknown";
  905. break;
  906. }
  907. clear_bit(k, devip->uas_bm);
  908. if (sdebug_verbose)
  909. sdev_printk(KERN_INFO, scp->device,
  910. "%s reports: Unit attention: %s\n",
  911. my_name, cp);
  912. return check_condition_result;
  913. }
  914. return 0;
  915. }
  916. /* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */
  917. static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  918. int arr_len)
  919. {
  920. int act_len;
  921. struct scsi_data_buffer *sdb = scsi_in(scp);
  922. if (!sdb->length)
  923. return 0;
  924. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
  925. return DID_ERROR << 16;
  926. act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
  927. arr, arr_len);
  928. sdb->resid = scsi_bufflen(scp) - act_len;
  929. return 0;
  930. }
  931. /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else
  932. * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple
  933. * calls, not required to write in ascending offset order. Assumes resid
  934. * set to scsi_bufflen() prior to any calls.
  935. */
  936. static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr,
  937. int arr_len, unsigned int off_dst)
  938. {
  939. int act_len, n;
  940. struct scsi_data_buffer *sdb = scsi_in(scp);
  941. off_t skip = off_dst;
  942. if (sdb->length <= off_dst)
  943. return 0;
  944. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
  945. return DID_ERROR << 16;
  946. act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents,
  947. arr, arr_len, skip);
  948. pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n",
  949. __func__, off_dst, scsi_bufflen(scp), act_len, sdb->resid);
  950. n = (int)scsi_bufflen(scp) - ((int)off_dst + act_len);
  951. sdb->resid = min(sdb->resid, n);
  952. return 0;
  953. }
  954. /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into
  955. * 'arr' or -1 if error.
  956. */
  957. static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  958. int arr_len)
  959. {
  960. if (!scsi_bufflen(scp))
  961. return 0;
  962. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE))
  963. return -1;
  964. return scsi_sg_copy_to_buffer(scp, arr, arr_len);
  965. }
  966. static char sdebug_inq_vendor_id[9] = "Linux ";
  967. static char sdebug_inq_product_id[17] = "scsi_debug ";
  968. static char sdebug_inq_product_rev[5] = SDEBUG_VERSION;
  969. /* Use some locally assigned NAAs for SAS addresses. */
  970. static const u64 naa3_comp_a = 0x3222222000000000ULL;
  971. static const u64 naa3_comp_b = 0x3333333000000000ULL;
  972. static const u64 naa3_comp_c = 0x3111111000000000ULL;
  973. /* Device identification VPD page. Returns number of bytes placed in arr */
  974. static int inquiry_vpd_83(unsigned char *arr, int port_group_id,
  975. int target_dev_id, int dev_id_num,
  976. const char *dev_id_str, int dev_id_str_len,
  977. const uuid_t *lu_name)
  978. {
  979. int num, port_a;
  980. char b[32];
  981. port_a = target_dev_id + 1;
  982. /* T10 vendor identifier field format (faked) */
  983. arr[0] = 0x2; /* ASCII */
  984. arr[1] = 0x1;
  985. arr[2] = 0x0;
  986. memcpy(&arr[4], sdebug_inq_vendor_id, 8);
  987. memcpy(&arr[12], sdebug_inq_product_id, 16);
  988. memcpy(&arr[28], dev_id_str, dev_id_str_len);
  989. num = 8 + 16 + dev_id_str_len;
  990. arr[3] = num;
  991. num += 4;
  992. if (dev_id_num >= 0) {
  993. if (sdebug_uuid_ctl) {
  994. /* Locally assigned UUID */
  995. arr[num++] = 0x1; /* binary (not necessarily sas) */
  996. arr[num++] = 0xa; /* PIV=0, lu, naa */
  997. arr[num++] = 0x0;
  998. arr[num++] = 0x12;
  999. arr[num++] = 0x10; /* uuid type=1, locally assigned */
  1000. arr[num++] = 0x0;
  1001. memcpy(arr + num, lu_name, 16);
  1002. num += 16;
  1003. } else {
  1004. /* NAA-3, Logical unit identifier (binary) */
  1005. arr[num++] = 0x1; /* binary (not necessarily sas) */
  1006. arr[num++] = 0x3; /* PIV=0, lu, naa */
  1007. arr[num++] = 0x0;
  1008. arr[num++] = 0x8;
  1009. put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num);
  1010. num += 8;
  1011. }
  1012. /* Target relative port number */
  1013. arr[num++] = 0x61; /* proto=sas, binary */
  1014. arr[num++] = 0x94; /* PIV=1, target port, rel port */
  1015. arr[num++] = 0x0; /* reserved */
  1016. arr[num++] = 0x4; /* length */
  1017. arr[num++] = 0x0; /* reserved */
  1018. arr[num++] = 0x0; /* reserved */
  1019. arr[num++] = 0x0;
  1020. arr[num++] = 0x1; /* relative port A */
  1021. }
  1022. /* NAA-3, Target port identifier */
  1023. arr[num++] = 0x61; /* proto=sas, binary */
  1024. arr[num++] = 0x93; /* piv=1, target port, naa */
  1025. arr[num++] = 0x0;
  1026. arr[num++] = 0x8;
  1027. put_unaligned_be64(naa3_comp_a + port_a, arr + num);
  1028. num += 8;
  1029. /* NAA-3, Target port group identifier */
  1030. arr[num++] = 0x61; /* proto=sas, binary */
  1031. arr[num++] = 0x95; /* piv=1, target port group id */
  1032. arr[num++] = 0x0;
  1033. arr[num++] = 0x4;
  1034. arr[num++] = 0;
  1035. arr[num++] = 0;
  1036. put_unaligned_be16(port_group_id, arr + num);
  1037. num += 2;
  1038. /* NAA-3, Target device identifier */
  1039. arr[num++] = 0x61; /* proto=sas, binary */
  1040. arr[num++] = 0xa3; /* piv=1, target device, naa */
  1041. arr[num++] = 0x0;
  1042. arr[num++] = 0x8;
  1043. put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num);
  1044. num += 8;
  1045. /* SCSI name string: Target device identifier */
  1046. arr[num++] = 0x63; /* proto=sas, UTF-8 */
  1047. arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
  1048. arr[num++] = 0x0;
  1049. arr[num++] = 24;
  1050. memcpy(arr + num, "naa.32222220", 12);
  1051. num += 12;
  1052. snprintf(b, sizeof(b), "%08X", target_dev_id);
  1053. memcpy(arr + num, b, 8);
  1054. num += 8;
  1055. memset(arr + num, 0, 4);
  1056. num += 4;
  1057. return num;
  1058. }
  1059. static unsigned char vpd84_data[] = {
  1060. /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
  1061. 0x22,0x22,0x22,0x0,0xbb,0x1,
  1062. 0x22,0x22,0x22,0x0,0xbb,0x2,
  1063. };
  1064. /* Software interface identification VPD page */
  1065. static int inquiry_vpd_84(unsigned char *arr)
  1066. {
  1067. memcpy(arr, vpd84_data, sizeof(vpd84_data));
  1068. return sizeof(vpd84_data);
  1069. }
  1070. /* Management network addresses VPD page */
  1071. static int inquiry_vpd_85(unsigned char *arr)
  1072. {
  1073. int num = 0;
  1074. const char *na1 = "https://www.kernel.org/config";
  1075. const char *na2 = "http://www.kernel.org/log";
  1076. int plen, olen;
  1077. arr[num++] = 0x1; /* lu, storage config */
  1078. arr[num++] = 0x0; /* reserved */
  1079. arr[num++] = 0x0;
  1080. olen = strlen(na1);
  1081. plen = olen + 1;
  1082. if (plen % 4)
  1083. plen = ((plen / 4) + 1) * 4;
  1084. arr[num++] = plen; /* length, null termianted, padded */
  1085. memcpy(arr + num, na1, olen);
  1086. memset(arr + num + olen, 0, plen - olen);
  1087. num += plen;
  1088. arr[num++] = 0x4; /* lu, logging */
  1089. arr[num++] = 0x0; /* reserved */
  1090. arr[num++] = 0x0;
  1091. olen = strlen(na2);
  1092. plen = olen + 1;
  1093. if (plen % 4)
  1094. plen = ((plen / 4) + 1) * 4;
  1095. arr[num++] = plen; /* length, null terminated, padded */
  1096. memcpy(arr + num, na2, olen);
  1097. memset(arr + num + olen, 0, plen - olen);
  1098. num += plen;
  1099. return num;
  1100. }
  1101. /* SCSI ports VPD page */
  1102. static int inquiry_vpd_88(unsigned char *arr, int target_dev_id)
  1103. {
  1104. int num = 0;
  1105. int port_a, port_b;
  1106. port_a = target_dev_id + 1;
  1107. port_b = port_a + 1;
  1108. arr[num++] = 0x0; /* reserved */
  1109. arr[num++] = 0x0; /* reserved */
  1110. arr[num++] = 0x0;
  1111. arr[num++] = 0x1; /* relative port 1 (primary) */
  1112. memset(arr + num, 0, 6);
  1113. num += 6;
  1114. arr[num++] = 0x0;
  1115. arr[num++] = 12; /* length tp descriptor */
  1116. /* naa-5 target port identifier (A) */
  1117. arr[num++] = 0x61; /* proto=sas, binary */
  1118. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  1119. arr[num++] = 0x0; /* reserved */
  1120. arr[num++] = 0x8; /* length */
  1121. put_unaligned_be64(naa3_comp_a + port_a, arr + num);
  1122. num += 8;
  1123. arr[num++] = 0x0; /* reserved */
  1124. arr[num++] = 0x0; /* reserved */
  1125. arr[num++] = 0x0;
  1126. arr[num++] = 0x2; /* relative port 2 (secondary) */
  1127. memset(arr + num, 0, 6);
  1128. num += 6;
  1129. arr[num++] = 0x0;
  1130. arr[num++] = 12; /* length tp descriptor */
  1131. /* naa-5 target port identifier (B) */
  1132. arr[num++] = 0x61; /* proto=sas, binary */
  1133. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  1134. arr[num++] = 0x0; /* reserved */
  1135. arr[num++] = 0x8; /* length */
  1136. put_unaligned_be64(naa3_comp_a + port_b, arr + num);
  1137. num += 8;
  1138. return num;
  1139. }
  1140. static unsigned char vpd89_data[] = {
  1141. /* from 4th byte */ 0,0,0,0,
  1142. 'l','i','n','u','x',' ',' ',' ',
  1143. 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
  1144. '1','2','3','4',
  1145. 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  1146. 0xec,0,0,0,
  1147. 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
  1148. 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
  1149. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
  1150. 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
  1151. 0x53,0x41,
  1152. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  1153. 0x20,0x20,
  1154. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  1155. 0x10,0x80,
  1156. 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
  1157. 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
  1158. 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
  1159. 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
  1160. 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
  1161. 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
  1162. 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
  1163. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1164. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1165. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1166. 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
  1167. 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
  1168. 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
  1169. 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
  1170. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1171. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1172. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1173. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1174. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1175. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1176. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1177. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1178. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1179. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1180. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1181. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
  1182. };
  1183. /* ATA Information VPD page */
  1184. static int inquiry_vpd_89(unsigned char *arr)
  1185. {
  1186. memcpy(arr, vpd89_data, sizeof(vpd89_data));
  1187. return sizeof(vpd89_data);
  1188. }
  1189. static unsigned char vpdb0_data[] = {
  1190. /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
  1191. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1192. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1193. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1194. };
  1195. /* Block limits VPD page (SBC-3) */
  1196. static int inquiry_vpd_b0(unsigned char *arr)
  1197. {
  1198. unsigned int gran;
  1199. memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
  1200. /* Optimal transfer length granularity */
  1201. if (sdebug_opt_xferlen_exp != 0 &&
  1202. sdebug_physblk_exp < sdebug_opt_xferlen_exp)
  1203. gran = 1 << sdebug_opt_xferlen_exp;
  1204. else
  1205. gran = 1 << sdebug_physblk_exp;
  1206. put_unaligned_be16(gran, arr + 2);
  1207. /* Maximum Transfer Length */
  1208. if (sdebug_store_sectors > 0x400)
  1209. put_unaligned_be32(sdebug_store_sectors, arr + 4);
  1210. /* Optimal Transfer Length */
  1211. put_unaligned_be32(sdebug_opt_blks, &arr[8]);
  1212. if (sdebug_lbpu) {
  1213. /* Maximum Unmap LBA Count */
  1214. put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]);
  1215. /* Maximum Unmap Block Descriptor Count */
  1216. put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]);
  1217. }
  1218. /* Unmap Granularity Alignment */
  1219. if (sdebug_unmap_alignment) {
  1220. put_unaligned_be32(sdebug_unmap_alignment, &arr[28]);
  1221. arr[28] |= 0x80; /* UGAVALID */
  1222. }
  1223. /* Optimal Unmap Granularity */
  1224. put_unaligned_be32(sdebug_unmap_granularity, &arr[24]);
  1225. /* Maximum WRITE SAME Length */
  1226. put_unaligned_be64(sdebug_write_same_length, &arr[32]);
  1227. return 0x3c; /* Mandatory page length for Logical Block Provisioning */
  1228. return sizeof(vpdb0_data);
  1229. }
  1230. /* Block device characteristics VPD page (SBC-3) */
  1231. static int inquiry_vpd_b1(unsigned char *arr)
  1232. {
  1233. memset(arr, 0, 0x3c);
  1234. arr[0] = 0;
  1235. arr[1] = 1; /* non rotating medium (e.g. solid state) */
  1236. arr[2] = 0;
  1237. arr[3] = 5; /* less than 1.8" */
  1238. return 0x3c;
  1239. }
  1240. /* Logical block provisioning VPD page (SBC-4) */
  1241. static int inquiry_vpd_b2(unsigned char *arr)
  1242. {
  1243. memset(arr, 0, 0x4);
  1244. arr[0] = 0; /* threshold exponent */
  1245. if (sdebug_lbpu)
  1246. arr[1] = 1 << 7;
  1247. if (sdebug_lbpws)
  1248. arr[1] |= 1 << 6;
  1249. if (sdebug_lbpws10)
  1250. arr[1] |= 1 << 5;
  1251. if (sdebug_lbprz && scsi_debug_lbp())
  1252. arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */
  1253. /* anc_sup=0; dp=0 (no provisioning group descriptor) */
  1254. /* minimum_percentage=0; provisioning_type=0 (unknown) */
  1255. /* threshold_percentage=0 */
  1256. return 0x4;
  1257. }
  1258. #define SDEBUG_LONG_INQ_SZ 96
  1259. #define SDEBUG_MAX_INQ_ARR_SZ 584
  1260. static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  1261. {
  1262. unsigned char pq_pdt;
  1263. unsigned char *arr;
  1264. unsigned char *cmd = scp->cmnd;
  1265. int alloc_len, n, ret;
  1266. bool have_wlun, is_disk;
  1267. alloc_len = get_unaligned_be16(cmd + 3);
  1268. arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
  1269. if (! arr)
  1270. return DID_REQUEUE << 16;
  1271. is_disk = (sdebug_ptype == TYPE_DISK);
  1272. have_wlun = scsi_is_wlun(scp->device->lun);
  1273. if (have_wlun)
  1274. pq_pdt = TYPE_WLUN; /* present, wlun */
  1275. else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL))
  1276. pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */
  1277. else
  1278. pq_pdt = (sdebug_ptype & 0x1f);
  1279. arr[0] = pq_pdt;
  1280. if (0x2 & cmd[1]) { /* CMDDT bit set */
  1281. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
  1282. kfree(arr);
  1283. return check_condition_result;
  1284. } else if (0x1 & cmd[1]) { /* EVPD bit set */
  1285. int lu_id_num, port_group_id, target_dev_id, len;
  1286. char lu_id_str[6];
  1287. int host_no = devip->sdbg_host->shost->host_no;
  1288. port_group_id = (((host_no + 1) & 0x7f) << 8) +
  1289. (devip->channel & 0x7f);
  1290. if (sdebug_vpd_use_hostno == 0)
  1291. host_no = 0;
  1292. lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
  1293. (devip->target * 1000) + devip->lun);
  1294. target_dev_id = ((host_no + 1) * 2000) +
  1295. (devip->target * 1000) - 3;
  1296. len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
  1297. if (0 == cmd[2]) { /* supported vital product data pages */
  1298. arr[1] = cmd[2]; /*sanity */
  1299. n = 4;
  1300. arr[n++] = 0x0; /* this page */
  1301. arr[n++] = 0x80; /* unit serial number */
  1302. arr[n++] = 0x83; /* device identification */
  1303. arr[n++] = 0x84; /* software interface ident. */
  1304. arr[n++] = 0x85; /* management network addresses */
  1305. arr[n++] = 0x86; /* extended inquiry */
  1306. arr[n++] = 0x87; /* mode page policy */
  1307. arr[n++] = 0x88; /* SCSI ports */
  1308. if (is_disk) { /* SBC only */
  1309. arr[n++] = 0x89; /* ATA information */
  1310. arr[n++] = 0xb0; /* Block limits */
  1311. arr[n++] = 0xb1; /* Block characteristics */
  1312. arr[n++] = 0xb2; /* Logical Block Prov */
  1313. }
  1314. arr[3] = n - 4; /* number of supported VPD pages */
  1315. } else if (0x80 == cmd[2]) { /* unit serial number */
  1316. arr[1] = cmd[2]; /*sanity */
  1317. arr[3] = len;
  1318. memcpy(&arr[4], lu_id_str, len);
  1319. } else if (0x83 == cmd[2]) { /* device identification */
  1320. arr[1] = cmd[2]; /*sanity */
  1321. arr[3] = inquiry_vpd_83(&arr[4], port_group_id,
  1322. target_dev_id, lu_id_num,
  1323. lu_id_str, len,
  1324. &devip->lu_name);
  1325. } else if (0x84 == cmd[2]) { /* Software interface ident. */
  1326. arr[1] = cmd[2]; /*sanity */
  1327. arr[3] = inquiry_vpd_84(&arr[4]);
  1328. } else if (0x85 == cmd[2]) { /* Management network addresses */
  1329. arr[1] = cmd[2]; /*sanity */
  1330. arr[3] = inquiry_vpd_85(&arr[4]);
  1331. } else if (0x86 == cmd[2]) { /* extended inquiry */
  1332. arr[1] = cmd[2]; /*sanity */
  1333. arr[3] = 0x3c; /* number of following entries */
  1334. if (sdebug_dif == T10_PI_TYPE3_PROTECTION)
  1335. arr[4] = 0x4; /* SPT: GRD_CHK:1 */
  1336. else if (have_dif_prot)
  1337. arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */
  1338. else
  1339. arr[4] = 0x0; /* no protection stuff */
  1340. arr[5] = 0x7; /* head of q, ordered + simple q's */
  1341. } else if (0x87 == cmd[2]) { /* mode page policy */
  1342. arr[1] = cmd[2]; /*sanity */
  1343. arr[3] = 0x8; /* number of following entries */
  1344. arr[4] = 0x2; /* disconnect-reconnect mp */
  1345. arr[6] = 0x80; /* mlus, shared */
  1346. arr[8] = 0x18; /* protocol specific lu */
  1347. arr[10] = 0x82; /* mlus, per initiator port */
  1348. } else if (0x88 == cmd[2]) { /* SCSI Ports */
  1349. arr[1] = cmd[2]; /*sanity */
  1350. arr[3] = inquiry_vpd_88(&arr[4], target_dev_id);
  1351. } else if (is_disk && 0x89 == cmd[2]) { /* ATA information */
  1352. arr[1] = cmd[2]; /*sanity */
  1353. n = inquiry_vpd_89(&arr[4]);
  1354. put_unaligned_be16(n, arr + 2);
  1355. } else if (is_disk && 0xb0 == cmd[2]) { /* Block limits */
  1356. arr[1] = cmd[2]; /*sanity */
  1357. arr[3] = inquiry_vpd_b0(&arr[4]);
  1358. } else if (is_disk && 0xb1 == cmd[2]) { /* Block char. */
  1359. arr[1] = cmd[2]; /*sanity */
  1360. arr[3] = inquiry_vpd_b1(&arr[4]);
  1361. } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */
  1362. arr[1] = cmd[2]; /*sanity */
  1363. arr[3] = inquiry_vpd_b2(&arr[4]);
  1364. } else {
  1365. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
  1366. kfree(arr);
  1367. return check_condition_result;
  1368. }
  1369. len = min(get_unaligned_be16(arr + 2) + 4, alloc_len);
  1370. ret = fill_from_dev_buffer(scp, arr,
  1371. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  1372. kfree(arr);
  1373. return ret;
  1374. }
  1375. /* drops through here for a standard inquiry */
  1376. arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */
  1377. arr[2] = sdebug_scsi_level;
  1378. arr[3] = 2; /* response_data_format==2 */
  1379. arr[4] = SDEBUG_LONG_INQ_SZ - 5;
  1380. arr[5] = (int)have_dif_prot; /* PROTECT bit */
  1381. if (sdebug_vpd_use_hostno == 0)
  1382. arr[5] |= 0x10; /* claim: implicit TPGS */
  1383. arr[6] = 0x10; /* claim: MultiP */
  1384. /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
  1385. arr[7] = 0xa; /* claim: LINKED + CMDQUE */
  1386. memcpy(&arr[8], sdebug_inq_vendor_id, 8);
  1387. memcpy(&arr[16], sdebug_inq_product_id, 16);
  1388. memcpy(&arr[32], sdebug_inq_product_rev, 4);
  1389. /* Use Vendor Specific area to place driver date in ASCII hex */
  1390. memcpy(&arr[36], sdebug_version_date, 8);
  1391. /* version descriptors (2 bytes each) follow */
  1392. put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */
  1393. put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */
  1394. n = 62;
  1395. if (is_disk) { /* SBC-4 no version claimed */
  1396. put_unaligned_be16(0x600, arr + n);
  1397. n += 2;
  1398. } else if (sdebug_ptype == TYPE_TAPE) { /* SSC-4 rev 3 */
  1399. put_unaligned_be16(0x525, arr + n);
  1400. n += 2;
  1401. }
  1402. put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */
  1403. ret = fill_from_dev_buffer(scp, arr,
  1404. min(alloc_len, SDEBUG_LONG_INQ_SZ));
  1405. kfree(arr);
  1406. return ret;
  1407. }
  1408. static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1409. 0, 0, 0x0, 0x0};
  1410. static int resp_requests(struct scsi_cmnd *scp,
  1411. struct sdebug_dev_info *devip)
  1412. {
  1413. unsigned char *sbuff;
  1414. unsigned char *cmd = scp->cmnd;
  1415. unsigned char arr[SCSI_SENSE_BUFFERSIZE];
  1416. bool dsense;
  1417. int len = 18;
  1418. memset(arr, 0, sizeof(arr));
  1419. dsense = !!(cmd[1] & 1);
  1420. sbuff = scp->sense_buffer;
  1421. if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
  1422. if (dsense) {
  1423. arr[0] = 0x72;
  1424. arr[1] = 0x0; /* NO_SENSE in sense_key */
  1425. arr[2] = THRESHOLD_EXCEEDED;
  1426. arr[3] = 0xff; /* TEST set and MRIE==6 */
  1427. len = 8;
  1428. } else {
  1429. arr[0] = 0x70;
  1430. arr[2] = 0x0; /* NO_SENSE in sense_key */
  1431. arr[7] = 0xa; /* 18 byte sense buffer */
  1432. arr[12] = THRESHOLD_EXCEEDED;
  1433. arr[13] = 0xff; /* TEST set and MRIE==6 */
  1434. }
  1435. } else {
  1436. memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE);
  1437. if (arr[0] >= 0x70 && dsense == sdebug_dsense)
  1438. ; /* have sense and formats match */
  1439. else if (arr[0] <= 0x70) {
  1440. if (dsense) {
  1441. memset(arr, 0, 8);
  1442. arr[0] = 0x72;
  1443. len = 8;
  1444. } else {
  1445. memset(arr, 0, 18);
  1446. arr[0] = 0x70;
  1447. arr[7] = 0xa;
  1448. }
  1449. } else if (dsense) {
  1450. memset(arr, 0, 8);
  1451. arr[0] = 0x72;
  1452. arr[1] = sbuff[2]; /* sense key */
  1453. arr[2] = sbuff[12]; /* asc */
  1454. arr[3] = sbuff[13]; /* ascq */
  1455. len = 8;
  1456. } else {
  1457. memset(arr, 0, 18);
  1458. arr[0] = 0x70;
  1459. arr[2] = sbuff[1];
  1460. arr[7] = 0xa;
  1461. arr[12] = sbuff[1];
  1462. arr[13] = sbuff[3];
  1463. }
  1464. }
  1465. mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0);
  1466. return fill_from_dev_buffer(scp, arr, len);
  1467. }
  1468. static int resp_start_stop(struct scsi_cmnd *scp,
  1469. struct sdebug_dev_info *devip)
  1470. {
  1471. unsigned char *cmd = scp->cmnd;
  1472. int power_cond, stop;
  1473. bool changing;
  1474. power_cond = (cmd[4] & 0xf0) >> 4;
  1475. if (power_cond) {
  1476. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
  1477. return check_condition_result;
  1478. }
  1479. stop = !(cmd[4] & 1);
  1480. changing = atomic_read(&devip->stopped) == !stop;
  1481. atomic_xchg(&devip->stopped, stop);
  1482. if (!changing || cmd[1] & 0x1) /* state unchanged or IMMED set */
  1483. return SDEG_RES_IMMED_MASK;
  1484. else
  1485. return 0;
  1486. }
  1487. static sector_t get_sdebug_capacity(void)
  1488. {
  1489. static const unsigned int gibibyte = 1073741824;
  1490. if (sdebug_virtual_gb > 0)
  1491. return (sector_t)sdebug_virtual_gb *
  1492. (gibibyte / sdebug_sector_size);
  1493. else
  1494. return sdebug_store_sectors;
  1495. }
  1496. #define SDEBUG_READCAP_ARR_SZ 8
  1497. static int resp_readcap(struct scsi_cmnd *scp,
  1498. struct sdebug_dev_info *devip)
  1499. {
  1500. unsigned char arr[SDEBUG_READCAP_ARR_SZ];
  1501. unsigned int capac;
  1502. /* following just in case virtual_gb changed */
  1503. sdebug_capacity = get_sdebug_capacity();
  1504. memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
  1505. if (sdebug_capacity < 0xffffffff) {
  1506. capac = (unsigned int)sdebug_capacity - 1;
  1507. put_unaligned_be32(capac, arr + 0);
  1508. } else
  1509. put_unaligned_be32(0xffffffff, arr + 0);
  1510. put_unaligned_be16(sdebug_sector_size, arr + 6);
  1511. return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
  1512. }
  1513. #define SDEBUG_READCAP16_ARR_SZ 32
  1514. static int resp_readcap16(struct scsi_cmnd *scp,
  1515. struct sdebug_dev_info *devip)
  1516. {
  1517. unsigned char *cmd = scp->cmnd;
  1518. unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
  1519. int alloc_len;
  1520. alloc_len = get_unaligned_be32(cmd + 10);
  1521. /* following just in case virtual_gb changed */
  1522. sdebug_capacity = get_sdebug_capacity();
  1523. memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
  1524. put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0);
  1525. put_unaligned_be32(sdebug_sector_size, arr + 8);
  1526. arr[13] = sdebug_physblk_exp & 0xf;
  1527. arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f;
  1528. if (scsi_debug_lbp()) {
  1529. arr[14] |= 0x80; /* LBPME */
  1530. /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in
  1531. * the LB Provisioning VPD page is 3 bits. Note that lbprz=2
  1532. * in the wider field maps to 0 in this field.
  1533. */
  1534. if (sdebug_lbprz & 1) /* precisely what the draft requires */
  1535. arr[14] |= 0x40;
  1536. }
  1537. arr[15] = sdebug_lowest_aligned & 0xff;
  1538. if (have_dif_prot) {
  1539. arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */
  1540. arr[12] |= 1; /* PROT_EN */
  1541. }
  1542. return fill_from_dev_buffer(scp, arr,
  1543. min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
  1544. }
  1545. #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
  1546. static int resp_report_tgtpgs(struct scsi_cmnd *scp,
  1547. struct sdebug_dev_info *devip)
  1548. {
  1549. unsigned char *cmd = scp->cmnd;
  1550. unsigned char *arr;
  1551. int host_no = devip->sdbg_host->shost->host_no;
  1552. int n, ret, alen, rlen;
  1553. int port_group_a, port_group_b, port_a, port_b;
  1554. alen = get_unaligned_be32(cmd + 6);
  1555. arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
  1556. if (! arr)
  1557. return DID_REQUEUE << 16;
  1558. /*
  1559. * EVPD page 0x88 states we have two ports, one
  1560. * real and a fake port with no device connected.
  1561. * So we create two port groups with one port each
  1562. * and set the group with port B to unavailable.
  1563. */
  1564. port_a = 0x1; /* relative port A */
  1565. port_b = 0x2; /* relative port B */
  1566. port_group_a = (((host_no + 1) & 0x7f) << 8) +
  1567. (devip->channel & 0x7f);
  1568. port_group_b = (((host_no + 1) & 0x7f) << 8) +
  1569. (devip->channel & 0x7f) + 0x80;
  1570. /*
  1571. * The asymmetric access state is cycled according to the host_id.
  1572. */
  1573. n = 4;
  1574. if (sdebug_vpd_use_hostno == 0) {
  1575. arr[n++] = host_no % 3; /* Asymm access state */
  1576. arr[n++] = 0x0F; /* claim: all states are supported */
  1577. } else {
  1578. arr[n++] = 0x0; /* Active/Optimized path */
  1579. arr[n++] = 0x01; /* only support active/optimized paths */
  1580. }
  1581. put_unaligned_be16(port_group_a, arr + n);
  1582. n += 2;
  1583. arr[n++] = 0; /* Reserved */
  1584. arr[n++] = 0; /* Status code */
  1585. arr[n++] = 0; /* Vendor unique */
  1586. arr[n++] = 0x1; /* One port per group */
  1587. arr[n++] = 0; /* Reserved */
  1588. arr[n++] = 0; /* Reserved */
  1589. put_unaligned_be16(port_a, arr + n);
  1590. n += 2;
  1591. arr[n++] = 3; /* Port unavailable */
  1592. arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
  1593. put_unaligned_be16(port_group_b, arr + n);
  1594. n += 2;
  1595. arr[n++] = 0; /* Reserved */
  1596. arr[n++] = 0; /* Status code */
  1597. arr[n++] = 0; /* Vendor unique */
  1598. arr[n++] = 0x1; /* One port per group */
  1599. arr[n++] = 0; /* Reserved */
  1600. arr[n++] = 0; /* Reserved */
  1601. put_unaligned_be16(port_b, arr + n);
  1602. n += 2;
  1603. rlen = n - 4;
  1604. put_unaligned_be32(rlen, arr + 0);
  1605. /*
  1606. * Return the smallest value of either
  1607. * - The allocated length
  1608. * - The constructed command length
  1609. * - The maximum array size
  1610. */
  1611. rlen = min(alen,n);
  1612. ret = fill_from_dev_buffer(scp, arr,
  1613. min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
  1614. kfree(arr);
  1615. return ret;
  1616. }
  1617. static int resp_rsup_opcodes(struct scsi_cmnd *scp,
  1618. struct sdebug_dev_info *devip)
  1619. {
  1620. bool rctd;
  1621. u8 reporting_opts, req_opcode, sdeb_i, supp;
  1622. u16 req_sa, u;
  1623. u32 alloc_len, a_len;
  1624. int k, offset, len, errsts, count, bump, na;
  1625. const struct opcode_info_t *oip;
  1626. const struct opcode_info_t *r_oip;
  1627. u8 *arr;
  1628. u8 *cmd = scp->cmnd;
  1629. rctd = !!(cmd[2] & 0x80);
  1630. reporting_opts = cmd[2] & 0x7;
  1631. req_opcode = cmd[3];
  1632. req_sa = get_unaligned_be16(cmd + 4);
  1633. alloc_len = get_unaligned_be32(cmd + 6);
  1634. if (alloc_len < 4 || alloc_len > 0xffff) {
  1635. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  1636. return check_condition_result;
  1637. }
  1638. if (alloc_len > 8192)
  1639. a_len = 8192;
  1640. else
  1641. a_len = alloc_len;
  1642. arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC);
  1643. if (NULL == arr) {
  1644. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  1645. INSUFF_RES_ASCQ);
  1646. return check_condition_result;
  1647. }
  1648. switch (reporting_opts) {
  1649. case 0: /* all commands */
  1650. /* count number of commands */
  1651. for (count = 0, oip = opcode_info_arr;
  1652. oip->num_attached != 0xff; ++oip) {
  1653. if (F_INV_OP & oip->flags)
  1654. continue;
  1655. count += (oip->num_attached + 1);
  1656. }
  1657. bump = rctd ? 20 : 8;
  1658. put_unaligned_be32(count * bump, arr);
  1659. for (offset = 4, oip = opcode_info_arr;
  1660. oip->num_attached != 0xff && offset < a_len; ++oip) {
  1661. if (F_INV_OP & oip->flags)
  1662. continue;
  1663. na = oip->num_attached;
  1664. arr[offset] = oip->opcode;
  1665. put_unaligned_be16(oip->sa, arr + offset + 2);
  1666. if (rctd)
  1667. arr[offset + 5] |= 0x2;
  1668. if (FF_SA & oip->flags)
  1669. arr[offset + 5] |= 0x1;
  1670. put_unaligned_be16(oip->len_mask[0], arr + offset + 6);
  1671. if (rctd)
  1672. put_unaligned_be16(0xa, arr + offset + 8);
  1673. r_oip = oip;
  1674. for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) {
  1675. if (F_INV_OP & oip->flags)
  1676. continue;
  1677. offset += bump;
  1678. arr[offset] = oip->opcode;
  1679. put_unaligned_be16(oip->sa, arr + offset + 2);
  1680. if (rctd)
  1681. arr[offset + 5] |= 0x2;
  1682. if (FF_SA & oip->flags)
  1683. arr[offset + 5] |= 0x1;
  1684. put_unaligned_be16(oip->len_mask[0],
  1685. arr + offset + 6);
  1686. if (rctd)
  1687. put_unaligned_be16(0xa,
  1688. arr + offset + 8);
  1689. }
  1690. oip = r_oip;
  1691. offset += bump;
  1692. }
  1693. break;
  1694. case 1: /* one command: opcode only */
  1695. case 2: /* one command: opcode plus service action */
  1696. case 3: /* one command: if sa==0 then opcode only else opcode+sa */
  1697. sdeb_i = opcode_ind_arr[req_opcode];
  1698. oip = &opcode_info_arr[sdeb_i];
  1699. if (F_INV_OP & oip->flags) {
  1700. supp = 1;
  1701. offset = 4;
  1702. } else {
  1703. if (1 == reporting_opts) {
  1704. if (FF_SA & oip->flags) {
  1705. mk_sense_invalid_fld(scp, SDEB_IN_CDB,
  1706. 2, 2);
  1707. kfree(arr);
  1708. return check_condition_result;
  1709. }
  1710. req_sa = 0;
  1711. } else if (2 == reporting_opts &&
  1712. 0 == (FF_SA & oip->flags)) {
  1713. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1);
  1714. kfree(arr); /* point at requested sa */
  1715. return check_condition_result;
  1716. }
  1717. if (0 == (FF_SA & oip->flags) &&
  1718. req_opcode == oip->opcode)
  1719. supp = 3;
  1720. else if (0 == (FF_SA & oip->flags)) {
  1721. na = oip->num_attached;
  1722. for (k = 0, oip = oip->arrp; k < na;
  1723. ++k, ++oip) {
  1724. if (req_opcode == oip->opcode)
  1725. break;
  1726. }
  1727. supp = (k >= na) ? 1 : 3;
  1728. } else if (req_sa != oip->sa) {
  1729. na = oip->num_attached;
  1730. for (k = 0, oip = oip->arrp; k < na;
  1731. ++k, ++oip) {
  1732. if (req_sa == oip->sa)
  1733. break;
  1734. }
  1735. supp = (k >= na) ? 1 : 3;
  1736. } else
  1737. supp = 3;
  1738. if (3 == supp) {
  1739. u = oip->len_mask[0];
  1740. put_unaligned_be16(u, arr + 2);
  1741. arr[4] = oip->opcode;
  1742. for (k = 1; k < u; ++k)
  1743. arr[4 + k] = (k < 16) ?
  1744. oip->len_mask[k] : 0xff;
  1745. offset = 4 + u;
  1746. } else
  1747. offset = 4;
  1748. }
  1749. arr[1] = (rctd ? 0x80 : 0) | supp;
  1750. if (rctd) {
  1751. put_unaligned_be16(0xa, arr + offset);
  1752. offset += 12;
  1753. }
  1754. break;
  1755. default:
  1756. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
  1757. kfree(arr);
  1758. return check_condition_result;
  1759. }
  1760. offset = (offset < a_len) ? offset : a_len;
  1761. len = (offset < alloc_len) ? offset : alloc_len;
  1762. errsts = fill_from_dev_buffer(scp, arr, len);
  1763. kfree(arr);
  1764. return errsts;
  1765. }
  1766. static int resp_rsup_tmfs(struct scsi_cmnd *scp,
  1767. struct sdebug_dev_info *devip)
  1768. {
  1769. bool repd;
  1770. u32 alloc_len, len;
  1771. u8 arr[16];
  1772. u8 *cmd = scp->cmnd;
  1773. memset(arr, 0, sizeof(arr));
  1774. repd = !!(cmd[2] & 0x80);
  1775. alloc_len = get_unaligned_be32(cmd + 6);
  1776. if (alloc_len < 4) {
  1777. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  1778. return check_condition_result;
  1779. }
  1780. arr[0] = 0xc8; /* ATS | ATSS | LURS */
  1781. arr[1] = 0x1; /* ITNRS */
  1782. if (repd) {
  1783. arr[3] = 0xc;
  1784. len = 16;
  1785. } else
  1786. len = 4;
  1787. len = (len < alloc_len) ? len : alloc_len;
  1788. return fill_from_dev_buffer(scp, arr, len);
  1789. }
  1790. /* <<Following mode page info copied from ST318451LW>> */
  1791. static int resp_err_recov_pg(unsigned char *p, int pcontrol, int target)
  1792. { /* Read-Write Error Recovery page for mode_sense */
  1793. unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
  1794. 5, 0, 0xff, 0xff};
  1795. memcpy(p, err_recov_pg, sizeof(err_recov_pg));
  1796. if (1 == pcontrol)
  1797. memset(p + 2, 0, sizeof(err_recov_pg) - 2);
  1798. return sizeof(err_recov_pg);
  1799. }
  1800. static int resp_disconnect_pg(unsigned char *p, int pcontrol, int target)
  1801. { /* Disconnect-Reconnect page for mode_sense */
  1802. unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
  1803. 0, 0, 0, 0, 0, 0, 0, 0};
  1804. memcpy(p, disconnect_pg, sizeof(disconnect_pg));
  1805. if (1 == pcontrol)
  1806. memset(p + 2, 0, sizeof(disconnect_pg) - 2);
  1807. return sizeof(disconnect_pg);
  1808. }
  1809. static int resp_format_pg(unsigned char *p, int pcontrol, int target)
  1810. { /* Format device page for mode_sense */
  1811. unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
  1812. 0, 0, 0, 0, 0, 0, 0, 0,
  1813. 0, 0, 0, 0, 0x40, 0, 0, 0};
  1814. memcpy(p, format_pg, sizeof(format_pg));
  1815. put_unaligned_be16(sdebug_sectors_per, p + 10);
  1816. put_unaligned_be16(sdebug_sector_size, p + 12);
  1817. if (sdebug_removable)
  1818. p[20] |= 0x20; /* should agree with INQUIRY */
  1819. if (1 == pcontrol)
  1820. memset(p + 2, 0, sizeof(format_pg) - 2);
  1821. return sizeof(format_pg);
  1822. }
  1823. static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  1824. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
  1825. 0, 0, 0, 0};
  1826. static int resp_caching_pg(unsigned char *p, int pcontrol, int target)
  1827. { /* Caching page for mode_sense */
  1828. unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
  1829. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1830. unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  1831. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
  1832. if (SDEBUG_OPT_N_WCE & sdebug_opts)
  1833. caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */
  1834. memcpy(p, caching_pg, sizeof(caching_pg));
  1835. if (1 == pcontrol)
  1836. memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
  1837. else if (2 == pcontrol)
  1838. memcpy(p, d_caching_pg, sizeof(d_caching_pg));
  1839. return sizeof(caching_pg);
  1840. }
  1841. static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  1842. 0, 0, 0x2, 0x4b};
  1843. static int resp_ctrl_m_pg(unsigned char *p, int pcontrol, int target)
  1844. { /* Control mode page for mode_sense */
  1845. unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
  1846. 0, 0, 0, 0};
  1847. unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  1848. 0, 0, 0x2, 0x4b};
  1849. if (sdebug_dsense)
  1850. ctrl_m_pg[2] |= 0x4;
  1851. else
  1852. ctrl_m_pg[2] &= ~0x4;
  1853. if (sdebug_ato)
  1854. ctrl_m_pg[5] |= 0x80; /* ATO=1 */
  1855. memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
  1856. if (1 == pcontrol)
  1857. memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
  1858. else if (2 == pcontrol)
  1859. memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
  1860. return sizeof(ctrl_m_pg);
  1861. }
  1862. static int resp_iec_m_pg(unsigned char *p, int pcontrol, int target)
  1863. { /* Informational Exceptions control mode page for mode_sense */
  1864. unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
  1865. 0, 0, 0x0, 0x0};
  1866. unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1867. 0, 0, 0x0, 0x0};
  1868. memcpy(p, iec_m_pg, sizeof(iec_m_pg));
  1869. if (1 == pcontrol)
  1870. memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
  1871. else if (2 == pcontrol)
  1872. memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
  1873. return sizeof(iec_m_pg);
  1874. }
  1875. static int resp_sas_sf_m_pg(unsigned char *p, int pcontrol, int target)
  1876. { /* SAS SSP mode page - short format for mode_sense */
  1877. unsigned char sas_sf_m_pg[] = {0x19, 0x6,
  1878. 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
  1879. memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
  1880. if (1 == pcontrol)
  1881. memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
  1882. return sizeof(sas_sf_m_pg);
  1883. }
  1884. static int resp_sas_pcd_m_spg(unsigned char *p, int pcontrol, int target,
  1885. int target_dev_id)
  1886. { /* SAS phy control and discover mode page for mode_sense */
  1887. unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
  1888. 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1889. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  1890. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  1891. 0x2, 0, 0, 0, 0, 0, 0, 0,
  1892. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1893. 0, 0, 0, 0, 0, 0, 0, 0,
  1894. 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1895. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  1896. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  1897. 0x3, 0, 0, 0, 0, 0, 0, 0,
  1898. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1899. 0, 0, 0, 0, 0, 0, 0, 0,
  1900. };
  1901. int port_a, port_b;
  1902. put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16);
  1903. put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24);
  1904. put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64);
  1905. put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72);
  1906. port_a = target_dev_id + 1;
  1907. port_b = port_a + 1;
  1908. memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
  1909. put_unaligned_be32(port_a, p + 20);
  1910. put_unaligned_be32(port_b, p + 48 + 20);
  1911. if (1 == pcontrol)
  1912. memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
  1913. return sizeof(sas_pcd_m_pg);
  1914. }
  1915. static int resp_sas_sha_m_spg(unsigned char *p, int pcontrol)
  1916. { /* SAS SSP shared protocol specific port mode subpage */
  1917. unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
  1918. 0, 0, 0, 0, 0, 0, 0, 0,
  1919. };
  1920. memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
  1921. if (1 == pcontrol)
  1922. memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
  1923. return sizeof(sas_sha_m_pg);
  1924. }
  1925. #define SDEBUG_MAX_MSENSE_SZ 256
  1926. static int resp_mode_sense(struct scsi_cmnd *scp,
  1927. struct sdebug_dev_info *devip)
  1928. {
  1929. int pcontrol, pcode, subpcode, bd_len;
  1930. unsigned char dev_spec;
  1931. int alloc_len, offset, len, target_dev_id;
  1932. int target = scp->device->id;
  1933. unsigned char *ap;
  1934. unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
  1935. unsigned char *cmd = scp->cmnd;
  1936. bool dbd, llbaa, msense_6, is_disk, bad_pcode;
  1937. dbd = !!(cmd[1] & 0x8); /* disable block descriptors */
  1938. pcontrol = (cmd[2] & 0xc0) >> 6;
  1939. pcode = cmd[2] & 0x3f;
  1940. subpcode = cmd[3];
  1941. msense_6 = (MODE_SENSE == cmd[0]);
  1942. llbaa = msense_6 ? false : !!(cmd[1] & 0x10);
  1943. is_disk = (sdebug_ptype == TYPE_DISK);
  1944. if (is_disk && !dbd)
  1945. bd_len = llbaa ? 16 : 8;
  1946. else
  1947. bd_len = 0;
  1948. alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7);
  1949. memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
  1950. if (0x3 == pcontrol) { /* Saving values not supported */
  1951. mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0);
  1952. return check_condition_result;
  1953. }
  1954. target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
  1955. (devip->target * 1000) - 3;
  1956. /* for disks set DPOFUA bit and clear write protect (WP) bit */
  1957. if (is_disk)
  1958. dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */
  1959. else
  1960. dev_spec = 0x0;
  1961. if (msense_6) {
  1962. arr[2] = dev_spec;
  1963. arr[3] = bd_len;
  1964. offset = 4;
  1965. } else {
  1966. arr[3] = dev_spec;
  1967. if (16 == bd_len)
  1968. arr[4] = 0x1; /* set LONGLBA bit */
  1969. arr[7] = bd_len; /* assume 255 or less */
  1970. offset = 8;
  1971. }
  1972. ap = arr + offset;
  1973. if ((bd_len > 0) && (!sdebug_capacity))
  1974. sdebug_capacity = get_sdebug_capacity();
  1975. if (8 == bd_len) {
  1976. if (sdebug_capacity > 0xfffffffe)
  1977. put_unaligned_be32(0xffffffff, ap + 0);
  1978. else
  1979. put_unaligned_be32(sdebug_capacity, ap + 0);
  1980. put_unaligned_be16(sdebug_sector_size, ap + 6);
  1981. offset += bd_len;
  1982. ap = arr + offset;
  1983. } else if (16 == bd_len) {
  1984. put_unaligned_be64((u64)sdebug_capacity, ap + 0);
  1985. put_unaligned_be32(sdebug_sector_size, ap + 12);
  1986. offset += bd_len;
  1987. ap = arr + offset;
  1988. }
  1989. if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
  1990. /* TODO: Control Extension page */
  1991. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  1992. return check_condition_result;
  1993. }
  1994. bad_pcode = false;
  1995. switch (pcode) {
  1996. case 0x1: /* Read-Write error recovery page, direct access */
  1997. len = resp_err_recov_pg(ap, pcontrol, target);
  1998. offset += len;
  1999. break;
  2000. case 0x2: /* Disconnect-Reconnect page, all devices */
  2001. len = resp_disconnect_pg(ap, pcontrol, target);
  2002. offset += len;
  2003. break;
  2004. case 0x3: /* Format device page, direct access */
  2005. if (is_disk) {
  2006. len = resp_format_pg(ap, pcontrol, target);
  2007. offset += len;
  2008. } else
  2009. bad_pcode = true;
  2010. break;
  2011. case 0x8: /* Caching page, direct access */
  2012. if (is_disk) {
  2013. len = resp_caching_pg(ap, pcontrol, target);
  2014. offset += len;
  2015. } else
  2016. bad_pcode = true;
  2017. break;
  2018. case 0xa: /* Control Mode page, all devices */
  2019. len = resp_ctrl_m_pg(ap, pcontrol, target);
  2020. offset += len;
  2021. break;
  2022. case 0x19: /* if spc==1 then sas phy, control+discover */
  2023. if ((subpcode > 0x2) && (subpcode < 0xff)) {
  2024. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2025. return check_condition_result;
  2026. }
  2027. len = 0;
  2028. if ((0x0 == subpcode) || (0xff == subpcode))
  2029. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  2030. if ((0x1 == subpcode) || (0xff == subpcode))
  2031. len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
  2032. target_dev_id);
  2033. if ((0x2 == subpcode) || (0xff == subpcode))
  2034. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  2035. offset += len;
  2036. break;
  2037. case 0x1c: /* Informational Exceptions Mode page, all devices */
  2038. len = resp_iec_m_pg(ap, pcontrol, target);
  2039. offset += len;
  2040. break;
  2041. case 0x3f: /* Read all Mode pages */
  2042. if ((0 == subpcode) || (0xff == subpcode)) {
  2043. len = resp_err_recov_pg(ap, pcontrol, target);
  2044. len += resp_disconnect_pg(ap + len, pcontrol, target);
  2045. if (is_disk) {
  2046. len += resp_format_pg(ap + len, pcontrol,
  2047. target);
  2048. len += resp_caching_pg(ap + len, pcontrol,
  2049. target);
  2050. }
  2051. len += resp_ctrl_m_pg(ap + len, pcontrol, target);
  2052. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  2053. if (0xff == subpcode) {
  2054. len += resp_sas_pcd_m_spg(ap + len, pcontrol,
  2055. target, target_dev_id);
  2056. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  2057. }
  2058. len += resp_iec_m_pg(ap + len, pcontrol, target);
  2059. offset += len;
  2060. } else {
  2061. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2062. return check_condition_result;
  2063. }
  2064. break;
  2065. default:
  2066. bad_pcode = true;
  2067. break;
  2068. }
  2069. if (bad_pcode) {
  2070. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2071. return check_condition_result;
  2072. }
  2073. if (msense_6)
  2074. arr[0] = offset - 1;
  2075. else
  2076. put_unaligned_be16((offset - 2), arr + 0);
  2077. return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
  2078. }
  2079. #define SDEBUG_MAX_MSELECT_SZ 512
  2080. static int resp_mode_select(struct scsi_cmnd *scp,
  2081. struct sdebug_dev_info *devip)
  2082. {
  2083. int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
  2084. int param_len, res, mpage;
  2085. unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
  2086. unsigned char *cmd = scp->cmnd;
  2087. int mselect6 = (MODE_SELECT == cmd[0]);
  2088. memset(arr, 0, sizeof(arr));
  2089. pf = cmd[1] & 0x10;
  2090. sp = cmd[1] & 0x1;
  2091. param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7);
  2092. if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
  2093. mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1);
  2094. return check_condition_result;
  2095. }
  2096. res = fetch_to_dev_buffer(scp, arr, param_len);
  2097. if (-1 == res)
  2098. return DID_ERROR << 16;
  2099. else if (sdebug_verbose && (res < param_len))
  2100. sdev_printk(KERN_INFO, scp->device,
  2101. "%s: cdb indicated=%d, IO sent=%d bytes\n",
  2102. __func__, param_len, res);
  2103. md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2);
  2104. bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6);
  2105. if (md_len > 2) {
  2106. mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
  2107. return check_condition_result;
  2108. }
  2109. off = bd_len + (mselect6 ? 4 : 8);
  2110. mpage = arr[off] & 0x3f;
  2111. ps = !!(arr[off] & 0x80);
  2112. if (ps) {
  2113. mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7);
  2114. return check_condition_result;
  2115. }
  2116. spf = !!(arr[off] & 0x40);
  2117. pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) :
  2118. (arr[off + 1] + 2);
  2119. if ((pg_len + off) > param_len) {
  2120. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2121. PARAMETER_LIST_LENGTH_ERR, 0);
  2122. return check_condition_result;
  2123. }
  2124. switch (mpage) {
  2125. case 0x8: /* Caching Mode page */
  2126. if (caching_pg[1] == arr[off + 1]) {
  2127. memcpy(caching_pg + 2, arr + off + 2,
  2128. sizeof(caching_pg) - 2);
  2129. goto set_mode_changed_ua;
  2130. }
  2131. break;
  2132. case 0xa: /* Control Mode page */
  2133. if (ctrl_m_pg[1] == arr[off + 1]) {
  2134. memcpy(ctrl_m_pg + 2, arr + off + 2,
  2135. sizeof(ctrl_m_pg) - 2);
  2136. sdebug_dsense = !!(ctrl_m_pg[2] & 0x4);
  2137. goto set_mode_changed_ua;
  2138. }
  2139. break;
  2140. case 0x1c: /* Informational Exceptions Mode page */
  2141. if (iec_m_pg[1] == arr[off + 1]) {
  2142. memcpy(iec_m_pg + 2, arr + off + 2,
  2143. sizeof(iec_m_pg) - 2);
  2144. goto set_mode_changed_ua;
  2145. }
  2146. break;
  2147. default:
  2148. break;
  2149. }
  2150. mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5);
  2151. return check_condition_result;
  2152. set_mode_changed_ua:
  2153. set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
  2154. return 0;
  2155. }
  2156. static int resp_temp_l_pg(unsigned char *arr)
  2157. {
  2158. unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
  2159. 0x0, 0x1, 0x3, 0x2, 0x0, 65,
  2160. };
  2161. memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
  2162. return sizeof(temp_l_pg);
  2163. }
  2164. static int resp_ie_l_pg(unsigned char *arr)
  2165. {
  2166. unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
  2167. };
  2168. memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
  2169. if (iec_m_pg[2] & 0x4) { /* TEST bit set */
  2170. arr[4] = THRESHOLD_EXCEEDED;
  2171. arr[5] = 0xff;
  2172. }
  2173. return sizeof(ie_l_pg);
  2174. }
  2175. #define SDEBUG_MAX_LSENSE_SZ 512
  2176. static int resp_log_sense(struct scsi_cmnd *scp,
  2177. struct sdebug_dev_info *devip)
  2178. {
  2179. int ppc, sp, pcode, subpcode, alloc_len, len, n;
  2180. unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
  2181. unsigned char *cmd = scp->cmnd;
  2182. memset(arr, 0, sizeof(arr));
  2183. ppc = cmd[1] & 0x2;
  2184. sp = cmd[1] & 0x1;
  2185. if (ppc || sp) {
  2186. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0);
  2187. return check_condition_result;
  2188. }
  2189. pcode = cmd[2] & 0x3f;
  2190. subpcode = cmd[3] & 0xff;
  2191. alloc_len = get_unaligned_be16(cmd + 7);
  2192. arr[0] = pcode;
  2193. if (0 == subpcode) {
  2194. switch (pcode) {
  2195. case 0x0: /* Supported log pages log page */
  2196. n = 4;
  2197. arr[n++] = 0x0; /* this page */
  2198. arr[n++] = 0xd; /* Temperature */
  2199. arr[n++] = 0x2f; /* Informational exceptions */
  2200. arr[3] = n - 4;
  2201. break;
  2202. case 0xd: /* Temperature log page */
  2203. arr[3] = resp_temp_l_pg(arr + 4);
  2204. break;
  2205. case 0x2f: /* Informational exceptions log page */
  2206. arr[3] = resp_ie_l_pg(arr + 4);
  2207. break;
  2208. default:
  2209. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2210. return check_condition_result;
  2211. }
  2212. } else if (0xff == subpcode) {
  2213. arr[0] |= 0x40;
  2214. arr[1] = subpcode;
  2215. switch (pcode) {
  2216. case 0x0: /* Supported log pages and subpages log page */
  2217. n = 4;
  2218. arr[n++] = 0x0;
  2219. arr[n++] = 0x0; /* 0,0 page */
  2220. arr[n++] = 0x0;
  2221. arr[n++] = 0xff; /* this page */
  2222. arr[n++] = 0xd;
  2223. arr[n++] = 0x0; /* Temperature */
  2224. arr[n++] = 0x2f;
  2225. arr[n++] = 0x0; /* Informational exceptions */
  2226. arr[3] = n - 4;
  2227. break;
  2228. case 0xd: /* Temperature subpages */
  2229. n = 4;
  2230. arr[n++] = 0xd;
  2231. arr[n++] = 0x0; /* Temperature */
  2232. arr[3] = n - 4;
  2233. break;
  2234. case 0x2f: /* Informational exceptions subpages */
  2235. n = 4;
  2236. arr[n++] = 0x2f;
  2237. arr[n++] = 0x0; /* Informational exceptions */
  2238. arr[3] = n - 4;
  2239. break;
  2240. default:
  2241. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2242. return check_condition_result;
  2243. }
  2244. } else {
  2245. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2246. return check_condition_result;
  2247. }
  2248. len = min(get_unaligned_be16(arr + 2) + 4, alloc_len);
  2249. return fill_from_dev_buffer(scp, arr,
  2250. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  2251. }
  2252. static int check_device_access_params(struct scsi_cmnd *scp,
  2253. unsigned long long lba, unsigned int num)
  2254. {
  2255. if (lba + num > sdebug_capacity) {
  2256. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  2257. return check_condition_result;
  2258. }
  2259. /* transfer length excessive (tie in to block limits VPD page) */
  2260. if (num > sdebug_store_sectors) {
  2261. /* needs work to find which cdb byte 'num' comes from */
  2262. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2263. return check_condition_result;
  2264. }
  2265. return 0;
  2266. }
  2267. /* Returns number of bytes copied or -1 if error. */
  2268. static int do_device_access(struct scsi_cmnd *scmd, u32 sg_skip, u64 lba,
  2269. u32 num, bool do_write)
  2270. {
  2271. int ret;
  2272. u64 block, rest = 0;
  2273. struct scsi_data_buffer *sdb;
  2274. enum dma_data_direction dir;
  2275. if (do_write) {
  2276. sdb = scsi_out(scmd);
  2277. dir = DMA_TO_DEVICE;
  2278. write_since_sync = true;
  2279. } else {
  2280. sdb = scsi_in(scmd);
  2281. dir = DMA_FROM_DEVICE;
  2282. }
  2283. if (!sdb->length)
  2284. return 0;
  2285. if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir))
  2286. return -1;
  2287. block = do_div(lba, sdebug_store_sectors);
  2288. if (block + num > sdebug_store_sectors)
  2289. rest = block + num - sdebug_store_sectors;
  2290. ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
  2291. fake_storep + (block * sdebug_sector_size),
  2292. (num - rest) * sdebug_sector_size, sg_skip, do_write);
  2293. if (ret != (num - rest) * sdebug_sector_size)
  2294. return ret;
  2295. if (rest) {
  2296. ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
  2297. fake_storep, rest * sdebug_sector_size,
  2298. sg_skip + ((num - rest) * sdebug_sector_size),
  2299. do_write);
  2300. }
  2301. return ret;
  2302. }
  2303. /* If lba2fake_store(lba,num) compares equal to arr(num), then copy top half of
  2304. * arr into lba2fake_store(lba,num) and return true. If comparison fails then
  2305. * return false. */
  2306. static bool comp_write_worker(u64 lba, u32 num, const u8 *arr)
  2307. {
  2308. bool res;
  2309. u64 block, rest = 0;
  2310. u32 store_blks = sdebug_store_sectors;
  2311. u32 lb_size = sdebug_sector_size;
  2312. block = do_div(lba, store_blks);
  2313. if (block + num > store_blks)
  2314. rest = block + num - store_blks;
  2315. res = !memcmp(fake_storep + (block * lb_size), arr,
  2316. (num - rest) * lb_size);
  2317. if (!res)
  2318. return res;
  2319. if (rest)
  2320. res = memcmp(fake_storep, arr + ((num - rest) * lb_size),
  2321. rest * lb_size);
  2322. if (!res)
  2323. return res;
  2324. arr += num * lb_size;
  2325. memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size);
  2326. if (rest)
  2327. memcpy(fake_storep, arr + ((num - rest) * lb_size),
  2328. rest * lb_size);
  2329. return res;
  2330. }
  2331. static __be16 dif_compute_csum(const void *buf, int len)
  2332. {
  2333. __be16 csum;
  2334. if (sdebug_guard)
  2335. csum = (__force __be16)ip_compute_csum(buf, len);
  2336. else
  2337. csum = cpu_to_be16(crc_t10dif(buf, len));
  2338. return csum;
  2339. }
  2340. static int dif_verify(struct t10_pi_tuple *sdt, const void *data,
  2341. sector_t sector, u32 ei_lba)
  2342. {
  2343. __be16 csum = dif_compute_csum(data, sdebug_sector_size);
  2344. if (sdt->guard_tag != csum) {
  2345. pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
  2346. (unsigned long)sector,
  2347. be16_to_cpu(sdt->guard_tag),
  2348. be16_to_cpu(csum));
  2349. return 0x01;
  2350. }
  2351. if (sdebug_dif == T10_PI_TYPE1_PROTECTION &&
  2352. be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
  2353. pr_err("REF check failed on sector %lu\n",
  2354. (unsigned long)sector);
  2355. return 0x03;
  2356. }
  2357. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2358. be32_to_cpu(sdt->ref_tag) != ei_lba) {
  2359. pr_err("REF check failed on sector %lu\n",
  2360. (unsigned long)sector);
  2361. return 0x03;
  2362. }
  2363. return 0;
  2364. }
  2365. static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector,
  2366. unsigned int sectors, bool read)
  2367. {
  2368. size_t resid;
  2369. void *paddr;
  2370. const void *dif_store_end = dif_storep + sdebug_store_sectors;
  2371. struct sg_mapping_iter miter;
  2372. /* Bytes of protection data to copy into sgl */
  2373. resid = sectors * sizeof(*dif_storep);
  2374. sg_miter_start(&miter, scsi_prot_sglist(SCpnt),
  2375. scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC |
  2376. (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
  2377. while (sg_miter_next(&miter) && resid > 0) {
  2378. size_t len = min(miter.length, resid);
  2379. void *start = dif_store(sector);
  2380. size_t rest = 0;
  2381. if (dif_store_end < start + len)
  2382. rest = start + len - dif_store_end;
  2383. paddr = miter.addr;
  2384. if (read)
  2385. memcpy(paddr, start, len - rest);
  2386. else
  2387. memcpy(start, paddr, len - rest);
  2388. if (rest) {
  2389. if (read)
  2390. memcpy(paddr + len - rest, dif_storep, rest);
  2391. else
  2392. memcpy(dif_storep, paddr + len - rest, rest);
  2393. }
  2394. sector += len / sizeof(*dif_storep);
  2395. resid -= len;
  2396. }
  2397. sg_miter_stop(&miter);
  2398. }
  2399. static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
  2400. unsigned int sectors, u32 ei_lba)
  2401. {
  2402. unsigned int i;
  2403. struct t10_pi_tuple *sdt;
  2404. sector_t sector;
  2405. for (i = 0; i < sectors; i++, ei_lba++) {
  2406. int ret;
  2407. sector = start_sec + i;
  2408. sdt = dif_store(sector);
  2409. if (sdt->app_tag == cpu_to_be16(0xffff))
  2410. continue;
  2411. ret = dif_verify(sdt, lba2fake_store(sector), sector, ei_lba);
  2412. if (ret) {
  2413. dif_errors++;
  2414. return ret;
  2415. }
  2416. }
  2417. dif_copy_prot(SCpnt, start_sec, sectors, true);
  2418. dix_reads++;
  2419. return 0;
  2420. }
  2421. static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  2422. {
  2423. u8 *cmd = scp->cmnd;
  2424. struct sdebug_queued_cmd *sqcp;
  2425. u64 lba;
  2426. u32 num;
  2427. u32 ei_lba;
  2428. unsigned long iflags;
  2429. int ret;
  2430. bool check_prot;
  2431. switch (cmd[0]) {
  2432. case READ_16:
  2433. ei_lba = 0;
  2434. lba = get_unaligned_be64(cmd + 2);
  2435. num = get_unaligned_be32(cmd + 10);
  2436. check_prot = true;
  2437. break;
  2438. case READ_10:
  2439. ei_lba = 0;
  2440. lba = get_unaligned_be32(cmd + 2);
  2441. num = get_unaligned_be16(cmd + 7);
  2442. check_prot = true;
  2443. break;
  2444. case READ_6:
  2445. ei_lba = 0;
  2446. lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
  2447. (u32)(cmd[1] & 0x1f) << 16;
  2448. num = (0 == cmd[4]) ? 256 : cmd[4];
  2449. check_prot = true;
  2450. break;
  2451. case READ_12:
  2452. ei_lba = 0;
  2453. lba = get_unaligned_be32(cmd + 2);
  2454. num = get_unaligned_be32(cmd + 6);
  2455. check_prot = true;
  2456. break;
  2457. case XDWRITEREAD_10:
  2458. ei_lba = 0;
  2459. lba = get_unaligned_be32(cmd + 2);
  2460. num = get_unaligned_be16(cmd + 7);
  2461. check_prot = false;
  2462. break;
  2463. default: /* assume READ(32) */
  2464. lba = get_unaligned_be64(cmd + 12);
  2465. ei_lba = get_unaligned_be32(cmd + 20);
  2466. num = get_unaligned_be32(cmd + 28);
  2467. check_prot = false;
  2468. break;
  2469. }
  2470. if (unlikely(have_dif_prot && check_prot)) {
  2471. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2472. (cmd[1] & 0xe0)) {
  2473. mk_sense_invalid_opcode(scp);
  2474. return check_condition_result;
  2475. }
  2476. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  2477. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  2478. (cmd[1] & 0xe0) == 0)
  2479. sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
  2480. "to DIF device\n");
  2481. }
  2482. if (unlikely(sdebug_any_injecting_opt)) {
  2483. sqcp = (struct sdebug_queued_cmd *)scp->host_scribble;
  2484. if (sqcp) {
  2485. if (sqcp->inj_short)
  2486. num /= 2;
  2487. }
  2488. } else
  2489. sqcp = NULL;
  2490. /* inline check_device_access_params() */
  2491. if (unlikely(lba + num > sdebug_capacity)) {
  2492. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  2493. return check_condition_result;
  2494. }
  2495. /* transfer length excessive (tie in to block limits VPD page) */
  2496. if (unlikely(num > sdebug_store_sectors)) {
  2497. /* needs work to find which cdb byte 'num' comes from */
  2498. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2499. return check_condition_result;
  2500. }
  2501. if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) &&
  2502. (lba <= (sdebug_medium_error_start + sdebug_medium_error_count - 1)) &&
  2503. ((lba + num) > sdebug_medium_error_start))) {
  2504. /* claim unrecoverable read error */
  2505. mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
  2506. /* set info field and valid bit for fixed descriptor */
  2507. if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
  2508. scp->sense_buffer[0] |= 0x80; /* Valid bit */
  2509. ret = (lba < OPT_MEDIUM_ERR_ADDR)
  2510. ? OPT_MEDIUM_ERR_ADDR : (int)lba;
  2511. put_unaligned_be32(ret, scp->sense_buffer + 3);
  2512. }
  2513. scsi_set_resid(scp, scsi_bufflen(scp));
  2514. return check_condition_result;
  2515. }
  2516. read_lock_irqsave(&atomic_rw, iflags);
  2517. /* DIX + T10 DIF */
  2518. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  2519. int prot_ret = prot_verify_read(scp, lba, num, ei_lba);
  2520. if (prot_ret) {
  2521. read_unlock_irqrestore(&atomic_rw, iflags);
  2522. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret);
  2523. return illegal_condition_result;
  2524. }
  2525. }
  2526. ret = do_device_access(scp, 0, lba, num, false);
  2527. read_unlock_irqrestore(&atomic_rw, iflags);
  2528. if (unlikely(ret == -1))
  2529. return DID_ERROR << 16;
  2530. scsi_in(scp)->resid = scsi_bufflen(scp) - ret;
  2531. if (unlikely(sqcp)) {
  2532. if (sqcp->inj_recovered) {
  2533. mk_sense_buffer(scp, RECOVERED_ERROR,
  2534. THRESHOLD_EXCEEDED, 0);
  2535. return check_condition_result;
  2536. } else if (sqcp->inj_transport) {
  2537. mk_sense_buffer(scp, ABORTED_COMMAND,
  2538. TRANSPORT_PROBLEM, ACK_NAK_TO);
  2539. return check_condition_result;
  2540. } else if (sqcp->inj_dif) {
  2541. /* Logical block guard check failed */
  2542. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  2543. return illegal_condition_result;
  2544. } else if (sqcp->inj_dix) {
  2545. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  2546. return illegal_condition_result;
  2547. }
  2548. }
  2549. return 0;
  2550. }
  2551. static void dump_sector(unsigned char *buf, int len)
  2552. {
  2553. int i, j, n;
  2554. pr_err(">>> Sector Dump <<<\n");
  2555. for (i = 0 ; i < len ; i += 16) {
  2556. char b[128];
  2557. for (j = 0, n = 0; j < 16; j++) {
  2558. unsigned char c = buf[i+j];
  2559. if (c >= 0x20 && c < 0x7e)
  2560. n += scnprintf(b + n, sizeof(b) - n,
  2561. " %c ", buf[i+j]);
  2562. else
  2563. n += scnprintf(b + n, sizeof(b) - n,
  2564. "%02x ", buf[i+j]);
  2565. }
  2566. pr_err("%04d: %s\n", i, b);
  2567. }
  2568. }
  2569. static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
  2570. unsigned int sectors, u32 ei_lba)
  2571. {
  2572. int ret;
  2573. struct t10_pi_tuple *sdt;
  2574. void *daddr;
  2575. sector_t sector = start_sec;
  2576. int ppage_offset;
  2577. int dpage_offset;
  2578. struct sg_mapping_iter diter;
  2579. struct sg_mapping_iter piter;
  2580. BUG_ON(scsi_sg_count(SCpnt) == 0);
  2581. BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
  2582. sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
  2583. scsi_prot_sg_count(SCpnt),
  2584. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  2585. sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
  2586. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  2587. /* For each protection page */
  2588. while (sg_miter_next(&piter)) {
  2589. dpage_offset = 0;
  2590. if (WARN_ON(!sg_miter_next(&diter))) {
  2591. ret = 0x01;
  2592. goto out;
  2593. }
  2594. for (ppage_offset = 0; ppage_offset < piter.length;
  2595. ppage_offset += sizeof(struct t10_pi_tuple)) {
  2596. /* If we're at the end of the current
  2597. * data page advance to the next one
  2598. */
  2599. if (dpage_offset >= diter.length) {
  2600. if (WARN_ON(!sg_miter_next(&diter))) {
  2601. ret = 0x01;
  2602. goto out;
  2603. }
  2604. dpage_offset = 0;
  2605. }
  2606. sdt = piter.addr + ppage_offset;
  2607. daddr = diter.addr + dpage_offset;
  2608. ret = dif_verify(sdt, daddr, sector, ei_lba);
  2609. if (ret) {
  2610. dump_sector(daddr, sdebug_sector_size);
  2611. goto out;
  2612. }
  2613. sector++;
  2614. ei_lba++;
  2615. dpage_offset += sdebug_sector_size;
  2616. }
  2617. diter.consumed = dpage_offset;
  2618. sg_miter_stop(&diter);
  2619. }
  2620. sg_miter_stop(&piter);
  2621. dif_copy_prot(SCpnt, start_sec, sectors, false);
  2622. dix_writes++;
  2623. return 0;
  2624. out:
  2625. dif_errors++;
  2626. sg_miter_stop(&diter);
  2627. sg_miter_stop(&piter);
  2628. return ret;
  2629. }
  2630. static unsigned long lba_to_map_index(sector_t lba)
  2631. {
  2632. if (sdebug_unmap_alignment)
  2633. lba += sdebug_unmap_granularity - sdebug_unmap_alignment;
  2634. sector_div(lba, sdebug_unmap_granularity);
  2635. return lba;
  2636. }
  2637. static sector_t map_index_to_lba(unsigned long index)
  2638. {
  2639. sector_t lba = index * sdebug_unmap_granularity;
  2640. if (sdebug_unmap_alignment)
  2641. lba -= sdebug_unmap_granularity - sdebug_unmap_alignment;
  2642. return lba;
  2643. }
  2644. static unsigned int map_state(sector_t lba, unsigned int *num)
  2645. {
  2646. sector_t end;
  2647. unsigned int mapped;
  2648. unsigned long index;
  2649. unsigned long next;
  2650. index = lba_to_map_index(lba);
  2651. mapped = test_bit(index, map_storep);
  2652. if (mapped)
  2653. next = find_next_zero_bit(map_storep, map_size, index);
  2654. else
  2655. next = find_next_bit(map_storep, map_size, index);
  2656. end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next));
  2657. *num = end - lba;
  2658. return mapped;
  2659. }
  2660. static void map_region(sector_t lba, unsigned int len)
  2661. {
  2662. sector_t end = lba + len;
  2663. while (lba < end) {
  2664. unsigned long index = lba_to_map_index(lba);
  2665. if (index < map_size)
  2666. set_bit(index, map_storep);
  2667. lba = map_index_to_lba(index + 1);
  2668. }
  2669. }
  2670. static void unmap_region(sector_t lba, unsigned int len)
  2671. {
  2672. sector_t end = lba + len;
  2673. while (lba < end) {
  2674. unsigned long index = lba_to_map_index(lba);
  2675. if (lba == map_index_to_lba(index) &&
  2676. lba + sdebug_unmap_granularity <= end &&
  2677. index < map_size) {
  2678. clear_bit(index, map_storep);
  2679. if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */
  2680. memset(fake_storep +
  2681. lba * sdebug_sector_size,
  2682. (sdebug_lbprz & 1) ? 0 : 0xff,
  2683. sdebug_sector_size *
  2684. sdebug_unmap_granularity);
  2685. }
  2686. if (dif_storep) {
  2687. memset(dif_storep + lba, 0xff,
  2688. sizeof(*dif_storep) *
  2689. sdebug_unmap_granularity);
  2690. }
  2691. }
  2692. lba = map_index_to_lba(index + 1);
  2693. }
  2694. }
  2695. static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  2696. {
  2697. u8 *cmd = scp->cmnd;
  2698. u64 lba;
  2699. u32 num;
  2700. u32 ei_lba;
  2701. unsigned long iflags;
  2702. int ret;
  2703. bool check_prot;
  2704. switch (cmd[0]) {
  2705. case WRITE_16:
  2706. ei_lba = 0;
  2707. lba = get_unaligned_be64(cmd + 2);
  2708. num = get_unaligned_be32(cmd + 10);
  2709. check_prot = true;
  2710. break;
  2711. case WRITE_10:
  2712. ei_lba = 0;
  2713. lba = get_unaligned_be32(cmd + 2);
  2714. num = get_unaligned_be16(cmd + 7);
  2715. check_prot = true;
  2716. break;
  2717. case WRITE_6:
  2718. ei_lba = 0;
  2719. lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
  2720. (u32)(cmd[1] & 0x1f) << 16;
  2721. num = (0 == cmd[4]) ? 256 : cmd[4];
  2722. check_prot = true;
  2723. break;
  2724. case WRITE_12:
  2725. ei_lba = 0;
  2726. lba = get_unaligned_be32(cmd + 2);
  2727. num = get_unaligned_be32(cmd + 6);
  2728. check_prot = true;
  2729. break;
  2730. case 0x53: /* XDWRITEREAD(10) */
  2731. ei_lba = 0;
  2732. lba = get_unaligned_be32(cmd + 2);
  2733. num = get_unaligned_be16(cmd + 7);
  2734. check_prot = false;
  2735. break;
  2736. default: /* assume WRITE(32) */
  2737. lba = get_unaligned_be64(cmd + 12);
  2738. ei_lba = get_unaligned_be32(cmd + 20);
  2739. num = get_unaligned_be32(cmd + 28);
  2740. check_prot = false;
  2741. break;
  2742. }
  2743. if (unlikely(have_dif_prot && check_prot)) {
  2744. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2745. (cmd[1] & 0xe0)) {
  2746. mk_sense_invalid_opcode(scp);
  2747. return check_condition_result;
  2748. }
  2749. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  2750. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  2751. (cmd[1] & 0xe0) == 0)
  2752. sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
  2753. "to DIF device\n");
  2754. }
  2755. /* inline check_device_access_params() */
  2756. if (unlikely(lba + num > sdebug_capacity)) {
  2757. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  2758. return check_condition_result;
  2759. }
  2760. /* transfer length excessive (tie in to block limits VPD page) */
  2761. if (unlikely(num > sdebug_store_sectors)) {
  2762. /* needs work to find which cdb byte 'num' comes from */
  2763. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2764. return check_condition_result;
  2765. }
  2766. write_lock_irqsave(&atomic_rw, iflags);
  2767. /* DIX + T10 DIF */
  2768. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  2769. int prot_ret = prot_verify_write(scp, lba, num, ei_lba);
  2770. if (prot_ret) {
  2771. write_unlock_irqrestore(&atomic_rw, iflags);
  2772. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret);
  2773. return illegal_condition_result;
  2774. }
  2775. }
  2776. ret = do_device_access(scp, 0, lba, num, true);
  2777. if (unlikely(scsi_debug_lbp()))
  2778. map_region(lba, num);
  2779. write_unlock_irqrestore(&atomic_rw, iflags);
  2780. if (unlikely(-1 == ret))
  2781. return DID_ERROR << 16;
  2782. else if (unlikely(sdebug_verbose &&
  2783. (ret < (num * sdebug_sector_size))))
  2784. sdev_printk(KERN_INFO, scp->device,
  2785. "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
  2786. my_name, num * sdebug_sector_size, ret);
  2787. if (unlikely(sdebug_any_injecting_opt)) {
  2788. struct sdebug_queued_cmd *sqcp =
  2789. (struct sdebug_queued_cmd *)scp->host_scribble;
  2790. if (sqcp) {
  2791. if (sqcp->inj_recovered) {
  2792. mk_sense_buffer(scp, RECOVERED_ERROR,
  2793. THRESHOLD_EXCEEDED, 0);
  2794. return check_condition_result;
  2795. } else if (sqcp->inj_dif) {
  2796. /* Logical block guard check failed */
  2797. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  2798. return illegal_condition_result;
  2799. } else if (sqcp->inj_dix) {
  2800. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  2801. return illegal_condition_result;
  2802. }
  2803. }
  2804. }
  2805. return 0;
  2806. }
  2807. /*
  2808. * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32).
  2809. * No READ GATHERED yet (requires bidi or long cdb holding gather list).
  2810. */
  2811. static int resp_write_scat(struct scsi_cmnd *scp,
  2812. struct sdebug_dev_info *devip)
  2813. {
  2814. u8 *cmd = scp->cmnd;
  2815. u8 *lrdp = NULL;
  2816. u8 *up;
  2817. u8 wrprotect;
  2818. u16 lbdof, num_lrd, k;
  2819. u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb;
  2820. u32 lb_size = sdebug_sector_size;
  2821. u32 ei_lba;
  2822. u64 lba;
  2823. unsigned long iflags;
  2824. int ret, res;
  2825. bool is_16;
  2826. static const u32 lrd_size = 32; /* + parameter list header size */
  2827. if (cmd[0] == VARIABLE_LENGTH_CMD) {
  2828. is_16 = false;
  2829. wrprotect = (cmd[10] >> 5) & 0x7;
  2830. lbdof = get_unaligned_be16(cmd + 12);
  2831. num_lrd = get_unaligned_be16(cmd + 16);
  2832. bt_len = get_unaligned_be32(cmd + 28);
  2833. } else { /* that leaves WRITE SCATTERED(16) */
  2834. is_16 = true;
  2835. wrprotect = (cmd[2] >> 5) & 0x7;
  2836. lbdof = get_unaligned_be16(cmd + 4);
  2837. num_lrd = get_unaligned_be16(cmd + 8);
  2838. bt_len = get_unaligned_be32(cmd + 10);
  2839. if (unlikely(have_dif_prot)) {
  2840. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2841. wrprotect) {
  2842. mk_sense_invalid_opcode(scp);
  2843. return illegal_condition_result;
  2844. }
  2845. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  2846. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  2847. wrprotect == 0)
  2848. sdev_printk(KERN_ERR, scp->device,
  2849. "Unprotected WR to DIF device\n");
  2850. }
  2851. }
  2852. if ((num_lrd == 0) || (bt_len == 0))
  2853. return 0; /* T10 says these do-nothings are not errors */
  2854. if (lbdof == 0) {
  2855. if (sdebug_verbose)
  2856. sdev_printk(KERN_INFO, scp->device,
  2857. "%s: %s: LB Data Offset field bad\n",
  2858. my_name, __func__);
  2859. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2860. return illegal_condition_result;
  2861. }
  2862. lbdof_blen = lbdof * lb_size;
  2863. if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) {
  2864. if (sdebug_verbose)
  2865. sdev_printk(KERN_INFO, scp->device,
  2866. "%s: %s: LBA range descriptors don't fit\n",
  2867. my_name, __func__);
  2868. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2869. return illegal_condition_result;
  2870. }
  2871. lrdp = kzalloc(lbdof_blen, GFP_ATOMIC);
  2872. if (lrdp == NULL)
  2873. return SCSI_MLQUEUE_HOST_BUSY;
  2874. if (sdebug_verbose)
  2875. sdev_printk(KERN_INFO, scp->device,
  2876. "%s: %s: Fetch header+scatter_list, lbdof_blen=%u\n",
  2877. my_name, __func__, lbdof_blen);
  2878. res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen);
  2879. if (res == -1) {
  2880. ret = DID_ERROR << 16;
  2881. goto err_out;
  2882. }
  2883. write_lock_irqsave(&atomic_rw, iflags);
  2884. sg_off = lbdof_blen;
  2885. /* Spec says Buffer xfer Length field in number of LBs in dout */
  2886. cum_lb = 0;
  2887. for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) {
  2888. lba = get_unaligned_be64(up + 0);
  2889. num = get_unaligned_be32(up + 8);
  2890. if (sdebug_verbose)
  2891. sdev_printk(KERN_INFO, scp->device,
  2892. "%s: %s: k=%d LBA=0x%llx num=%u sg_off=%u\n",
  2893. my_name, __func__, k, lba, num, sg_off);
  2894. if (num == 0)
  2895. continue;
  2896. ret = check_device_access_params(scp, lba, num);
  2897. if (ret)
  2898. goto err_out_unlock;
  2899. num_by = num * lb_size;
  2900. ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12);
  2901. if ((cum_lb + num) > bt_len) {
  2902. if (sdebug_verbose)
  2903. sdev_printk(KERN_INFO, scp->device,
  2904. "%s: %s: sum of blocks > data provided\n",
  2905. my_name, __func__);
  2906. mk_sense_buffer(scp, ILLEGAL_REQUEST, WRITE_ERROR_ASC,
  2907. 0);
  2908. ret = illegal_condition_result;
  2909. goto err_out_unlock;
  2910. }
  2911. /* DIX + T10 DIF */
  2912. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  2913. int prot_ret = prot_verify_write(scp, lba, num,
  2914. ei_lba);
  2915. if (prot_ret) {
  2916. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10,
  2917. prot_ret);
  2918. ret = illegal_condition_result;
  2919. goto err_out_unlock;
  2920. }
  2921. }
  2922. ret = do_device_access(scp, sg_off, lba, num, true);
  2923. if (unlikely(scsi_debug_lbp()))
  2924. map_region(lba, num);
  2925. if (unlikely(-1 == ret)) {
  2926. ret = DID_ERROR << 16;
  2927. goto err_out_unlock;
  2928. } else if (unlikely(sdebug_verbose && (ret < num_by)))
  2929. sdev_printk(KERN_INFO, scp->device,
  2930. "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
  2931. my_name, num_by, ret);
  2932. if (unlikely(sdebug_any_injecting_opt)) {
  2933. struct sdebug_queued_cmd *sqcp =
  2934. (struct sdebug_queued_cmd *)scp->host_scribble;
  2935. if (sqcp) {
  2936. if (sqcp->inj_recovered) {
  2937. mk_sense_buffer(scp, RECOVERED_ERROR,
  2938. THRESHOLD_EXCEEDED, 0);
  2939. ret = illegal_condition_result;
  2940. goto err_out_unlock;
  2941. } else if (sqcp->inj_dif) {
  2942. /* Logical block guard check failed */
  2943. mk_sense_buffer(scp, ABORTED_COMMAND,
  2944. 0x10, 1);
  2945. ret = illegal_condition_result;
  2946. goto err_out_unlock;
  2947. } else if (sqcp->inj_dix) {
  2948. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2949. 0x10, 1);
  2950. ret = illegal_condition_result;
  2951. goto err_out_unlock;
  2952. }
  2953. }
  2954. }
  2955. sg_off += num_by;
  2956. cum_lb += num;
  2957. }
  2958. ret = 0;
  2959. err_out_unlock:
  2960. write_unlock_irqrestore(&atomic_rw, iflags);
  2961. err_out:
  2962. kfree(lrdp);
  2963. return ret;
  2964. }
  2965. static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
  2966. u32 ei_lba, bool unmap, bool ndob)
  2967. {
  2968. int ret;
  2969. unsigned long iflags;
  2970. unsigned long long i;
  2971. u32 lb_size = sdebug_sector_size;
  2972. u64 block, lbaa;
  2973. u8 *fs1p;
  2974. ret = check_device_access_params(scp, lba, num);
  2975. if (ret)
  2976. return ret;
  2977. write_lock_irqsave(&atomic_rw, iflags);
  2978. if (unmap && scsi_debug_lbp()) {
  2979. unmap_region(lba, num);
  2980. goto out;
  2981. }
  2982. lbaa = lba;
  2983. block = do_div(lbaa, sdebug_store_sectors);
  2984. /* if ndob then zero 1 logical block, else fetch 1 logical block */
  2985. fs1p = fake_storep + (block * lb_size);
  2986. if (ndob) {
  2987. memset(fs1p, 0, lb_size);
  2988. ret = 0;
  2989. } else
  2990. ret = fetch_to_dev_buffer(scp, fs1p, lb_size);
  2991. if (-1 == ret) {
  2992. write_unlock_irqrestore(&atomic_rw, iflags);
  2993. return DID_ERROR << 16;
  2994. } else if (sdebug_verbose && !ndob && (ret < lb_size))
  2995. sdev_printk(KERN_INFO, scp->device,
  2996. "%s: %s: lb size=%u, IO sent=%d bytes\n",
  2997. my_name, "write same", lb_size, ret);
  2998. /* Copy first sector to remaining blocks */
  2999. for (i = 1 ; i < num ; i++) {
  3000. lbaa = lba + i;
  3001. block = do_div(lbaa, sdebug_store_sectors);
  3002. memmove(fake_storep + (block * lb_size), fs1p, lb_size);
  3003. }
  3004. if (scsi_debug_lbp())
  3005. map_region(lba, num);
  3006. out:
  3007. write_unlock_irqrestore(&atomic_rw, iflags);
  3008. return 0;
  3009. }
  3010. static int resp_write_same_10(struct scsi_cmnd *scp,
  3011. struct sdebug_dev_info *devip)
  3012. {
  3013. u8 *cmd = scp->cmnd;
  3014. u32 lba;
  3015. u16 num;
  3016. u32 ei_lba = 0;
  3017. bool unmap = false;
  3018. if (cmd[1] & 0x8) {
  3019. if (sdebug_lbpws10 == 0) {
  3020. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
  3021. return check_condition_result;
  3022. } else
  3023. unmap = true;
  3024. }
  3025. lba = get_unaligned_be32(cmd + 2);
  3026. num = get_unaligned_be16(cmd + 7);
  3027. if (num > sdebug_write_same_length) {
  3028. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
  3029. return check_condition_result;
  3030. }
  3031. return resp_write_same(scp, lba, num, ei_lba, unmap, false);
  3032. }
  3033. static int resp_write_same_16(struct scsi_cmnd *scp,
  3034. struct sdebug_dev_info *devip)
  3035. {
  3036. u8 *cmd = scp->cmnd;
  3037. u64 lba;
  3038. u32 num;
  3039. u32 ei_lba = 0;
  3040. bool unmap = false;
  3041. bool ndob = false;
  3042. if (cmd[1] & 0x8) { /* UNMAP */
  3043. if (sdebug_lbpws == 0) {
  3044. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
  3045. return check_condition_result;
  3046. } else
  3047. unmap = true;
  3048. }
  3049. if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */
  3050. ndob = true;
  3051. lba = get_unaligned_be64(cmd + 2);
  3052. num = get_unaligned_be32(cmd + 10);
  3053. if (num > sdebug_write_same_length) {
  3054. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
  3055. return check_condition_result;
  3056. }
  3057. return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
  3058. }
  3059. /* Note the mode field is in the same position as the (lower) service action
  3060. * field. For the Report supported operation codes command, SPC-4 suggests
  3061. * each mode of this command should be reported separately; for future. */
  3062. static int resp_write_buffer(struct scsi_cmnd *scp,
  3063. struct sdebug_dev_info *devip)
  3064. {
  3065. u8 *cmd = scp->cmnd;
  3066. struct scsi_device *sdp = scp->device;
  3067. struct sdebug_dev_info *dp;
  3068. u8 mode;
  3069. mode = cmd[1] & 0x1f;
  3070. switch (mode) {
  3071. case 0x4: /* download microcode (MC) and activate (ACT) */
  3072. /* set UAs on this device only */
  3073. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3074. set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm);
  3075. break;
  3076. case 0x5: /* download MC, save and ACT */
  3077. set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm);
  3078. break;
  3079. case 0x6: /* download MC with offsets and ACT */
  3080. /* set UAs on most devices (LUs) in this target */
  3081. list_for_each_entry(dp,
  3082. &devip->sdbg_host->dev_info_list,
  3083. dev_list)
  3084. if (dp->target == sdp->id) {
  3085. set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm);
  3086. if (devip != dp)
  3087. set_bit(SDEBUG_UA_MICROCODE_CHANGED,
  3088. dp->uas_bm);
  3089. }
  3090. break;
  3091. case 0x7: /* download MC with offsets, save, and ACT */
  3092. /* set UA on all devices (LUs) in this target */
  3093. list_for_each_entry(dp,
  3094. &devip->sdbg_host->dev_info_list,
  3095. dev_list)
  3096. if (dp->target == sdp->id)
  3097. set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET,
  3098. dp->uas_bm);
  3099. break;
  3100. default:
  3101. /* do nothing for this command for other mode values */
  3102. break;
  3103. }
  3104. return 0;
  3105. }
  3106. static int resp_comp_write(struct scsi_cmnd *scp,
  3107. struct sdebug_dev_info *devip)
  3108. {
  3109. u8 *cmd = scp->cmnd;
  3110. u8 *arr;
  3111. u8 *fake_storep_hold;
  3112. u64 lba;
  3113. u32 dnum;
  3114. u32 lb_size = sdebug_sector_size;
  3115. u8 num;
  3116. unsigned long iflags;
  3117. int ret;
  3118. int retval = 0;
  3119. lba = get_unaligned_be64(cmd + 2);
  3120. num = cmd[13]; /* 1 to a maximum of 255 logical blocks */
  3121. if (0 == num)
  3122. return 0; /* degenerate case, not an error */
  3123. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  3124. (cmd[1] & 0xe0)) {
  3125. mk_sense_invalid_opcode(scp);
  3126. return check_condition_result;
  3127. }
  3128. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  3129. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  3130. (cmd[1] & 0xe0) == 0)
  3131. sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
  3132. "to DIF device\n");
  3133. /* inline check_device_access_params() */
  3134. if (lba + num > sdebug_capacity) {
  3135. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  3136. return check_condition_result;
  3137. }
  3138. /* transfer length excessive (tie in to block limits VPD page) */
  3139. if (num > sdebug_store_sectors) {
  3140. /* needs work to find which cdb byte 'num' comes from */
  3141. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  3142. return check_condition_result;
  3143. }
  3144. dnum = 2 * num;
  3145. arr = kcalloc(lb_size, dnum, GFP_ATOMIC);
  3146. if (NULL == arr) {
  3147. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3148. INSUFF_RES_ASCQ);
  3149. return check_condition_result;
  3150. }
  3151. write_lock_irqsave(&atomic_rw, iflags);
  3152. /* trick do_device_access() to fetch both compare and write buffers
  3153. * from data-in into arr. Safe (atomic) since write_lock held. */
  3154. fake_storep_hold = fake_storep;
  3155. fake_storep = arr;
  3156. ret = do_device_access(scp, 0, 0, dnum, true);
  3157. fake_storep = fake_storep_hold;
  3158. if (ret == -1) {
  3159. retval = DID_ERROR << 16;
  3160. goto cleanup;
  3161. } else if (sdebug_verbose && (ret < (dnum * lb_size)))
  3162. sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb "
  3163. "indicated=%u, IO sent=%d bytes\n", my_name,
  3164. dnum * lb_size, ret);
  3165. if (!comp_write_worker(lba, num, arr)) {
  3166. mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0);
  3167. retval = check_condition_result;
  3168. goto cleanup;
  3169. }
  3170. if (scsi_debug_lbp())
  3171. map_region(lba, num);
  3172. cleanup:
  3173. write_unlock_irqrestore(&atomic_rw, iflags);
  3174. kfree(arr);
  3175. return retval;
  3176. }
  3177. struct unmap_block_desc {
  3178. __be64 lba;
  3179. __be32 blocks;
  3180. __be32 __reserved;
  3181. };
  3182. static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  3183. {
  3184. unsigned char *buf;
  3185. struct unmap_block_desc *desc;
  3186. unsigned int i, payload_len, descriptors;
  3187. int ret;
  3188. unsigned long iflags;
  3189. if (!scsi_debug_lbp())
  3190. return 0; /* fib and say its done */
  3191. payload_len = get_unaligned_be16(scp->cmnd + 7);
  3192. BUG_ON(scsi_bufflen(scp) != payload_len);
  3193. descriptors = (payload_len - 8) / 16;
  3194. if (descriptors > sdebug_unmap_max_desc) {
  3195. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
  3196. return check_condition_result;
  3197. }
  3198. buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
  3199. if (!buf) {
  3200. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3201. INSUFF_RES_ASCQ);
  3202. return check_condition_result;
  3203. }
  3204. scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
  3205. BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
  3206. BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
  3207. desc = (void *)&buf[8];
  3208. write_lock_irqsave(&atomic_rw, iflags);
  3209. for (i = 0 ; i < descriptors ; i++) {
  3210. unsigned long long lba = get_unaligned_be64(&desc[i].lba);
  3211. unsigned int num = get_unaligned_be32(&desc[i].blocks);
  3212. ret = check_device_access_params(scp, lba, num);
  3213. if (ret)
  3214. goto out;
  3215. unmap_region(lba, num);
  3216. }
  3217. ret = 0;
  3218. out:
  3219. write_unlock_irqrestore(&atomic_rw, iflags);
  3220. kfree(buf);
  3221. return ret;
  3222. }
  3223. #define SDEBUG_GET_LBA_STATUS_LEN 32
  3224. static int resp_get_lba_status(struct scsi_cmnd *scp,
  3225. struct sdebug_dev_info *devip)
  3226. {
  3227. u8 *cmd = scp->cmnd;
  3228. u64 lba;
  3229. u32 alloc_len, mapped, num;
  3230. u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
  3231. int ret;
  3232. lba = get_unaligned_be64(cmd + 2);
  3233. alloc_len = get_unaligned_be32(cmd + 10);
  3234. if (alloc_len < 24)
  3235. return 0;
  3236. ret = check_device_access_params(scp, lba, 1);
  3237. if (ret)
  3238. return ret;
  3239. if (scsi_debug_lbp())
  3240. mapped = map_state(lba, &num);
  3241. else {
  3242. mapped = 1;
  3243. /* following just in case virtual_gb changed */
  3244. sdebug_capacity = get_sdebug_capacity();
  3245. if (sdebug_capacity - lba <= 0xffffffff)
  3246. num = sdebug_capacity - lba;
  3247. else
  3248. num = 0xffffffff;
  3249. }
  3250. memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
  3251. put_unaligned_be32(20, arr); /* Parameter Data Length */
  3252. put_unaligned_be64(lba, arr + 8); /* LBA */
  3253. put_unaligned_be32(num, arr + 16); /* Number of blocks */
  3254. arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */
  3255. return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
  3256. }
  3257. static int resp_sync_cache(struct scsi_cmnd *scp,
  3258. struct sdebug_dev_info *devip)
  3259. {
  3260. int res = 0;
  3261. u64 lba;
  3262. u32 num_blocks;
  3263. u8 *cmd = scp->cmnd;
  3264. if (cmd[0] == SYNCHRONIZE_CACHE) { /* 10 byte cdb */
  3265. lba = get_unaligned_be32(cmd + 2);
  3266. num_blocks = get_unaligned_be16(cmd + 7);
  3267. } else { /* SYNCHRONIZE_CACHE(16) */
  3268. lba = get_unaligned_be64(cmd + 2);
  3269. num_blocks = get_unaligned_be32(cmd + 10);
  3270. }
  3271. if (lba + num_blocks > sdebug_capacity) {
  3272. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  3273. return check_condition_result;
  3274. }
  3275. if (!write_since_sync || cmd[1] & 0x2)
  3276. res = SDEG_RES_IMMED_MASK;
  3277. else /* delay if write_since_sync and IMMED clear */
  3278. write_since_sync = false;
  3279. return res;
  3280. }
  3281. #define RL_BUCKET_ELEMS 8
  3282. /* Even though each pseudo target has a REPORT LUNS "well known logical unit"
  3283. * (W-LUN), the normal Linux scanning logic does not associate it with a
  3284. * device (e.g. /dev/sg7). The following magic will make that association:
  3285. * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan"
  3286. * where <n> is a host number. If there are multiple targets in a host then
  3287. * the above will associate a W-LUN to each target. To only get a W-LUN
  3288. * for target 2, then use "echo '- 2 49409' > scan" .
  3289. */
  3290. static int resp_report_luns(struct scsi_cmnd *scp,
  3291. struct sdebug_dev_info *devip)
  3292. {
  3293. unsigned char *cmd = scp->cmnd;
  3294. unsigned int alloc_len;
  3295. unsigned char select_report;
  3296. u64 lun;
  3297. struct scsi_lun *lun_p;
  3298. u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)];
  3299. unsigned int lun_cnt; /* normal LUN count (max: 256) */
  3300. unsigned int wlun_cnt; /* report luns W-LUN count */
  3301. unsigned int tlun_cnt; /* total LUN count */
  3302. unsigned int rlen; /* response length (in bytes) */
  3303. int k, j, n, res;
  3304. unsigned int off_rsp = 0;
  3305. const int sz_lun = sizeof(struct scsi_lun);
  3306. clear_luns_changed_on_target(devip);
  3307. select_report = cmd[2];
  3308. alloc_len = get_unaligned_be32(cmd + 6);
  3309. if (alloc_len < 4) {
  3310. pr_err("alloc len too small %d\n", alloc_len);
  3311. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  3312. return check_condition_result;
  3313. }
  3314. switch (select_report) {
  3315. case 0: /* all LUNs apart from W-LUNs */
  3316. lun_cnt = sdebug_max_luns;
  3317. wlun_cnt = 0;
  3318. break;
  3319. case 1: /* only W-LUNs */
  3320. lun_cnt = 0;
  3321. wlun_cnt = 1;
  3322. break;
  3323. case 2: /* all LUNs */
  3324. lun_cnt = sdebug_max_luns;
  3325. wlun_cnt = 1;
  3326. break;
  3327. case 0x10: /* only administrative LUs */
  3328. case 0x11: /* see SPC-5 */
  3329. case 0x12: /* only subsiduary LUs owned by referenced LU */
  3330. default:
  3331. pr_debug("select report invalid %d\n", select_report);
  3332. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
  3333. return check_condition_result;
  3334. }
  3335. if (sdebug_no_lun_0 && (lun_cnt > 0))
  3336. --lun_cnt;
  3337. tlun_cnt = lun_cnt + wlun_cnt;
  3338. rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */
  3339. scsi_set_resid(scp, scsi_bufflen(scp));
  3340. pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n",
  3341. select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0);
  3342. /* loops rely on sizeof response header same as sizeof lun (both 8) */
  3343. lun = sdebug_no_lun_0 ? 1 : 0;
  3344. for (k = 0, j = 0, res = 0; true; ++k, j = 0) {
  3345. memset(arr, 0, sizeof(arr));
  3346. lun_p = (struct scsi_lun *)&arr[0];
  3347. if (k == 0) {
  3348. put_unaligned_be32(rlen, &arr[0]);
  3349. ++lun_p;
  3350. j = 1;
  3351. }
  3352. for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) {
  3353. if ((k * RL_BUCKET_ELEMS) + j > lun_cnt)
  3354. break;
  3355. int_to_scsilun(lun++, lun_p);
  3356. }
  3357. if (j < RL_BUCKET_ELEMS)
  3358. break;
  3359. n = j * sz_lun;
  3360. res = p_fill_from_dev_buffer(scp, arr, n, off_rsp);
  3361. if (res)
  3362. return res;
  3363. off_rsp += n;
  3364. }
  3365. if (wlun_cnt) {
  3366. int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p);
  3367. ++j;
  3368. }
  3369. if (j > 0)
  3370. res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp);
  3371. return res;
  3372. }
  3373. static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
  3374. unsigned int num, struct sdebug_dev_info *devip)
  3375. {
  3376. int j;
  3377. unsigned char *kaddr, *buf;
  3378. unsigned int offset;
  3379. struct scsi_data_buffer *sdb = scsi_in(scp);
  3380. struct sg_mapping_iter miter;
  3381. /* better not to use temporary buffer. */
  3382. buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
  3383. if (!buf) {
  3384. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3385. INSUFF_RES_ASCQ);
  3386. return check_condition_result;
  3387. }
  3388. scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
  3389. offset = 0;
  3390. sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents,
  3391. SG_MITER_ATOMIC | SG_MITER_TO_SG);
  3392. while (sg_miter_next(&miter)) {
  3393. kaddr = miter.addr;
  3394. for (j = 0; j < miter.length; j++)
  3395. *(kaddr + j) ^= *(buf + offset + j);
  3396. offset += miter.length;
  3397. }
  3398. sg_miter_stop(&miter);
  3399. kfree(buf);
  3400. return 0;
  3401. }
  3402. static int resp_xdwriteread_10(struct scsi_cmnd *scp,
  3403. struct sdebug_dev_info *devip)
  3404. {
  3405. u8 *cmd = scp->cmnd;
  3406. u64 lba;
  3407. u32 num;
  3408. int errsts;
  3409. if (!scsi_bidi_cmnd(scp)) {
  3410. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3411. INSUFF_RES_ASCQ);
  3412. return check_condition_result;
  3413. }
  3414. errsts = resp_read_dt0(scp, devip);
  3415. if (errsts)
  3416. return errsts;
  3417. if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */
  3418. errsts = resp_write_dt0(scp, devip);
  3419. if (errsts)
  3420. return errsts;
  3421. }
  3422. lba = get_unaligned_be32(cmd + 2);
  3423. num = get_unaligned_be16(cmd + 7);
  3424. return resp_xdwriteread(scp, lba, num, devip);
  3425. }
  3426. static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd)
  3427. {
  3428. u32 tag = blk_mq_unique_tag(cmnd->request);
  3429. u16 hwq = blk_mq_unique_tag_to_hwq(tag);
  3430. pr_debug("tag=%#x, hwq=%d\n", tag, hwq);
  3431. if (WARN_ON_ONCE(hwq >= submit_queues))
  3432. hwq = 0;
  3433. return sdebug_q_arr + hwq;
  3434. }
  3435. /* Queued (deferred) command completions converge here. */
  3436. static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)
  3437. {
  3438. bool aborted = sd_dp->aborted;
  3439. int qc_idx;
  3440. int retiring = 0;
  3441. unsigned long iflags;
  3442. struct sdebug_queue *sqp;
  3443. struct sdebug_queued_cmd *sqcp;
  3444. struct scsi_cmnd *scp;
  3445. struct sdebug_dev_info *devip;
  3446. sd_dp->defer_t = SDEB_DEFER_NONE;
  3447. if (unlikely(aborted))
  3448. sd_dp->aborted = false;
  3449. qc_idx = sd_dp->qc_idx;
  3450. sqp = sdebug_q_arr + sd_dp->sqa_idx;
  3451. if (sdebug_statistics) {
  3452. atomic_inc(&sdebug_completions);
  3453. if (raw_smp_processor_id() != sd_dp->issuing_cpu)
  3454. atomic_inc(&sdebug_miss_cpus);
  3455. }
  3456. if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) {
  3457. pr_err("wild qc_idx=%d\n", qc_idx);
  3458. return;
  3459. }
  3460. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3461. sqcp = &sqp->qc_arr[qc_idx];
  3462. scp = sqcp->a_cmnd;
  3463. if (unlikely(scp == NULL)) {
  3464. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3465. pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d\n",
  3466. sd_dp->sqa_idx, qc_idx);
  3467. return;
  3468. }
  3469. devip = (struct sdebug_dev_info *)scp->device->hostdata;
  3470. if (likely(devip))
  3471. atomic_dec(&devip->num_in_q);
  3472. else
  3473. pr_err("devip=NULL\n");
  3474. if (unlikely(atomic_read(&retired_max_queue) > 0))
  3475. retiring = 1;
  3476. sqcp->a_cmnd = NULL;
  3477. if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) {
  3478. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3479. pr_err("Unexpected completion\n");
  3480. return;
  3481. }
  3482. if (unlikely(retiring)) { /* user has reduced max_queue */
  3483. int k, retval;
  3484. retval = atomic_read(&retired_max_queue);
  3485. if (qc_idx >= retval) {
  3486. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3487. pr_err("index %d too large\n", retval);
  3488. return;
  3489. }
  3490. k = find_last_bit(sqp->in_use_bm, retval);
  3491. if ((k < sdebug_max_queue) || (k == retval))
  3492. atomic_set(&retired_max_queue, 0);
  3493. else
  3494. atomic_set(&retired_max_queue, k + 1);
  3495. }
  3496. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3497. if (unlikely(aborted)) {
  3498. if (sdebug_verbose)
  3499. pr_info("bypassing scsi_done() due to aborted cmd\n");
  3500. return;
  3501. }
  3502. scp->scsi_done(scp); /* callback to mid level */
  3503. }
  3504. /* When high resolution timer goes off this function is called. */
  3505. static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
  3506. {
  3507. struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer,
  3508. hrt);
  3509. sdebug_q_cmd_complete(sd_dp);
  3510. return HRTIMER_NORESTART;
  3511. }
  3512. /* When work queue schedules work, it calls this function. */
  3513. static void sdebug_q_cmd_wq_complete(struct work_struct *work)
  3514. {
  3515. struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer,
  3516. ew.work);
  3517. sdebug_q_cmd_complete(sd_dp);
  3518. }
  3519. static bool got_shared_uuid;
  3520. static uuid_t shared_uuid;
  3521. static struct sdebug_dev_info *sdebug_device_create(
  3522. struct sdebug_host_info *sdbg_host, gfp_t flags)
  3523. {
  3524. struct sdebug_dev_info *devip;
  3525. devip = kzalloc(sizeof(*devip), flags);
  3526. if (devip) {
  3527. if (sdebug_uuid_ctl == 1)
  3528. uuid_gen(&devip->lu_name);
  3529. else if (sdebug_uuid_ctl == 2) {
  3530. if (got_shared_uuid)
  3531. devip->lu_name = shared_uuid;
  3532. else {
  3533. uuid_gen(&shared_uuid);
  3534. got_shared_uuid = true;
  3535. devip->lu_name = shared_uuid;
  3536. }
  3537. }
  3538. devip->sdbg_host = sdbg_host;
  3539. list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
  3540. }
  3541. return devip;
  3542. }
  3543. static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev)
  3544. {
  3545. struct sdebug_host_info *sdbg_host;
  3546. struct sdebug_dev_info *open_devip = NULL;
  3547. struct sdebug_dev_info *devip;
  3548. sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
  3549. if (!sdbg_host) {
  3550. pr_err("Host info NULL\n");
  3551. return NULL;
  3552. }
  3553. list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
  3554. if ((devip->used) && (devip->channel == sdev->channel) &&
  3555. (devip->target == sdev->id) &&
  3556. (devip->lun == sdev->lun))
  3557. return devip;
  3558. else {
  3559. if ((!devip->used) && (!open_devip))
  3560. open_devip = devip;
  3561. }
  3562. }
  3563. if (!open_devip) { /* try and make a new one */
  3564. open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
  3565. if (!open_devip) {
  3566. pr_err("out of memory at line %d\n", __LINE__);
  3567. return NULL;
  3568. }
  3569. }
  3570. open_devip->channel = sdev->channel;
  3571. open_devip->target = sdev->id;
  3572. open_devip->lun = sdev->lun;
  3573. open_devip->sdbg_host = sdbg_host;
  3574. atomic_set(&open_devip->num_in_q, 0);
  3575. set_bit(SDEBUG_UA_POR, open_devip->uas_bm);
  3576. open_devip->used = true;
  3577. return open_devip;
  3578. }
  3579. static int scsi_debug_slave_alloc(struct scsi_device *sdp)
  3580. {
  3581. if (sdebug_verbose)
  3582. pr_info("slave_alloc <%u %u %u %llu>\n",
  3583. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  3584. blk_queue_flag_set(QUEUE_FLAG_BIDI, sdp->request_queue);
  3585. return 0;
  3586. }
  3587. static int scsi_debug_slave_configure(struct scsi_device *sdp)
  3588. {
  3589. struct sdebug_dev_info *devip =
  3590. (struct sdebug_dev_info *)sdp->hostdata;
  3591. if (sdebug_verbose)
  3592. pr_info("slave_configure <%u %u %u %llu>\n",
  3593. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  3594. if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN)
  3595. sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN;
  3596. if (devip == NULL) {
  3597. devip = find_build_dev_info(sdp);
  3598. if (devip == NULL)
  3599. return 1; /* no resources, will be marked offline */
  3600. }
  3601. sdp->hostdata = devip;
  3602. blk_queue_max_segment_size(sdp->request_queue, -1U);
  3603. if (sdebug_no_uld)
  3604. sdp->no_uld_attach = 1;
  3605. config_cdb_len(sdp);
  3606. return 0;
  3607. }
  3608. static void scsi_debug_slave_destroy(struct scsi_device *sdp)
  3609. {
  3610. struct sdebug_dev_info *devip =
  3611. (struct sdebug_dev_info *)sdp->hostdata;
  3612. if (sdebug_verbose)
  3613. pr_info("slave_destroy <%u %u %u %llu>\n",
  3614. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  3615. if (devip) {
  3616. /* make this slot available for re-use */
  3617. devip->used = false;
  3618. sdp->hostdata = NULL;
  3619. }
  3620. }
  3621. static void stop_qc_helper(struct sdebug_defer *sd_dp,
  3622. enum sdeb_defer_type defer_t)
  3623. {
  3624. if (!sd_dp)
  3625. return;
  3626. if (defer_t == SDEB_DEFER_HRT)
  3627. hrtimer_cancel(&sd_dp->hrt);
  3628. else if (defer_t == SDEB_DEFER_WQ)
  3629. cancel_work_sync(&sd_dp->ew.work);
  3630. }
  3631. /* If @cmnd found deletes its timer or work queue and returns true; else
  3632. returns false */
  3633. static bool stop_queued_cmnd(struct scsi_cmnd *cmnd)
  3634. {
  3635. unsigned long iflags;
  3636. int j, k, qmax, r_qmax;
  3637. enum sdeb_defer_type l_defer_t;
  3638. struct sdebug_queue *sqp;
  3639. struct sdebug_queued_cmd *sqcp;
  3640. struct sdebug_dev_info *devip;
  3641. struct sdebug_defer *sd_dp;
  3642. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  3643. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3644. qmax = sdebug_max_queue;
  3645. r_qmax = atomic_read(&retired_max_queue);
  3646. if (r_qmax > qmax)
  3647. qmax = r_qmax;
  3648. for (k = 0; k < qmax; ++k) {
  3649. if (test_bit(k, sqp->in_use_bm)) {
  3650. sqcp = &sqp->qc_arr[k];
  3651. if (cmnd != sqcp->a_cmnd)
  3652. continue;
  3653. /* found */
  3654. devip = (struct sdebug_dev_info *)
  3655. cmnd->device->hostdata;
  3656. if (devip)
  3657. atomic_dec(&devip->num_in_q);
  3658. sqcp->a_cmnd = NULL;
  3659. sd_dp = sqcp->sd_dp;
  3660. if (sd_dp) {
  3661. l_defer_t = sd_dp->defer_t;
  3662. sd_dp->defer_t = SDEB_DEFER_NONE;
  3663. } else
  3664. l_defer_t = SDEB_DEFER_NONE;
  3665. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3666. stop_qc_helper(sd_dp, l_defer_t);
  3667. clear_bit(k, sqp->in_use_bm);
  3668. return true;
  3669. }
  3670. }
  3671. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3672. }
  3673. return false;
  3674. }
  3675. /* Deletes (stops) timers or work queues of all queued commands */
  3676. static void stop_all_queued(void)
  3677. {
  3678. unsigned long iflags;
  3679. int j, k;
  3680. enum sdeb_defer_type l_defer_t;
  3681. struct sdebug_queue *sqp;
  3682. struct sdebug_queued_cmd *sqcp;
  3683. struct sdebug_dev_info *devip;
  3684. struct sdebug_defer *sd_dp;
  3685. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  3686. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3687. for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
  3688. if (test_bit(k, sqp->in_use_bm)) {
  3689. sqcp = &sqp->qc_arr[k];
  3690. if (sqcp->a_cmnd == NULL)
  3691. continue;
  3692. devip = (struct sdebug_dev_info *)
  3693. sqcp->a_cmnd->device->hostdata;
  3694. if (devip)
  3695. atomic_dec(&devip->num_in_q);
  3696. sqcp->a_cmnd = NULL;
  3697. sd_dp = sqcp->sd_dp;
  3698. if (sd_dp) {
  3699. l_defer_t = sd_dp->defer_t;
  3700. sd_dp->defer_t = SDEB_DEFER_NONE;
  3701. } else
  3702. l_defer_t = SDEB_DEFER_NONE;
  3703. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3704. stop_qc_helper(sd_dp, l_defer_t);
  3705. clear_bit(k, sqp->in_use_bm);
  3706. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3707. }
  3708. }
  3709. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3710. }
  3711. }
  3712. /* Free queued command memory on heap */
  3713. static void free_all_queued(void)
  3714. {
  3715. int j, k;
  3716. struct sdebug_queue *sqp;
  3717. struct sdebug_queued_cmd *sqcp;
  3718. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  3719. for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
  3720. sqcp = &sqp->qc_arr[k];
  3721. kfree(sqcp->sd_dp);
  3722. sqcp->sd_dp = NULL;
  3723. }
  3724. }
  3725. }
  3726. static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
  3727. {
  3728. bool ok;
  3729. ++num_aborts;
  3730. if (SCpnt) {
  3731. ok = stop_queued_cmnd(SCpnt);
  3732. if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
  3733. sdev_printk(KERN_INFO, SCpnt->device,
  3734. "%s: command%s found\n", __func__,
  3735. ok ? "" : " not");
  3736. }
  3737. return SUCCESS;
  3738. }
  3739. static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt)
  3740. {
  3741. ++num_dev_resets;
  3742. if (SCpnt && SCpnt->device) {
  3743. struct scsi_device *sdp = SCpnt->device;
  3744. struct sdebug_dev_info *devip =
  3745. (struct sdebug_dev_info *)sdp->hostdata;
  3746. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  3747. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  3748. if (devip)
  3749. set_bit(SDEBUG_UA_POR, devip->uas_bm);
  3750. }
  3751. return SUCCESS;
  3752. }
  3753. static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
  3754. {
  3755. struct sdebug_host_info *sdbg_host;
  3756. struct sdebug_dev_info *devip;
  3757. struct scsi_device *sdp;
  3758. struct Scsi_Host *hp;
  3759. int k = 0;
  3760. ++num_target_resets;
  3761. if (!SCpnt)
  3762. goto lie;
  3763. sdp = SCpnt->device;
  3764. if (!sdp)
  3765. goto lie;
  3766. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  3767. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  3768. hp = sdp->host;
  3769. if (!hp)
  3770. goto lie;
  3771. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  3772. if (sdbg_host) {
  3773. list_for_each_entry(devip,
  3774. &sdbg_host->dev_info_list,
  3775. dev_list)
  3776. if (devip->target == sdp->id) {
  3777. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3778. ++k;
  3779. }
  3780. }
  3781. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  3782. sdev_printk(KERN_INFO, sdp,
  3783. "%s: %d device(s) found in target\n", __func__, k);
  3784. lie:
  3785. return SUCCESS;
  3786. }
  3787. static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt)
  3788. {
  3789. struct sdebug_host_info *sdbg_host;
  3790. struct sdebug_dev_info *devip;
  3791. struct scsi_device *sdp;
  3792. struct Scsi_Host *hp;
  3793. int k = 0;
  3794. ++num_bus_resets;
  3795. if (!(SCpnt && SCpnt->device))
  3796. goto lie;
  3797. sdp = SCpnt->device;
  3798. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  3799. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  3800. hp = sdp->host;
  3801. if (hp) {
  3802. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  3803. if (sdbg_host) {
  3804. list_for_each_entry(devip,
  3805. &sdbg_host->dev_info_list,
  3806. dev_list) {
  3807. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3808. ++k;
  3809. }
  3810. }
  3811. }
  3812. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  3813. sdev_printk(KERN_INFO, sdp,
  3814. "%s: %d device(s) found in host\n", __func__, k);
  3815. lie:
  3816. return SUCCESS;
  3817. }
  3818. static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt)
  3819. {
  3820. struct sdebug_host_info *sdbg_host;
  3821. struct sdebug_dev_info *devip;
  3822. int k = 0;
  3823. ++num_host_resets;
  3824. if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
  3825. sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__);
  3826. spin_lock(&sdebug_host_list_lock);
  3827. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  3828. list_for_each_entry(devip, &sdbg_host->dev_info_list,
  3829. dev_list) {
  3830. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3831. ++k;
  3832. }
  3833. }
  3834. spin_unlock(&sdebug_host_list_lock);
  3835. stop_all_queued();
  3836. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  3837. sdev_printk(KERN_INFO, SCpnt->device,
  3838. "%s: %d device(s) found\n", __func__, k);
  3839. return SUCCESS;
  3840. }
  3841. static void __init sdebug_build_parts(unsigned char *ramp,
  3842. unsigned long store_size)
  3843. {
  3844. struct partition *pp;
  3845. int starts[SDEBUG_MAX_PARTS + 2];
  3846. int sectors_per_part, num_sectors, k;
  3847. int heads_by_sects, start_sec, end_sec;
  3848. /* assume partition table already zeroed */
  3849. if ((sdebug_num_parts < 1) || (store_size < 1048576))
  3850. return;
  3851. if (sdebug_num_parts > SDEBUG_MAX_PARTS) {
  3852. sdebug_num_parts = SDEBUG_MAX_PARTS;
  3853. pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS);
  3854. }
  3855. num_sectors = (int)sdebug_store_sectors;
  3856. sectors_per_part = (num_sectors - sdebug_sectors_per)
  3857. / sdebug_num_parts;
  3858. heads_by_sects = sdebug_heads * sdebug_sectors_per;
  3859. starts[0] = sdebug_sectors_per;
  3860. for (k = 1; k < sdebug_num_parts; ++k)
  3861. starts[k] = ((k * sectors_per_part) / heads_by_sects)
  3862. * heads_by_sects;
  3863. starts[sdebug_num_parts] = num_sectors;
  3864. starts[sdebug_num_parts + 1] = 0;
  3865. ramp[510] = 0x55; /* magic partition markings */
  3866. ramp[511] = 0xAA;
  3867. pp = (struct partition *)(ramp + 0x1be);
  3868. for (k = 0; starts[k + 1]; ++k, ++pp) {
  3869. start_sec = starts[k];
  3870. end_sec = starts[k + 1] - 1;
  3871. pp->boot_ind = 0;
  3872. pp->cyl = start_sec / heads_by_sects;
  3873. pp->head = (start_sec - (pp->cyl * heads_by_sects))
  3874. / sdebug_sectors_per;
  3875. pp->sector = (start_sec % sdebug_sectors_per) + 1;
  3876. pp->end_cyl = end_sec / heads_by_sects;
  3877. pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
  3878. / sdebug_sectors_per;
  3879. pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
  3880. pp->start_sect = cpu_to_le32(start_sec);
  3881. pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
  3882. pp->sys_ind = 0x83; /* plain Linux partition */
  3883. }
  3884. }
  3885. static void block_unblock_all_queues(bool block)
  3886. {
  3887. int j;
  3888. struct sdebug_queue *sqp;
  3889. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp)
  3890. atomic_set(&sqp->blocked, (int)block);
  3891. }
  3892. /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
  3893. * commands will be processed normally before triggers occur.
  3894. */
  3895. static void tweak_cmnd_count(void)
  3896. {
  3897. int count, modulo;
  3898. modulo = abs(sdebug_every_nth);
  3899. if (modulo < 2)
  3900. return;
  3901. block_unblock_all_queues(true);
  3902. count = atomic_read(&sdebug_cmnd_count);
  3903. atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo);
  3904. block_unblock_all_queues(false);
  3905. }
  3906. static void clear_queue_stats(void)
  3907. {
  3908. atomic_set(&sdebug_cmnd_count, 0);
  3909. atomic_set(&sdebug_completions, 0);
  3910. atomic_set(&sdebug_miss_cpus, 0);
  3911. atomic_set(&sdebug_a_tsf, 0);
  3912. }
  3913. static void setup_inject(struct sdebug_queue *sqp,
  3914. struct sdebug_queued_cmd *sqcp)
  3915. {
  3916. if ((atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) > 0) {
  3917. if (sdebug_every_nth > 0)
  3918. sqcp->inj_recovered = sqcp->inj_transport
  3919. = sqcp->inj_dif
  3920. = sqcp->inj_dix = sqcp->inj_short
  3921. = sqcp->inj_host_busy = sqcp->inj_cmd_abort = 0;
  3922. return;
  3923. }
  3924. sqcp->inj_recovered = !!(SDEBUG_OPT_RECOVERED_ERR & sdebug_opts);
  3925. sqcp->inj_transport = !!(SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts);
  3926. sqcp->inj_dif = !!(SDEBUG_OPT_DIF_ERR & sdebug_opts);
  3927. sqcp->inj_dix = !!(SDEBUG_OPT_DIX_ERR & sdebug_opts);
  3928. sqcp->inj_short = !!(SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts);
  3929. sqcp->inj_host_busy = !!(SDEBUG_OPT_HOST_BUSY & sdebug_opts);
  3930. sqcp->inj_cmd_abort = !!(SDEBUG_OPT_CMD_ABORT & sdebug_opts);
  3931. }
  3932. /* Complete the processing of the thread that queued a SCSI command to this
  3933. * driver. It either completes the command by calling cmnd_done() or
  3934. * schedules a hr timer or work queue then returns 0. Returns
  3935. * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources.
  3936. */
  3937. static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
  3938. int scsi_result,
  3939. int (*pfp)(struct scsi_cmnd *,
  3940. struct sdebug_dev_info *),
  3941. int delta_jiff, int ndelay)
  3942. {
  3943. unsigned long iflags;
  3944. int k, num_in_q, qdepth, inject;
  3945. struct sdebug_queue *sqp;
  3946. struct sdebug_queued_cmd *sqcp;
  3947. struct scsi_device *sdp;
  3948. struct sdebug_defer *sd_dp;
  3949. if (unlikely(devip == NULL)) {
  3950. if (scsi_result == 0)
  3951. scsi_result = DID_NO_CONNECT << 16;
  3952. goto respond_in_thread;
  3953. }
  3954. sdp = cmnd->device;
  3955. if (delta_jiff == 0)
  3956. goto respond_in_thread;
  3957. /* schedule the response at a later time if resources permit */
  3958. sqp = get_queue(cmnd);
  3959. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3960. if (unlikely(atomic_read(&sqp->blocked))) {
  3961. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3962. return SCSI_MLQUEUE_HOST_BUSY;
  3963. }
  3964. num_in_q = atomic_read(&devip->num_in_q);
  3965. qdepth = cmnd->device->queue_depth;
  3966. inject = 0;
  3967. if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) {
  3968. if (scsi_result) {
  3969. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3970. goto respond_in_thread;
  3971. } else
  3972. scsi_result = device_qfull_result;
  3973. } else if (unlikely(sdebug_every_nth &&
  3974. (SDEBUG_OPT_RARE_TSF & sdebug_opts) &&
  3975. (scsi_result == 0))) {
  3976. if ((num_in_q == (qdepth - 1)) &&
  3977. (atomic_inc_return(&sdebug_a_tsf) >=
  3978. abs(sdebug_every_nth))) {
  3979. atomic_set(&sdebug_a_tsf, 0);
  3980. inject = 1;
  3981. scsi_result = device_qfull_result;
  3982. }
  3983. }
  3984. k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue);
  3985. if (unlikely(k >= sdebug_max_queue)) {
  3986. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3987. if (scsi_result)
  3988. goto respond_in_thread;
  3989. else if (SDEBUG_OPT_ALL_TSF & sdebug_opts)
  3990. scsi_result = device_qfull_result;
  3991. if (SDEBUG_OPT_Q_NOISE & sdebug_opts)
  3992. sdev_printk(KERN_INFO, sdp,
  3993. "%s: max_queue=%d exceeded, %s\n",
  3994. __func__, sdebug_max_queue,
  3995. (scsi_result ? "status: TASK SET FULL" :
  3996. "report: host busy"));
  3997. if (scsi_result)
  3998. goto respond_in_thread;
  3999. else
  4000. return SCSI_MLQUEUE_HOST_BUSY;
  4001. }
  4002. __set_bit(k, sqp->in_use_bm);
  4003. atomic_inc(&devip->num_in_q);
  4004. sqcp = &sqp->qc_arr[k];
  4005. sqcp->a_cmnd = cmnd;
  4006. cmnd->host_scribble = (unsigned char *)sqcp;
  4007. sd_dp = sqcp->sd_dp;
  4008. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4009. if (unlikely(sdebug_every_nth && sdebug_any_injecting_opt))
  4010. setup_inject(sqp, sqcp);
  4011. if (sd_dp == NULL) {
  4012. sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC);
  4013. if (sd_dp == NULL)
  4014. return SCSI_MLQUEUE_HOST_BUSY;
  4015. }
  4016. cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
  4017. if (cmnd->result & SDEG_RES_IMMED_MASK) {
  4018. /*
  4019. * This is the F_DELAY_OVERR case. No delay.
  4020. */
  4021. cmnd->result &= ~SDEG_RES_IMMED_MASK;
  4022. delta_jiff = ndelay = 0;
  4023. }
  4024. if (cmnd->result == 0 && scsi_result != 0)
  4025. cmnd->result = scsi_result;
  4026. if (unlikely(sdebug_verbose && cmnd->result))
  4027. sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n",
  4028. __func__, cmnd->result);
  4029. if (delta_jiff > 0 || ndelay > 0) {
  4030. ktime_t kt;
  4031. if (delta_jiff > 0) {
  4032. kt = ns_to_ktime((u64)delta_jiff * (NSEC_PER_SEC / HZ));
  4033. } else
  4034. kt = ndelay;
  4035. if (!sd_dp->init_hrt) {
  4036. sd_dp->init_hrt = true;
  4037. sqcp->sd_dp = sd_dp;
  4038. hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC,
  4039. HRTIMER_MODE_REL_PINNED);
  4040. sd_dp->hrt.function = sdebug_q_cmd_hrt_complete;
  4041. sd_dp->sqa_idx = sqp - sdebug_q_arr;
  4042. sd_dp->qc_idx = k;
  4043. }
  4044. if (sdebug_statistics)
  4045. sd_dp->issuing_cpu = raw_smp_processor_id();
  4046. sd_dp->defer_t = SDEB_DEFER_HRT;
  4047. hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED);
  4048. } else { /* jdelay < 0, use work queue */
  4049. if (!sd_dp->init_wq) {
  4050. sd_dp->init_wq = true;
  4051. sqcp->sd_dp = sd_dp;
  4052. sd_dp->sqa_idx = sqp - sdebug_q_arr;
  4053. sd_dp->qc_idx = k;
  4054. INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete);
  4055. }
  4056. if (sdebug_statistics)
  4057. sd_dp->issuing_cpu = raw_smp_processor_id();
  4058. sd_dp->defer_t = SDEB_DEFER_WQ;
  4059. if (unlikely(sqcp->inj_cmd_abort))
  4060. sd_dp->aborted = true;
  4061. schedule_work(&sd_dp->ew.work);
  4062. if (unlikely(sqcp->inj_cmd_abort)) {
  4063. sdev_printk(KERN_INFO, sdp, "abort request tag %d\n",
  4064. cmnd->request->tag);
  4065. blk_abort_request(cmnd->request);
  4066. }
  4067. }
  4068. if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) &&
  4069. (scsi_result == device_qfull_result)))
  4070. sdev_printk(KERN_INFO, sdp,
  4071. "%s: num_in_q=%d +1, %s%s\n", __func__,
  4072. num_in_q, (inject ? "<inject> " : ""),
  4073. "status: TASK SET FULL");
  4074. return 0;
  4075. respond_in_thread: /* call back to mid-layer using invocation thread */
  4076. cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
  4077. cmnd->result &= ~SDEG_RES_IMMED_MASK;
  4078. if (cmnd->result == 0 && scsi_result != 0)
  4079. cmnd->result = scsi_result;
  4080. cmnd->scsi_done(cmnd);
  4081. return 0;
  4082. }
  4083. /* Note: The following macros create attribute files in the
  4084. /sys/module/scsi_debug/parameters directory. Unfortunately this
  4085. driver is unaware of a change and cannot trigger auxiliary actions
  4086. as it can when the corresponding attribute in the
  4087. /sys/bus/pseudo/drivers/scsi_debug directory is changed.
  4088. */
  4089. module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR);
  4090. module_param_named(ato, sdebug_ato, int, S_IRUGO);
  4091. module_param_named(cdb_len, sdebug_cdb_len, int, 0644);
  4092. module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR);
  4093. module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR);
  4094. module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO);
  4095. module_param_named(dif, sdebug_dif, int, S_IRUGO);
  4096. module_param_named(dix, sdebug_dix, int, S_IRUGO);
  4097. module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR);
  4098. module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR);
  4099. module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR);
  4100. module_param_named(guard, sdebug_guard, uint, S_IRUGO);
  4101. module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR);
  4102. module_param_string(inq_vendor, sdebug_inq_vendor_id,
  4103. sizeof(sdebug_inq_vendor_id), S_IRUGO|S_IWUSR);
  4104. module_param_string(inq_product, sdebug_inq_product_id,
  4105. sizeof(sdebug_inq_product_id), S_IRUGO|S_IWUSR);
  4106. module_param_string(inq_rev, sdebug_inq_product_rev,
  4107. sizeof(sdebug_inq_product_rev), S_IRUGO|S_IWUSR);
  4108. module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO);
  4109. module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO);
  4110. module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO);
  4111. module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO);
  4112. module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO);
  4113. module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR);
  4114. module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR);
  4115. module_param_named(medium_error_start, sdebug_medium_error_start, int, S_IRUGO | S_IWUSR);
  4116. module_param_named(medium_error_count, sdebug_medium_error_count, int, S_IRUGO | S_IWUSR);
  4117. module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR);
  4118. module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR);
  4119. module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO);
  4120. module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO);
  4121. module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR);
  4122. module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO);
  4123. module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
  4124. module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
  4125. module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO);
  4126. module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
  4127. module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
  4128. module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
  4129. module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
  4130. module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR);
  4131. module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR);
  4132. module_param_named(submit_queues, submit_queues, int, S_IRUGO);
  4133. module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO);
  4134. module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO);
  4135. module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO);
  4136. module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO);
  4137. module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR);
  4138. module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO);
  4139. module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int,
  4140. S_IRUGO | S_IWUSR);
  4141. module_param_named(write_same_length, sdebug_write_same_length, int,
  4142. S_IRUGO | S_IWUSR);
  4143. MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
  4144. MODULE_DESCRIPTION("SCSI debug adapter driver");
  4145. MODULE_LICENSE("GPL");
  4146. MODULE_VERSION(SDEBUG_VERSION);
  4147. MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
  4148. MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
  4149. MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)");
  4150. MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
  4151. MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
  4152. MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
  4153. MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
  4154. MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
  4155. MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
  4156. MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
  4157. MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
  4158. MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
  4159. MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)");
  4160. MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")");
  4161. MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")");
  4162. MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\""
  4163. SDEBUG_VERSION "\")");
  4164. MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
  4165. MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
  4166. MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
  4167. MODULE_PARM_DESC(lbprz,
  4168. "on read unmapped LBs return 0 when 1 (def), return 0xff when 2");
  4169. MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
  4170. MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
  4171. MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
  4172. MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error");
  4173. MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error");
  4174. MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
  4175. MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
  4176. MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
  4177. MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
  4178. MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
  4179. MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)");
  4180. MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
  4181. MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
  4182. MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)");
  4183. MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
  4184. MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
  4185. MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])");
  4186. MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
  4187. MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)");
  4188. MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
  4189. MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)");
  4190. MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
  4191. MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
  4192. MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
  4193. MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
  4194. MODULE_PARM_DESC(uuid_ctl,
  4195. "1->use uuid for lu name, 0->don't, 2->all use same (def=0)");
  4196. MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
  4197. MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
  4198. MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
  4199. #define SDEBUG_INFO_LEN 256
  4200. static char sdebug_info[SDEBUG_INFO_LEN];
  4201. static const char *scsi_debug_info(struct Scsi_Host *shp)
  4202. {
  4203. int k;
  4204. k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n",
  4205. my_name, SDEBUG_VERSION, sdebug_version_date);
  4206. if (k >= (SDEBUG_INFO_LEN - 1))
  4207. return sdebug_info;
  4208. scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k,
  4209. " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d",
  4210. sdebug_dev_size_mb, sdebug_opts, submit_queues,
  4211. "statistics", (int)sdebug_statistics);
  4212. return sdebug_info;
  4213. }
  4214. /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
  4215. static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer,
  4216. int length)
  4217. {
  4218. char arr[16];
  4219. int opts;
  4220. int minLen = length > 15 ? 15 : length;
  4221. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  4222. return -EACCES;
  4223. memcpy(arr, buffer, minLen);
  4224. arr[minLen] = '\0';
  4225. if (1 != sscanf(arr, "%d", &opts))
  4226. return -EINVAL;
  4227. sdebug_opts = opts;
  4228. sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
  4229. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
  4230. if (sdebug_every_nth != 0)
  4231. tweak_cmnd_count();
  4232. return length;
  4233. }
  4234. /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
  4235. * same for each scsi_debug host (if more than one). Some of the counters
  4236. * output are not atomics so might be inaccurate in a busy system. */
  4237. static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
  4238. {
  4239. int f, j, l;
  4240. struct sdebug_queue *sqp;
  4241. seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n",
  4242. SDEBUG_VERSION, sdebug_version_date);
  4243. seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n",
  4244. sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb,
  4245. sdebug_opts, sdebug_every_nth);
  4246. seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n",
  4247. sdebug_jdelay, sdebug_ndelay, sdebug_max_luns,
  4248. sdebug_sector_size, "bytes");
  4249. seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n",
  4250. sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
  4251. num_aborts);
  4252. seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n",
  4253. num_dev_resets, num_target_resets, num_bus_resets,
  4254. num_host_resets);
  4255. seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n",
  4256. dix_reads, dix_writes, dif_errors);
  4257. seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000,
  4258. sdebug_statistics);
  4259. seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n",
  4260. atomic_read(&sdebug_cmnd_count),
  4261. atomic_read(&sdebug_completions),
  4262. "miss_cpus", atomic_read(&sdebug_miss_cpus),
  4263. atomic_read(&sdebug_a_tsf));
  4264. seq_printf(m, "submit_queues=%d\n", submit_queues);
  4265. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  4266. seq_printf(m, " queue %d:\n", j);
  4267. f = find_first_bit(sqp->in_use_bm, sdebug_max_queue);
  4268. if (f != sdebug_max_queue) {
  4269. l = find_last_bit(sqp->in_use_bm, sdebug_max_queue);
  4270. seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n",
  4271. "first,last bits", f, l);
  4272. }
  4273. }
  4274. return 0;
  4275. }
  4276. static ssize_t delay_show(struct device_driver *ddp, char *buf)
  4277. {
  4278. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay);
  4279. }
  4280. /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit
  4281. * of delay is jiffies.
  4282. */
  4283. static ssize_t delay_store(struct device_driver *ddp, const char *buf,
  4284. size_t count)
  4285. {
  4286. int jdelay, res;
  4287. if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) {
  4288. res = count;
  4289. if (sdebug_jdelay != jdelay) {
  4290. int j, k;
  4291. struct sdebug_queue *sqp;
  4292. block_unblock_all_queues(true);
  4293. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  4294. ++j, ++sqp) {
  4295. k = find_first_bit(sqp->in_use_bm,
  4296. sdebug_max_queue);
  4297. if (k != sdebug_max_queue) {
  4298. res = -EBUSY; /* queued commands */
  4299. break;
  4300. }
  4301. }
  4302. if (res > 0) {
  4303. sdebug_jdelay = jdelay;
  4304. sdebug_ndelay = 0;
  4305. }
  4306. block_unblock_all_queues(false);
  4307. }
  4308. return res;
  4309. }
  4310. return -EINVAL;
  4311. }
  4312. static DRIVER_ATTR_RW(delay);
  4313. static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
  4314. {
  4315. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay);
  4316. }
  4317. /* Returns -EBUSY if ndelay is being changed and commands are queued */
  4318. /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */
  4319. static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
  4320. size_t count)
  4321. {
  4322. int ndelay, res;
  4323. if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
  4324. (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) {
  4325. res = count;
  4326. if (sdebug_ndelay != ndelay) {
  4327. int j, k;
  4328. struct sdebug_queue *sqp;
  4329. block_unblock_all_queues(true);
  4330. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  4331. ++j, ++sqp) {
  4332. k = find_first_bit(sqp->in_use_bm,
  4333. sdebug_max_queue);
  4334. if (k != sdebug_max_queue) {
  4335. res = -EBUSY; /* queued commands */
  4336. break;
  4337. }
  4338. }
  4339. if (res > 0) {
  4340. sdebug_ndelay = ndelay;
  4341. sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN
  4342. : DEF_JDELAY;
  4343. }
  4344. block_unblock_all_queues(false);
  4345. }
  4346. return res;
  4347. }
  4348. return -EINVAL;
  4349. }
  4350. static DRIVER_ATTR_RW(ndelay);
  4351. static ssize_t opts_show(struct device_driver *ddp, char *buf)
  4352. {
  4353. return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts);
  4354. }
  4355. static ssize_t opts_store(struct device_driver *ddp, const char *buf,
  4356. size_t count)
  4357. {
  4358. int opts;
  4359. char work[20];
  4360. if (sscanf(buf, "%10s", work) == 1) {
  4361. if (strncasecmp(work, "0x", 2) == 0) {
  4362. if (kstrtoint(work + 2, 16, &opts) == 0)
  4363. goto opts_done;
  4364. } else {
  4365. if (kstrtoint(work, 10, &opts) == 0)
  4366. goto opts_done;
  4367. }
  4368. }
  4369. return -EINVAL;
  4370. opts_done:
  4371. sdebug_opts = opts;
  4372. sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
  4373. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
  4374. tweak_cmnd_count();
  4375. return count;
  4376. }
  4377. static DRIVER_ATTR_RW(opts);
  4378. static ssize_t ptype_show(struct device_driver *ddp, char *buf)
  4379. {
  4380. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype);
  4381. }
  4382. static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
  4383. size_t count)
  4384. {
  4385. int n;
  4386. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4387. sdebug_ptype = n;
  4388. return count;
  4389. }
  4390. return -EINVAL;
  4391. }
  4392. static DRIVER_ATTR_RW(ptype);
  4393. static ssize_t dsense_show(struct device_driver *ddp, char *buf)
  4394. {
  4395. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense);
  4396. }
  4397. static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
  4398. size_t count)
  4399. {
  4400. int n;
  4401. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4402. sdebug_dsense = n;
  4403. return count;
  4404. }
  4405. return -EINVAL;
  4406. }
  4407. static DRIVER_ATTR_RW(dsense);
  4408. static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
  4409. {
  4410. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw);
  4411. }
  4412. static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
  4413. size_t count)
  4414. {
  4415. int n;
  4416. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4417. n = (n > 0);
  4418. sdebug_fake_rw = (sdebug_fake_rw > 0);
  4419. if (sdebug_fake_rw != n) {
  4420. if ((0 == n) && (NULL == fake_storep)) {
  4421. unsigned long sz =
  4422. (unsigned long)sdebug_dev_size_mb *
  4423. 1048576;
  4424. fake_storep = vzalloc(sz);
  4425. if (NULL == fake_storep) {
  4426. pr_err("out of memory, 9\n");
  4427. return -ENOMEM;
  4428. }
  4429. }
  4430. sdebug_fake_rw = n;
  4431. }
  4432. return count;
  4433. }
  4434. return -EINVAL;
  4435. }
  4436. static DRIVER_ATTR_RW(fake_rw);
  4437. static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
  4438. {
  4439. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0);
  4440. }
  4441. static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
  4442. size_t count)
  4443. {
  4444. int n;
  4445. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4446. sdebug_no_lun_0 = n;
  4447. return count;
  4448. }
  4449. return -EINVAL;
  4450. }
  4451. static DRIVER_ATTR_RW(no_lun_0);
  4452. static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
  4453. {
  4454. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts);
  4455. }
  4456. static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
  4457. size_t count)
  4458. {
  4459. int n;
  4460. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4461. sdebug_num_tgts = n;
  4462. sdebug_max_tgts_luns();
  4463. return count;
  4464. }
  4465. return -EINVAL;
  4466. }
  4467. static DRIVER_ATTR_RW(num_tgts);
  4468. static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
  4469. {
  4470. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb);
  4471. }
  4472. static DRIVER_ATTR_RO(dev_size_mb);
  4473. static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
  4474. {
  4475. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts);
  4476. }
  4477. static DRIVER_ATTR_RO(num_parts);
  4478. static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
  4479. {
  4480. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth);
  4481. }
  4482. static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
  4483. size_t count)
  4484. {
  4485. int nth;
  4486. if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
  4487. sdebug_every_nth = nth;
  4488. if (nth && !sdebug_statistics) {
  4489. pr_info("every_nth needs statistics=1, set it\n");
  4490. sdebug_statistics = true;
  4491. }
  4492. tweak_cmnd_count();
  4493. return count;
  4494. }
  4495. return -EINVAL;
  4496. }
  4497. static DRIVER_ATTR_RW(every_nth);
  4498. static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
  4499. {
  4500. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns);
  4501. }
  4502. static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
  4503. size_t count)
  4504. {
  4505. int n;
  4506. bool changed;
  4507. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4508. if (n > 256) {
  4509. pr_warn("max_luns can be no more than 256\n");
  4510. return -EINVAL;
  4511. }
  4512. changed = (sdebug_max_luns != n);
  4513. sdebug_max_luns = n;
  4514. sdebug_max_tgts_luns();
  4515. if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */
  4516. struct sdebug_host_info *sdhp;
  4517. struct sdebug_dev_info *dp;
  4518. spin_lock(&sdebug_host_list_lock);
  4519. list_for_each_entry(sdhp, &sdebug_host_list,
  4520. host_list) {
  4521. list_for_each_entry(dp, &sdhp->dev_info_list,
  4522. dev_list) {
  4523. set_bit(SDEBUG_UA_LUNS_CHANGED,
  4524. dp->uas_bm);
  4525. }
  4526. }
  4527. spin_unlock(&sdebug_host_list_lock);
  4528. }
  4529. return count;
  4530. }
  4531. return -EINVAL;
  4532. }
  4533. static DRIVER_ATTR_RW(max_luns);
  4534. static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
  4535. {
  4536. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue);
  4537. }
  4538. /* N.B. max_queue can be changed while there are queued commands. In flight
  4539. * commands beyond the new max_queue will be completed. */
  4540. static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
  4541. size_t count)
  4542. {
  4543. int j, n, k, a;
  4544. struct sdebug_queue *sqp;
  4545. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
  4546. (n <= SDEBUG_CANQUEUE)) {
  4547. block_unblock_all_queues(true);
  4548. k = 0;
  4549. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  4550. ++j, ++sqp) {
  4551. a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE);
  4552. if (a > k)
  4553. k = a;
  4554. }
  4555. sdebug_max_queue = n;
  4556. if (k == SDEBUG_CANQUEUE)
  4557. atomic_set(&retired_max_queue, 0);
  4558. else if (k >= n)
  4559. atomic_set(&retired_max_queue, k + 1);
  4560. else
  4561. atomic_set(&retired_max_queue, 0);
  4562. block_unblock_all_queues(false);
  4563. return count;
  4564. }
  4565. return -EINVAL;
  4566. }
  4567. static DRIVER_ATTR_RW(max_queue);
  4568. static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
  4569. {
  4570. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld);
  4571. }
  4572. static DRIVER_ATTR_RO(no_uld);
  4573. static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
  4574. {
  4575. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level);
  4576. }
  4577. static DRIVER_ATTR_RO(scsi_level);
  4578. static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
  4579. {
  4580. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb);
  4581. }
  4582. static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
  4583. size_t count)
  4584. {
  4585. int n;
  4586. bool changed;
  4587. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4588. changed = (sdebug_virtual_gb != n);
  4589. sdebug_virtual_gb = n;
  4590. sdebug_capacity = get_sdebug_capacity();
  4591. if (changed) {
  4592. struct sdebug_host_info *sdhp;
  4593. struct sdebug_dev_info *dp;
  4594. spin_lock(&sdebug_host_list_lock);
  4595. list_for_each_entry(sdhp, &sdebug_host_list,
  4596. host_list) {
  4597. list_for_each_entry(dp, &sdhp->dev_info_list,
  4598. dev_list) {
  4599. set_bit(SDEBUG_UA_CAPACITY_CHANGED,
  4600. dp->uas_bm);
  4601. }
  4602. }
  4603. spin_unlock(&sdebug_host_list_lock);
  4604. }
  4605. return count;
  4606. }
  4607. return -EINVAL;
  4608. }
  4609. static DRIVER_ATTR_RW(virtual_gb);
  4610. static ssize_t add_host_show(struct device_driver *ddp, char *buf)
  4611. {
  4612. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host);
  4613. }
  4614. static int sdebug_add_adapter(void);
  4615. static void sdebug_remove_adapter(void);
  4616. static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
  4617. size_t count)
  4618. {
  4619. int delta_hosts;
  4620. if (sscanf(buf, "%d", &delta_hosts) != 1)
  4621. return -EINVAL;
  4622. if (delta_hosts > 0) {
  4623. do {
  4624. sdebug_add_adapter();
  4625. } while (--delta_hosts);
  4626. } else if (delta_hosts < 0) {
  4627. do {
  4628. sdebug_remove_adapter();
  4629. } while (++delta_hosts);
  4630. }
  4631. return count;
  4632. }
  4633. static DRIVER_ATTR_RW(add_host);
  4634. static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
  4635. {
  4636. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno);
  4637. }
  4638. static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
  4639. size_t count)
  4640. {
  4641. int n;
  4642. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4643. sdebug_vpd_use_hostno = n;
  4644. return count;
  4645. }
  4646. return -EINVAL;
  4647. }
  4648. static DRIVER_ATTR_RW(vpd_use_hostno);
  4649. static ssize_t statistics_show(struct device_driver *ddp, char *buf)
  4650. {
  4651. return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics);
  4652. }
  4653. static ssize_t statistics_store(struct device_driver *ddp, const char *buf,
  4654. size_t count)
  4655. {
  4656. int n;
  4657. if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) {
  4658. if (n > 0)
  4659. sdebug_statistics = true;
  4660. else {
  4661. clear_queue_stats();
  4662. sdebug_statistics = false;
  4663. }
  4664. return count;
  4665. }
  4666. return -EINVAL;
  4667. }
  4668. static DRIVER_ATTR_RW(statistics);
  4669. static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
  4670. {
  4671. return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size);
  4672. }
  4673. static DRIVER_ATTR_RO(sector_size);
  4674. static ssize_t submit_queues_show(struct device_driver *ddp, char *buf)
  4675. {
  4676. return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues);
  4677. }
  4678. static DRIVER_ATTR_RO(submit_queues);
  4679. static ssize_t dix_show(struct device_driver *ddp, char *buf)
  4680. {
  4681. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix);
  4682. }
  4683. static DRIVER_ATTR_RO(dix);
  4684. static ssize_t dif_show(struct device_driver *ddp, char *buf)
  4685. {
  4686. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif);
  4687. }
  4688. static DRIVER_ATTR_RO(dif);
  4689. static ssize_t guard_show(struct device_driver *ddp, char *buf)
  4690. {
  4691. return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard);
  4692. }
  4693. static DRIVER_ATTR_RO(guard);
  4694. static ssize_t ato_show(struct device_driver *ddp, char *buf)
  4695. {
  4696. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato);
  4697. }
  4698. static DRIVER_ATTR_RO(ato);
  4699. static ssize_t map_show(struct device_driver *ddp, char *buf)
  4700. {
  4701. ssize_t count;
  4702. if (!scsi_debug_lbp())
  4703. return scnprintf(buf, PAGE_SIZE, "0-%u\n",
  4704. sdebug_store_sectors);
  4705. count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  4706. (int)map_size, map_storep);
  4707. buf[count++] = '\n';
  4708. buf[count] = '\0';
  4709. return count;
  4710. }
  4711. static DRIVER_ATTR_RO(map);
  4712. static ssize_t removable_show(struct device_driver *ddp, char *buf)
  4713. {
  4714. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
  4715. }
  4716. static ssize_t removable_store(struct device_driver *ddp, const char *buf,
  4717. size_t count)
  4718. {
  4719. int n;
  4720. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4721. sdebug_removable = (n > 0);
  4722. return count;
  4723. }
  4724. return -EINVAL;
  4725. }
  4726. static DRIVER_ATTR_RW(removable);
  4727. static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
  4728. {
  4729. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock);
  4730. }
  4731. /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */
  4732. static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
  4733. size_t count)
  4734. {
  4735. int n;
  4736. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4737. sdebug_host_lock = (n > 0);
  4738. return count;
  4739. }
  4740. return -EINVAL;
  4741. }
  4742. static DRIVER_ATTR_RW(host_lock);
  4743. static ssize_t strict_show(struct device_driver *ddp, char *buf)
  4744. {
  4745. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict);
  4746. }
  4747. static ssize_t strict_store(struct device_driver *ddp, const char *buf,
  4748. size_t count)
  4749. {
  4750. int n;
  4751. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4752. sdebug_strict = (n > 0);
  4753. return count;
  4754. }
  4755. return -EINVAL;
  4756. }
  4757. static DRIVER_ATTR_RW(strict);
  4758. static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf)
  4759. {
  4760. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl);
  4761. }
  4762. static DRIVER_ATTR_RO(uuid_ctl);
  4763. static ssize_t cdb_len_show(struct device_driver *ddp, char *buf)
  4764. {
  4765. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len);
  4766. }
  4767. static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf,
  4768. size_t count)
  4769. {
  4770. int ret, n;
  4771. ret = kstrtoint(buf, 0, &n);
  4772. if (ret)
  4773. return ret;
  4774. sdebug_cdb_len = n;
  4775. all_config_cdb_len();
  4776. return count;
  4777. }
  4778. static DRIVER_ATTR_RW(cdb_len);
  4779. /* Note: The following array creates attribute files in the
  4780. /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
  4781. files (over those found in the /sys/module/scsi_debug/parameters
  4782. directory) is that auxiliary actions can be triggered when an attribute
  4783. is changed. For example see: sdebug_add_host_store() above.
  4784. */
  4785. static struct attribute *sdebug_drv_attrs[] = {
  4786. &driver_attr_delay.attr,
  4787. &driver_attr_opts.attr,
  4788. &driver_attr_ptype.attr,
  4789. &driver_attr_dsense.attr,
  4790. &driver_attr_fake_rw.attr,
  4791. &driver_attr_no_lun_0.attr,
  4792. &driver_attr_num_tgts.attr,
  4793. &driver_attr_dev_size_mb.attr,
  4794. &driver_attr_num_parts.attr,
  4795. &driver_attr_every_nth.attr,
  4796. &driver_attr_max_luns.attr,
  4797. &driver_attr_max_queue.attr,
  4798. &driver_attr_no_uld.attr,
  4799. &driver_attr_scsi_level.attr,
  4800. &driver_attr_virtual_gb.attr,
  4801. &driver_attr_add_host.attr,
  4802. &driver_attr_vpd_use_hostno.attr,
  4803. &driver_attr_sector_size.attr,
  4804. &driver_attr_statistics.attr,
  4805. &driver_attr_submit_queues.attr,
  4806. &driver_attr_dix.attr,
  4807. &driver_attr_dif.attr,
  4808. &driver_attr_guard.attr,
  4809. &driver_attr_ato.attr,
  4810. &driver_attr_map.attr,
  4811. &driver_attr_removable.attr,
  4812. &driver_attr_host_lock.attr,
  4813. &driver_attr_ndelay.attr,
  4814. &driver_attr_strict.attr,
  4815. &driver_attr_uuid_ctl.attr,
  4816. &driver_attr_cdb_len.attr,
  4817. NULL,
  4818. };
  4819. ATTRIBUTE_GROUPS(sdebug_drv);
  4820. static struct device *pseudo_primary;
  4821. static int __init scsi_debug_init(void)
  4822. {
  4823. unsigned long sz;
  4824. int host_to_add;
  4825. int k;
  4826. int ret;
  4827. atomic_set(&retired_max_queue, 0);
  4828. if (sdebug_ndelay >= 1000 * 1000 * 1000) {
  4829. pr_warn("ndelay must be less than 1 second, ignored\n");
  4830. sdebug_ndelay = 0;
  4831. } else if (sdebug_ndelay > 0)
  4832. sdebug_jdelay = JDELAY_OVERRIDDEN;
  4833. switch (sdebug_sector_size) {
  4834. case 512:
  4835. case 1024:
  4836. case 2048:
  4837. case 4096:
  4838. break;
  4839. default:
  4840. pr_err("invalid sector_size %d\n", sdebug_sector_size);
  4841. return -EINVAL;
  4842. }
  4843. switch (sdebug_dif) {
  4844. case T10_PI_TYPE0_PROTECTION:
  4845. break;
  4846. case T10_PI_TYPE1_PROTECTION:
  4847. case T10_PI_TYPE2_PROTECTION:
  4848. case T10_PI_TYPE3_PROTECTION:
  4849. have_dif_prot = true;
  4850. break;
  4851. default:
  4852. pr_err("dif must be 0, 1, 2 or 3\n");
  4853. return -EINVAL;
  4854. }
  4855. if (sdebug_num_tgts < 0) {
  4856. pr_err("num_tgts must be >= 0\n");
  4857. return -EINVAL;
  4858. }
  4859. if (sdebug_guard > 1) {
  4860. pr_err("guard must be 0 or 1\n");
  4861. return -EINVAL;
  4862. }
  4863. if (sdebug_ato > 1) {
  4864. pr_err("ato must be 0 or 1\n");
  4865. return -EINVAL;
  4866. }
  4867. if (sdebug_physblk_exp > 15) {
  4868. pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp);
  4869. return -EINVAL;
  4870. }
  4871. if (sdebug_max_luns > 256) {
  4872. pr_warn("max_luns can be no more than 256, use default\n");
  4873. sdebug_max_luns = DEF_MAX_LUNS;
  4874. }
  4875. if (sdebug_lowest_aligned > 0x3fff) {
  4876. pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned);
  4877. return -EINVAL;
  4878. }
  4879. if (submit_queues < 1) {
  4880. pr_err("submit_queues must be 1 or more\n");
  4881. return -EINVAL;
  4882. }
  4883. if ((sdebug_max_queue > SDEBUG_CANQUEUE) || (sdebug_max_queue < 1)) {
  4884. pr_err("max_queue must be in range [1, %d]\n", SDEBUG_CANQUEUE);
  4885. return -EINVAL;
  4886. }
  4887. sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue),
  4888. GFP_KERNEL);
  4889. if (sdebug_q_arr == NULL)
  4890. return -ENOMEM;
  4891. for (k = 0; k < submit_queues; ++k)
  4892. spin_lock_init(&sdebug_q_arr[k].qc_lock);
  4893. if (sdebug_dev_size_mb < 1)
  4894. sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
  4895. sz = (unsigned long)sdebug_dev_size_mb * 1048576;
  4896. sdebug_store_sectors = sz / sdebug_sector_size;
  4897. sdebug_capacity = get_sdebug_capacity();
  4898. /* play around with geometry, don't waste too much on track 0 */
  4899. sdebug_heads = 8;
  4900. sdebug_sectors_per = 32;
  4901. if (sdebug_dev_size_mb >= 256)
  4902. sdebug_heads = 64;
  4903. else if (sdebug_dev_size_mb >= 16)
  4904. sdebug_heads = 32;
  4905. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  4906. (sdebug_sectors_per * sdebug_heads);
  4907. if (sdebug_cylinders_per >= 1024) {
  4908. /* other LLDs do this; implies >= 1GB ram disk ... */
  4909. sdebug_heads = 255;
  4910. sdebug_sectors_per = 63;
  4911. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  4912. (sdebug_sectors_per * sdebug_heads);
  4913. }
  4914. if (sdebug_fake_rw == 0) {
  4915. fake_storep = vzalloc(sz);
  4916. if (NULL == fake_storep) {
  4917. pr_err("out of memory, 1\n");
  4918. ret = -ENOMEM;
  4919. goto free_q_arr;
  4920. }
  4921. if (sdebug_num_parts > 0)
  4922. sdebug_build_parts(fake_storep, sz);
  4923. }
  4924. if (sdebug_dix) {
  4925. int dif_size;
  4926. dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple);
  4927. dif_storep = vmalloc(dif_size);
  4928. pr_err("dif_storep %u bytes @ %p\n", dif_size, dif_storep);
  4929. if (dif_storep == NULL) {
  4930. pr_err("out of mem. (DIX)\n");
  4931. ret = -ENOMEM;
  4932. goto free_vm;
  4933. }
  4934. memset(dif_storep, 0xff, dif_size);
  4935. }
  4936. /* Logical Block Provisioning */
  4937. if (scsi_debug_lbp()) {
  4938. sdebug_unmap_max_blocks =
  4939. clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU);
  4940. sdebug_unmap_max_desc =
  4941. clamp(sdebug_unmap_max_desc, 0U, 256U);
  4942. sdebug_unmap_granularity =
  4943. clamp(sdebug_unmap_granularity, 1U, 0xffffffffU);
  4944. if (sdebug_unmap_alignment &&
  4945. sdebug_unmap_granularity <=
  4946. sdebug_unmap_alignment) {
  4947. pr_err("ERR: unmap_granularity <= unmap_alignment\n");
  4948. ret = -EINVAL;
  4949. goto free_vm;
  4950. }
  4951. map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
  4952. map_storep = vmalloc(array_size(sizeof(long),
  4953. BITS_TO_LONGS(map_size)));
  4954. pr_info("%lu provisioning blocks\n", map_size);
  4955. if (map_storep == NULL) {
  4956. pr_err("out of mem. (MAP)\n");
  4957. ret = -ENOMEM;
  4958. goto free_vm;
  4959. }
  4960. bitmap_zero(map_storep, map_size);
  4961. /* Map first 1KB for partition table */
  4962. if (sdebug_num_parts)
  4963. map_region(0, 2);
  4964. }
  4965. pseudo_primary = root_device_register("pseudo_0");
  4966. if (IS_ERR(pseudo_primary)) {
  4967. pr_warn("root_device_register() error\n");
  4968. ret = PTR_ERR(pseudo_primary);
  4969. goto free_vm;
  4970. }
  4971. ret = bus_register(&pseudo_lld_bus);
  4972. if (ret < 0) {
  4973. pr_warn("bus_register error: %d\n", ret);
  4974. goto dev_unreg;
  4975. }
  4976. ret = driver_register(&sdebug_driverfs_driver);
  4977. if (ret < 0) {
  4978. pr_warn("driver_register error: %d\n", ret);
  4979. goto bus_unreg;
  4980. }
  4981. host_to_add = sdebug_add_host;
  4982. sdebug_add_host = 0;
  4983. for (k = 0; k < host_to_add; k++) {
  4984. if (sdebug_add_adapter()) {
  4985. pr_err("sdebug_add_adapter failed k=%d\n", k);
  4986. break;
  4987. }
  4988. }
  4989. if (sdebug_verbose)
  4990. pr_info("built %d host(s)\n", sdebug_add_host);
  4991. return 0;
  4992. bus_unreg:
  4993. bus_unregister(&pseudo_lld_bus);
  4994. dev_unreg:
  4995. root_device_unregister(pseudo_primary);
  4996. free_vm:
  4997. vfree(map_storep);
  4998. vfree(dif_storep);
  4999. vfree(fake_storep);
  5000. free_q_arr:
  5001. kfree(sdebug_q_arr);
  5002. return ret;
  5003. }
  5004. static void __exit scsi_debug_exit(void)
  5005. {
  5006. int k = sdebug_add_host;
  5007. stop_all_queued();
  5008. for (; k; k--)
  5009. sdebug_remove_adapter();
  5010. free_all_queued();
  5011. driver_unregister(&sdebug_driverfs_driver);
  5012. bus_unregister(&pseudo_lld_bus);
  5013. root_device_unregister(pseudo_primary);
  5014. vfree(map_storep);
  5015. vfree(dif_storep);
  5016. vfree(fake_storep);
  5017. kfree(sdebug_q_arr);
  5018. }
  5019. device_initcall(scsi_debug_init);
  5020. module_exit(scsi_debug_exit);
  5021. static void sdebug_release_adapter(struct device *dev)
  5022. {
  5023. struct sdebug_host_info *sdbg_host;
  5024. sdbg_host = to_sdebug_host(dev);
  5025. kfree(sdbg_host);
  5026. }
  5027. static int sdebug_add_adapter(void)
  5028. {
  5029. int k, devs_per_host;
  5030. int error = 0;
  5031. struct sdebug_host_info *sdbg_host;
  5032. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  5033. sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL);
  5034. if (sdbg_host == NULL) {
  5035. pr_err("out of memory at line %d\n", __LINE__);
  5036. return -ENOMEM;
  5037. }
  5038. INIT_LIST_HEAD(&sdbg_host->dev_info_list);
  5039. devs_per_host = sdebug_num_tgts * sdebug_max_luns;
  5040. for (k = 0; k < devs_per_host; k++) {
  5041. sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
  5042. if (!sdbg_devinfo) {
  5043. pr_err("out of memory at line %d\n", __LINE__);
  5044. error = -ENOMEM;
  5045. goto clean;
  5046. }
  5047. }
  5048. spin_lock(&sdebug_host_list_lock);
  5049. list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
  5050. spin_unlock(&sdebug_host_list_lock);
  5051. sdbg_host->dev.bus = &pseudo_lld_bus;
  5052. sdbg_host->dev.parent = pseudo_primary;
  5053. sdbg_host->dev.release = &sdebug_release_adapter;
  5054. dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host);
  5055. error = device_register(&sdbg_host->dev);
  5056. if (error)
  5057. goto clean;
  5058. ++sdebug_add_host;
  5059. return error;
  5060. clean:
  5061. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  5062. dev_list) {
  5063. list_del(&sdbg_devinfo->dev_list);
  5064. kfree(sdbg_devinfo);
  5065. }
  5066. kfree(sdbg_host);
  5067. return error;
  5068. }
  5069. static void sdebug_remove_adapter(void)
  5070. {
  5071. struct sdebug_host_info *sdbg_host = NULL;
  5072. spin_lock(&sdebug_host_list_lock);
  5073. if (!list_empty(&sdebug_host_list)) {
  5074. sdbg_host = list_entry(sdebug_host_list.prev,
  5075. struct sdebug_host_info, host_list);
  5076. list_del(&sdbg_host->host_list);
  5077. }
  5078. spin_unlock(&sdebug_host_list_lock);
  5079. if (!sdbg_host)
  5080. return;
  5081. device_unregister(&sdbg_host->dev);
  5082. --sdebug_add_host;
  5083. }
  5084. static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
  5085. {
  5086. int num_in_q = 0;
  5087. struct sdebug_dev_info *devip;
  5088. block_unblock_all_queues(true);
  5089. devip = (struct sdebug_dev_info *)sdev->hostdata;
  5090. if (NULL == devip) {
  5091. block_unblock_all_queues(false);
  5092. return -ENODEV;
  5093. }
  5094. num_in_q = atomic_read(&devip->num_in_q);
  5095. if (qdepth < 1)
  5096. qdepth = 1;
  5097. /* allow to exceed max host qc_arr elements for testing */
  5098. if (qdepth > SDEBUG_CANQUEUE + 10)
  5099. qdepth = SDEBUG_CANQUEUE + 10;
  5100. scsi_change_queue_depth(sdev, qdepth);
  5101. if (SDEBUG_OPT_Q_NOISE & sdebug_opts) {
  5102. sdev_printk(KERN_INFO, sdev, "%s: qdepth=%d, num_in_q=%d\n",
  5103. __func__, qdepth, num_in_q);
  5104. }
  5105. block_unblock_all_queues(false);
  5106. return sdev->queue_depth;
  5107. }
  5108. static bool fake_timeout(struct scsi_cmnd *scp)
  5109. {
  5110. if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) {
  5111. if (sdebug_every_nth < -1)
  5112. sdebug_every_nth = -1;
  5113. if (SDEBUG_OPT_TIMEOUT & sdebug_opts)
  5114. return true; /* ignore command causing timeout */
  5115. else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts &&
  5116. scsi_medium_access_command(scp))
  5117. return true; /* time out reads and writes */
  5118. }
  5119. return false;
  5120. }
  5121. static bool fake_host_busy(struct scsi_cmnd *scp)
  5122. {
  5123. return (sdebug_opts & SDEBUG_OPT_HOST_BUSY) &&
  5124. (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0;
  5125. }
  5126. static int scsi_debug_queuecommand(struct Scsi_Host *shost,
  5127. struct scsi_cmnd *scp)
  5128. {
  5129. u8 sdeb_i;
  5130. struct scsi_device *sdp = scp->device;
  5131. const struct opcode_info_t *oip;
  5132. const struct opcode_info_t *r_oip;
  5133. struct sdebug_dev_info *devip;
  5134. u8 *cmd = scp->cmnd;
  5135. int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
  5136. int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL;
  5137. int k, na;
  5138. int errsts = 0;
  5139. u32 flags;
  5140. u16 sa;
  5141. u8 opcode = cmd[0];
  5142. bool has_wlun_rl;
  5143. scsi_set_resid(scp, 0);
  5144. if (sdebug_statistics)
  5145. atomic_inc(&sdebug_cmnd_count);
  5146. if (unlikely(sdebug_verbose &&
  5147. !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) {
  5148. char b[120];
  5149. int n, len, sb;
  5150. len = scp->cmd_len;
  5151. sb = (int)sizeof(b);
  5152. if (len > 32)
  5153. strcpy(b, "too long, over 32 bytes");
  5154. else {
  5155. for (k = 0, n = 0; k < len && n < sb; ++k)
  5156. n += scnprintf(b + n, sb - n, "%02x ",
  5157. (u32)cmd[k]);
  5158. }
  5159. sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name,
  5160. blk_mq_unique_tag(scp->request), b);
  5161. }
  5162. if (fake_host_busy(scp))
  5163. return SCSI_MLQUEUE_HOST_BUSY;
  5164. has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS);
  5165. if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl))
  5166. goto err_out;
  5167. sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */
  5168. oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */
  5169. devip = (struct sdebug_dev_info *)sdp->hostdata;
  5170. if (unlikely(!devip)) {
  5171. devip = find_build_dev_info(sdp);
  5172. if (NULL == devip)
  5173. goto err_out;
  5174. }
  5175. na = oip->num_attached;
  5176. r_pfp = oip->pfp;
  5177. if (na) { /* multiple commands with this opcode */
  5178. r_oip = oip;
  5179. if (FF_SA & r_oip->flags) {
  5180. if (F_SA_LOW & oip->flags)
  5181. sa = 0x1f & cmd[1];
  5182. else
  5183. sa = get_unaligned_be16(cmd + 8);
  5184. for (k = 0; k <= na; oip = r_oip->arrp + k++) {
  5185. if (opcode == oip->opcode && sa == oip->sa)
  5186. break;
  5187. }
  5188. } else { /* since no service action only check opcode */
  5189. for (k = 0; k <= na; oip = r_oip->arrp + k++) {
  5190. if (opcode == oip->opcode)
  5191. break;
  5192. }
  5193. }
  5194. if (k > na) {
  5195. if (F_SA_LOW & r_oip->flags)
  5196. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
  5197. else if (F_SA_HIGH & r_oip->flags)
  5198. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
  5199. else
  5200. mk_sense_invalid_opcode(scp);
  5201. goto check_cond;
  5202. }
  5203. } /* else (when na==0) we assume the oip is a match */
  5204. flags = oip->flags;
  5205. if (unlikely(F_INV_OP & flags)) {
  5206. mk_sense_invalid_opcode(scp);
  5207. goto check_cond;
  5208. }
  5209. if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) {
  5210. if (sdebug_verbose)
  5211. sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n",
  5212. my_name, opcode, " supported for wlun");
  5213. mk_sense_invalid_opcode(scp);
  5214. goto check_cond;
  5215. }
  5216. if (unlikely(sdebug_strict)) { /* check cdb against mask */
  5217. u8 rem;
  5218. int j;
  5219. for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
  5220. rem = ~oip->len_mask[k] & cmd[k];
  5221. if (rem) {
  5222. for (j = 7; j >= 0; --j, rem <<= 1) {
  5223. if (0x80 & rem)
  5224. break;
  5225. }
  5226. mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
  5227. goto check_cond;
  5228. }
  5229. }
  5230. }
  5231. if (unlikely(!(F_SKIP_UA & flags) &&
  5232. find_first_bit(devip->uas_bm,
  5233. SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) {
  5234. errsts = make_ua(scp, devip);
  5235. if (errsts)
  5236. goto check_cond;
  5237. }
  5238. if (unlikely((F_M_ACCESS & flags) && atomic_read(&devip->stopped))) {
  5239. mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
  5240. if (sdebug_verbose)
  5241. sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: "
  5242. "%s\n", my_name, "initializing command "
  5243. "required");
  5244. errsts = check_condition_result;
  5245. goto fini;
  5246. }
  5247. if (sdebug_fake_rw && (F_FAKE_RW & flags))
  5248. goto fini;
  5249. if (unlikely(sdebug_every_nth)) {
  5250. if (fake_timeout(scp))
  5251. return 0; /* ignore command: make trouble */
  5252. }
  5253. if (likely(oip->pfp))
  5254. pfp = oip->pfp; /* calls a resp_* function */
  5255. else
  5256. pfp = r_pfp; /* if leaf function ptr NULL, try the root's */
  5257. fini:
  5258. if (F_DELAY_OVERR & flags)
  5259. return schedule_resp(scp, devip, errsts, pfp, 0, 0);
  5260. else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 ||
  5261. sdebug_ndelay > 10000)) {
  5262. /*
  5263. * Skip long delays if ndelay <= 10 microseconds. Otherwise
  5264. * for Start Stop Unit (SSU) want at least 1 second delay and
  5265. * if sdebug_jdelay>1 want a long delay of that many seconds.
  5266. * For Synchronize Cache want 1/20 of SSU's delay.
  5267. */
  5268. int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay;
  5269. int denom = (flags & F_SYNC_DELAY) ? 20 : 1;
  5270. jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ);
  5271. return schedule_resp(scp, devip, errsts, pfp, jdelay, 0);
  5272. } else
  5273. return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay,
  5274. sdebug_ndelay);
  5275. check_cond:
  5276. return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0);
  5277. err_out:
  5278. return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0);
  5279. }
  5280. static struct scsi_host_template sdebug_driver_template = {
  5281. .show_info = scsi_debug_show_info,
  5282. .write_info = scsi_debug_write_info,
  5283. .proc_name = sdebug_proc_name,
  5284. .name = "SCSI DEBUG",
  5285. .info = scsi_debug_info,
  5286. .slave_alloc = scsi_debug_slave_alloc,
  5287. .slave_configure = scsi_debug_slave_configure,
  5288. .slave_destroy = scsi_debug_slave_destroy,
  5289. .ioctl = scsi_debug_ioctl,
  5290. .queuecommand = scsi_debug_queuecommand,
  5291. .change_queue_depth = sdebug_change_qdepth,
  5292. .eh_abort_handler = scsi_debug_abort,
  5293. .eh_device_reset_handler = scsi_debug_device_reset,
  5294. .eh_target_reset_handler = scsi_debug_target_reset,
  5295. .eh_bus_reset_handler = scsi_debug_bus_reset,
  5296. .eh_host_reset_handler = scsi_debug_host_reset,
  5297. .can_queue = SDEBUG_CANQUEUE,
  5298. .this_id = 7,
  5299. .sg_tablesize = SG_MAX_SEGMENTS,
  5300. .cmd_per_lun = DEF_CMD_PER_LUN,
  5301. .max_sectors = -1U,
  5302. .use_clustering = DISABLE_CLUSTERING,
  5303. .module = THIS_MODULE,
  5304. .track_queue_depth = 1,
  5305. };
  5306. static int sdebug_driver_probe(struct device *dev)
  5307. {
  5308. int error = 0;
  5309. struct sdebug_host_info *sdbg_host;
  5310. struct Scsi_Host *hpnt;
  5311. int hprot;
  5312. sdbg_host = to_sdebug_host(dev);
  5313. sdebug_driver_template.can_queue = sdebug_max_queue;
  5314. if (sdebug_clustering)
  5315. sdebug_driver_template.use_clustering = ENABLE_CLUSTERING;
  5316. hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
  5317. if (NULL == hpnt) {
  5318. pr_err("scsi_host_alloc failed\n");
  5319. error = -ENODEV;
  5320. return error;
  5321. }
  5322. if (submit_queues > nr_cpu_ids) {
  5323. pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n",
  5324. my_name, submit_queues, nr_cpu_ids);
  5325. submit_queues = nr_cpu_ids;
  5326. }
  5327. /* Decide whether to tell scsi subsystem that we want mq */
  5328. /* Following should give the same answer for each host */
  5329. if (shost_use_blk_mq(hpnt))
  5330. hpnt->nr_hw_queues = submit_queues;
  5331. sdbg_host->shost = hpnt;
  5332. *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
  5333. if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id))
  5334. hpnt->max_id = sdebug_num_tgts + 1;
  5335. else
  5336. hpnt->max_id = sdebug_num_tgts;
  5337. /* = sdebug_max_luns; */
  5338. hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
  5339. hprot = 0;
  5340. switch (sdebug_dif) {
  5341. case T10_PI_TYPE1_PROTECTION:
  5342. hprot = SHOST_DIF_TYPE1_PROTECTION;
  5343. if (sdebug_dix)
  5344. hprot |= SHOST_DIX_TYPE1_PROTECTION;
  5345. break;
  5346. case T10_PI_TYPE2_PROTECTION:
  5347. hprot = SHOST_DIF_TYPE2_PROTECTION;
  5348. if (sdebug_dix)
  5349. hprot |= SHOST_DIX_TYPE2_PROTECTION;
  5350. break;
  5351. case T10_PI_TYPE3_PROTECTION:
  5352. hprot = SHOST_DIF_TYPE3_PROTECTION;
  5353. if (sdebug_dix)
  5354. hprot |= SHOST_DIX_TYPE3_PROTECTION;
  5355. break;
  5356. default:
  5357. if (sdebug_dix)
  5358. hprot |= SHOST_DIX_TYPE0_PROTECTION;
  5359. break;
  5360. }
  5361. scsi_host_set_prot(hpnt, hprot);
  5362. if (have_dif_prot || sdebug_dix)
  5363. pr_info("host protection%s%s%s%s%s%s%s\n",
  5364. (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
  5365. (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
  5366. (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
  5367. (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
  5368. (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
  5369. (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
  5370. (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
  5371. if (sdebug_guard == 1)
  5372. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
  5373. else
  5374. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
  5375. sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts);
  5376. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts);
  5377. if (sdebug_every_nth) /* need stats counters for every_nth */
  5378. sdebug_statistics = true;
  5379. error = scsi_add_host(hpnt, &sdbg_host->dev);
  5380. if (error) {
  5381. pr_err("scsi_add_host failed\n");
  5382. error = -ENODEV;
  5383. scsi_host_put(hpnt);
  5384. } else
  5385. scsi_scan_host(hpnt);
  5386. return error;
  5387. }
  5388. static int sdebug_driver_remove(struct device *dev)
  5389. {
  5390. struct sdebug_host_info *sdbg_host;
  5391. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  5392. sdbg_host = to_sdebug_host(dev);
  5393. if (!sdbg_host) {
  5394. pr_err("Unable to locate host info\n");
  5395. return -ENODEV;
  5396. }
  5397. scsi_remove_host(sdbg_host->shost);
  5398. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  5399. dev_list) {
  5400. list_del(&sdbg_devinfo->dev_list);
  5401. kfree(sdbg_devinfo);
  5402. }
  5403. scsi_host_put(sdbg_host->shost);
  5404. return 0;
  5405. }
  5406. static int pseudo_lld_bus_match(struct device *dev,
  5407. struct device_driver *dev_driver)
  5408. {
  5409. return 1;
  5410. }
  5411. static struct bus_type pseudo_lld_bus = {
  5412. .name = "pseudo",
  5413. .match = pseudo_lld_bus_match,
  5414. .probe = sdebug_driver_probe,
  5415. .remove = sdebug_driver_remove,
  5416. .drv_groups = sdebug_drv_groups,
  5417. };