scsi_debug.c 162 KB

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