scsi_debug.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701
  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. * This version is more generic, simulating a variable number of disk
  10. * (or disk like devices) sharing a common amount of RAM. To be more
  11. * realistic, the simulated devices have the transport attributes of
  12. * SAS disks.
  13. *
  14. *
  15. * For documentation see http://sg.danny.cz/sg/sdebug26.html
  16. *
  17. * D. Gilbert (dpg) work for Magneto-Optical device test [20010421]
  18. * dpg: work for devfs large number of disks [20010809]
  19. * forked for lk 2.5 series [20011216, 20020101]
  20. * use vmalloc() more inquiry+mode_sense [20020302]
  21. * add timers for delayed responses [20020721]
  22. * Patrick Mansfield <patmans@us.ibm.com> max_luns+scsi_level [20021031]
  23. * Mike Anderson <andmike@us.ibm.com> sysfs work [20021118]
  24. * dpg: change style of boot options to "scsi_debug.num_tgts=2" and
  25. * module options to "modprobe scsi_debug num_tgts=2" [20021221]
  26. */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/errno.h>
  30. #include <linux/timer.h>
  31. #include <linux/slab.h>
  32. #include <linux/types.h>
  33. #include <linux/string.h>
  34. #include <linux/genhd.h>
  35. #include <linux/fs.h>
  36. #include <linux/init.h>
  37. #include <linux/proc_fs.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/scatterlist.h>
  41. #include <linux/blkdev.h>
  42. #include <linux/crc-t10dif.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/atomic.h>
  46. #include <linux/hrtimer.h>
  47. #include <net/checksum.h>
  48. #include <asm/unaligned.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsicam.h>
  54. #include <scsi/scsi_eh.h>
  55. #include <scsi/scsi_tcq.h>
  56. #include <scsi/scsi_dbg.h>
  57. #include "sd.h"
  58. #include "scsi_logging.h"
  59. #define SCSI_DEBUG_VERSION "1.84"
  60. static const char *scsi_debug_version_date = "20140706";
  61. #define MY_NAME "scsi_debug"
  62. /* Additional Sense Code (ASC) */
  63. #define NO_ADDITIONAL_SENSE 0x0
  64. #define LOGICAL_UNIT_NOT_READY 0x4
  65. #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
  66. #define UNRECOVERED_READ_ERR 0x11
  67. #define PARAMETER_LIST_LENGTH_ERR 0x1a
  68. #define INVALID_OPCODE 0x20
  69. #define ADDR_OUT_OF_RANGE 0x21
  70. #define INVALID_COMMAND_OPCODE 0x20
  71. #define INVALID_FIELD_IN_CDB 0x24
  72. #define INVALID_FIELD_IN_PARAM_LIST 0x26
  73. #define UA_RESET_ASC 0x29
  74. #define UA_CHANGED_ASC 0x2a
  75. #define POWER_ON_RESET_ASCQ 0x0
  76. #define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */
  77. #define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */
  78. #define SAVING_PARAMS_UNSUP 0x39
  79. #define TRANSPORT_PROBLEM 0x4b
  80. #define THRESHOLD_EXCEEDED 0x5d
  81. #define LOW_POWER_COND_ON 0x5e
  82. /* Additional Sense Code Qualifier (ASCQ) */
  83. #define ACK_NAK_TO 0x3
  84. /* Default values for driver parameters */
  85. #define DEF_NUM_HOST 1
  86. #define DEF_NUM_TGTS 1
  87. #define DEF_MAX_LUNS 1
  88. /* With these defaults, this driver will make 1 host with 1 target
  89. * (id 0) containing 1 logical unit (lun 0). That is 1 device.
  90. */
  91. #define DEF_ATO 1
  92. #define DEF_DELAY 1 /* if > 0 unit is a jiffy */
  93. #define DEF_DEV_SIZE_MB 8
  94. #define DEF_DIF 0
  95. #define DEF_DIX 0
  96. #define DEF_D_SENSE 0
  97. #define DEF_EVERY_NTH 0
  98. #define DEF_FAKE_RW 0
  99. #define DEF_GUARD 0
  100. #define DEF_HOST_LOCK 0
  101. #define DEF_LBPU 0
  102. #define DEF_LBPWS 0
  103. #define DEF_LBPWS10 0
  104. #define DEF_LBPRZ 1
  105. #define DEF_LOWEST_ALIGNED 0
  106. #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */
  107. #define DEF_NO_LUN_0 0
  108. #define DEF_NUM_PARTS 0
  109. #define DEF_OPTS 0
  110. #define DEF_OPT_BLKS 64
  111. #define DEF_PHYSBLK_EXP 0
  112. #define DEF_PTYPE 0
  113. #define DEF_REMOVABLE false
  114. #define DEF_SCSI_LEVEL 6 /* INQUIRY, byte2 [6->SPC-4] */
  115. #define DEF_SECTOR_SIZE 512
  116. #define DEF_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
  117. #define DEF_UNMAP_ALIGNMENT 0
  118. #define DEF_UNMAP_GRANULARITY 1
  119. #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
  120. #define DEF_UNMAP_MAX_DESC 256
  121. #define DEF_VIRTUAL_GB 0
  122. #define DEF_VPD_USE_HOSTNO 1
  123. #define DEF_WRITESAME_LENGTH 0xFFFF
  124. #define DELAY_OVERRIDDEN -9999
  125. /* bit mask values for scsi_debug_opts */
  126. #define SCSI_DEBUG_OPT_NOISE 1
  127. #define SCSI_DEBUG_OPT_MEDIUM_ERR 2
  128. #define SCSI_DEBUG_OPT_TIMEOUT 4
  129. #define SCSI_DEBUG_OPT_RECOVERED_ERR 8
  130. #define SCSI_DEBUG_OPT_TRANSPORT_ERR 16
  131. #define SCSI_DEBUG_OPT_DIF_ERR 32
  132. #define SCSI_DEBUG_OPT_DIX_ERR 64
  133. #define SCSI_DEBUG_OPT_MAC_TIMEOUT 128
  134. #define SCSI_DEBUG_OPT_SHORT_TRANSFER 0x100
  135. #define SCSI_DEBUG_OPT_Q_NOISE 0x200
  136. #define SCSI_DEBUG_OPT_ALL_TSF 0x400
  137. #define SCSI_DEBUG_OPT_RARE_TSF 0x800
  138. #define SCSI_DEBUG_OPT_N_WCE 0x1000
  139. #define SCSI_DEBUG_OPT_RESET_NOISE 0x2000
  140. #define SCSI_DEBUG_OPT_NO_CDB_NOISE 0x4000
  141. #define SCSI_DEBUG_OPT_ALL_NOISE (0x1 | 0x200 | 0x2000)
  142. /* When "every_nth" > 0 then modulo "every_nth" commands:
  143. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  144. * - a RECOVERED_ERROR is simulated on successful read and write
  145. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  146. * - a TRANSPORT_ERROR is simulated on successful read and write
  147. * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
  148. *
  149. * When "every_nth" < 0 then after "- every_nth" commands:
  150. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  151. * - a RECOVERED_ERROR is simulated on successful read and write
  152. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  153. * - a TRANSPORT_ERROR is simulated on successful read and write
  154. * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
  155. * This will continue until some other action occurs (e.g. the user
  156. * writing a new value (other than -1 or 1) to every_nth via sysfs).
  157. */
  158. /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs)are returned in
  159. * priority order. In the subset implemented here lower numbers have higher
  160. * priority. The UA numbers should be a sequence starting from 0 with
  161. * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
  162. #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */
  163. #define SDEBUG_UA_BUS_RESET 1
  164. #define SDEBUG_UA_MODE_CHANGED 2
  165. #define SDEBUG_NUM_UAS 3
  166. /* for check_readiness() */
  167. #define UAS_ONLY 1
  168. #define UAS_TUR 0
  169. /* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
  170. * sector on read commands: */
  171. #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
  172. #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */
  173. /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
  174. * or "peripheral device" addressing (value 0) */
  175. #define SAM2_LUN_ADDRESS_METHOD 0
  176. #define SAM2_WLUN_REPORT_LUNS 0xc101
  177. /* SCSI_DEBUG_CANQUEUE is the maximum number of commands that can be queued
  178. * (for response) at one time. Can be reduced by max_queue option. Command
  179. * responses are not queued when delay=0 and ndelay=0. The per-device
  180. * DEF_CMD_PER_LUN can be changed via sysfs:
  181. * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth but cannot exceed
  182. * SCSI_DEBUG_CANQUEUE. */
  183. #define SCSI_DEBUG_CANQUEUE_WORDS 9 /* a WORD is bits in a long */
  184. #define SCSI_DEBUG_CANQUEUE (SCSI_DEBUG_CANQUEUE_WORDS * BITS_PER_LONG)
  185. #define DEF_CMD_PER_LUN 255
  186. #if DEF_CMD_PER_LUN > SCSI_DEBUG_CANQUEUE
  187. #warning "Expect DEF_CMD_PER_LUN <= SCSI_DEBUG_CANQUEUE"
  188. #endif
  189. static int scsi_debug_add_host = DEF_NUM_HOST;
  190. static int scsi_debug_ato = DEF_ATO;
  191. static int scsi_debug_delay = DEF_DELAY;
  192. static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
  193. static int scsi_debug_dif = DEF_DIF;
  194. static int scsi_debug_dix = DEF_DIX;
  195. static int scsi_debug_dsense = DEF_D_SENSE;
  196. static int scsi_debug_every_nth = DEF_EVERY_NTH;
  197. static int scsi_debug_fake_rw = DEF_FAKE_RW;
  198. static unsigned int scsi_debug_guard = DEF_GUARD;
  199. static int scsi_debug_lowest_aligned = DEF_LOWEST_ALIGNED;
  200. static int scsi_debug_max_luns = DEF_MAX_LUNS;
  201. static int scsi_debug_max_queue = SCSI_DEBUG_CANQUEUE;
  202. static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */
  203. static int scsi_debug_ndelay = DEF_NDELAY;
  204. static int scsi_debug_no_lun_0 = DEF_NO_LUN_0;
  205. static int scsi_debug_no_uld = 0;
  206. static int scsi_debug_num_parts = DEF_NUM_PARTS;
  207. static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
  208. static int scsi_debug_opt_blks = DEF_OPT_BLKS;
  209. static int scsi_debug_opts = DEF_OPTS;
  210. static int scsi_debug_physblk_exp = DEF_PHYSBLK_EXP;
  211. static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
  212. static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
  213. static int scsi_debug_sector_size = DEF_SECTOR_SIZE;
  214. static int scsi_debug_virtual_gb = DEF_VIRTUAL_GB;
  215. static int scsi_debug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
  216. static unsigned int scsi_debug_lbpu = DEF_LBPU;
  217. static unsigned int scsi_debug_lbpws = DEF_LBPWS;
  218. static unsigned int scsi_debug_lbpws10 = DEF_LBPWS10;
  219. static unsigned int scsi_debug_lbprz = DEF_LBPRZ;
  220. static unsigned int scsi_debug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
  221. static unsigned int scsi_debug_unmap_granularity = DEF_UNMAP_GRANULARITY;
  222. static unsigned int scsi_debug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
  223. static unsigned int scsi_debug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
  224. static unsigned int scsi_debug_write_same_length = DEF_WRITESAME_LENGTH;
  225. static bool scsi_debug_removable = DEF_REMOVABLE;
  226. static bool scsi_debug_clustering;
  227. static bool scsi_debug_host_lock = DEF_HOST_LOCK;
  228. static atomic_t sdebug_cmnd_count;
  229. static atomic_t sdebug_completions;
  230. static atomic_t sdebug_a_tsf; /* counter of 'almost' TSFs */
  231. #define DEV_READONLY(TGT) (0)
  232. static unsigned int sdebug_store_sectors;
  233. static sector_t sdebug_capacity; /* in sectors */
  234. /* old BIOS stuff, kernel may get rid of them but some mode sense pages
  235. may still need them */
  236. static int sdebug_heads; /* heads per disk */
  237. static int sdebug_cylinders_per; /* cylinders per surface */
  238. static int sdebug_sectors_per; /* sectors per cylinder */
  239. #define SDEBUG_MAX_PARTS 4
  240. #define SCSI_DEBUG_MAX_CMD_LEN 32
  241. static unsigned int scsi_debug_lbp(void)
  242. {
  243. return ((0 == scsi_debug_fake_rw) &&
  244. (scsi_debug_lbpu | scsi_debug_lbpws | scsi_debug_lbpws10));
  245. }
  246. struct sdebug_dev_info {
  247. struct list_head dev_list;
  248. unsigned int channel;
  249. unsigned int target;
  250. u64 lun;
  251. struct sdebug_host_info *sdbg_host;
  252. u64 wlun;
  253. unsigned long uas_bm[1];
  254. atomic_t num_in_q;
  255. char stopped;
  256. char used;
  257. };
  258. struct sdebug_host_info {
  259. struct list_head host_list;
  260. struct Scsi_Host *shost;
  261. struct device dev;
  262. struct list_head dev_info_list;
  263. };
  264. #define to_sdebug_host(d) \
  265. container_of(d, struct sdebug_host_info, dev)
  266. static LIST_HEAD(sdebug_host_list);
  267. static DEFINE_SPINLOCK(sdebug_host_list_lock);
  268. struct sdebug_hrtimer { /* ... is derived from hrtimer */
  269. struct hrtimer hrt; /* must be first element */
  270. int qa_indx;
  271. };
  272. struct sdebug_queued_cmd {
  273. /* in_use flagged by a bit in queued_in_use_bm[] */
  274. struct timer_list *cmnd_timerp;
  275. struct tasklet_struct *tletp;
  276. struct sdebug_hrtimer *sd_hrtp;
  277. struct scsi_cmnd * a_cmnd;
  278. };
  279. static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
  280. static unsigned long queued_in_use_bm[SCSI_DEBUG_CANQUEUE_WORDS];
  281. static unsigned char * fake_storep; /* ramdisk storage */
  282. static struct sd_dif_tuple *dif_storep; /* protection info */
  283. static void *map_storep; /* provisioning map */
  284. static unsigned long map_size;
  285. static int num_aborts;
  286. static int num_dev_resets;
  287. static int num_target_resets;
  288. static int num_bus_resets;
  289. static int num_host_resets;
  290. static int dix_writes;
  291. static int dix_reads;
  292. static int dif_errors;
  293. static DEFINE_SPINLOCK(queued_arr_lock);
  294. static DEFINE_RWLOCK(atomic_rw);
  295. static char sdebug_proc_name[] = MY_NAME;
  296. static const char *my_name = MY_NAME;
  297. static struct bus_type pseudo_lld_bus;
  298. static struct device_driver sdebug_driverfs_driver = {
  299. .name = sdebug_proc_name,
  300. .bus = &pseudo_lld_bus,
  301. };
  302. static const int check_condition_result =
  303. (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  304. static const int illegal_condition_result =
  305. (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
  306. static const int device_qfull_result =
  307. (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL;
  308. static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  309. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
  310. 0, 0, 0, 0};
  311. static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  312. 0, 0, 0x2, 0x4b};
  313. static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  314. 0, 0, 0x0, 0x0};
  315. static void *fake_store(unsigned long long lba)
  316. {
  317. lba = do_div(lba, sdebug_store_sectors);
  318. return fake_storep + lba * scsi_debug_sector_size;
  319. }
  320. static struct sd_dif_tuple *dif_store(sector_t sector)
  321. {
  322. sector = do_div(sector, sdebug_store_sectors);
  323. return dif_storep + sector;
  324. }
  325. static int sdebug_add_adapter(void);
  326. static void sdebug_remove_adapter(void);
  327. static void sdebug_max_tgts_luns(void)
  328. {
  329. struct sdebug_host_info *sdbg_host;
  330. struct Scsi_Host *hpnt;
  331. spin_lock(&sdebug_host_list_lock);
  332. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  333. hpnt = sdbg_host->shost;
  334. if ((hpnt->this_id >= 0) &&
  335. (scsi_debug_num_tgts > hpnt->this_id))
  336. hpnt->max_id = scsi_debug_num_tgts + 1;
  337. else
  338. hpnt->max_id = scsi_debug_num_tgts;
  339. /* scsi_debug_max_luns; */
  340. hpnt->max_lun = SAM2_WLUN_REPORT_LUNS;
  341. }
  342. spin_unlock(&sdebug_host_list_lock);
  343. }
  344. static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq)
  345. {
  346. unsigned char *sbuff;
  347. sbuff = scp->sense_buffer;
  348. if (!sbuff) {
  349. sdev_printk(KERN_ERR, scp->device,
  350. "%s: sense_buffer is NULL\n", __func__);
  351. return;
  352. }
  353. memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
  354. scsi_build_sense_buffer(scsi_debug_dsense, sbuff, key, asc, asq);
  355. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  356. sdev_printk(KERN_INFO, scp->device,
  357. "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n",
  358. my_name, key, asc, asq);
  359. }
  360. static void get_data_transfer_info(unsigned char *cmd,
  361. unsigned long long *lba, unsigned int *num,
  362. u32 *ei_lba)
  363. {
  364. *ei_lba = 0;
  365. switch (*cmd) {
  366. case VARIABLE_LENGTH_CMD:
  367. *lba = (u64)cmd[19] | (u64)cmd[18] << 8 |
  368. (u64)cmd[17] << 16 | (u64)cmd[16] << 24 |
  369. (u64)cmd[15] << 32 | (u64)cmd[14] << 40 |
  370. (u64)cmd[13] << 48 | (u64)cmd[12] << 56;
  371. *ei_lba = (u32)cmd[23] | (u32)cmd[22] << 8 |
  372. (u32)cmd[21] << 16 | (u32)cmd[20] << 24;
  373. *num = (u32)cmd[31] | (u32)cmd[30] << 8 | (u32)cmd[29] << 16 |
  374. (u32)cmd[28] << 24;
  375. break;
  376. case WRITE_SAME_16:
  377. case WRITE_16:
  378. case READ_16:
  379. *lba = (u64)cmd[9] | (u64)cmd[8] << 8 |
  380. (u64)cmd[7] << 16 | (u64)cmd[6] << 24 |
  381. (u64)cmd[5] << 32 | (u64)cmd[4] << 40 |
  382. (u64)cmd[3] << 48 | (u64)cmd[2] << 56;
  383. *num = (u32)cmd[13] | (u32)cmd[12] << 8 | (u32)cmd[11] << 16 |
  384. (u32)cmd[10] << 24;
  385. break;
  386. case WRITE_12:
  387. case READ_12:
  388. *lba = (u32)cmd[5] | (u32)cmd[4] << 8 | (u32)cmd[3] << 16 |
  389. (u32)cmd[2] << 24;
  390. *num = (u32)cmd[9] | (u32)cmd[8] << 8 | (u32)cmd[7] << 16 |
  391. (u32)cmd[6] << 24;
  392. break;
  393. case WRITE_SAME:
  394. case WRITE_10:
  395. case READ_10:
  396. case XDWRITEREAD_10:
  397. *lba = (u32)cmd[5] | (u32)cmd[4] << 8 | (u32)cmd[3] << 16 |
  398. (u32)cmd[2] << 24;
  399. *num = (u32)cmd[8] | (u32)cmd[7] << 8;
  400. break;
  401. case WRITE_6:
  402. case READ_6:
  403. *lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
  404. (u32)(cmd[1] & 0x1f) << 16;
  405. *num = (0 == cmd[4]) ? 256 : cmd[4];
  406. break;
  407. default:
  408. break;
  409. }
  410. }
  411. static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  412. {
  413. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  414. if (0x1261 == cmd)
  415. sdev_printk(KERN_INFO, dev,
  416. "%s: BLKFLSBUF [0x1261]\n", __func__);
  417. else if (0x5331 == cmd)
  418. sdev_printk(KERN_INFO, dev,
  419. "%s: CDROM_GET_CAPABILITY [0x5331]\n",
  420. __func__);
  421. else
  422. sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n",
  423. __func__, cmd);
  424. }
  425. return -EINVAL;
  426. /* return -ENOTTY; // correct return but upsets fdisk */
  427. }
  428. static int check_readiness(struct scsi_cmnd *SCpnt, int uas_only,
  429. struct sdebug_dev_info * devip)
  430. {
  431. int k;
  432. bool debug = !!(SCSI_DEBUG_OPT_NOISE & scsi_debug_opts);
  433. k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
  434. if (k != SDEBUG_NUM_UAS) {
  435. const char *cp = NULL;
  436. switch (k) {
  437. case SDEBUG_UA_POR:
  438. mk_sense_buffer(SCpnt, UNIT_ATTENTION,
  439. UA_RESET_ASC, POWER_ON_RESET_ASCQ);
  440. if (debug)
  441. cp = "power on reset";
  442. break;
  443. case SDEBUG_UA_BUS_RESET:
  444. mk_sense_buffer(SCpnt, UNIT_ATTENTION,
  445. UA_RESET_ASC, BUS_RESET_ASCQ);
  446. if (debug)
  447. cp = "bus reset";
  448. break;
  449. case SDEBUG_UA_MODE_CHANGED:
  450. mk_sense_buffer(SCpnt, UNIT_ATTENTION,
  451. UA_CHANGED_ASC, MODE_CHANGED_ASCQ);
  452. if (debug)
  453. cp = "mode parameters changed";
  454. break;
  455. default:
  456. pr_warn("%s: unexpected unit attention code=%d\n",
  457. __func__, k);
  458. if (debug)
  459. cp = "unknown";
  460. break;
  461. }
  462. clear_bit(k, devip->uas_bm);
  463. if (debug)
  464. sdev_printk(KERN_INFO, SCpnt->device,
  465. "%s reports: Unit attention: %s\n",
  466. my_name, cp);
  467. return check_condition_result;
  468. }
  469. if ((UAS_TUR == uas_only) && devip->stopped) {
  470. mk_sense_buffer(SCpnt, NOT_READY, LOGICAL_UNIT_NOT_READY,
  471. 0x2);
  472. if (debug)
  473. sdev_printk(KERN_INFO, SCpnt->device,
  474. "%s reports: Not ready: %s\n", my_name,
  475. "initializing command required");
  476. return check_condition_result;
  477. }
  478. return 0;
  479. }
  480. /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
  481. static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  482. int arr_len)
  483. {
  484. int act_len;
  485. struct scsi_data_buffer *sdb = scsi_in(scp);
  486. if (!sdb->length)
  487. return 0;
  488. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
  489. return (DID_ERROR << 16);
  490. act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
  491. arr, arr_len);
  492. sdb->resid = scsi_bufflen(scp) - act_len;
  493. return 0;
  494. }
  495. /* Returns number of bytes fetched into 'arr' or -1 if error. */
  496. static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  497. int arr_len)
  498. {
  499. if (!scsi_bufflen(scp))
  500. return 0;
  501. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE))
  502. return -1;
  503. return scsi_sg_copy_to_buffer(scp, arr, arr_len);
  504. }
  505. static const char * inq_vendor_id = "Linux ";
  506. static const char * inq_product_id = "scsi_debug ";
  507. static const char *inq_product_rev = "0184"; /* version less '.' */
  508. /* Device identification VPD page. Returns number of bytes placed in arr */
  509. static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
  510. int target_dev_id, int dev_id_num,
  511. const char * dev_id_str,
  512. int dev_id_str_len)
  513. {
  514. int num, port_a;
  515. char b[32];
  516. port_a = target_dev_id + 1;
  517. /* T10 vendor identifier field format (faked) */
  518. arr[0] = 0x2; /* ASCII */
  519. arr[1] = 0x1;
  520. arr[2] = 0x0;
  521. memcpy(&arr[4], inq_vendor_id, 8);
  522. memcpy(&arr[12], inq_product_id, 16);
  523. memcpy(&arr[28], dev_id_str, dev_id_str_len);
  524. num = 8 + 16 + dev_id_str_len;
  525. arr[3] = num;
  526. num += 4;
  527. if (dev_id_num >= 0) {
  528. /* NAA-5, Logical unit identifier (binary) */
  529. arr[num++] = 0x1; /* binary (not necessarily sas) */
  530. arr[num++] = 0x3; /* PIV=0, lu, naa */
  531. arr[num++] = 0x0;
  532. arr[num++] = 0x8;
  533. arr[num++] = 0x53; /* naa-5 ieee company id=0x333333 (fake) */
  534. arr[num++] = 0x33;
  535. arr[num++] = 0x33;
  536. arr[num++] = 0x30;
  537. arr[num++] = (dev_id_num >> 24);
  538. arr[num++] = (dev_id_num >> 16) & 0xff;
  539. arr[num++] = (dev_id_num >> 8) & 0xff;
  540. arr[num++] = dev_id_num & 0xff;
  541. /* Target relative port number */
  542. arr[num++] = 0x61; /* proto=sas, binary */
  543. arr[num++] = 0x94; /* PIV=1, target port, rel port */
  544. arr[num++] = 0x0; /* reserved */
  545. arr[num++] = 0x4; /* length */
  546. arr[num++] = 0x0; /* reserved */
  547. arr[num++] = 0x0; /* reserved */
  548. arr[num++] = 0x0;
  549. arr[num++] = 0x1; /* relative port A */
  550. }
  551. /* NAA-5, Target port identifier */
  552. arr[num++] = 0x61; /* proto=sas, binary */
  553. arr[num++] = 0x93; /* piv=1, target port, naa */
  554. arr[num++] = 0x0;
  555. arr[num++] = 0x8;
  556. arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
  557. arr[num++] = 0x22;
  558. arr[num++] = 0x22;
  559. arr[num++] = 0x20;
  560. arr[num++] = (port_a >> 24);
  561. arr[num++] = (port_a >> 16) & 0xff;
  562. arr[num++] = (port_a >> 8) & 0xff;
  563. arr[num++] = port_a & 0xff;
  564. /* NAA-5, Target port group identifier */
  565. arr[num++] = 0x61; /* proto=sas, binary */
  566. arr[num++] = 0x95; /* piv=1, target port group id */
  567. arr[num++] = 0x0;
  568. arr[num++] = 0x4;
  569. arr[num++] = 0;
  570. arr[num++] = 0;
  571. arr[num++] = (port_group_id >> 8) & 0xff;
  572. arr[num++] = port_group_id & 0xff;
  573. /* NAA-5, Target device identifier */
  574. arr[num++] = 0x61; /* proto=sas, binary */
  575. arr[num++] = 0xa3; /* piv=1, target device, naa */
  576. arr[num++] = 0x0;
  577. arr[num++] = 0x8;
  578. arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
  579. arr[num++] = 0x22;
  580. arr[num++] = 0x22;
  581. arr[num++] = 0x20;
  582. arr[num++] = (target_dev_id >> 24);
  583. arr[num++] = (target_dev_id >> 16) & 0xff;
  584. arr[num++] = (target_dev_id >> 8) & 0xff;
  585. arr[num++] = target_dev_id & 0xff;
  586. /* SCSI name string: Target device identifier */
  587. arr[num++] = 0x63; /* proto=sas, UTF-8 */
  588. arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
  589. arr[num++] = 0x0;
  590. arr[num++] = 24;
  591. memcpy(arr + num, "naa.52222220", 12);
  592. num += 12;
  593. snprintf(b, sizeof(b), "%08X", target_dev_id);
  594. memcpy(arr + num, b, 8);
  595. num += 8;
  596. memset(arr + num, 0, 4);
  597. num += 4;
  598. return num;
  599. }
  600. static unsigned char vpd84_data[] = {
  601. /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
  602. 0x22,0x22,0x22,0x0,0xbb,0x1,
  603. 0x22,0x22,0x22,0x0,0xbb,0x2,
  604. };
  605. /* Software interface identification VPD page */
  606. static int inquiry_evpd_84(unsigned char * arr)
  607. {
  608. memcpy(arr, vpd84_data, sizeof(vpd84_data));
  609. return sizeof(vpd84_data);
  610. }
  611. /* Management network addresses VPD page */
  612. static int inquiry_evpd_85(unsigned char * arr)
  613. {
  614. int num = 0;
  615. const char * na1 = "https://www.kernel.org/config";
  616. const char * na2 = "http://www.kernel.org/log";
  617. int plen, olen;
  618. arr[num++] = 0x1; /* lu, storage config */
  619. arr[num++] = 0x0; /* reserved */
  620. arr[num++] = 0x0;
  621. olen = strlen(na1);
  622. plen = olen + 1;
  623. if (plen % 4)
  624. plen = ((plen / 4) + 1) * 4;
  625. arr[num++] = plen; /* length, null termianted, padded */
  626. memcpy(arr + num, na1, olen);
  627. memset(arr + num + olen, 0, plen - olen);
  628. num += plen;
  629. arr[num++] = 0x4; /* lu, logging */
  630. arr[num++] = 0x0; /* reserved */
  631. arr[num++] = 0x0;
  632. olen = strlen(na2);
  633. plen = olen + 1;
  634. if (plen % 4)
  635. plen = ((plen / 4) + 1) * 4;
  636. arr[num++] = plen; /* length, null terminated, padded */
  637. memcpy(arr + num, na2, olen);
  638. memset(arr + num + olen, 0, plen - olen);
  639. num += plen;
  640. return num;
  641. }
  642. /* SCSI ports VPD page */
  643. static int inquiry_evpd_88(unsigned char * arr, int target_dev_id)
  644. {
  645. int num = 0;
  646. int port_a, port_b;
  647. port_a = target_dev_id + 1;
  648. port_b = port_a + 1;
  649. arr[num++] = 0x0; /* reserved */
  650. arr[num++] = 0x0; /* reserved */
  651. arr[num++] = 0x0;
  652. arr[num++] = 0x1; /* relative port 1 (primary) */
  653. memset(arr + num, 0, 6);
  654. num += 6;
  655. arr[num++] = 0x0;
  656. arr[num++] = 12; /* length tp descriptor */
  657. /* naa-5 target port identifier (A) */
  658. arr[num++] = 0x61; /* proto=sas, binary */
  659. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  660. arr[num++] = 0x0; /* reserved */
  661. arr[num++] = 0x8; /* length */
  662. arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
  663. arr[num++] = 0x22;
  664. arr[num++] = 0x22;
  665. arr[num++] = 0x20;
  666. arr[num++] = (port_a >> 24);
  667. arr[num++] = (port_a >> 16) & 0xff;
  668. arr[num++] = (port_a >> 8) & 0xff;
  669. arr[num++] = port_a & 0xff;
  670. arr[num++] = 0x0; /* reserved */
  671. arr[num++] = 0x0; /* reserved */
  672. arr[num++] = 0x0;
  673. arr[num++] = 0x2; /* relative port 2 (secondary) */
  674. memset(arr + num, 0, 6);
  675. num += 6;
  676. arr[num++] = 0x0;
  677. arr[num++] = 12; /* length tp descriptor */
  678. /* naa-5 target port identifier (B) */
  679. arr[num++] = 0x61; /* proto=sas, binary */
  680. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  681. arr[num++] = 0x0; /* reserved */
  682. arr[num++] = 0x8; /* length */
  683. arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
  684. arr[num++] = 0x22;
  685. arr[num++] = 0x22;
  686. arr[num++] = 0x20;
  687. arr[num++] = (port_b >> 24);
  688. arr[num++] = (port_b >> 16) & 0xff;
  689. arr[num++] = (port_b >> 8) & 0xff;
  690. arr[num++] = port_b & 0xff;
  691. return num;
  692. }
  693. static unsigned char vpd89_data[] = {
  694. /* from 4th byte */ 0,0,0,0,
  695. 'l','i','n','u','x',' ',' ',' ',
  696. 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
  697. '1','2','3','4',
  698. 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  699. 0xec,0,0,0,
  700. 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
  701. 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
  702. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
  703. 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
  704. 0x53,0x41,
  705. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  706. 0x20,0x20,
  707. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  708. 0x10,0x80,
  709. 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
  710. 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
  711. 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
  712. 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
  713. 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
  714. 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
  715. 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
  716. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  717. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  718. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  719. 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
  720. 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
  721. 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
  722. 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
  723. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  724. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  725. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  726. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  727. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  728. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  729. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  730. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  731. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  732. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  733. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  734. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
  735. };
  736. /* ATA Information VPD page */
  737. static int inquiry_evpd_89(unsigned char * arr)
  738. {
  739. memcpy(arr, vpd89_data, sizeof(vpd89_data));
  740. return sizeof(vpd89_data);
  741. }
  742. static unsigned char vpdb0_data[] = {
  743. /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
  744. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  745. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  746. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  747. };
  748. /* Block limits VPD page (SBC-3) */
  749. static int inquiry_evpd_b0(unsigned char * arr)
  750. {
  751. unsigned int gran;
  752. memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
  753. /* Optimal transfer length granularity */
  754. gran = 1 << scsi_debug_physblk_exp;
  755. arr[2] = (gran >> 8) & 0xff;
  756. arr[3] = gran & 0xff;
  757. /* Maximum Transfer Length */
  758. if (sdebug_store_sectors > 0x400) {
  759. arr[4] = (sdebug_store_sectors >> 24) & 0xff;
  760. arr[5] = (sdebug_store_sectors >> 16) & 0xff;
  761. arr[6] = (sdebug_store_sectors >> 8) & 0xff;
  762. arr[7] = sdebug_store_sectors & 0xff;
  763. }
  764. /* Optimal Transfer Length */
  765. put_unaligned_be32(scsi_debug_opt_blks, &arr[8]);
  766. if (scsi_debug_lbpu) {
  767. /* Maximum Unmap LBA Count */
  768. put_unaligned_be32(scsi_debug_unmap_max_blocks, &arr[16]);
  769. /* Maximum Unmap Block Descriptor Count */
  770. put_unaligned_be32(scsi_debug_unmap_max_desc, &arr[20]);
  771. }
  772. /* Unmap Granularity Alignment */
  773. if (scsi_debug_unmap_alignment) {
  774. put_unaligned_be32(scsi_debug_unmap_alignment, &arr[28]);
  775. arr[28] |= 0x80; /* UGAVALID */
  776. }
  777. /* Optimal Unmap Granularity */
  778. put_unaligned_be32(scsi_debug_unmap_granularity, &arr[24]);
  779. /* Maximum WRITE SAME Length */
  780. put_unaligned_be64(scsi_debug_write_same_length, &arr[32]);
  781. return 0x3c; /* Mandatory page length for Logical Block Provisioning */
  782. return sizeof(vpdb0_data);
  783. }
  784. /* Block device characteristics VPD page (SBC-3) */
  785. static int inquiry_evpd_b1(unsigned char *arr)
  786. {
  787. memset(arr, 0, 0x3c);
  788. arr[0] = 0;
  789. arr[1] = 1; /* non rotating medium (e.g. solid state) */
  790. arr[2] = 0;
  791. arr[3] = 5; /* less than 1.8" */
  792. return 0x3c;
  793. }
  794. /* Logical block provisioning VPD page (SBC-3) */
  795. static int inquiry_evpd_b2(unsigned char *arr)
  796. {
  797. memset(arr, 0, 0x4);
  798. arr[0] = 0; /* threshold exponent */
  799. if (scsi_debug_lbpu)
  800. arr[1] = 1 << 7;
  801. if (scsi_debug_lbpws)
  802. arr[1] |= 1 << 6;
  803. if (scsi_debug_lbpws10)
  804. arr[1] |= 1 << 5;
  805. if (scsi_debug_lbprz)
  806. arr[1] |= 1 << 2;
  807. return 0x4;
  808. }
  809. #define SDEBUG_LONG_INQ_SZ 96
  810. #define SDEBUG_MAX_INQ_ARR_SZ 584
  811. static int resp_inquiry(struct scsi_cmnd *scp, int target,
  812. struct sdebug_dev_info * devip)
  813. {
  814. unsigned char pq_pdt;
  815. unsigned char * arr;
  816. unsigned char *cmd = scp->cmnd;
  817. int alloc_len, n, ret;
  818. alloc_len = (cmd[3] << 8) + cmd[4];
  819. arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
  820. if (! arr)
  821. return DID_REQUEUE << 16;
  822. if (devip->wlun)
  823. pq_pdt = 0x1e; /* present, wlun */
  824. else if (scsi_debug_no_lun_0 && (0 == devip->lun))
  825. pq_pdt = 0x7f; /* not present, no device type */
  826. else
  827. pq_pdt = (scsi_debug_ptype & 0x1f);
  828. arr[0] = pq_pdt;
  829. if (0x2 & cmd[1]) { /* CMDDT bit set */
  830. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  831. 0);
  832. kfree(arr);
  833. return check_condition_result;
  834. } else if (0x1 & cmd[1]) { /* EVPD bit set */
  835. int lu_id_num, port_group_id, target_dev_id, len;
  836. char lu_id_str[6];
  837. int host_no = devip->sdbg_host->shost->host_no;
  838. port_group_id = (((host_no + 1) & 0x7f) << 8) +
  839. (devip->channel & 0x7f);
  840. if (0 == scsi_debug_vpd_use_hostno)
  841. host_no = 0;
  842. lu_id_num = devip->wlun ? -1 : (((host_no + 1) * 2000) +
  843. (devip->target * 1000) + devip->lun);
  844. target_dev_id = ((host_no + 1) * 2000) +
  845. (devip->target * 1000) - 3;
  846. len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
  847. if (0 == cmd[2]) { /* supported vital product data pages */
  848. arr[1] = cmd[2]; /*sanity */
  849. n = 4;
  850. arr[n++] = 0x0; /* this page */
  851. arr[n++] = 0x80; /* unit serial number */
  852. arr[n++] = 0x83; /* device identification */
  853. arr[n++] = 0x84; /* software interface ident. */
  854. arr[n++] = 0x85; /* management network addresses */
  855. arr[n++] = 0x86; /* extended inquiry */
  856. arr[n++] = 0x87; /* mode page policy */
  857. arr[n++] = 0x88; /* SCSI ports */
  858. arr[n++] = 0x89; /* ATA information */
  859. arr[n++] = 0xb0; /* Block limits (SBC) */
  860. arr[n++] = 0xb1; /* Block characteristics (SBC) */
  861. if (scsi_debug_lbp()) /* Logical Block Prov. (SBC) */
  862. arr[n++] = 0xb2;
  863. arr[3] = n - 4; /* number of supported VPD pages */
  864. } else if (0x80 == cmd[2]) { /* unit serial number */
  865. arr[1] = cmd[2]; /*sanity */
  866. arr[3] = len;
  867. memcpy(&arr[4], lu_id_str, len);
  868. } else if (0x83 == cmd[2]) { /* device identification */
  869. arr[1] = cmd[2]; /*sanity */
  870. arr[3] = inquiry_evpd_83(&arr[4], port_group_id,
  871. target_dev_id, lu_id_num,
  872. lu_id_str, len);
  873. } else if (0x84 == cmd[2]) { /* Software interface ident. */
  874. arr[1] = cmd[2]; /*sanity */
  875. arr[3] = inquiry_evpd_84(&arr[4]);
  876. } else if (0x85 == cmd[2]) { /* Management network addresses */
  877. arr[1] = cmd[2]; /*sanity */
  878. arr[3] = inquiry_evpd_85(&arr[4]);
  879. } else if (0x86 == cmd[2]) { /* extended inquiry */
  880. arr[1] = cmd[2]; /*sanity */
  881. arr[3] = 0x3c; /* number of following entries */
  882. if (scsi_debug_dif == SD_DIF_TYPE3_PROTECTION)
  883. arr[4] = 0x4; /* SPT: GRD_CHK:1 */
  884. else if (scsi_debug_dif)
  885. arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */
  886. else
  887. arr[4] = 0x0; /* no protection stuff */
  888. arr[5] = 0x7; /* head of q, ordered + simple q's */
  889. } else if (0x87 == cmd[2]) { /* mode page policy */
  890. arr[1] = cmd[2]; /*sanity */
  891. arr[3] = 0x8; /* number of following entries */
  892. arr[4] = 0x2; /* disconnect-reconnect mp */
  893. arr[6] = 0x80; /* mlus, shared */
  894. arr[8] = 0x18; /* protocol specific lu */
  895. arr[10] = 0x82; /* mlus, per initiator port */
  896. } else if (0x88 == cmd[2]) { /* SCSI Ports */
  897. arr[1] = cmd[2]; /*sanity */
  898. arr[3] = inquiry_evpd_88(&arr[4], target_dev_id);
  899. } else if (0x89 == cmd[2]) { /* ATA information */
  900. arr[1] = cmd[2]; /*sanity */
  901. n = inquiry_evpd_89(&arr[4]);
  902. arr[2] = (n >> 8);
  903. arr[3] = (n & 0xff);
  904. } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */
  905. arr[1] = cmd[2]; /*sanity */
  906. arr[3] = inquiry_evpd_b0(&arr[4]);
  907. } else if (0xb1 == cmd[2]) { /* Block characteristics (SBC) */
  908. arr[1] = cmd[2]; /*sanity */
  909. arr[3] = inquiry_evpd_b1(&arr[4]);
  910. } else if (0xb2 == cmd[2]) { /* Logical Block Prov. (SBC) */
  911. arr[1] = cmd[2]; /*sanity */
  912. arr[3] = inquiry_evpd_b2(&arr[4]);
  913. } else {
  914. /* Illegal request, invalid field in cdb */
  915. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  916. INVALID_FIELD_IN_CDB, 0);
  917. kfree(arr);
  918. return check_condition_result;
  919. }
  920. len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
  921. ret = fill_from_dev_buffer(scp, arr,
  922. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  923. kfree(arr);
  924. return ret;
  925. }
  926. /* drops through here for a standard inquiry */
  927. arr[1] = scsi_debug_removable ? 0x80 : 0; /* Removable disk */
  928. arr[2] = scsi_debug_scsi_level;
  929. arr[3] = 2; /* response_data_format==2 */
  930. arr[4] = SDEBUG_LONG_INQ_SZ - 5;
  931. arr[5] = scsi_debug_dif ? 1 : 0; /* PROTECT bit */
  932. if (0 == scsi_debug_vpd_use_hostno)
  933. arr[5] = 0x10; /* claim: implicit TGPS */
  934. arr[6] = 0x10; /* claim: MultiP */
  935. /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
  936. arr[7] = 0xa; /* claim: LINKED + CMDQUE */
  937. memcpy(&arr[8], inq_vendor_id, 8);
  938. memcpy(&arr[16], inq_product_id, 16);
  939. memcpy(&arr[32], inq_product_rev, 4);
  940. /* version descriptors (2 bytes each) follow */
  941. arr[58] = 0x0; arr[59] = 0xa2; /* SAM-5 rev 4 */
  942. arr[60] = 0x4; arr[61] = 0x68; /* SPC-4 rev 37 */
  943. n = 62;
  944. if (scsi_debug_ptype == 0) {
  945. arr[n++] = 0x4; arr[n++] = 0xc5; /* SBC-4 rev 36 */
  946. } else if (scsi_debug_ptype == 1) {
  947. arr[n++] = 0x5; arr[n++] = 0x25; /* SSC-4 rev 3 */
  948. }
  949. arr[n++] = 0x20; arr[n++] = 0xe6; /* SPL-3 rev 7 */
  950. ret = fill_from_dev_buffer(scp, arr,
  951. min(alloc_len, SDEBUG_LONG_INQ_SZ));
  952. kfree(arr);
  953. return ret;
  954. }
  955. static int resp_requests(struct scsi_cmnd * scp,
  956. struct sdebug_dev_info * devip)
  957. {
  958. unsigned char * sbuff;
  959. unsigned char *cmd = scp->cmnd;
  960. unsigned char arr[SCSI_SENSE_BUFFERSIZE];
  961. int want_dsense;
  962. int len = 18;
  963. memset(arr, 0, sizeof(arr));
  964. want_dsense = !!(cmd[1] & 1) || scsi_debug_dsense;
  965. sbuff = scp->sense_buffer;
  966. if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
  967. if (want_dsense) {
  968. arr[0] = 0x72;
  969. arr[1] = 0x0; /* NO_SENSE in sense_key */
  970. arr[2] = THRESHOLD_EXCEEDED;
  971. arr[3] = 0xff; /* TEST set and MRIE==6 */
  972. } else {
  973. arr[0] = 0x70;
  974. arr[2] = 0x0; /* NO_SENSE in sense_key */
  975. arr[7] = 0xa; /* 18 byte sense buffer */
  976. arr[12] = THRESHOLD_EXCEEDED;
  977. arr[13] = 0xff; /* TEST set and MRIE==6 */
  978. }
  979. } else {
  980. memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE);
  981. if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
  982. /* DESC bit set and sense_buff in fixed format */
  983. memset(arr, 0, sizeof(arr));
  984. arr[0] = 0x72;
  985. arr[1] = sbuff[2]; /* sense key */
  986. arr[2] = sbuff[12]; /* asc */
  987. arr[3] = sbuff[13]; /* ascq */
  988. len = 8;
  989. }
  990. }
  991. mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0);
  992. return fill_from_dev_buffer(scp, arr, len);
  993. }
  994. static int resp_start_stop(struct scsi_cmnd * scp,
  995. struct sdebug_dev_info * devip)
  996. {
  997. unsigned char *cmd = scp->cmnd;
  998. int power_cond, errsts, start;
  999. errsts = check_readiness(scp, UAS_ONLY, devip);
  1000. if (errsts)
  1001. return errsts;
  1002. power_cond = (cmd[4] & 0xf0) >> 4;
  1003. if (power_cond) {
  1004. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1005. 0);
  1006. return check_condition_result;
  1007. }
  1008. start = cmd[4] & 1;
  1009. if (start == devip->stopped)
  1010. devip->stopped = !start;
  1011. return 0;
  1012. }
  1013. static sector_t get_sdebug_capacity(void)
  1014. {
  1015. if (scsi_debug_virtual_gb > 0)
  1016. return (sector_t)scsi_debug_virtual_gb *
  1017. (1073741824 / scsi_debug_sector_size);
  1018. else
  1019. return sdebug_store_sectors;
  1020. }
  1021. #define SDEBUG_READCAP_ARR_SZ 8
  1022. static int resp_readcap(struct scsi_cmnd * scp,
  1023. struct sdebug_dev_info * devip)
  1024. {
  1025. unsigned char arr[SDEBUG_READCAP_ARR_SZ];
  1026. unsigned int capac;
  1027. int errsts;
  1028. errsts = check_readiness(scp, UAS_ONLY, devip);
  1029. if (errsts)
  1030. return errsts;
  1031. /* following just in case virtual_gb changed */
  1032. sdebug_capacity = get_sdebug_capacity();
  1033. memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
  1034. if (sdebug_capacity < 0xffffffff) {
  1035. capac = (unsigned int)sdebug_capacity - 1;
  1036. arr[0] = (capac >> 24);
  1037. arr[1] = (capac >> 16) & 0xff;
  1038. arr[2] = (capac >> 8) & 0xff;
  1039. arr[3] = capac & 0xff;
  1040. } else {
  1041. arr[0] = 0xff;
  1042. arr[1] = 0xff;
  1043. arr[2] = 0xff;
  1044. arr[3] = 0xff;
  1045. }
  1046. arr[6] = (scsi_debug_sector_size >> 8) & 0xff;
  1047. arr[7] = scsi_debug_sector_size & 0xff;
  1048. return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
  1049. }
  1050. #define SDEBUG_READCAP16_ARR_SZ 32
  1051. static int resp_readcap16(struct scsi_cmnd * scp,
  1052. struct sdebug_dev_info * devip)
  1053. {
  1054. unsigned char *cmd = scp->cmnd;
  1055. unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
  1056. unsigned long long capac;
  1057. int errsts, k, alloc_len;
  1058. errsts = check_readiness(scp, UAS_ONLY, devip);
  1059. if (errsts)
  1060. return errsts;
  1061. alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8)
  1062. + cmd[13]);
  1063. /* following just in case virtual_gb changed */
  1064. sdebug_capacity = get_sdebug_capacity();
  1065. memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
  1066. capac = sdebug_capacity - 1;
  1067. for (k = 0; k < 8; ++k, capac >>= 8)
  1068. arr[7 - k] = capac & 0xff;
  1069. arr[8] = (scsi_debug_sector_size >> 24) & 0xff;
  1070. arr[9] = (scsi_debug_sector_size >> 16) & 0xff;
  1071. arr[10] = (scsi_debug_sector_size >> 8) & 0xff;
  1072. arr[11] = scsi_debug_sector_size & 0xff;
  1073. arr[13] = scsi_debug_physblk_exp & 0xf;
  1074. arr[14] = (scsi_debug_lowest_aligned >> 8) & 0x3f;
  1075. if (scsi_debug_lbp()) {
  1076. arr[14] |= 0x80; /* LBPME */
  1077. if (scsi_debug_lbprz)
  1078. arr[14] |= 0x40; /* LBPRZ */
  1079. }
  1080. arr[15] = scsi_debug_lowest_aligned & 0xff;
  1081. if (scsi_debug_dif) {
  1082. arr[12] = (scsi_debug_dif - 1) << 1; /* P_TYPE */
  1083. arr[12] |= 1; /* PROT_EN */
  1084. }
  1085. return fill_from_dev_buffer(scp, arr,
  1086. min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
  1087. }
  1088. #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
  1089. static int resp_report_tgtpgs(struct scsi_cmnd * scp,
  1090. struct sdebug_dev_info * devip)
  1091. {
  1092. unsigned char *cmd = scp->cmnd;
  1093. unsigned char * arr;
  1094. int host_no = devip->sdbg_host->shost->host_no;
  1095. int n, ret, alen, rlen;
  1096. int port_group_a, port_group_b, port_a, port_b;
  1097. alen = ((cmd[6] << 24) + (cmd[7] << 16) + (cmd[8] << 8)
  1098. + cmd[9]);
  1099. arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
  1100. if (! arr)
  1101. return DID_REQUEUE << 16;
  1102. /*
  1103. * EVPD page 0x88 states we have two ports, one
  1104. * real and a fake port with no device connected.
  1105. * So we create two port groups with one port each
  1106. * and set the group with port B to unavailable.
  1107. */
  1108. port_a = 0x1; /* relative port A */
  1109. port_b = 0x2; /* relative port B */
  1110. port_group_a = (((host_no + 1) & 0x7f) << 8) +
  1111. (devip->channel & 0x7f);
  1112. port_group_b = (((host_no + 1) & 0x7f) << 8) +
  1113. (devip->channel & 0x7f) + 0x80;
  1114. /*
  1115. * The asymmetric access state is cycled according to the host_id.
  1116. */
  1117. n = 4;
  1118. if (0 == scsi_debug_vpd_use_hostno) {
  1119. arr[n++] = host_no % 3; /* Asymm access state */
  1120. arr[n++] = 0x0F; /* claim: all states are supported */
  1121. } else {
  1122. arr[n++] = 0x0; /* Active/Optimized path */
  1123. arr[n++] = 0x01; /* claim: only support active/optimized paths */
  1124. }
  1125. arr[n++] = (port_group_a >> 8) & 0xff;
  1126. arr[n++] = port_group_a & 0xff;
  1127. arr[n++] = 0; /* Reserved */
  1128. arr[n++] = 0; /* Status code */
  1129. arr[n++] = 0; /* Vendor unique */
  1130. arr[n++] = 0x1; /* One port per group */
  1131. arr[n++] = 0; /* Reserved */
  1132. arr[n++] = 0; /* Reserved */
  1133. arr[n++] = (port_a >> 8) & 0xff;
  1134. arr[n++] = port_a & 0xff;
  1135. arr[n++] = 3; /* Port unavailable */
  1136. arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
  1137. arr[n++] = (port_group_b >> 8) & 0xff;
  1138. arr[n++] = port_group_b & 0xff;
  1139. arr[n++] = 0; /* Reserved */
  1140. arr[n++] = 0; /* Status code */
  1141. arr[n++] = 0; /* Vendor unique */
  1142. arr[n++] = 0x1; /* One port per group */
  1143. arr[n++] = 0; /* Reserved */
  1144. arr[n++] = 0; /* Reserved */
  1145. arr[n++] = (port_b >> 8) & 0xff;
  1146. arr[n++] = port_b & 0xff;
  1147. rlen = n - 4;
  1148. arr[0] = (rlen >> 24) & 0xff;
  1149. arr[1] = (rlen >> 16) & 0xff;
  1150. arr[2] = (rlen >> 8) & 0xff;
  1151. arr[3] = rlen & 0xff;
  1152. /*
  1153. * Return the smallest value of either
  1154. * - The allocated length
  1155. * - The constructed command length
  1156. * - The maximum array size
  1157. */
  1158. rlen = min(alen,n);
  1159. ret = fill_from_dev_buffer(scp, arr,
  1160. min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
  1161. kfree(arr);
  1162. return ret;
  1163. }
  1164. /* <<Following mode page info copied from ST318451LW>> */
  1165. static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
  1166. { /* Read-Write Error Recovery page for mode_sense */
  1167. unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
  1168. 5, 0, 0xff, 0xff};
  1169. memcpy(p, err_recov_pg, sizeof(err_recov_pg));
  1170. if (1 == pcontrol)
  1171. memset(p + 2, 0, sizeof(err_recov_pg) - 2);
  1172. return sizeof(err_recov_pg);
  1173. }
  1174. static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
  1175. { /* Disconnect-Reconnect page for mode_sense */
  1176. unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
  1177. 0, 0, 0, 0, 0, 0, 0, 0};
  1178. memcpy(p, disconnect_pg, sizeof(disconnect_pg));
  1179. if (1 == pcontrol)
  1180. memset(p + 2, 0, sizeof(disconnect_pg) - 2);
  1181. return sizeof(disconnect_pg);
  1182. }
  1183. static int resp_format_pg(unsigned char * p, int pcontrol, int target)
  1184. { /* Format device page for mode_sense */
  1185. unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
  1186. 0, 0, 0, 0, 0, 0, 0, 0,
  1187. 0, 0, 0, 0, 0x40, 0, 0, 0};
  1188. memcpy(p, format_pg, sizeof(format_pg));
  1189. p[10] = (sdebug_sectors_per >> 8) & 0xff;
  1190. p[11] = sdebug_sectors_per & 0xff;
  1191. p[12] = (scsi_debug_sector_size >> 8) & 0xff;
  1192. p[13] = scsi_debug_sector_size & 0xff;
  1193. if (scsi_debug_removable)
  1194. p[20] |= 0x20; /* should agree with INQUIRY */
  1195. if (1 == pcontrol)
  1196. memset(p + 2, 0, sizeof(format_pg) - 2);
  1197. return sizeof(format_pg);
  1198. }
  1199. static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
  1200. { /* Caching page for mode_sense */
  1201. unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
  1202. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1203. unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  1204. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
  1205. if (SCSI_DEBUG_OPT_N_WCE & scsi_debug_opts)
  1206. caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */
  1207. memcpy(p, caching_pg, sizeof(caching_pg));
  1208. if (1 == pcontrol)
  1209. memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
  1210. else if (2 == pcontrol)
  1211. memcpy(p, d_caching_pg, sizeof(d_caching_pg));
  1212. return sizeof(caching_pg);
  1213. }
  1214. static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
  1215. { /* Control mode page for mode_sense */
  1216. unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
  1217. 0, 0, 0, 0};
  1218. unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  1219. 0, 0, 0x2, 0x4b};
  1220. if (scsi_debug_dsense)
  1221. ctrl_m_pg[2] |= 0x4;
  1222. else
  1223. ctrl_m_pg[2] &= ~0x4;
  1224. if (scsi_debug_ato)
  1225. ctrl_m_pg[5] |= 0x80; /* ATO=1 */
  1226. memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
  1227. if (1 == pcontrol)
  1228. memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
  1229. else if (2 == pcontrol)
  1230. memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
  1231. return sizeof(ctrl_m_pg);
  1232. }
  1233. static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
  1234. { /* Informational Exceptions control mode page for mode_sense */
  1235. unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
  1236. 0, 0, 0x0, 0x0};
  1237. unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1238. 0, 0, 0x0, 0x0};
  1239. memcpy(p, iec_m_pg, sizeof(iec_m_pg));
  1240. if (1 == pcontrol)
  1241. memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
  1242. else if (2 == pcontrol)
  1243. memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
  1244. return sizeof(iec_m_pg);
  1245. }
  1246. static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target)
  1247. { /* SAS SSP mode page - short format for mode_sense */
  1248. unsigned char sas_sf_m_pg[] = {0x19, 0x6,
  1249. 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
  1250. memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
  1251. if (1 == pcontrol)
  1252. memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
  1253. return sizeof(sas_sf_m_pg);
  1254. }
  1255. static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target,
  1256. int target_dev_id)
  1257. { /* SAS phy control and discover mode page for mode_sense */
  1258. unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
  1259. 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1260. 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
  1261. 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
  1262. 0x2, 0, 0, 0, 0, 0, 0, 0,
  1263. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1264. 0, 0, 0, 0, 0, 0, 0, 0,
  1265. 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1266. 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
  1267. 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
  1268. 0x3, 0, 0, 0, 0, 0, 0, 0,
  1269. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1270. 0, 0, 0, 0, 0, 0, 0, 0,
  1271. };
  1272. int port_a, port_b;
  1273. port_a = target_dev_id + 1;
  1274. port_b = port_a + 1;
  1275. memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
  1276. p[20] = (port_a >> 24);
  1277. p[21] = (port_a >> 16) & 0xff;
  1278. p[22] = (port_a >> 8) & 0xff;
  1279. p[23] = port_a & 0xff;
  1280. p[48 + 20] = (port_b >> 24);
  1281. p[48 + 21] = (port_b >> 16) & 0xff;
  1282. p[48 + 22] = (port_b >> 8) & 0xff;
  1283. p[48 + 23] = port_b & 0xff;
  1284. if (1 == pcontrol)
  1285. memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
  1286. return sizeof(sas_pcd_m_pg);
  1287. }
  1288. static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
  1289. { /* SAS SSP shared protocol specific port mode subpage */
  1290. unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
  1291. 0, 0, 0, 0, 0, 0, 0, 0,
  1292. };
  1293. memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
  1294. if (1 == pcontrol)
  1295. memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
  1296. return sizeof(sas_sha_m_pg);
  1297. }
  1298. #define SDEBUG_MAX_MSENSE_SZ 256
  1299. static int resp_mode_sense(struct scsi_cmnd * scp, int target,
  1300. struct sdebug_dev_info * devip)
  1301. {
  1302. unsigned char dbd, llbaa;
  1303. int pcontrol, pcode, subpcode, bd_len;
  1304. unsigned char dev_spec;
  1305. int k, alloc_len, msense_6, offset, len, errsts, target_dev_id;
  1306. unsigned char * ap;
  1307. unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
  1308. unsigned char *cmd = scp->cmnd;
  1309. errsts = check_readiness(scp, UAS_ONLY, devip);
  1310. if (errsts)
  1311. return errsts;
  1312. dbd = !!(cmd[1] & 0x8);
  1313. pcontrol = (cmd[2] & 0xc0) >> 6;
  1314. pcode = cmd[2] & 0x3f;
  1315. subpcode = cmd[3];
  1316. msense_6 = (MODE_SENSE == cmd[0]);
  1317. llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10);
  1318. if ((0 == scsi_debug_ptype) && (0 == dbd))
  1319. bd_len = llbaa ? 16 : 8;
  1320. else
  1321. bd_len = 0;
  1322. alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
  1323. memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
  1324. if (0x3 == pcontrol) { /* Saving values not supported */
  1325. mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0);
  1326. return check_condition_result;
  1327. }
  1328. target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
  1329. (devip->target * 1000) - 3;
  1330. /* set DPOFUA bit for disks */
  1331. if (0 == scsi_debug_ptype)
  1332. dev_spec = (DEV_READONLY(target) ? 0x80 : 0x0) | 0x10;
  1333. else
  1334. dev_spec = 0x0;
  1335. if (msense_6) {
  1336. arr[2] = dev_spec;
  1337. arr[3] = bd_len;
  1338. offset = 4;
  1339. } else {
  1340. arr[3] = dev_spec;
  1341. if (16 == bd_len)
  1342. arr[4] = 0x1; /* set LONGLBA bit */
  1343. arr[7] = bd_len; /* assume 255 or less */
  1344. offset = 8;
  1345. }
  1346. ap = arr + offset;
  1347. if ((bd_len > 0) && (!sdebug_capacity))
  1348. sdebug_capacity = get_sdebug_capacity();
  1349. if (8 == bd_len) {
  1350. if (sdebug_capacity > 0xfffffffe) {
  1351. ap[0] = 0xff;
  1352. ap[1] = 0xff;
  1353. ap[2] = 0xff;
  1354. ap[3] = 0xff;
  1355. } else {
  1356. ap[0] = (sdebug_capacity >> 24) & 0xff;
  1357. ap[1] = (sdebug_capacity >> 16) & 0xff;
  1358. ap[2] = (sdebug_capacity >> 8) & 0xff;
  1359. ap[3] = sdebug_capacity & 0xff;
  1360. }
  1361. ap[6] = (scsi_debug_sector_size >> 8) & 0xff;
  1362. ap[7] = scsi_debug_sector_size & 0xff;
  1363. offset += bd_len;
  1364. ap = arr + offset;
  1365. } else if (16 == bd_len) {
  1366. unsigned long long capac = sdebug_capacity;
  1367. for (k = 0; k < 8; ++k, capac >>= 8)
  1368. ap[7 - k] = capac & 0xff;
  1369. ap[12] = (scsi_debug_sector_size >> 24) & 0xff;
  1370. ap[13] = (scsi_debug_sector_size >> 16) & 0xff;
  1371. ap[14] = (scsi_debug_sector_size >> 8) & 0xff;
  1372. ap[15] = scsi_debug_sector_size & 0xff;
  1373. offset += bd_len;
  1374. ap = arr + offset;
  1375. }
  1376. if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
  1377. /* TODO: Control Extension page */
  1378. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1379. 0);
  1380. return check_condition_result;
  1381. }
  1382. switch (pcode) {
  1383. case 0x1: /* Read-Write error recovery page, direct access */
  1384. len = resp_err_recov_pg(ap, pcontrol, target);
  1385. offset += len;
  1386. break;
  1387. case 0x2: /* Disconnect-Reconnect page, all devices */
  1388. len = resp_disconnect_pg(ap, pcontrol, target);
  1389. offset += len;
  1390. break;
  1391. case 0x3: /* Format device page, direct access */
  1392. len = resp_format_pg(ap, pcontrol, target);
  1393. offset += len;
  1394. break;
  1395. case 0x8: /* Caching page, direct access */
  1396. len = resp_caching_pg(ap, pcontrol, target);
  1397. offset += len;
  1398. break;
  1399. case 0xa: /* Control Mode page, all devices */
  1400. len = resp_ctrl_m_pg(ap, pcontrol, target);
  1401. offset += len;
  1402. break;
  1403. case 0x19: /* if spc==1 then sas phy, control+discover */
  1404. if ((subpcode > 0x2) && (subpcode < 0xff)) {
  1405. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1406. INVALID_FIELD_IN_CDB, 0);
  1407. return check_condition_result;
  1408. }
  1409. len = 0;
  1410. if ((0x0 == subpcode) || (0xff == subpcode))
  1411. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  1412. if ((0x1 == subpcode) || (0xff == subpcode))
  1413. len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
  1414. target_dev_id);
  1415. if ((0x2 == subpcode) || (0xff == subpcode))
  1416. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  1417. offset += len;
  1418. break;
  1419. case 0x1c: /* Informational Exceptions Mode page, all devices */
  1420. len = resp_iec_m_pg(ap, pcontrol, target);
  1421. offset += len;
  1422. break;
  1423. case 0x3f: /* Read all Mode pages */
  1424. if ((0 == subpcode) || (0xff == subpcode)) {
  1425. len = resp_err_recov_pg(ap, pcontrol, target);
  1426. len += resp_disconnect_pg(ap + len, pcontrol, target);
  1427. len += resp_format_pg(ap + len, pcontrol, target);
  1428. len += resp_caching_pg(ap + len, pcontrol, target);
  1429. len += resp_ctrl_m_pg(ap + len, pcontrol, target);
  1430. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  1431. if (0xff == subpcode) {
  1432. len += resp_sas_pcd_m_spg(ap + len, pcontrol,
  1433. target, target_dev_id);
  1434. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  1435. }
  1436. len += resp_iec_m_pg(ap + len, pcontrol, target);
  1437. } else {
  1438. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1439. INVALID_FIELD_IN_CDB, 0);
  1440. return check_condition_result;
  1441. }
  1442. offset += len;
  1443. break;
  1444. default:
  1445. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1446. 0);
  1447. return check_condition_result;
  1448. }
  1449. if (msense_6)
  1450. arr[0] = offset - 1;
  1451. else {
  1452. arr[0] = ((offset - 2) >> 8) & 0xff;
  1453. arr[1] = (offset - 2) & 0xff;
  1454. }
  1455. return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
  1456. }
  1457. #define SDEBUG_MAX_MSELECT_SZ 512
  1458. static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
  1459. struct sdebug_dev_info * devip)
  1460. {
  1461. int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
  1462. int param_len, res, errsts, mpage;
  1463. unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
  1464. unsigned char *cmd = scp->cmnd;
  1465. errsts = check_readiness(scp, UAS_ONLY, devip);
  1466. if (errsts)
  1467. return errsts;
  1468. memset(arr, 0, sizeof(arr));
  1469. pf = cmd[1] & 0x10;
  1470. sp = cmd[1] & 0x1;
  1471. param_len = mselect6 ? cmd[4] : ((cmd[7] << 8) + cmd[8]);
  1472. if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
  1473. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1474. INVALID_FIELD_IN_CDB, 0);
  1475. return check_condition_result;
  1476. }
  1477. res = fetch_to_dev_buffer(scp, arr, param_len);
  1478. if (-1 == res)
  1479. return (DID_ERROR << 16);
  1480. else if ((res < param_len) &&
  1481. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  1482. sdev_printk(KERN_INFO, scp->device,
  1483. "%s: cdb indicated=%d, IO sent=%d bytes\n",
  1484. __func__, param_len, res);
  1485. md_len = mselect6 ? (arr[0] + 1) : ((arr[0] << 8) + arr[1] + 2);
  1486. bd_len = mselect6 ? arr[3] : ((arr[6] << 8) + arr[7]);
  1487. if (md_len > 2) {
  1488. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1489. INVALID_FIELD_IN_PARAM_LIST, 0);
  1490. return check_condition_result;
  1491. }
  1492. off = bd_len + (mselect6 ? 4 : 8);
  1493. mpage = arr[off] & 0x3f;
  1494. ps = !!(arr[off] & 0x80);
  1495. if (ps) {
  1496. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1497. INVALID_FIELD_IN_PARAM_LIST, 0);
  1498. return check_condition_result;
  1499. }
  1500. spf = !!(arr[off] & 0x40);
  1501. pg_len = spf ? ((arr[off + 2] << 8) + arr[off + 3] + 4) :
  1502. (arr[off + 1] + 2);
  1503. if ((pg_len + off) > param_len) {
  1504. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1505. PARAMETER_LIST_LENGTH_ERR, 0);
  1506. return check_condition_result;
  1507. }
  1508. switch (mpage) {
  1509. case 0x8: /* Caching Mode page */
  1510. if (caching_pg[1] == arr[off + 1]) {
  1511. memcpy(caching_pg + 2, arr + off + 2,
  1512. sizeof(caching_pg) - 2);
  1513. goto set_mode_changed_ua;
  1514. }
  1515. break;
  1516. case 0xa: /* Control Mode page */
  1517. if (ctrl_m_pg[1] == arr[off + 1]) {
  1518. memcpy(ctrl_m_pg + 2, arr + off + 2,
  1519. sizeof(ctrl_m_pg) - 2);
  1520. scsi_debug_dsense = !!(ctrl_m_pg[2] & 0x4);
  1521. goto set_mode_changed_ua;
  1522. }
  1523. break;
  1524. case 0x1c: /* Informational Exceptions Mode page */
  1525. if (iec_m_pg[1] == arr[off + 1]) {
  1526. memcpy(iec_m_pg + 2, arr + off + 2,
  1527. sizeof(iec_m_pg) - 2);
  1528. goto set_mode_changed_ua;
  1529. }
  1530. break;
  1531. default:
  1532. break;
  1533. }
  1534. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1535. INVALID_FIELD_IN_PARAM_LIST, 0);
  1536. return check_condition_result;
  1537. set_mode_changed_ua:
  1538. set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
  1539. return 0;
  1540. }
  1541. static int resp_temp_l_pg(unsigned char * arr)
  1542. {
  1543. unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
  1544. 0x0, 0x1, 0x3, 0x2, 0x0, 65,
  1545. };
  1546. memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
  1547. return sizeof(temp_l_pg);
  1548. }
  1549. static int resp_ie_l_pg(unsigned char * arr)
  1550. {
  1551. unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
  1552. };
  1553. memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
  1554. if (iec_m_pg[2] & 0x4) { /* TEST bit set */
  1555. arr[4] = THRESHOLD_EXCEEDED;
  1556. arr[5] = 0xff;
  1557. }
  1558. return sizeof(ie_l_pg);
  1559. }
  1560. #define SDEBUG_MAX_LSENSE_SZ 512
  1561. static int resp_log_sense(struct scsi_cmnd * scp,
  1562. struct sdebug_dev_info * devip)
  1563. {
  1564. int ppc, sp, pcontrol, pcode, subpcode, alloc_len, errsts, len, n;
  1565. unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
  1566. unsigned char *cmd = scp->cmnd;
  1567. errsts = check_readiness(scp, UAS_ONLY, devip);
  1568. if (errsts)
  1569. return errsts;
  1570. memset(arr, 0, sizeof(arr));
  1571. ppc = cmd[1] & 0x2;
  1572. sp = cmd[1] & 0x1;
  1573. if (ppc || sp) {
  1574. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1575. INVALID_FIELD_IN_CDB, 0);
  1576. return check_condition_result;
  1577. }
  1578. pcontrol = (cmd[2] & 0xc0) >> 6;
  1579. pcode = cmd[2] & 0x3f;
  1580. subpcode = cmd[3] & 0xff;
  1581. alloc_len = (cmd[7] << 8) + cmd[8];
  1582. arr[0] = pcode;
  1583. if (0 == subpcode) {
  1584. switch (pcode) {
  1585. case 0x0: /* Supported log pages log page */
  1586. n = 4;
  1587. arr[n++] = 0x0; /* this page */
  1588. arr[n++] = 0xd; /* Temperature */
  1589. arr[n++] = 0x2f; /* Informational exceptions */
  1590. arr[3] = n - 4;
  1591. break;
  1592. case 0xd: /* Temperature log page */
  1593. arr[3] = resp_temp_l_pg(arr + 4);
  1594. break;
  1595. case 0x2f: /* Informational exceptions log page */
  1596. arr[3] = resp_ie_l_pg(arr + 4);
  1597. break;
  1598. default:
  1599. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1600. INVALID_FIELD_IN_CDB, 0);
  1601. return check_condition_result;
  1602. }
  1603. } else if (0xff == subpcode) {
  1604. arr[0] |= 0x40;
  1605. arr[1] = subpcode;
  1606. switch (pcode) {
  1607. case 0x0: /* Supported log pages and subpages log page */
  1608. n = 4;
  1609. arr[n++] = 0x0;
  1610. arr[n++] = 0x0; /* 0,0 page */
  1611. arr[n++] = 0x0;
  1612. arr[n++] = 0xff; /* this page */
  1613. arr[n++] = 0xd;
  1614. arr[n++] = 0x0; /* Temperature */
  1615. arr[n++] = 0x2f;
  1616. arr[n++] = 0x0; /* Informational exceptions */
  1617. arr[3] = n - 4;
  1618. break;
  1619. case 0xd: /* Temperature subpages */
  1620. n = 4;
  1621. arr[n++] = 0xd;
  1622. arr[n++] = 0x0; /* Temperature */
  1623. arr[3] = n - 4;
  1624. break;
  1625. case 0x2f: /* Informational exceptions subpages */
  1626. n = 4;
  1627. arr[n++] = 0x2f;
  1628. arr[n++] = 0x0; /* Informational exceptions */
  1629. arr[3] = n - 4;
  1630. break;
  1631. default:
  1632. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1633. INVALID_FIELD_IN_CDB, 0);
  1634. return check_condition_result;
  1635. }
  1636. } else {
  1637. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  1638. INVALID_FIELD_IN_CDB, 0);
  1639. return check_condition_result;
  1640. }
  1641. len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
  1642. return fill_from_dev_buffer(scp, arr,
  1643. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  1644. }
  1645. static int check_device_access_params(struct scsi_cmnd *scp,
  1646. unsigned long long lba, unsigned int num)
  1647. {
  1648. if (lba + num > sdebug_capacity) {
  1649. mk_sense_buffer(scp, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE, 0);
  1650. return check_condition_result;
  1651. }
  1652. /* transfer length excessive (tie in to block limits VPD page) */
  1653. if (num > sdebug_store_sectors) {
  1654. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  1655. return check_condition_result;
  1656. }
  1657. return 0;
  1658. }
  1659. /* Returns number of bytes copied or -1 if error. */
  1660. static int do_device_access(struct scsi_cmnd *scmd,
  1661. unsigned long long lba, unsigned int num, int write)
  1662. {
  1663. int ret;
  1664. unsigned long long block, rest = 0;
  1665. struct scsi_data_buffer *sdb;
  1666. enum dma_data_direction dir;
  1667. size_t (*func)(struct scatterlist *, unsigned int, void *, size_t,
  1668. off_t);
  1669. if (write) {
  1670. sdb = scsi_out(scmd);
  1671. dir = DMA_TO_DEVICE;
  1672. func = sg_pcopy_to_buffer;
  1673. } else {
  1674. sdb = scsi_in(scmd);
  1675. dir = DMA_FROM_DEVICE;
  1676. func = sg_pcopy_from_buffer;
  1677. }
  1678. if (!sdb->length)
  1679. return 0;
  1680. if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir))
  1681. return -1;
  1682. block = do_div(lba, sdebug_store_sectors);
  1683. if (block + num > sdebug_store_sectors)
  1684. rest = block + num - sdebug_store_sectors;
  1685. ret = func(sdb->table.sgl, sdb->table.nents,
  1686. fake_storep + (block * scsi_debug_sector_size),
  1687. (num - rest) * scsi_debug_sector_size, 0);
  1688. if (ret != (num - rest) * scsi_debug_sector_size)
  1689. return ret;
  1690. if (rest) {
  1691. ret += func(sdb->table.sgl, sdb->table.nents,
  1692. fake_storep, rest * scsi_debug_sector_size,
  1693. (num - rest) * scsi_debug_sector_size);
  1694. }
  1695. return ret;
  1696. }
  1697. static __be16 dif_compute_csum(const void *buf, int len)
  1698. {
  1699. __be16 csum;
  1700. if (scsi_debug_guard)
  1701. csum = (__force __be16)ip_compute_csum(buf, len);
  1702. else
  1703. csum = cpu_to_be16(crc_t10dif(buf, len));
  1704. return csum;
  1705. }
  1706. static int dif_verify(struct sd_dif_tuple *sdt, const void *data,
  1707. sector_t sector, u32 ei_lba)
  1708. {
  1709. __be16 csum = dif_compute_csum(data, scsi_debug_sector_size);
  1710. if (sdt->guard_tag != csum) {
  1711. pr_err("%s: GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
  1712. __func__,
  1713. (unsigned long)sector,
  1714. be16_to_cpu(sdt->guard_tag),
  1715. be16_to_cpu(csum));
  1716. return 0x01;
  1717. }
  1718. if (scsi_debug_dif == SD_DIF_TYPE1_PROTECTION &&
  1719. be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
  1720. pr_err("%s: REF check failed on sector %lu\n",
  1721. __func__, (unsigned long)sector);
  1722. return 0x03;
  1723. }
  1724. if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
  1725. be32_to_cpu(sdt->ref_tag) != ei_lba) {
  1726. pr_err("%s: REF check failed on sector %lu\n",
  1727. __func__, (unsigned long)sector);
  1728. return 0x03;
  1729. }
  1730. return 0;
  1731. }
  1732. static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector,
  1733. unsigned int sectors, bool read)
  1734. {
  1735. size_t resid;
  1736. void *paddr;
  1737. const void *dif_store_end = dif_storep + sdebug_store_sectors;
  1738. struct sg_mapping_iter miter;
  1739. /* Bytes of protection data to copy into sgl */
  1740. resid = sectors * sizeof(*dif_storep);
  1741. sg_miter_start(&miter, scsi_prot_sglist(SCpnt),
  1742. scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC |
  1743. (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
  1744. while (sg_miter_next(&miter) && resid > 0) {
  1745. size_t len = min(miter.length, resid);
  1746. void *start = dif_store(sector);
  1747. size_t rest = 0;
  1748. if (dif_store_end < start + len)
  1749. rest = start + len - dif_store_end;
  1750. paddr = miter.addr;
  1751. if (read)
  1752. memcpy(paddr, start, len - rest);
  1753. else
  1754. memcpy(start, paddr, len - rest);
  1755. if (rest) {
  1756. if (read)
  1757. memcpy(paddr + len - rest, dif_storep, rest);
  1758. else
  1759. memcpy(dif_storep, paddr + len - rest, rest);
  1760. }
  1761. sector += len / sizeof(*dif_storep);
  1762. resid -= len;
  1763. }
  1764. sg_miter_stop(&miter);
  1765. }
  1766. static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
  1767. unsigned int sectors, u32 ei_lba)
  1768. {
  1769. unsigned int i;
  1770. struct sd_dif_tuple *sdt;
  1771. sector_t sector;
  1772. for (i = 0; i < sectors; i++, ei_lba++) {
  1773. int ret;
  1774. sector = start_sec + i;
  1775. sdt = dif_store(sector);
  1776. if (sdt->app_tag == cpu_to_be16(0xffff))
  1777. continue;
  1778. ret = dif_verify(sdt, fake_store(sector), sector, ei_lba);
  1779. if (ret) {
  1780. dif_errors++;
  1781. return ret;
  1782. }
  1783. }
  1784. dif_copy_prot(SCpnt, start_sec, sectors, true);
  1785. dix_reads++;
  1786. return 0;
  1787. }
  1788. static int resp_read(struct scsi_cmnd *SCpnt, unsigned long long lba,
  1789. unsigned int num, u32 ei_lba)
  1790. {
  1791. unsigned long iflags;
  1792. int ret;
  1793. ret = check_device_access_params(SCpnt, lba, num);
  1794. if (ret)
  1795. return ret;
  1796. if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
  1797. (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) &&
  1798. ((lba + num) > OPT_MEDIUM_ERR_ADDR)) {
  1799. /* claim unrecoverable read error */
  1800. mk_sense_buffer(SCpnt, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
  1801. /* set info field and valid bit for fixed descriptor */
  1802. if (0x70 == (SCpnt->sense_buffer[0] & 0x7f)) {
  1803. SCpnt->sense_buffer[0] |= 0x80; /* Valid bit */
  1804. ret = (lba < OPT_MEDIUM_ERR_ADDR)
  1805. ? OPT_MEDIUM_ERR_ADDR : (int)lba;
  1806. SCpnt->sense_buffer[3] = (ret >> 24) & 0xff;
  1807. SCpnt->sense_buffer[4] = (ret >> 16) & 0xff;
  1808. SCpnt->sense_buffer[5] = (ret >> 8) & 0xff;
  1809. SCpnt->sense_buffer[6] = ret & 0xff;
  1810. }
  1811. scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
  1812. return check_condition_result;
  1813. }
  1814. read_lock_irqsave(&atomic_rw, iflags);
  1815. /* DIX + T10 DIF */
  1816. if (scsi_debug_dix && scsi_prot_sg_count(SCpnt)) {
  1817. int prot_ret = prot_verify_read(SCpnt, lba, num, ei_lba);
  1818. if (prot_ret) {
  1819. read_unlock_irqrestore(&atomic_rw, iflags);
  1820. mk_sense_buffer(SCpnt, ABORTED_COMMAND, 0x10, prot_ret);
  1821. return illegal_condition_result;
  1822. }
  1823. }
  1824. ret = do_device_access(SCpnt, lba, num, 0);
  1825. read_unlock_irqrestore(&atomic_rw, iflags);
  1826. if (ret == -1)
  1827. return DID_ERROR << 16;
  1828. scsi_in(SCpnt)->resid = scsi_bufflen(SCpnt) - ret;
  1829. return 0;
  1830. }
  1831. void dump_sector(unsigned char *buf, int len)
  1832. {
  1833. int i, j, n;
  1834. pr_err(">>> Sector Dump <<<\n");
  1835. for (i = 0 ; i < len ; i += 16) {
  1836. char b[128];
  1837. for (j = 0, n = 0; j < 16; j++) {
  1838. unsigned char c = buf[i+j];
  1839. if (c >= 0x20 && c < 0x7e)
  1840. n += scnprintf(b + n, sizeof(b) - n,
  1841. " %c ", buf[i+j]);
  1842. else
  1843. n += scnprintf(b + n, sizeof(b) - n,
  1844. "%02x ", buf[i+j]);
  1845. }
  1846. pr_err("%04d: %s\n", i, b);
  1847. }
  1848. }
  1849. static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
  1850. unsigned int sectors, u32 ei_lba)
  1851. {
  1852. int ret;
  1853. struct sd_dif_tuple *sdt;
  1854. void *daddr;
  1855. sector_t sector = start_sec;
  1856. int ppage_offset;
  1857. int dpage_offset;
  1858. struct sg_mapping_iter diter;
  1859. struct sg_mapping_iter piter;
  1860. BUG_ON(scsi_sg_count(SCpnt) == 0);
  1861. BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
  1862. sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
  1863. scsi_prot_sg_count(SCpnt),
  1864. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  1865. sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
  1866. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  1867. /* For each protection page */
  1868. while (sg_miter_next(&piter)) {
  1869. dpage_offset = 0;
  1870. if (WARN_ON(!sg_miter_next(&diter))) {
  1871. ret = 0x01;
  1872. goto out;
  1873. }
  1874. for (ppage_offset = 0; ppage_offset < piter.length;
  1875. ppage_offset += sizeof(struct sd_dif_tuple)) {
  1876. /* If we're at the end of the current
  1877. * data page advance to the next one
  1878. */
  1879. if (dpage_offset >= diter.length) {
  1880. if (WARN_ON(!sg_miter_next(&diter))) {
  1881. ret = 0x01;
  1882. goto out;
  1883. }
  1884. dpage_offset = 0;
  1885. }
  1886. sdt = piter.addr + ppage_offset;
  1887. daddr = diter.addr + dpage_offset;
  1888. ret = dif_verify(sdt, daddr, sector, ei_lba);
  1889. if (ret) {
  1890. dump_sector(daddr, scsi_debug_sector_size);
  1891. goto out;
  1892. }
  1893. sector++;
  1894. ei_lba++;
  1895. dpage_offset += scsi_debug_sector_size;
  1896. }
  1897. diter.consumed = dpage_offset;
  1898. sg_miter_stop(&diter);
  1899. }
  1900. sg_miter_stop(&piter);
  1901. dif_copy_prot(SCpnt, start_sec, sectors, false);
  1902. dix_writes++;
  1903. return 0;
  1904. out:
  1905. dif_errors++;
  1906. sg_miter_stop(&diter);
  1907. sg_miter_stop(&piter);
  1908. return ret;
  1909. }
  1910. static unsigned long lba_to_map_index(sector_t lba)
  1911. {
  1912. if (scsi_debug_unmap_alignment) {
  1913. lba += scsi_debug_unmap_granularity -
  1914. scsi_debug_unmap_alignment;
  1915. }
  1916. do_div(lba, scsi_debug_unmap_granularity);
  1917. return lba;
  1918. }
  1919. static sector_t map_index_to_lba(unsigned long index)
  1920. {
  1921. sector_t lba = index * scsi_debug_unmap_granularity;
  1922. if (scsi_debug_unmap_alignment) {
  1923. lba -= scsi_debug_unmap_granularity -
  1924. scsi_debug_unmap_alignment;
  1925. }
  1926. return lba;
  1927. }
  1928. static unsigned int map_state(sector_t lba, unsigned int *num)
  1929. {
  1930. sector_t end;
  1931. unsigned int mapped;
  1932. unsigned long index;
  1933. unsigned long next;
  1934. index = lba_to_map_index(lba);
  1935. mapped = test_bit(index, map_storep);
  1936. if (mapped)
  1937. next = find_next_zero_bit(map_storep, map_size, index);
  1938. else
  1939. next = find_next_bit(map_storep, map_size, index);
  1940. end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next));
  1941. *num = end - lba;
  1942. return mapped;
  1943. }
  1944. static void map_region(sector_t lba, unsigned int len)
  1945. {
  1946. sector_t end = lba + len;
  1947. while (lba < end) {
  1948. unsigned long index = lba_to_map_index(lba);
  1949. if (index < map_size)
  1950. set_bit(index, map_storep);
  1951. lba = map_index_to_lba(index + 1);
  1952. }
  1953. }
  1954. static void unmap_region(sector_t lba, unsigned int len)
  1955. {
  1956. sector_t end = lba + len;
  1957. while (lba < end) {
  1958. unsigned long index = lba_to_map_index(lba);
  1959. if (lba == map_index_to_lba(index) &&
  1960. lba + scsi_debug_unmap_granularity <= end &&
  1961. index < map_size) {
  1962. clear_bit(index, map_storep);
  1963. if (scsi_debug_lbprz) {
  1964. memset(fake_storep +
  1965. lba * scsi_debug_sector_size, 0,
  1966. scsi_debug_sector_size *
  1967. scsi_debug_unmap_granularity);
  1968. }
  1969. if (dif_storep) {
  1970. memset(dif_storep + lba, 0xff,
  1971. sizeof(*dif_storep) *
  1972. scsi_debug_unmap_granularity);
  1973. }
  1974. }
  1975. lba = map_index_to_lba(index + 1);
  1976. }
  1977. }
  1978. static int resp_write(struct scsi_cmnd *SCpnt, unsigned long long lba,
  1979. unsigned int num, u32 ei_lba)
  1980. {
  1981. unsigned long iflags;
  1982. int ret;
  1983. ret = check_device_access_params(SCpnt, lba, num);
  1984. if (ret)
  1985. return ret;
  1986. write_lock_irqsave(&atomic_rw, iflags);
  1987. /* DIX + T10 DIF */
  1988. if (scsi_debug_dix && scsi_prot_sg_count(SCpnt)) {
  1989. int prot_ret = prot_verify_write(SCpnt, lba, num, ei_lba);
  1990. if (prot_ret) {
  1991. write_unlock_irqrestore(&atomic_rw, iflags);
  1992. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST, 0x10,
  1993. prot_ret);
  1994. return illegal_condition_result;
  1995. }
  1996. }
  1997. ret = do_device_access(SCpnt, lba, num, 1);
  1998. if (scsi_debug_lbp())
  1999. map_region(lba, num);
  2000. write_unlock_irqrestore(&atomic_rw, iflags);
  2001. if (-1 == ret)
  2002. return (DID_ERROR << 16);
  2003. else if ((ret < (num * scsi_debug_sector_size)) &&
  2004. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  2005. sdev_printk(KERN_INFO, SCpnt->device,
  2006. "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
  2007. my_name, num * scsi_debug_sector_size, ret);
  2008. return 0;
  2009. }
  2010. static int resp_write_same(struct scsi_cmnd *scmd, unsigned long long lba,
  2011. unsigned int num, u32 ei_lba, unsigned int unmap)
  2012. {
  2013. unsigned long iflags;
  2014. unsigned long long i;
  2015. int ret;
  2016. ret = check_device_access_params(scmd, lba, num);
  2017. if (ret)
  2018. return ret;
  2019. if (num > scsi_debug_write_same_length) {
  2020. mk_sense_buffer(scmd, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  2021. 0);
  2022. return check_condition_result;
  2023. }
  2024. write_lock_irqsave(&atomic_rw, iflags);
  2025. if (unmap && scsi_debug_lbp()) {
  2026. unmap_region(lba, num);
  2027. goto out;
  2028. }
  2029. /* Else fetch one logical block */
  2030. ret = fetch_to_dev_buffer(scmd,
  2031. fake_storep + (lba * scsi_debug_sector_size),
  2032. scsi_debug_sector_size);
  2033. if (-1 == ret) {
  2034. write_unlock_irqrestore(&atomic_rw, iflags);
  2035. return (DID_ERROR << 16);
  2036. } else if ((ret < (num * scsi_debug_sector_size)) &&
  2037. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  2038. sdev_printk(KERN_INFO, scmd->device,
  2039. "%s: %s: cdb indicated=%u, IO sent=%d bytes\n",
  2040. my_name, "write same",
  2041. num * scsi_debug_sector_size, ret);
  2042. /* Copy first sector to remaining blocks */
  2043. for (i = 1 ; i < num ; i++)
  2044. memcpy(fake_storep + ((lba + i) * scsi_debug_sector_size),
  2045. fake_storep + (lba * scsi_debug_sector_size),
  2046. scsi_debug_sector_size);
  2047. if (scsi_debug_lbp())
  2048. map_region(lba, num);
  2049. out:
  2050. write_unlock_irqrestore(&atomic_rw, iflags);
  2051. return 0;
  2052. }
  2053. struct unmap_block_desc {
  2054. __be64 lba;
  2055. __be32 blocks;
  2056. __be32 __reserved;
  2057. };
  2058. static int resp_unmap(struct scsi_cmnd * scmd, struct sdebug_dev_info * devip)
  2059. {
  2060. unsigned char *buf;
  2061. struct unmap_block_desc *desc;
  2062. unsigned int i, payload_len, descriptors;
  2063. int ret;
  2064. unsigned long iflags;
  2065. ret = check_readiness(scmd, UAS_ONLY, devip);
  2066. if (ret)
  2067. return ret;
  2068. payload_len = get_unaligned_be16(&scmd->cmnd[7]);
  2069. BUG_ON(scsi_bufflen(scmd) != payload_len);
  2070. descriptors = (payload_len - 8) / 16;
  2071. buf = kmalloc(scsi_bufflen(scmd), GFP_ATOMIC);
  2072. if (!buf)
  2073. return check_condition_result;
  2074. scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
  2075. BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
  2076. BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
  2077. desc = (void *)&buf[8];
  2078. write_lock_irqsave(&atomic_rw, iflags);
  2079. for (i = 0 ; i < descriptors ; i++) {
  2080. unsigned long long lba = get_unaligned_be64(&desc[i].lba);
  2081. unsigned int num = get_unaligned_be32(&desc[i].blocks);
  2082. ret = check_device_access_params(scmd, lba, num);
  2083. if (ret)
  2084. goto out;
  2085. unmap_region(lba, num);
  2086. }
  2087. ret = 0;
  2088. out:
  2089. write_unlock_irqrestore(&atomic_rw, iflags);
  2090. kfree(buf);
  2091. return ret;
  2092. }
  2093. #define SDEBUG_GET_LBA_STATUS_LEN 32
  2094. static int resp_get_lba_status(struct scsi_cmnd * scmd,
  2095. struct sdebug_dev_info * devip)
  2096. {
  2097. unsigned long long lba;
  2098. unsigned int alloc_len, mapped, num;
  2099. unsigned char arr[SDEBUG_GET_LBA_STATUS_LEN];
  2100. int ret;
  2101. ret = check_readiness(scmd, UAS_ONLY, devip);
  2102. if (ret)
  2103. return ret;
  2104. lba = get_unaligned_be64(&scmd->cmnd[2]);
  2105. alloc_len = get_unaligned_be32(&scmd->cmnd[10]);
  2106. if (alloc_len < 24)
  2107. return 0;
  2108. ret = check_device_access_params(scmd, lba, 1);
  2109. if (ret)
  2110. return ret;
  2111. mapped = map_state(lba, &num);
  2112. memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
  2113. put_unaligned_be32(20, &arr[0]); /* Parameter Data Length */
  2114. put_unaligned_be64(lba, &arr[8]); /* LBA */
  2115. put_unaligned_be32(num, &arr[16]); /* Number of blocks */
  2116. arr[20] = !mapped; /* mapped = 0, unmapped = 1 */
  2117. return fill_from_dev_buffer(scmd, arr, SDEBUG_GET_LBA_STATUS_LEN);
  2118. }
  2119. #define SDEBUG_RLUN_ARR_SZ 256
  2120. static int resp_report_luns(struct scsi_cmnd * scp,
  2121. struct sdebug_dev_info * devip)
  2122. {
  2123. unsigned int alloc_len;
  2124. int lun_cnt, i, upper, num, n;
  2125. u64 wlun, lun;
  2126. unsigned char *cmd = scp->cmnd;
  2127. int select_report = (int)cmd[2];
  2128. struct scsi_lun *one_lun;
  2129. unsigned char arr[SDEBUG_RLUN_ARR_SZ];
  2130. unsigned char * max_addr;
  2131. alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
  2132. if ((alloc_len < 4) || (select_report > 2)) {
  2133. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  2134. 0);
  2135. return check_condition_result;
  2136. }
  2137. /* can produce response with up to 16k luns (lun 0 to lun 16383) */
  2138. memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
  2139. lun_cnt = scsi_debug_max_luns;
  2140. if (1 == select_report)
  2141. lun_cnt = 0;
  2142. else if (scsi_debug_no_lun_0 && (lun_cnt > 0))
  2143. --lun_cnt;
  2144. wlun = (select_report > 0) ? 1 : 0;
  2145. num = lun_cnt + wlun;
  2146. arr[2] = ((sizeof(struct scsi_lun) * num) >> 8) & 0xff;
  2147. arr[3] = (sizeof(struct scsi_lun) * num) & 0xff;
  2148. n = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
  2149. sizeof(struct scsi_lun)), num);
  2150. if (n < num) {
  2151. wlun = 0;
  2152. lun_cnt = n;
  2153. }
  2154. one_lun = (struct scsi_lun *) &arr[8];
  2155. max_addr = arr + SDEBUG_RLUN_ARR_SZ;
  2156. for (i = 0, lun = (scsi_debug_no_lun_0 ? 1 : 0);
  2157. ((i < lun_cnt) && ((unsigned char *)(one_lun + i) < max_addr));
  2158. i++, lun++) {
  2159. upper = (lun >> 8) & 0x3f;
  2160. if (upper)
  2161. one_lun[i].scsi_lun[0] =
  2162. (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
  2163. one_lun[i].scsi_lun[1] = lun & 0xff;
  2164. }
  2165. if (wlun) {
  2166. one_lun[i].scsi_lun[0] = (SAM2_WLUN_REPORT_LUNS >> 8) & 0xff;
  2167. one_lun[i].scsi_lun[1] = SAM2_WLUN_REPORT_LUNS & 0xff;
  2168. i++;
  2169. }
  2170. alloc_len = (unsigned char *)(one_lun + i) - arr;
  2171. return fill_from_dev_buffer(scp, arr,
  2172. min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
  2173. }
  2174. static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
  2175. unsigned int num, struct sdebug_dev_info *devip)
  2176. {
  2177. int j;
  2178. unsigned char *kaddr, *buf;
  2179. unsigned int offset;
  2180. struct scsi_data_buffer *sdb = scsi_in(scp);
  2181. struct sg_mapping_iter miter;
  2182. /* better not to use temporary buffer. */
  2183. buf = kmalloc(scsi_bufflen(scp), GFP_ATOMIC);
  2184. if (!buf) {
  2185. mk_sense_buffer(scp, NOT_READY,
  2186. LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  2187. return check_condition_result;
  2188. }
  2189. scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
  2190. offset = 0;
  2191. sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents,
  2192. SG_MITER_ATOMIC | SG_MITER_TO_SG);
  2193. while (sg_miter_next(&miter)) {
  2194. kaddr = miter.addr;
  2195. for (j = 0; j < miter.length; j++)
  2196. *(kaddr + j) ^= *(buf + offset + j);
  2197. offset += miter.length;
  2198. }
  2199. sg_miter_stop(&miter);
  2200. kfree(buf);
  2201. return 0;
  2202. }
  2203. /* When timer or tasklet goes off this function is called. */
  2204. static void sdebug_q_cmd_complete(unsigned long indx)
  2205. {
  2206. int qa_indx;
  2207. int retiring = 0;
  2208. unsigned long iflags;
  2209. struct sdebug_queued_cmd *sqcp;
  2210. struct scsi_cmnd *scp;
  2211. struct sdebug_dev_info *devip;
  2212. atomic_inc(&sdebug_completions);
  2213. qa_indx = indx;
  2214. if ((qa_indx < 0) || (qa_indx >= SCSI_DEBUG_CANQUEUE)) {
  2215. pr_err("%s: wild qa_indx=%d\n", __func__, qa_indx);
  2216. return;
  2217. }
  2218. spin_lock_irqsave(&queued_arr_lock, iflags);
  2219. sqcp = &queued_arr[qa_indx];
  2220. scp = sqcp->a_cmnd;
  2221. if (NULL == scp) {
  2222. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2223. pr_err("%s: scp is NULL\n", __func__);
  2224. return;
  2225. }
  2226. devip = (struct sdebug_dev_info *)scp->device->hostdata;
  2227. if (devip)
  2228. atomic_dec(&devip->num_in_q);
  2229. else
  2230. pr_err("%s: devip=NULL\n", __func__);
  2231. if (atomic_read(&retired_max_queue) > 0)
  2232. retiring = 1;
  2233. sqcp->a_cmnd = NULL;
  2234. if (!test_and_clear_bit(qa_indx, queued_in_use_bm)) {
  2235. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2236. pr_err("%s: Unexpected completion\n", __func__);
  2237. return;
  2238. }
  2239. if (unlikely(retiring)) { /* user has reduced max_queue */
  2240. int k, retval;
  2241. retval = atomic_read(&retired_max_queue);
  2242. if (qa_indx >= retval) {
  2243. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2244. pr_err("%s: index %d too large\n", __func__, retval);
  2245. return;
  2246. }
  2247. k = find_last_bit(queued_in_use_bm, retval);
  2248. if ((k < scsi_debug_max_queue) || (k == retval))
  2249. atomic_set(&retired_max_queue, 0);
  2250. else
  2251. atomic_set(&retired_max_queue, k + 1);
  2252. }
  2253. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2254. scp->scsi_done(scp); /* callback to mid level */
  2255. }
  2256. /* When high resolution timer goes off this function is called. */
  2257. static enum hrtimer_restart
  2258. sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
  2259. {
  2260. int qa_indx;
  2261. int retiring = 0;
  2262. unsigned long iflags;
  2263. struct sdebug_hrtimer *sd_hrtp = (struct sdebug_hrtimer *)timer;
  2264. struct sdebug_queued_cmd *sqcp;
  2265. struct scsi_cmnd *scp;
  2266. struct sdebug_dev_info *devip;
  2267. atomic_inc(&sdebug_completions);
  2268. qa_indx = sd_hrtp->qa_indx;
  2269. if ((qa_indx < 0) || (qa_indx >= SCSI_DEBUG_CANQUEUE)) {
  2270. pr_err("%s: wild qa_indx=%d\n", __func__, qa_indx);
  2271. goto the_end;
  2272. }
  2273. spin_lock_irqsave(&queued_arr_lock, iflags);
  2274. sqcp = &queued_arr[qa_indx];
  2275. scp = sqcp->a_cmnd;
  2276. if (NULL == scp) {
  2277. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2278. pr_err("%s: scp is NULL\n", __func__);
  2279. goto the_end;
  2280. }
  2281. devip = (struct sdebug_dev_info *)scp->device->hostdata;
  2282. if (devip)
  2283. atomic_dec(&devip->num_in_q);
  2284. else
  2285. pr_err("%s: devip=NULL\n", __func__);
  2286. if (atomic_read(&retired_max_queue) > 0)
  2287. retiring = 1;
  2288. sqcp->a_cmnd = NULL;
  2289. if (!test_and_clear_bit(qa_indx, queued_in_use_bm)) {
  2290. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2291. pr_err("%s: Unexpected completion\n", __func__);
  2292. goto the_end;
  2293. }
  2294. if (unlikely(retiring)) { /* user has reduced max_queue */
  2295. int k, retval;
  2296. retval = atomic_read(&retired_max_queue);
  2297. if (qa_indx >= retval) {
  2298. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2299. pr_err("%s: index %d too large\n", __func__, retval);
  2300. goto the_end;
  2301. }
  2302. k = find_last_bit(queued_in_use_bm, retval);
  2303. if ((k < scsi_debug_max_queue) || (k == retval))
  2304. atomic_set(&retired_max_queue, 0);
  2305. else
  2306. atomic_set(&retired_max_queue, k + 1);
  2307. }
  2308. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2309. scp->scsi_done(scp); /* callback to mid level */
  2310. the_end:
  2311. return HRTIMER_NORESTART;
  2312. }
  2313. static struct sdebug_dev_info *
  2314. sdebug_device_create(struct sdebug_host_info *sdbg_host, gfp_t flags)
  2315. {
  2316. struct sdebug_dev_info *devip;
  2317. devip = kzalloc(sizeof(*devip), flags);
  2318. if (devip) {
  2319. devip->sdbg_host = sdbg_host;
  2320. list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
  2321. }
  2322. return devip;
  2323. }
  2324. static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
  2325. {
  2326. struct sdebug_host_info * sdbg_host;
  2327. struct sdebug_dev_info * open_devip = NULL;
  2328. struct sdebug_dev_info * devip =
  2329. (struct sdebug_dev_info *)sdev->hostdata;
  2330. if (devip)
  2331. return devip;
  2332. sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
  2333. if (!sdbg_host) {
  2334. pr_err("%s: Host info NULL\n", __func__);
  2335. return NULL;
  2336. }
  2337. list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
  2338. if ((devip->used) && (devip->channel == sdev->channel) &&
  2339. (devip->target == sdev->id) &&
  2340. (devip->lun == sdev->lun))
  2341. return devip;
  2342. else {
  2343. if ((!devip->used) && (!open_devip))
  2344. open_devip = devip;
  2345. }
  2346. }
  2347. if (!open_devip) { /* try and make a new one */
  2348. open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
  2349. if (!open_devip) {
  2350. printk(KERN_ERR "%s: out of memory at line %d\n",
  2351. __func__, __LINE__);
  2352. return NULL;
  2353. }
  2354. }
  2355. open_devip->channel = sdev->channel;
  2356. open_devip->target = sdev->id;
  2357. open_devip->lun = sdev->lun;
  2358. open_devip->sdbg_host = sdbg_host;
  2359. atomic_set(&open_devip->num_in_q, 0);
  2360. set_bit(SDEBUG_UA_POR, open_devip->uas_bm);
  2361. open_devip->used = 1;
  2362. if (sdev->lun == SAM2_WLUN_REPORT_LUNS)
  2363. open_devip->wlun = SAM2_WLUN_REPORT_LUNS & 0xff;
  2364. return open_devip;
  2365. }
  2366. static int scsi_debug_slave_alloc(struct scsi_device *sdp)
  2367. {
  2368. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2369. printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %llu>\n",
  2370. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  2371. queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue);
  2372. return 0;
  2373. }
  2374. static int scsi_debug_slave_configure(struct scsi_device *sdp)
  2375. {
  2376. struct sdebug_dev_info *devip;
  2377. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2378. printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %llu>\n",
  2379. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  2380. if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
  2381. sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
  2382. devip = devInfoReg(sdp);
  2383. if (NULL == devip)
  2384. return 1; /* no resources, will be marked offline */
  2385. sdp->tagged_supported = 1;
  2386. sdp->hostdata = devip;
  2387. blk_queue_max_segment_size(sdp->request_queue, -1U);
  2388. if (scsi_debug_no_uld)
  2389. sdp->no_uld_attach = 1;
  2390. return 0;
  2391. }
  2392. static void scsi_debug_slave_destroy(struct scsi_device *sdp)
  2393. {
  2394. struct sdebug_dev_info *devip =
  2395. (struct sdebug_dev_info *)sdp->hostdata;
  2396. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2397. printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %llu>\n",
  2398. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  2399. if (devip) {
  2400. /* make this slot available for re-use */
  2401. devip->used = 0;
  2402. sdp->hostdata = NULL;
  2403. }
  2404. }
  2405. /* Returns 1 if cmnd found (deletes its timer or tasklet), else returns 0 */
  2406. static int stop_queued_cmnd(struct scsi_cmnd *cmnd)
  2407. {
  2408. unsigned long iflags;
  2409. int k, qmax, r_qmax;
  2410. struct sdebug_queued_cmd *sqcp;
  2411. struct sdebug_dev_info *devip;
  2412. spin_lock_irqsave(&queued_arr_lock, iflags);
  2413. qmax = scsi_debug_max_queue;
  2414. r_qmax = atomic_read(&retired_max_queue);
  2415. if (r_qmax > qmax)
  2416. qmax = r_qmax;
  2417. for (k = 0; k < qmax; ++k) {
  2418. if (test_bit(k, queued_in_use_bm)) {
  2419. sqcp = &queued_arr[k];
  2420. if (cmnd == sqcp->a_cmnd) {
  2421. devip = (struct sdebug_dev_info *)
  2422. cmnd->device->hostdata;
  2423. if (devip)
  2424. atomic_dec(&devip->num_in_q);
  2425. sqcp->a_cmnd = NULL;
  2426. spin_unlock_irqrestore(&queued_arr_lock,
  2427. iflags);
  2428. if (scsi_debug_ndelay > 0) {
  2429. if (sqcp->sd_hrtp)
  2430. hrtimer_cancel(
  2431. &sqcp->sd_hrtp->hrt);
  2432. } else if (scsi_debug_delay > 0) {
  2433. if (sqcp->cmnd_timerp)
  2434. del_timer_sync(
  2435. sqcp->cmnd_timerp);
  2436. } else if (scsi_debug_delay < 0) {
  2437. if (sqcp->tletp)
  2438. tasklet_kill(sqcp->tletp);
  2439. }
  2440. clear_bit(k, queued_in_use_bm);
  2441. return 1;
  2442. }
  2443. }
  2444. }
  2445. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2446. return 0;
  2447. }
  2448. /* Deletes (stops) timers or tasklets of all queued commands */
  2449. static void stop_all_queued(void)
  2450. {
  2451. unsigned long iflags;
  2452. int k;
  2453. struct sdebug_queued_cmd *sqcp;
  2454. struct sdebug_dev_info *devip;
  2455. spin_lock_irqsave(&queued_arr_lock, iflags);
  2456. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2457. if (test_bit(k, queued_in_use_bm)) {
  2458. sqcp = &queued_arr[k];
  2459. if (sqcp->a_cmnd) {
  2460. devip = (struct sdebug_dev_info *)
  2461. sqcp->a_cmnd->device->hostdata;
  2462. if (devip)
  2463. atomic_dec(&devip->num_in_q);
  2464. sqcp->a_cmnd = NULL;
  2465. spin_unlock_irqrestore(&queued_arr_lock,
  2466. iflags);
  2467. if (scsi_debug_ndelay > 0) {
  2468. if (sqcp->sd_hrtp)
  2469. hrtimer_cancel(
  2470. &sqcp->sd_hrtp->hrt);
  2471. } else if (scsi_debug_delay > 0) {
  2472. if (sqcp->cmnd_timerp)
  2473. del_timer_sync(
  2474. sqcp->cmnd_timerp);
  2475. } else if (scsi_debug_delay < 0) {
  2476. if (sqcp->tletp)
  2477. tasklet_kill(sqcp->tletp);
  2478. }
  2479. clear_bit(k, queued_in_use_bm);
  2480. spin_lock_irqsave(&queued_arr_lock, iflags);
  2481. }
  2482. }
  2483. }
  2484. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2485. }
  2486. /* Free queued command memory on heap */
  2487. static void free_all_queued(void)
  2488. {
  2489. unsigned long iflags;
  2490. int k;
  2491. struct sdebug_queued_cmd *sqcp;
  2492. spin_lock_irqsave(&queued_arr_lock, iflags);
  2493. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2494. sqcp = &queued_arr[k];
  2495. kfree(sqcp->cmnd_timerp);
  2496. sqcp->cmnd_timerp = NULL;
  2497. kfree(sqcp->tletp);
  2498. sqcp->tletp = NULL;
  2499. kfree(sqcp->sd_hrtp);
  2500. sqcp->sd_hrtp = NULL;
  2501. }
  2502. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2503. }
  2504. static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
  2505. {
  2506. ++num_aborts;
  2507. if (SCpnt) {
  2508. if (SCpnt->device &&
  2509. (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts))
  2510. sdev_printk(KERN_INFO, SCpnt->device, "%s\n",
  2511. __func__);
  2512. stop_queued_cmnd(SCpnt);
  2513. }
  2514. return SUCCESS;
  2515. }
  2516. static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
  2517. {
  2518. struct sdebug_dev_info * devip;
  2519. ++num_dev_resets;
  2520. if (SCpnt && SCpnt->device) {
  2521. struct scsi_device *sdp = SCpnt->device;
  2522. if (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts)
  2523. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  2524. devip = devInfoReg(sdp);
  2525. if (devip)
  2526. set_bit(SDEBUG_UA_POR, devip->uas_bm);
  2527. }
  2528. return SUCCESS;
  2529. }
  2530. static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
  2531. {
  2532. struct sdebug_host_info *sdbg_host;
  2533. struct sdebug_dev_info *devip;
  2534. struct scsi_device *sdp;
  2535. struct Scsi_Host *hp;
  2536. int k = 0;
  2537. ++num_target_resets;
  2538. if (!SCpnt)
  2539. goto lie;
  2540. sdp = SCpnt->device;
  2541. if (!sdp)
  2542. goto lie;
  2543. if (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts)
  2544. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  2545. hp = sdp->host;
  2546. if (!hp)
  2547. goto lie;
  2548. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  2549. if (sdbg_host) {
  2550. list_for_each_entry(devip,
  2551. &sdbg_host->dev_info_list,
  2552. dev_list)
  2553. if (devip->target == sdp->id) {
  2554. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  2555. ++k;
  2556. }
  2557. }
  2558. if (SCSI_DEBUG_OPT_RESET_NOISE & scsi_debug_opts)
  2559. sdev_printk(KERN_INFO, sdp,
  2560. "%s: %d device(s) found in target\n", __func__, k);
  2561. lie:
  2562. return SUCCESS;
  2563. }
  2564. static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
  2565. {
  2566. struct sdebug_host_info *sdbg_host;
  2567. struct sdebug_dev_info *devip;
  2568. struct scsi_device * sdp;
  2569. struct Scsi_Host * hp;
  2570. int k = 0;
  2571. ++num_bus_resets;
  2572. if (!(SCpnt && SCpnt->device))
  2573. goto lie;
  2574. sdp = SCpnt->device;
  2575. if (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts)
  2576. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  2577. hp = sdp->host;
  2578. if (hp) {
  2579. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  2580. if (sdbg_host) {
  2581. list_for_each_entry(devip,
  2582. &sdbg_host->dev_info_list,
  2583. dev_list) {
  2584. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  2585. ++k;
  2586. }
  2587. }
  2588. }
  2589. if (SCSI_DEBUG_OPT_RESET_NOISE & scsi_debug_opts)
  2590. sdev_printk(KERN_INFO, sdp,
  2591. "%s: %d device(s) found in host\n", __func__, k);
  2592. lie:
  2593. return SUCCESS;
  2594. }
  2595. static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
  2596. {
  2597. struct sdebug_host_info * sdbg_host;
  2598. struct sdebug_dev_info *devip;
  2599. int k = 0;
  2600. ++num_host_resets;
  2601. if ((SCpnt->device) && (SCSI_DEBUG_OPT_ALL_NOISE & scsi_debug_opts))
  2602. sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__);
  2603. spin_lock(&sdebug_host_list_lock);
  2604. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  2605. list_for_each_entry(devip, &sdbg_host->dev_info_list,
  2606. dev_list) {
  2607. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  2608. ++k;
  2609. }
  2610. }
  2611. spin_unlock(&sdebug_host_list_lock);
  2612. stop_all_queued();
  2613. if (SCSI_DEBUG_OPT_RESET_NOISE & scsi_debug_opts)
  2614. sdev_printk(KERN_INFO, SCpnt->device,
  2615. "%s: %d device(s) found\n", __func__, k);
  2616. return SUCCESS;
  2617. }
  2618. static void __init sdebug_build_parts(unsigned char *ramp,
  2619. unsigned long store_size)
  2620. {
  2621. struct partition * pp;
  2622. int starts[SDEBUG_MAX_PARTS + 2];
  2623. int sectors_per_part, num_sectors, k;
  2624. int heads_by_sects, start_sec, end_sec;
  2625. /* assume partition table already zeroed */
  2626. if ((scsi_debug_num_parts < 1) || (store_size < 1048576))
  2627. return;
  2628. if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
  2629. scsi_debug_num_parts = SDEBUG_MAX_PARTS;
  2630. pr_warn("%s: reducing partitions to %d\n", __func__,
  2631. SDEBUG_MAX_PARTS);
  2632. }
  2633. num_sectors = (int)sdebug_store_sectors;
  2634. sectors_per_part = (num_sectors - sdebug_sectors_per)
  2635. / scsi_debug_num_parts;
  2636. heads_by_sects = sdebug_heads * sdebug_sectors_per;
  2637. starts[0] = sdebug_sectors_per;
  2638. for (k = 1; k < scsi_debug_num_parts; ++k)
  2639. starts[k] = ((k * sectors_per_part) / heads_by_sects)
  2640. * heads_by_sects;
  2641. starts[scsi_debug_num_parts] = num_sectors;
  2642. starts[scsi_debug_num_parts + 1] = 0;
  2643. ramp[510] = 0x55; /* magic partition markings */
  2644. ramp[511] = 0xAA;
  2645. pp = (struct partition *)(ramp + 0x1be);
  2646. for (k = 0; starts[k + 1]; ++k, ++pp) {
  2647. start_sec = starts[k];
  2648. end_sec = starts[k + 1] - 1;
  2649. pp->boot_ind = 0;
  2650. pp->cyl = start_sec / heads_by_sects;
  2651. pp->head = (start_sec - (pp->cyl * heads_by_sects))
  2652. / sdebug_sectors_per;
  2653. pp->sector = (start_sec % sdebug_sectors_per) + 1;
  2654. pp->end_cyl = end_sec / heads_by_sects;
  2655. pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
  2656. / sdebug_sectors_per;
  2657. pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
  2658. pp->start_sect = cpu_to_le32(start_sec);
  2659. pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
  2660. pp->sys_ind = 0x83; /* plain Linux partition */
  2661. }
  2662. }
  2663. static int
  2664. schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
  2665. int scsi_result, int delta_jiff)
  2666. {
  2667. unsigned long iflags;
  2668. int k, num_in_q, qdepth, inject;
  2669. struct sdebug_queued_cmd *sqcp = NULL;
  2670. struct scsi_device *sdp = cmnd->device;
  2671. if (NULL == cmnd || NULL == devip) {
  2672. pr_warn("%s: called with NULL cmnd or devip pointer\n",
  2673. __func__);
  2674. /* no particularly good error to report back */
  2675. return SCSI_MLQUEUE_HOST_BUSY;
  2676. }
  2677. if ((scsi_result) && (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  2678. sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n",
  2679. __func__, scsi_result);
  2680. if (delta_jiff == 0)
  2681. goto respond_in_thread;
  2682. /* schedule the response at a later time if resources permit */
  2683. spin_lock_irqsave(&queued_arr_lock, iflags);
  2684. num_in_q = atomic_read(&devip->num_in_q);
  2685. qdepth = cmnd->device->queue_depth;
  2686. inject = 0;
  2687. if ((qdepth > 0) && (num_in_q >= qdepth)) {
  2688. if (scsi_result) {
  2689. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2690. goto respond_in_thread;
  2691. } else
  2692. scsi_result = device_qfull_result;
  2693. } else if ((scsi_debug_every_nth != 0) &&
  2694. (SCSI_DEBUG_OPT_RARE_TSF & scsi_debug_opts) &&
  2695. (scsi_result == 0)) {
  2696. if ((num_in_q == (qdepth - 1)) &&
  2697. (atomic_inc_return(&sdebug_a_tsf) >=
  2698. abs(scsi_debug_every_nth))) {
  2699. atomic_set(&sdebug_a_tsf, 0);
  2700. inject = 1;
  2701. scsi_result = device_qfull_result;
  2702. }
  2703. }
  2704. k = find_first_zero_bit(queued_in_use_bm, scsi_debug_max_queue);
  2705. if (k >= scsi_debug_max_queue) {
  2706. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2707. if (scsi_result)
  2708. goto respond_in_thread;
  2709. else if (SCSI_DEBUG_OPT_ALL_TSF & scsi_debug_opts)
  2710. scsi_result = device_qfull_result;
  2711. if (SCSI_DEBUG_OPT_Q_NOISE & scsi_debug_opts)
  2712. sdev_printk(KERN_INFO, sdp,
  2713. "%s: max_queue=%d exceeded, %s\n",
  2714. __func__, scsi_debug_max_queue,
  2715. (scsi_result ? "status: TASK SET FULL" :
  2716. "report: host busy"));
  2717. if (scsi_result)
  2718. goto respond_in_thread;
  2719. else
  2720. return SCSI_MLQUEUE_HOST_BUSY;
  2721. }
  2722. __set_bit(k, queued_in_use_bm);
  2723. atomic_inc(&devip->num_in_q);
  2724. sqcp = &queued_arr[k];
  2725. sqcp->a_cmnd = cmnd;
  2726. cmnd->result = scsi_result;
  2727. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2728. if (delta_jiff > 0) {
  2729. if (NULL == sqcp->cmnd_timerp) {
  2730. sqcp->cmnd_timerp = kmalloc(sizeof(struct timer_list),
  2731. GFP_ATOMIC);
  2732. if (NULL == sqcp->cmnd_timerp)
  2733. return SCSI_MLQUEUE_HOST_BUSY;
  2734. init_timer(sqcp->cmnd_timerp);
  2735. }
  2736. sqcp->cmnd_timerp->function = sdebug_q_cmd_complete;
  2737. sqcp->cmnd_timerp->data = k;
  2738. sqcp->cmnd_timerp->expires = get_jiffies_64() + delta_jiff;
  2739. add_timer(sqcp->cmnd_timerp);
  2740. } else if (scsi_debug_ndelay > 0) {
  2741. ktime_t kt = ktime_set(0, scsi_debug_ndelay);
  2742. struct sdebug_hrtimer *sd_hp = sqcp->sd_hrtp;
  2743. if (NULL == sd_hp) {
  2744. sd_hp = kmalloc(sizeof(*sd_hp), GFP_ATOMIC);
  2745. if (NULL == sd_hp)
  2746. return SCSI_MLQUEUE_HOST_BUSY;
  2747. sqcp->sd_hrtp = sd_hp;
  2748. hrtimer_init(&sd_hp->hrt, CLOCK_MONOTONIC,
  2749. HRTIMER_MODE_REL);
  2750. sd_hp->hrt.function = sdebug_q_cmd_hrt_complete;
  2751. sd_hp->qa_indx = k;
  2752. }
  2753. hrtimer_start(&sd_hp->hrt, kt, HRTIMER_MODE_REL);
  2754. } else { /* delay < 0 */
  2755. if (NULL == sqcp->tletp) {
  2756. sqcp->tletp = kmalloc(sizeof(*sqcp->tletp),
  2757. GFP_ATOMIC);
  2758. if (NULL == sqcp->tletp)
  2759. return SCSI_MLQUEUE_HOST_BUSY;
  2760. tasklet_init(sqcp->tletp,
  2761. sdebug_q_cmd_complete, k);
  2762. }
  2763. if (-1 == delta_jiff)
  2764. tasklet_hi_schedule(sqcp->tletp);
  2765. else
  2766. tasklet_schedule(sqcp->tletp);
  2767. }
  2768. if ((SCSI_DEBUG_OPT_Q_NOISE & scsi_debug_opts) &&
  2769. (scsi_result == device_qfull_result))
  2770. sdev_printk(KERN_INFO, sdp,
  2771. "%s: num_in_q=%d +1, %s%s\n", __func__,
  2772. num_in_q, (inject ? "<inject> " : ""),
  2773. "status: TASK SET FULL");
  2774. return 0;
  2775. respond_in_thread: /* call back to mid-layer using invocation thread */
  2776. cmnd->result = scsi_result;
  2777. cmnd->scsi_done(cmnd);
  2778. return 0;
  2779. }
  2780. /* Note: The following macros create attribute files in the
  2781. /sys/module/scsi_debug/parameters directory. Unfortunately this
  2782. driver is unaware of a change and cannot trigger auxiliary actions
  2783. as it can when the corresponding attribute in the
  2784. /sys/bus/pseudo/drivers/scsi_debug directory is changed.
  2785. */
  2786. module_param_named(add_host, scsi_debug_add_host, int, S_IRUGO | S_IWUSR);
  2787. module_param_named(ato, scsi_debug_ato, int, S_IRUGO);
  2788. module_param_named(clustering, scsi_debug_clustering, bool, S_IRUGO | S_IWUSR);
  2789. module_param_named(delay, scsi_debug_delay, int, S_IRUGO | S_IWUSR);
  2790. module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, S_IRUGO);
  2791. module_param_named(dif, scsi_debug_dif, int, S_IRUGO);
  2792. module_param_named(dix, scsi_debug_dix, int, S_IRUGO);
  2793. module_param_named(dsense, scsi_debug_dsense, int, S_IRUGO | S_IWUSR);
  2794. module_param_named(every_nth, scsi_debug_every_nth, int, S_IRUGO | S_IWUSR);
  2795. module_param_named(fake_rw, scsi_debug_fake_rw, int, S_IRUGO | S_IWUSR);
  2796. module_param_named(guard, scsi_debug_guard, uint, S_IRUGO);
  2797. module_param_named(host_lock, scsi_debug_host_lock, bool, S_IRUGO | S_IWUSR);
  2798. module_param_named(lbpu, scsi_debug_lbpu, int, S_IRUGO);
  2799. module_param_named(lbpws, scsi_debug_lbpws, int, S_IRUGO);
  2800. module_param_named(lbpws10, scsi_debug_lbpws10, int, S_IRUGO);
  2801. module_param_named(lbprz, scsi_debug_lbprz, int, S_IRUGO);
  2802. module_param_named(lowest_aligned, scsi_debug_lowest_aligned, int, S_IRUGO);
  2803. module_param_named(max_luns, scsi_debug_max_luns, int, S_IRUGO | S_IWUSR);
  2804. module_param_named(max_queue, scsi_debug_max_queue, int, S_IRUGO | S_IWUSR);
  2805. module_param_named(ndelay, scsi_debug_ndelay, int, S_IRUGO | S_IWUSR);
  2806. module_param_named(no_lun_0, scsi_debug_no_lun_0, int, S_IRUGO | S_IWUSR);
  2807. module_param_named(no_uld, scsi_debug_no_uld, int, S_IRUGO);
  2808. module_param_named(num_parts, scsi_debug_num_parts, int, S_IRUGO);
  2809. module_param_named(num_tgts, scsi_debug_num_tgts, int, S_IRUGO | S_IWUSR);
  2810. module_param_named(opt_blks, scsi_debug_opt_blks, int, S_IRUGO);
  2811. module_param_named(opts, scsi_debug_opts, int, S_IRUGO | S_IWUSR);
  2812. module_param_named(physblk_exp, scsi_debug_physblk_exp, int, S_IRUGO);
  2813. module_param_named(ptype, scsi_debug_ptype, int, S_IRUGO | S_IWUSR);
  2814. module_param_named(removable, scsi_debug_removable, bool, S_IRUGO | S_IWUSR);
  2815. module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO);
  2816. module_param_named(sector_size, scsi_debug_sector_size, int, S_IRUGO);
  2817. module_param_named(unmap_alignment, scsi_debug_unmap_alignment, int, S_IRUGO);
  2818. module_param_named(unmap_granularity, scsi_debug_unmap_granularity, int, S_IRUGO);
  2819. module_param_named(unmap_max_blocks, scsi_debug_unmap_max_blocks, int, S_IRUGO);
  2820. module_param_named(unmap_max_desc, scsi_debug_unmap_max_desc, int, S_IRUGO);
  2821. module_param_named(virtual_gb, scsi_debug_virtual_gb, int, S_IRUGO | S_IWUSR);
  2822. module_param_named(vpd_use_hostno, scsi_debug_vpd_use_hostno, int,
  2823. S_IRUGO | S_IWUSR);
  2824. module_param_named(write_same_length, scsi_debug_write_same_length, int,
  2825. S_IRUGO | S_IWUSR);
  2826. MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
  2827. MODULE_DESCRIPTION("SCSI debug adapter driver");
  2828. MODULE_LICENSE("GPL");
  2829. MODULE_VERSION(SCSI_DEBUG_VERSION);
  2830. MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
  2831. MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
  2832. MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
  2833. MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
  2834. MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs(def=8)");
  2835. MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
  2836. MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
  2837. MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
  2838. MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
  2839. MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
  2840. MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
  2841. MODULE_PARM_DESC(host_lock, "use host_lock around all commands (def=0)");
  2842. MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
  2843. MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
  2844. MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
  2845. MODULE_PARM_DESC(lbprz, "unmapped blocks return 0 on read (def=1)");
  2846. MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
  2847. MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
  2848. MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
  2849. MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
  2850. MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
  2851. MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
  2852. MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
  2853. MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
  2854. MODULE_PARM_DESC(opt_blks, "optimal transfer length in block (def=64)");
  2855. MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
  2856. MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
  2857. MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
  2858. MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
  2859. MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=6[SPC-4])");
  2860. MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
  2861. MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
  2862. MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
  2863. MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
  2864. MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
  2865. MODULE_PARM_DESC(virtual_gb, "virtual gigabyte size (def=0 -> use dev_size_mb)");
  2866. MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
  2867. MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
  2868. static char sdebug_info[256];
  2869. static const char * scsi_debug_info(struct Scsi_Host * shp)
  2870. {
  2871. sprintf(sdebug_info, "scsi_debug, version %s [%s], "
  2872. "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
  2873. scsi_debug_version_date, scsi_debug_dev_size_mb,
  2874. scsi_debug_opts);
  2875. return sdebug_info;
  2876. }
  2877. /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
  2878. static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, int length)
  2879. {
  2880. char arr[16];
  2881. int opts;
  2882. int minLen = length > 15 ? 15 : length;
  2883. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2884. return -EACCES;
  2885. memcpy(arr, buffer, minLen);
  2886. arr[minLen] = '\0';
  2887. if (1 != sscanf(arr, "%d", &opts))
  2888. return -EINVAL;
  2889. scsi_debug_opts = opts;
  2890. if (scsi_debug_every_nth != 0)
  2891. atomic_set(&sdebug_cmnd_count, 0);
  2892. return length;
  2893. }
  2894. /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
  2895. * same for each scsi_debug host (if more than one). Some of the counters
  2896. * output are not atomics so might be inaccurate in a busy system. */
  2897. static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
  2898. {
  2899. int f, l;
  2900. char b[32];
  2901. if (scsi_debug_every_nth > 0)
  2902. snprintf(b, sizeof(b), " (curr:%d)",
  2903. ((SCSI_DEBUG_OPT_RARE_TSF & scsi_debug_opts) ?
  2904. atomic_read(&sdebug_a_tsf) :
  2905. atomic_read(&sdebug_cmnd_count)));
  2906. else
  2907. b[0] = '\0';
  2908. seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n"
  2909. "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
  2910. "every_nth=%d%s\n"
  2911. "delay=%d, ndelay=%d, max_luns=%d, q_completions=%d\n"
  2912. "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
  2913. "command aborts=%d; RESETs: device=%d, target=%d, bus=%d, "
  2914. "host=%d\ndix_reads=%d dix_writes=%d dif_errors=%d "
  2915. "usec_in_jiffy=%lu\n",
  2916. SCSI_DEBUG_VERSION, scsi_debug_version_date,
  2917. scsi_debug_num_tgts, scsi_debug_dev_size_mb, scsi_debug_opts,
  2918. scsi_debug_every_nth, b, scsi_debug_delay, scsi_debug_ndelay,
  2919. scsi_debug_max_luns, atomic_read(&sdebug_completions),
  2920. scsi_debug_sector_size, sdebug_cylinders_per, sdebug_heads,
  2921. sdebug_sectors_per, num_aborts, num_dev_resets,
  2922. num_target_resets, num_bus_resets, num_host_resets,
  2923. dix_reads, dix_writes, dif_errors, TICK_NSEC / 1000);
  2924. f = find_first_bit(queued_in_use_bm, scsi_debug_max_queue);
  2925. if (f != scsi_debug_max_queue) {
  2926. l = find_last_bit(queued_in_use_bm, scsi_debug_max_queue);
  2927. seq_printf(m, " %s BUSY: first,last bits set: %d,%d\n",
  2928. "queued_in_use_bm", f, l);
  2929. }
  2930. return 0;
  2931. }
  2932. static ssize_t delay_show(struct device_driver *ddp, char *buf)
  2933. {
  2934. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
  2935. }
  2936. /* Returns -EBUSY if delay is being changed and commands are queued */
  2937. static ssize_t delay_store(struct device_driver *ddp, const char *buf,
  2938. size_t count)
  2939. {
  2940. int delay, res;
  2941. if ((count > 0) && (1 == sscanf(buf, "%d", &delay))) {
  2942. res = count;
  2943. if (scsi_debug_delay != delay) {
  2944. unsigned long iflags;
  2945. int k;
  2946. spin_lock_irqsave(&queued_arr_lock, iflags);
  2947. k = find_first_bit(queued_in_use_bm,
  2948. scsi_debug_max_queue);
  2949. if (k != scsi_debug_max_queue)
  2950. res = -EBUSY; /* have queued commands */
  2951. else {
  2952. scsi_debug_delay = delay;
  2953. scsi_debug_ndelay = 0;
  2954. }
  2955. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2956. }
  2957. return res;
  2958. }
  2959. return -EINVAL;
  2960. }
  2961. static DRIVER_ATTR_RW(delay);
  2962. static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
  2963. {
  2964. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ndelay);
  2965. }
  2966. /* Returns -EBUSY if ndelay is being changed and commands are queued */
  2967. /* If > 0 and accepted then scsi_debug_delay is set to DELAY_OVERRIDDEN */
  2968. static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
  2969. size_t count)
  2970. {
  2971. unsigned long iflags;
  2972. int ndelay, res, k;
  2973. if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
  2974. (ndelay >= 0) && (ndelay < 1000000000)) {
  2975. res = count;
  2976. if (scsi_debug_ndelay != ndelay) {
  2977. spin_lock_irqsave(&queued_arr_lock, iflags);
  2978. k = find_first_bit(queued_in_use_bm,
  2979. scsi_debug_max_queue);
  2980. if (k != scsi_debug_max_queue)
  2981. res = -EBUSY; /* have queued commands */
  2982. else {
  2983. scsi_debug_ndelay = ndelay;
  2984. scsi_debug_delay = ndelay ? DELAY_OVERRIDDEN
  2985. : DEF_DELAY;
  2986. }
  2987. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2988. }
  2989. return res;
  2990. }
  2991. return -EINVAL;
  2992. }
  2993. static DRIVER_ATTR_RW(ndelay);
  2994. static ssize_t opts_show(struct device_driver *ddp, char *buf)
  2995. {
  2996. return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
  2997. }
  2998. static ssize_t opts_store(struct device_driver *ddp, const char *buf,
  2999. size_t count)
  3000. {
  3001. int opts;
  3002. char work[20];
  3003. if (1 == sscanf(buf, "%10s", work)) {
  3004. if (0 == strncasecmp(work,"0x", 2)) {
  3005. if (1 == sscanf(&work[2], "%x", &opts))
  3006. goto opts_done;
  3007. } else {
  3008. if (1 == sscanf(work, "%d", &opts))
  3009. goto opts_done;
  3010. }
  3011. }
  3012. return -EINVAL;
  3013. opts_done:
  3014. scsi_debug_opts = opts;
  3015. atomic_set(&sdebug_cmnd_count, 0);
  3016. atomic_set(&sdebug_a_tsf, 0);
  3017. return count;
  3018. }
  3019. static DRIVER_ATTR_RW(opts);
  3020. static ssize_t ptype_show(struct device_driver *ddp, char *buf)
  3021. {
  3022. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
  3023. }
  3024. static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
  3025. size_t count)
  3026. {
  3027. int n;
  3028. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3029. scsi_debug_ptype = n;
  3030. return count;
  3031. }
  3032. return -EINVAL;
  3033. }
  3034. static DRIVER_ATTR_RW(ptype);
  3035. static ssize_t dsense_show(struct device_driver *ddp, char *buf)
  3036. {
  3037. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
  3038. }
  3039. static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
  3040. size_t count)
  3041. {
  3042. int n;
  3043. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3044. scsi_debug_dsense = n;
  3045. return count;
  3046. }
  3047. return -EINVAL;
  3048. }
  3049. static DRIVER_ATTR_RW(dsense);
  3050. static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
  3051. {
  3052. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_fake_rw);
  3053. }
  3054. static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
  3055. size_t count)
  3056. {
  3057. int n;
  3058. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3059. n = (n > 0);
  3060. scsi_debug_fake_rw = (scsi_debug_fake_rw > 0);
  3061. if (scsi_debug_fake_rw != n) {
  3062. if ((0 == n) && (NULL == fake_storep)) {
  3063. unsigned long sz =
  3064. (unsigned long)scsi_debug_dev_size_mb *
  3065. 1048576;
  3066. fake_storep = vmalloc(sz);
  3067. if (NULL == fake_storep) {
  3068. pr_err("%s: out of memory, 9\n",
  3069. __func__);
  3070. return -ENOMEM;
  3071. }
  3072. memset(fake_storep, 0, sz);
  3073. }
  3074. scsi_debug_fake_rw = n;
  3075. }
  3076. return count;
  3077. }
  3078. return -EINVAL;
  3079. }
  3080. static DRIVER_ATTR_RW(fake_rw);
  3081. static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
  3082. {
  3083. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_lun_0);
  3084. }
  3085. static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
  3086. size_t count)
  3087. {
  3088. int n;
  3089. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3090. scsi_debug_no_lun_0 = n;
  3091. return count;
  3092. }
  3093. return -EINVAL;
  3094. }
  3095. static DRIVER_ATTR_RW(no_lun_0);
  3096. static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
  3097. {
  3098. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
  3099. }
  3100. static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
  3101. size_t count)
  3102. {
  3103. int n;
  3104. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3105. scsi_debug_num_tgts = n;
  3106. sdebug_max_tgts_luns();
  3107. return count;
  3108. }
  3109. return -EINVAL;
  3110. }
  3111. static DRIVER_ATTR_RW(num_tgts);
  3112. static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
  3113. {
  3114. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
  3115. }
  3116. static DRIVER_ATTR_RO(dev_size_mb);
  3117. static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
  3118. {
  3119. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
  3120. }
  3121. static DRIVER_ATTR_RO(num_parts);
  3122. static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
  3123. {
  3124. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
  3125. }
  3126. static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
  3127. size_t count)
  3128. {
  3129. int nth;
  3130. if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
  3131. scsi_debug_every_nth = nth;
  3132. atomic_set(&sdebug_cmnd_count, 0);
  3133. return count;
  3134. }
  3135. return -EINVAL;
  3136. }
  3137. static DRIVER_ATTR_RW(every_nth);
  3138. static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
  3139. {
  3140. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
  3141. }
  3142. static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
  3143. size_t count)
  3144. {
  3145. int n;
  3146. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3147. scsi_debug_max_luns = n;
  3148. sdebug_max_tgts_luns();
  3149. return count;
  3150. }
  3151. return -EINVAL;
  3152. }
  3153. static DRIVER_ATTR_RW(max_luns);
  3154. static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
  3155. {
  3156. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_queue);
  3157. }
  3158. /* N.B. max_queue can be changed while there are queued commands. In flight
  3159. * commands beyond the new max_queue will be completed. */
  3160. static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
  3161. size_t count)
  3162. {
  3163. unsigned long iflags;
  3164. int n, k;
  3165. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
  3166. (n <= SCSI_DEBUG_CANQUEUE)) {
  3167. spin_lock_irqsave(&queued_arr_lock, iflags);
  3168. k = find_last_bit(queued_in_use_bm, SCSI_DEBUG_CANQUEUE);
  3169. scsi_debug_max_queue = n;
  3170. if (SCSI_DEBUG_CANQUEUE == k)
  3171. atomic_set(&retired_max_queue, 0);
  3172. else if (k >= n)
  3173. atomic_set(&retired_max_queue, k + 1);
  3174. else
  3175. atomic_set(&retired_max_queue, 0);
  3176. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  3177. return count;
  3178. }
  3179. return -EINVAL;
  3180. }
  3181. static DRIVER_ATTR_RW(max_queue);
  3182. static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
  3183. {
  3184. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_uld);
  3185. }
  3186. static DRIVER_ATTR_RO(no_uld);
  3187. static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
  3188. {
  3189. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
  3190. }
  3191. static DRIVER_ATTR_RO(scsi_level);
  3192. static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
  3193. {
  3194. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb);
  3195. }
  3196. static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
  3197. size_t count)
  3198. {
  3199. int n;
  3200. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3201. scsi_debug_virtual_gb = n;
  3202. sdebug_capacity = get_sdebug_capacity();
  3203. return count;
  3204. }
  3205. return -EINVAL;
  3206. }
  3207. static DRIVER_ATTR_RW(virtual_gb);
  3208. static ssize_t add_host_show(struct device_driver *ddp, char *buf)
  3209. {
  3210. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
  3211. }
  3212. static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
  3213. size_t count)
  3214. {
  3215. int delta_hosts;
  3216. if (sscanf(buf, "%d", &delta_hosts) != 1)
  3217. return -EINVAL;
  3218. if (delta_hosts > 0) {
  3219. do {
  3220. sdebug_add_adapter();
  3221. } while (--delta_hosts);
  3222. } else if (delta_hosts < 0) {
  3223. do {
  3224. sdebug_remove_adapter();
  3225. } while (++delta_hosts);
  3226. }
  3227. return count;
  3228. }
  3229. static DRIVER_ATTR_RW(add_host);
  3230. static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
  3231. {
  3232. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_vpd_use_hostno);
  3233. }
  3234. static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
  3235. size_t count)
  3236. {
  3237. int n;
  3238. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3239. scsi_debug_vpd_use_hostno = n;
  3240. return count;
  3241. }
  3242. return -EINVAL;
  3243. }
  3244. static DRIVER_ATTR_RW(vpd_use_hostno);
  3245. static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
  3246. {
  3247. return scnprintf(buf, PAGE_SIZE, "%u\n", scsi_debug_sector_size);
  3248. }
  3249. static DRIVER_ATTR_RO(sector_size);
  3250. static ssize_t dix_show(struct device_driver *ddp, char *buf)
  3251. {
  3252. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dix);
  3253. }
  3254. static DRIVER_ATTR_RO(dix);
  3255. static ssize_t dif_show(struct device_driver *ddp, char *buf)
  3256. {
  3257. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dif);
  3258. }
  3259. static DRIVER_ATTR_RO(dif);
  3260. static ssize_t guard_show(struct device_driver *ddp, char *buf)
  3261. {
  3262. return scnprintf(buf, PAGE_SIZE, "%u\n", scsi_debug_guard);
  3263. }
  3264. static DRIVER_ATTR_RO(guard);
  3265. static ssize_t ato_show(struct device_driver *ddp, char *buf)
  3266. {
  3267. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ato);
  3268. }
  3269. static DRIVER_ATTR_RO(ato);
  3270. static ssize_t map_show(struct device_driver *ddp, char *buf)
  3271. {
  3272. ssize_t count;
  3273. if (!scsi_debug_lbp())
  3274. return scnprintf(buf, PAGE_SIZE, "0-%u\n",
  3275. sdebug_store_sectors);
  3276. count = bitmap_scnlistprintf(buf, PAGE_SIZE, map_storep, map_size);
  3277. buf[count++] = '\n';
  3278. buf[count++] = 0;
  3279. return count;
  3280. }
  3281. static DRIVER_ATTR_RO(map);
  3282. static ssize_t removable_show(struct device_driver *ddp, char *buf)
  3283. {
  3284. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_removable ? 1 : 0);
  3285. }
  3286. static ssize_t removable_store(struct device_driver *ddp, const char *buf,
  3287. size_t count)
  3288. {
  3289. int n;
  3290. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3291. scsi_debug_removable = (n > 0);
  3292. return count;
  3293. }
  3294. return -EINVAL;
  3295. }
  3296. static DRIVER_ATTR_RW(removable);
  3297. static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
  3298. {
  3299. return scnprintf(buf, PAGE_SIZE, "%d\n", !!scsi_debug_host_lock);
  3300. }
  3301. /* Returns -EBUSY if host_lock is being changed and commands are queued */
  3302. static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
  3303. size_t count)
  3304. {
  3305. int n, res;
  3306. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  3307. bool new_host_lock = (n > 0);
  3308. res = count;
  3309. if (new_host_lock != scsi_debug_host_lock) {
  3310. unsigned long iflags;
  3311. int k;
  3312. spin_lock_irqsave(&queued_arr_lock, iflags);
  3313. k = find_first_bit(queued_in_use_bm,
  3314. scsi_debug_max_queue);
  3315. if (k != scsi_debug_max_queue)
  3316. res = -EBUSY; /* have queued commands */
  3317. else
  3318. scsi_debug_host_lock = new_host_lock;
  3319. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  3320. }
  3321. return res;
  3322. }
  3323. return -EINVAL;
  3324. }
  3325. static DRIVER_ATTR_RW(host_lock);
  3326. /* Note: The following array creates attribute files in the
  3327. /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
  3328. files (over those found in the /sys/module/scsi_debug/parameters
  3329. directory) is that auxiliary actions can be triggered when an attribute
  3330. is changed. For example see: sdebug_add_host_store() above.
  3331. */
  3332. static struct attribute *sdebug_drv_attrs[] = {
  3333. &driver_attr_delay.attr,
  3334. &driver_attr_opts.attr,
  3335. &driver_attr_ptype.attr,
  3336. &driver_attr_dsense.attr,
  3337. &driver_attr_fake_rw.attr,
  3338. &driver_attr_no_lun_0.attr,
  3339. &driver_attr_num_tgts.attr,
  3340. &driver_attr_dev_size_mb.attr,
  3341. &driver_attr_num_parts.attr,
  3342. &driver_attr_every_nth.attr,
  3343. &driver_attr_max_luns.attr,
  3344. &driver_attr_max_queue.attr,
  3345. &driver_attr_no_uld.attr,
  3346. &driver_attr_scsi_level.attr,
  3347. &driver_attr_virtual_gb.attr,
  3348. &driver_attr_add_host.attr,
  3349. &driver_attr_vpd_use_hostno.attr,
  3350. &driver_attr_sector_size.attr,
  3351. &driver_attr_dix.attr,
  3352. &driver_attr_dif.attr,
  3353. &driver_attr_guard.attr,
  3354. &driver_attr_ato.attr,
  3355. &driver_attr_map.attr,
  3356. &driver_attr_removable.attr,
  3357. &driver_attr_host_lock.attr,
  3358. &driver_attr_ndelay.attr,
  3359. NULL,
  3360. };
  3361. ATTRIBUTE_GROUPS(sdebug_drv);
  3362. static struct device *pseudo_primary;
  3363. static int __init scsi_debug_init(void)
  3364. {
  3365. unsigned long sz;
  3366. int host_to_add;
  3367. int k;
  3368. int ret;
  3369. atomic_set(&sdebug_cmnd_count, 0);
  3370. atomic_set(&sdebug_completions, 0);
  3371. atomic_set(&retired_max_queue, 0);
  3372. if (scsi_debug_ndelay >= 1000000000) {
  3373. pr_warn("%s: ndelay must be less than 1 second, ignored\n",
  3374. __func__);
  3375. scsi_debug_ndelay = 0;
  3376. } else if (scsi_debug_ndelay > 0)
  3377. scsi_debug_delay = DELAY_OVERRIDDEN;
  3378. switch (scsi_debug_sector_size) {
  3379. case 512:
  3380. case 1024:
  3381. case 2048:
  3382. case 4096:
  3383. break;
  3384. default:
  3385. pr_err("%s: invalid sector_size %d\n", __func__,
  3386. scsi_debug_sector_size);
  3387. return -EINVAL;
  3388. }
  3389. switch (scsi_debug_dif) {
  3390. case SD_DIF_TYPE0_PROTECTION:
  3391. case SD_DIF_TYPE1_PROTECTION:
  3392. case SD_DIF_TYPE2_PROTECTION:
  3393. case SD_DIF_TYPE3_PROTECTION:
  3394. break;
  3395. default:
  3396. pr_err("%s: dif must be 0, 1, 2 or 3\n", __func__);
  3397. return -EINVAL;
  3398. }
  3399. if (scsi_debug_guard > 1) {
  3400. pr_err("%s: guard must be 0 or 1\n", __func__);
  3401. return -EINVAL;
  3402. }
  3403. if (scsi_debug_ato > 1) {
  3404. pr_err("%s: ato must be 0 or 1\n", __func__);
  3405. return -EINVAL;
  3406. }
  3407. if (scsi_debug_physblk_exp > 15) {
  3408. pr_err("%s: invalid physblk_exp %u\n", __func__,
  3409. scsi_debug_physblk_exp);
  3410. return -EINVAL;
  3411. }
  3412. if (scsi_debug_lowest_aligned > 0x3fff) {
  3413. pr_err("%s: lowest_aligned too big: %u\n", __func__,
  3414. scsi_debug_lowest_aligned);
  3415. return -EINVAL;
  3416. }
  3417. if (scsi_debug_dev_size_mb < 1)
  3418. scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
  3419. sz = (unsigned long)scsi_debug_dev_size_mb * 1048576;
  3420. sdebug_store_sectors = sz / scsi_debug_sector_size;
  3421. sdebug_capacity = get_sdebug_capacity();
  3422. /* play around with geometry, don't waste too much on track 0 */
  3423. sdebug_heads = 8;
  3424. sdebug_sectors_per = 32;
  3425. if (scsi_debug_dev_size_mb >= 16)
  3426. sdebug_heads = 32;
  3427. else if (scsi_debug_dev_size_mb >= 256)
  3428. sdebug_heads = 64;
  3429. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  3430. (sdebug_sectors_per * sdebug_heads);
  3431. if (sdebug_cylinders_per >= 1024) {
  3432. /* other LLDs do this; implies >= 1GB ram disk ... */
  3433. sdebug_heads = 255;
  3434. sdebug_sectors_per = 63;
  3435. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  3436. (sdebug_sectors_per * sdebug_heads);
  3437. }
  3438. if (0 == scsi_debug_fake_rw) {
  3439. fake_storep = vmalloc(sz);
  3440. if (NULL == fake_storep) {
  3441. pr_err("%s: out of memory, 1\n", __func__);
  3442. return -ENOMEM;
  3443. }
  3444. memset(fake_storep, 0, sz);
  3445. if (scsi_debug_num_parts > 0)
  3446. sdebug_build_parts(fake_storep, sz);
  3447. }
  3448. if (scsi_debug_dix) {
  3449. int dif_size;
  3450. dif_size = sdebug_store_sectors * sizeof(struct sd_dif_tuple);
  3451. dif_storep = vmalloc(dif_size);
  3452. pr_err("%s: dif_storep %u bytes @ %p\n", __func__, dif_size,
  3453. dif_storep);
  3454. if (dif_storep == NULL) {
  3455. pr_err("%s: out of mem. (DIX)\n", __func__);
  3456. ret = -ENOMEM;
  3457. goto free_vm;
  3458. }
  3459. memset(dif_storep, 0xff, dif_size);
  3460. }
  3461. /* Logical Block Provisioning */
  3462. if (scsi_debug_lbp()) {
  3463. scsi_debug_unmap_max_blocks =
  3464. clamp(scsi_debug_unmap_max_blocks, 0U, 0xffffffffU);
  3465. scsi_debug_unmap_max_desc =
  3466. clamp(scsi_debug_unmap_max_desc, 0U, 256U);
  3467. scsi_debug_unmap_granularity =
  3468. clamp(scsi_debug_unmap_granularity, 1U, 0xffffffffU);
  3469. if (scsi_debug_unmap_alignment &&
  3470. scsi_debug_unmap_granularity <=
  3471. scsi_debug_unmap_alignment) {
  3472. pr_err("%s: ERR: unmap_granularity <= unmap_alignment\n",
  3473. __func__);
  3474. return -EINVAL;
  3475. }
  3476. map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
  3477. map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long));
  3478. pr_info("%s: %lu provisioning blocks\n", __func__, map_size);
  3479. if (map_storep == NULL) {
  3480. pr_err("%s: out of mem. (MAP)\n", __func__);
  3481. ret = -ENOMEM;
  3482. goto free_vm;
  3483. }
  3484. bitmap_zero(map_storep, map_size);
  3485. /* Map first 1KB for partition table */
  3486. if (scsi_debug_num_parts)
  3487. map_region(0, 2);
  3488. }
  3489. pseudo_primary = root_device_register("pseudo_0");
  3490. if (IS_ERR(pseudo_primary)) {
  3491. pr_warn("%s: root_device_register() error\n", __func__);
  3492. ret = PTR_ERR(pseudo_primary);
  3493. goto free_vm;
  3494. }
  3495. ret = bus_register(&pseudo_lld_bus);
  3496. if (ret < 0) {
  3497. pr_warn("%s: bus_register error: %d\n", __func__, ret);
  3498. goto dev_unreg;
  3499. }
  3500. ret = driver_register(&sdebug_driverfs_driver);
  3501. if (ret < 0) {
  3502. pr_warn("%s: driver_register error: %d\n", __func__, ret);
  3503. goto bus_unreg;
  3504. }
  3505. host_to_add = scsi_debug_add_host;
  3506. scsi_debug_add_host = 0;
  3507. for (k = 0; k < host_to_add; k++) {
  3508. if (sdebug_add_adapter()) {
  3509. pr_err("%s: sdebug_add_adapter failed k=%d\n",
  3510. __func__, k);
  3511. break;
  3512. }
  3513. }
  3514. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  3515. pr_info("%s: built %d host(s)\n", __func__,
  3516. scsi_debug_add_host);
  3517. }
  3518. return 0;
  3519. bus_unreg:
  3520. bus_unregister(&pseudo_lld_bus);
  3521. dev_unreg:
  3522. root_device_unregister(pseudo_primary);
  3523. free_vm:
  3524. if (map_storep)
  3525. vfree(map_storep);
  3526. if (dif_storep)
  3527. vfree(dif_storep);
  3528. vfree(fake_storep);
  3529. return ret;
  3530. }
  3531. static void __exit scsi_debug_exit(void)
  3532. {
  3533. int k = scsi_debug_add_host;
  3534. stop_all_queued();
  3535. free_all_queued();
  3536. for (; k; k--)
  3537. sdebug_remove_adapter();
  3538. driver_unregister(&sdebug_driverfs_driver);
  3539. bus_unregister(&pseudo_lld_bus);
  3540. root_device_unregister(pseudo_primary);
  3541. if (dif_storep)
  3542. vfree(dif_storep);
  3543. vfree(fake_storep);
  3544. }
  3545. device_initcall(scsi_debug_init);
  3546. module_exit(scsi_debug_exit);
  3547. static void sdebug_release_adapter(struct device * dev)
  3548. {
  3549. struct sdebug_host_info *sdbg_host;
  3550. sdbg_host = to_sdebug_host(dev);
  3551. kfree(sdbg_host);
  3552. }
  3553. static int sdebug_add_adapter(void)
  3554. {
  3555. int k, devs_per_host;
  3556. int error = 0;
  3557. struct sdebug_host_info *sdbg_host;
  3558. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  3559. sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL);
  3560. if (NULL == sdbg_host) {
  3561. printk(KERN_ERR "%s: out of memory at line %d\n",
  3562. __func__, __LINE__);
  3563. return -ENOMEM;
  3564. }
  3565. INIT_LIST_HEAD(&sdbg_host->dev_info_list);
  3566. devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
  3567. for (k = 0; k < devs_per_host; k++) {
  3568. sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
  3569. if (!sdbg_devinfo) {
  3570. printk(KERN_ERR "%s: out of memory at line %d\n",
  3571. __func__, __LINE__);
  3572. error = -ENOMEM;
  3573. goto clean;
  3574. }
  3575. }
  3576. spin_lock(&sdebug_host_list_lock);
  3577. list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
  3578. spin_unlock(&sdebug_host_list_lock);
  3579. sdbg_host->dev.bus = &pseudo_lld_bus;
  3580. sdbg_host->dev.parent = pseudo_primary;
  3581. sdbg_host->dev.release = &sdebug_release_adapter;
  3582. dev_set_name(&sdbg_host->dev, "adapter%d", scsi_debug_add_host);
  3583. error = device_register(&sdbg_host->dev);
  3584. if (error)
  3585. goto clean;
  3586. ++scsi_debug_add_host;
  3587. return error;
  3588. clean:
  3589. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  3590. dev_list) {
  3591. list_del(&sdbg_devinfo->dev_list);
  3592. kfree(sdbg_devinfo);
  3593. }
  3594. kfree(sdbg_host);
  3595. return error;
  3596. }
  3597. static void sdebug_remove_adapter(void)
  3598. {
  3599. struct sdebug_host_info * sdbg_host = NULL;
  3600. spin_lock(&sdebug_host_list_lock);
  3601. if (!list_empty(&sdebug_host_list)) {
  3602. sdbg_host = list_entry(sdebug_host_list.prev,
  3603. struct sdebug_host_info, host_list);
  3604. list_del(&sdbg_host->host_list);
  3605. }
  3606. spin_unlock(&sdebug_host_list_lock);
  3607. if (!sdbg_host)
  3608. return;
  3609. device_unregister(&sdbg_host->dev);
  3610. --scsi_debug_add_host;
  3611. }
  3612. static int
  3613. scsi_debug_queuecommand(struct scsi_cmnd *SCpnt)
  3614. {
  3615. unsigned char *cmd = SCpnt->cmnd;
  3616. int len, k;
  3617. unsigned int num;
  3618. unsigned long long lba;
  3619. u32 ei_lba;
  3620. int errsts = 0;
  3621. int target = SCpnt->device->id;
  3622. struct sdebug_dev_info *devip = NULL;
  3623. int inj_recovered = 0;
  3624. int inj_transport = 0;
  3625. int inj_dif = 0;
  3626. int inj_dix = 0;
  3627. int inj_short = 0;
  3628. int delay_override = 0;
  3629. int unmap = 0;
  3630. scsi_set_resid(SCpnt, 0);
  3631. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) &&
  3632. !(SCSI_DEBUG_OPT_NO_CDB_NOISE & scsi_debug_opts)) {
  3633. char b[120];
  3634. int n;
  3635. len = SCpnt->cmd_len;
  3636. if (len > 32)
  3637. strcpy(b, "too long, over 32 bytes");
  3638. else {
  3639. for (k = 0, n = 0; k < len; ++k)
  3640. n += scnprintf(b + n, sizeof(b) - n, "%02x ",
  3641. (unsigned int)cmd[k]);
  3642. }
  3643. sdev_printk(KERN_INFO, SCpnt->device, "%s: cmd %s\n", my_name,
  3644. b);
  3645. }
  3646. if ((SCpnt->device->lun >= scsi_debug_max_luns) &&
  3647. (SCpnt->device->lun != SAM2_WLUN_REPORT_LUNS))
  3648. return schedule_resp(SCpnt, NULL, DID_NO_CONNECT << 16, 0);
  3649. devip = devInfoReg(SCpnt->device);
  3650. if (NULL == devip)
  3651. return schedule_resp(SCpnt, NULL, DID_NO_CONNECT << 16, 0);
  3652. if ((scsi_debug_every_nth != 0) &&
  3653. (atomic_inc_return(&sdebug_cmnd_count) >=
  3654. abs(scsi_debug_every_nth))) {
  3655. atomic_set(&sdebug_cmnd_count, 0);
  3656. if (scsi_debug_every_nth < -1)
  3657. scsi_debug_every_nth = -1;
  3658. if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
  3659. return 0; /* ignore command causing timeout */
  3660. else if (SCSI_DEBUG_OPT_MAC_TIMEOUT & scsi_debug_opts &&
  3661. scsi_medium_access_command(SCpnt))
  3662. return 0; /* time out reads and writes */
  3663. else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
  3664. inj_recovered = 1; /* to reads and writes below */
  3665. else if (SCSI_DEBUG_OPT_TRANSPORT_ERR & scsi_debug_opts)
  3666. inj_transport = 1; /* to reads and writes below */
  3667. else if (SCSI_DEBUG_OPT_DIF_ERR & scsi_debug_opts)
  3668. inj_dif = 1; /* to reads and writes below */
  3669. else if (SCSI_DEBUG_OPT_DIX_ERR & scsi_debug_opts)
  3670. inj_dix = 1; /* to reads and writes below */
  3671. else if (SCSI_DEBUG_OPT_SHORT_TRANSFER & scsi_debug_opts)
  3672. inj_short = 1;
  3673. }
  3674. if (devip->wlun) {
  3675. switch (*cmd) {
  3676. case INQUIRY:
  3677. case REQUEST_SENSE:
  3678. case TEST_UNIT_READY:
  3679. case REPORT_LUNS:
  3680. break; /* only allowable wlun commands */
  3681. default:
  3682. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  3683. printk(KERN_INFO "scsi_debug: Opcode: 0x%x "
  3684. "not supported for wlun\n", *cmd);
  3685. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3686. INVALID_OPCODE, 0);
  3687. errsts = check_condition_result;
  3688. return schedule_resp(SCpnt, devip, errsts, 0);
  3689. }
  3690. }
  3691. switch (*cmd) {
  3692. case INQUIRY: /* mandatory, ignore unit attention */
  3693. delay_override = 1;
  3694. errsts = resp_inquiry(SCpnt, target, devip);
  3695. break;
  3696. case REQUEST_SENSE: /* mandatory, ignore unit attention */
  3697. delay_override = 1;
  3698. errsts = resp_requests(SCpnt, devip);
  3699. break;
  3700. case REZERO_UNIT: /* actually this is REWIND for SSC */
  3701. case START_STOP:
  3702. errsts = resp_start_stop(SCpnt, devip);
  3703. break;
  3704. case ALLOW_MEDIUM_REMOVAL:
  3705. errsts = check_readiness(SCpnt, UAS_ONLY, devip);
  3706. if (errsts)
  3707. break;
  3708. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  3709. printk(KERN_INFO "scsi_debug: Medium removal %s\n",
  3710. cmd[4] ? "inhibited" : "enabled");
  3711. break;
  3712. case SEND_DIAGNOSTIC: /* mandatory */
  3713. errsts = check_readiness(SCpnt, UAS_ONLY, devip);
  3714. break;
  3715. case TEST_UNIT_READY: /* mandatory */
  3716. /* delay_override = 1; */
  3717. errsts = check_readiness(SCpnt, UAS_TUR, devip);
  3718. break;
  3719. case RESERVE:
  3720. errsts = check_readiness(SCpnt, UAS_ONLY, devip);
  3721. break;
  3722. case RESERVE_10:
  3723. errsts = check_readiness(SCpnt, UAS_ONLY, devip);
  3724. break;
  3725. case RELEASE:
  3726. errsts = check_readiness(SCpnt, UAS_ONLY, devip);
  3727. break;
  3728. case RELEASE_10:
  3729. errsts = check_readiness(SCpnt, UAS_ONLY, devip);
  3730. break;
  3731. case READ_CAPACITY:
  3732. errsts = resp_readcap(SCpnt, devip);
  3733. break;
  3734. case SERVICE_ACTION_IN:
  3735. if (cmd[1] == SAI_READ_CAPACITY_16)
  3736. errsts = resp_readcap16(SCpnt, devip);
  3737. else if (cmd[1] == SAI_GET_LBA_STATUS) {
  3738. if (scsi_debug_lbp() == 0) {
  3739. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3740. INVALID_COMMAND_OPCODE, 0);
  3741. errsts = check_condition_result;
  3742. } else
  3743. errsts = resp_get_lba_status(SCpnt, devip);
  3744. } else {
  3745. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3746. INVALID_OPCODE, 0);
  3747. errsts = check_condition_result;
  3748. }
  3749. break;
  3750. case MAINTENANCE_IN:
  3751. if (MI_REPORT_TARGET_PGS != cmd[1]) {
  3752. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3753. INVALID_OPCODE, 0);
  3754. errsts = check_condition_result;
  3755. break;
  3756. }
  3757. errsts = resp_report_tgtpgs(SCpnt, devip);
  3758. break;
  3759. case READ_16:
  3760. case READ_12:
  3761. case READ_10:
  3762. /* READ{10,12,16} and DIF Type 2 are natural enemies */
  3763. if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
  3764. cmd[1] & 0xe0) {
  3765. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3766. INVALID_COMMAND_OPCODE, 0);
  3767. errsts = check_condition_result;
  3768. break;
  3769. }
  3770. if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
  3771. scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
  3772. (cmd[1] & 0xe0) == 0)
  3773. printk(KERN_ERR "Unprotected RD/WR to DIF device\n");
  3774. /* fall through */
  3775. case READ_6:
  3776. read:
  3777. errsts = check_readiness(SCpnt, UAS_TUR, devip);
  3778. if (errsts)
  3779. break;
  3780. if (scsi_debug_fake_rw)
  3781. break;
  3782. get_data_transfer_info(cmd, &lba, &num, &ei_lba);
  3783. if (inj_short)
  3784. num /= 2;
  3785. errsts = resp_read(SCpnt, lba, num, ei_lba);
  3786. if (inj_recovered && (0 == errsts)) {
  3787. mk_sense_buffer(SCpnt, RECOVERED_ERROR,
  3788. THRESHOLD_EXCEEDED, 0);
  3789. errsts = check_condition_result;
  3790. } else if (inj_transport && (0 == errsts)) {
  3791. mk_sense_buffer(SCpnt, ABORTED_COMMAND,
  3792. TRANSPORT_PROBLEM, ACK_NAK_TO);
  3793. errsts = check_condition_result;
  3794. } else if (inj_dif && (0 == errsts)) {
  3795. /* Logical block guard check failed */
  3796. mk_sense_buffer(SCpnt, ABORTED_COMMAND, 0x10, 1);
  3797. errsts = illegal_condition_result;
  3798. } else if (inj_dix && (0 == errsts)) {
  3799. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST, 0x10, 1);
  3800. errsts = illegal_condition_result;
  3801. }
  3802. break;
  3803. case REPORT_LUNS: /* mandatory, ignore unit attention */
  3804. delay_override = 1;
  3805. errsts = resp_report_luns(SCpnt, devip);
  3806. break;
  3807. case VERIFY: /* 10 byte SBC-2 command */
  3808. errsts = check_readiness(SCpnt, UAS_TUR, devip);
  3809. break;
  3810. case WRITE_16:
  3811. case WRITE_12:
  3812. case WRITE_10:
  3813. /* WRITE{10,12,16} and DIF Type 2 are natural enemies */
  3814. if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
  3815. cmd[1] & 0xe0) {
  3816. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3817. INVALID_COMMAND_OPCODE, 0);
  3818. errsts = check_condition_result;
  3819. break;
  3820. }
  3821. if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
  3822. scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
  3823. (cmd[1] & 0xe0) == 0)
  3824. printk(KERN_ERR "Unprotected RD/WR to DIF device\n");
  3825. /* fall through */
  3826. case WRITE_6:
  3827. write:
  3828. errsts = check_readiness(SCpnt, UAS_TUR, devip);
  3829. if (errsts)
  3830. break;
  3831. if (scsi_debug_fake_rw)
  3832. break;
  3833. get_data_transfer_info(cmd, &lba, &num, &ei_lba);
  3834. errsts = resp_write(SCpnt, lba, num, ei_lba);
  3835. if (inj_recovered && (0 == errsts)) {
  3836. mk_sense_buffer(SCpnt, RECOVERED_ERROR,
  3837. THRESHOLD_EXCEEDED, 0);
  3838. errsts = check_condition_result;
  3839. } else if (inj_dif && (0 == errsts)) {
  3840. mk_sense_buffer(SCpnt, ABORTED_COMMAND, 0x10, 1);
  3841. errsts = illegal_condition_result;
  3842. } else if (inj_dix && (0 == errsts)) {
  3843. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST, 0x10, 1);
  3844. errsts = illegal_condition_result;
  3845. }
  3846. break;
  3847. case WRITE_SAME_16:
  3848. case WRITE_SAME:
  3849. if (cmd[1] & 0x8) {
  3850. if ((*cmd == WRITE_SAME_16 && scsi_debug_lbpws == 0) ||
  3851. (*cmd == WRITE_SAME && scsi_debug_lbpws10 == 0)) {
  3852. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3853. INVALID_FIELD_IN_CDB, 0);
  3854. errsts = check_condition_result;
  3855. } else
  3856. unmap = 1;
  3857. }
  3858. if (errsts)
  3859. break;
  3860. errsts = check_readiness(SCpnt, UAS_TUR, devip);
  3861. if (errsts)
  3862. break;
  3863. if (scsi_debug_fake_rw)
  3864. break;
  3865. get_data_transfer_info(cmd, &lba, &num, &ei_lba);
  3866. errsts = resp_write_same(SCpnt, lba, num, ei_lba, unmap);
  3867. break;
  3868. case UNMAP:
  3869. errsts = check_readiness(SCpnt, UAS_TUR, devip);
  3870. if (errsts)
  3871. break;
  3872. if (scsi_debug_fake_rw)
  3873. break;
  3874. if (scsi_debug_unmap_max_desc == 0 || scsi_debug_lbpu == 0) {
  3875. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3876. INVALID_COMMAND_OPCODE, 0);
  3877. errsts = check_condition_result;
  3878. } else
  3879. errsts = resp_unmap(SCpnt, devip);
  3880. break;
  3881. case MODE_SENSE:
  3882. case MODE_SENSE_10:
  3883. errsts = resp_mode_sense(SCpnt, target, devip);
  3884. break;
  3885. case MODE_SELECT:
  3886. errsts = resp_mode_select(SCpnt, 1, devip);
  3887. break;
  3888. case MODE_SELECT_10:
  3889. errsts = resp_mode_select(SCpnt, 0, devip);
  3890. break;
  3891. case LOG_SENSE:
  3892. errsts = resp_log_sense(SCpnt, devip);
  3893. break;
  3894. case SYNCHRONIZE_CACHE:
  3895. delay_override = 1;
  3896. errsts = check_readiness(SCpnt, UAS_TUR, devip);
  3897. break;
  3898. case WRITE_BUFFER:
  3899. errsts = check_readiness(SCpnt, UAS_ONLY, devip);
  3900. break;
  3901. case XDWRITEREAD_10:
  3902. if (!scsi_bidi_cmnd(SCpnt)) {
  3903. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3904. INVALID_FIELD_IN_CDB, 0);
  3905. errsts = check_condition_result;
  3906. break;
  3907. }
  3908. errsts = check_readiness(SCpnt, UAS_TUR, devip);
  3909. if (errsts)
  3910. break;
  3911. if (scsi_debug_fake_rw)
  3912. break;
  3913. get_data_transfer_info(cmd, &lba, &num, &ei_lba);
  3914. errsts = resp_read(SCpnt, lba, num, ei_lba);
  3915. if (errsts)
  3916. break;
  3917. errsts = resp_write(SCpnt, lba, num, ei_lba);
  3918. if (errsts)
  3919. break;
  3920. errsts = resp_xdwriteread(SCpnt, lba, num, devip);
  3921. break;
  3922. case VARIABLE_LENGTH_CMD:
  3923. if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION) {
  3924. if ((cmd[10] & 0xe0) == 0)
  3925. printk(KERN_ERR
  3926. "Unprotected RD/WR to DIF device\n");
  3927. if (cmd[9] == READ_32) {
  3928. BUG_ON(SCpnt->cmd_len < 32);
  3929. goto read;
  3930. }
  3931. if (cmd[9] == WRITE_32) {
  3932. BUG_ON(SCpnt->cmd_len < 32);
  3933. goto write;
  3934. }
  3935. }
  3936. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3937. INVALID_FIELD_IN_CDB, 0);
  3938. errsts = check_condition_result;
  3939. break;
  3940. case 0x85:
  3941. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  3942. sdev_printk(KERN_INFO, SCpnt->device,
  3943. "%s: ATA PASS-THROUGH(16) not supported\n", my_name);
  3944. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST,
  3945. INVALID_OPCODE, 0);
  3946. errsts = check_condition_result;
  3947. break;
  3948. default:
  3949. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  3950. sdev_printk(KERN_INFO, SCpnt->device,
  3951. "%s: Opcode: 0x%x not supported\n",
  3952. my_name, *cmd);
  3953. errsts = check_readiness(SCpnt, UAS_ONLY, devip);
  3954. if (errsts)
  3955. break; /* Unit attention takes precedence */
  3956. mk_sense_buffer(SCpnt, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
  3957. errsts = check_condition_result;
  3958. break;
  3959. }
  3960. return schedule_resp(SCpnt, devip, errsts,
  3961. (delay_override ? 0 : scsi_debug_delay));
  3962. }
  3963. static int
  3964. sdebug_queuecommand_lock_or_not(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  3965. {
  3966. if (scsi_debug_host_lock) {
  3967. unsigned long iflags;
  3968. int rc;
  3969. spin_lock_irqsave(shost->host_lock, iflags);
  3970. rc = scsi_debug_queuecommand(cmd);
  3971. spin_unlock_irqrestore(shost->host_lock, iflags);
  3972. return rc;
  3973. } else
  3974. return scsi_debug_queuecommand(cmd);
  3975. }
  3976. static int
  3977. sdebug_change_qdepth(struct scsi_device *sdev, int qdepth, int reason)
  3978. {
  3979. int num_in_q = 0;
  3980. int bad = 0;
  3981. unsigned long iflags;
  3982. struct sdebug_dev_info *devip;
  3983. spin_lock_irqsave(&queued_arr_lock, iflags);
  3984. devip = (struct sdebug_dev_info *)sdev->hostdata;
  3985. if (NULL == devip) {
  3986. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  3987. return -ENODEV;
  3988. }
  3989. num_in_q = atomic_read(&devip->num_in_q);
  3990. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  3991. if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
  3992. if (qdepth < 1)
  3993. qdepth = 1;
  3994. /* allow to exceed max host queued_arr elements for testing */
  3995. if (qdepth > SCSI_DEBUG_CANQUEUE + 10)
  3996. qdepth = SCSI_DEBUG_CANQUEUE + 10;
  3997. scsi_adjust_queue_depth(sdev, qdepth);
  3998. } else if (reason == SCSI_QDEPTH_QFULL)
  3999. scsi_track_queue_full(sdev, qdepth);
  4000. else
  4001. bad = 1;
  4002. if (bad)
  4003. sdev_printk(KERN_WARNING, sdev,
  4004. "%s: unknown reason=0x%x\n", __func__, reason);
  4005. if (SCSI_DEBUG_OPT_Q_NOISE & scsi_debug_opts) {
  4006. if (SCSI_QDEPTH_QFULL == reason)
  4007. sdev_printk(KERN_INFO, sdev,
  4008. "%s: -> %d, num_in_q=%d, reason: queue full\n",
  4009. __func__, qdepth, num_in_q);
  4010. else {
  4011. const char *cp;
  4012. switch (reason) {
  4013. case SCSI_QDEPTH_DEFAULT:
  4014. cp = "default (sysfs ?)";
  4015. break;
  4016. case SCSI_QDEPTH_RAMP_UP:
  4017. cp = "ramp up";
  4018. break;
  4019. default:
  4020. cp = "unknown";
  4021. break;
  4022. }
  4023. sdev_printk(KERN_INFO, sdev,
  4024. "%s: qdepth=%d, num_in_q=%d, reason: %s\n",
  4025. __func__, qdepth, num_in_q, cp);
  4026. }
  4027. }
  4028. return sdev->queue_depth;
  4029. }
  4030. static int
  4031. sdebug_change_qtype(struct scsi_device *sdev, int qtype)
  4032. {
  4033. qtype = scsi_change_queue_type(sdev, qtype);
  4034. if (SCSI_DEBUG_OPT_Q_NOISE & scsi_debug_opts) {
  4035. const char *cp;
  4036. switch (qtype) {
  4037. case 0:
  4038. cp = "untagged";
  4039. break;
  4040. case MSG_SIMPLE_TAG:
  4041. cp = "simple tags";
  4042. break;
  4043. case MSG_ORDERED_TAG:
  4044. cp = "ordered tags";
  4045. break;
  4046. default:
  4047. cp = "unknown";
  4048. break;
  4049. }
  4050. sdev_printk(KERN_INFO, sdev, "%s: to %s\n", __func__, cp);
  4051. }
  4052. return qtype;
  4053. }
  4054. static struct scsi_host_template sdebug_driver_template = {
  4055. .show_info = scsi_debug_show_info,
  4056. .write_info = scsi_debug_write_info,
  4057. .proc_name = sdebug_proc_name,
  4058. .name = "SCSI DEBUG",
  4059. .info = scsi_debug_info,
  4060. .slave_alloc = scsi_debug_slave_alloc,
  4061. .slave_configure = scsi_debug_slave_configure,
  4062. .slave_destroy = scsi_debug_slave_destroy,
  4063. .ioctl = scsi_debug_ioctl,
  4064. .queuecommand = sdebug_queuecommand_lock_or_not,
  4065. .change_queue_depth = sdebug_change_qdepth,
  4066. .change_queue_type = sdebug_change_qtype,
  4067. .eh_abort_handler = scsi_debug_abort,
  4068. .eh_device_reset_handler = scsi_debug_device_reset,
  4069. .eh_target_reset_handler = scsi_debug_target_reset,
  4070. .eh_bus_reset_handler = scsi_debug_bus_reset,
  4071. .eh_host_reset_handler = scsi_debug_host_reset,
  4072. .can_queue = SCSI_DEBUG_CANQUEUE,
  4073. .this_id = 7,
  4074. .sg_tablesize = SCSI_MAX_SG_CHAIN_SEGMENTS,
  4075. .cmd_per_lun = DEF_CMD_PER_LUN,
  4076. .max_sectors = -1U,
  4077. .use_clustering = DISABLE_CLUSTERING,
  4078. .module = THIS_MODULE,
  4079. };
  4080. static int sdebug_driver_probe(struct device * dev)
  4081. {
  4082. int error = 0;
  4083. struct sdebug_host_info *sdbg_host;
  4084. struct Scsi_Host *hpnt;
  4085. int host_prot;
  4086. sdbg_host = to_sdebug_host(dev);
  4087. sdebug_driver_template.can_queue = scsi_debug_max_queue;
  4088. if (scsi_debug_clustering)
  4089. sdebug_driver_template.use_clustering = ENABLE_CLUSTERING;
  4090. hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
  4091. if (NULL == hpnt) {
  4092. printk(KERN_ERR "%s: scsi_register failed\n", __func__);
  4093. error = -ENODEV;
  4094. return error;
  4095. }
  4096. sdbg_host->shost = hpnt;
  4097. *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
  4098. if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
  4099. hpnt->max_id = scsi_debug_num_tgts + 1;
  4100. else
  4101. hpnt->max_id = scsi_debug_num_tgts;
  4102. hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* = scsi_debug_max_luns; */
  4103. host_prot = 0;
  4104. switch (scsi_debug_dif) {
  4105. case SD_DIF_TYPE1_PROTECTION:
  4106. host_prot = SHOST_DIF_TYPE1_PROTECTION;
  4107. if (scsi_debug_dix)
  4108. host_prot |= SHOST_DIX_TYPE1_PROTECTION;
  4109. break;
  4110. case SD_DIF_TYPE2_PROTECTION:
  4111. host_prot = SHOST_DIF_TYPE2_PROTECTION;
  4112. if (scsi_debug_dix)
  4113. host_prot |= SHOST_DIX_TYPE2_PROTECTION;
  4114. break;
  4115. case SD_DIF_TYPE3_PROTECTION:
  4116. host_prot = SHOST_DIF_TYPE3_PROTECTION;
  4117. if (scsi_debug_dix)
  4118. host_prot |= SHOST_DIX_TYPE3_PROTECTION;
  4119. break;
  4120. default:
  4121. if (scsi_debug_dix)
  4122. host_prot |= SHOST_DIX_TYPE0_PROTECTION;
  4123. break;
  4124. }
  4125. scsi_host_set_prot(hpnt, host_prot);
  4126. printk(KERN_INFO "scsi_debug: host protection%s%s%s%s%s%s%s\n",
  4127. (host_prot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
  4128. (host_prot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
  4129. (host_prot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
  4130. (host_prot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
  4131. (host_prot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
  4132. (host_prot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
  4133. (host_prot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
  4134. if (scsi_debug_guard == 1)
  4135. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
  4136. else
  4137. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
  4138. error = scsi_add_host(hpnt, &sdbg_host->dev);
  4139. if (error) {
  4140. printk(KERN_ERR "%s: scsi_add_host failed\n", __func__);
  4141. error = -ENODEV;
  4142. scsi_host_put(hpnt);
  4143. } else
  4144. scsi_scan_host(hpnt);
  4145. return error;
  4146. }
  4147. static int sdebug_driver_remove(struct device * dev)
  4148. {
  4149. struct sdebug_host_info *sdbg_host;
  4150. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  4151. sdbg_host = to_sdebug_host(dev);
  4152. if (!sdbg_host) {
  4153. printk(KERN_ERR "%s: Unable to locate host info\n",
  4154. __func__);
  4155. return -ENODEV;
  4156. }
  4157. scsi_remove_host(sdbg_host->shost);
  4158. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  4159. dev_list) {
  4160. list_del(&sdbg_devinfo->dev_list);
  4161. kfree(sdbg_devinfo);
  4162. }
  4163. scsi_host_put(sdbg_host->shost);
  4164. return 0;
  4165. }
  4166. static int pseudo_lld_bus_match(struct device *dev,
  4167. struct device_driver *dev_driver)
  4168. {
  4169. return 1;
  4170. }
  4171. static struct bus_type pseudo_lld_bus = {
  4172. .name = "pseudo",
  4173. .match = pseudo_lld_bus_match,
  4174. .probe = sdebug_driver_probe,
  4175. .remove = sdebug_driver_remove,
  4176. .drv_groups = sdebug_drv_groups,
  4177. };