scsi_debug.c 162 KB

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