scsi_debug.c 162 KB

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