scsi_debug.c 133 KB

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