scsi_debug.c 152 KB

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