scsi_debug.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095
  1. /*
  2. * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  3. * Copyright (C) 1992 Eric Youngdale
  4. * Simulate a host adapter with 2 disks attached. Do a lot of checking
  5. * to make sure that we are not getting blocks mixed up, and PANIC if
  6. * anything out of the ordinary is seen.
  7. * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. *
  9. * This version is more generic, simulating a variable number of disk
  10. * (or disk like devices) sharing a common amount of RAM. To be more
  11. * realistic, the simulated devices have the transport attributes of
  12. * SAS disks.
  13. *
  14. *
  15. * For documentation see http://www.torque.net/sg/sdebug26.html
  16. *
  17. * D. Gilbert (dpg) work for Magneto-Optical device test [20010421]
  18. * dpg: work for devfs large number of disks [20010809]
  19. * forked for lk 2.5 series [20011216, 20020101]
  20. * use vmalloc() more inquiry+mode_sense [20020302]
  21. * add timers for delayed responses [20020721]
  22. * Patrick Mansfield <patmans@us.ibm.com> max_luns+scsi_level [20021031]
  23. * Mike Anderson <andmike@us.ibm.com> sysfs work [20021118]
  24. * dpg: change style of boot options to "scsi_debug.num_tgts=2" and
  25. * module options to "modprobe scsi_debug num_tgts=2" [20021221]
  26. */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/timer.h>
  32. #include <linux/types.h>
  33. #include <linux/string.h>
  34. #include <linux/genhd.h>
  35. #include <linux/fs.h>
  36. #include <linux/init.h>
  37. #include <linux/proc_fs.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/blkdev.h>
  42. #include "scsi.h"
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsicam.h>
  45. #include <linux/stat.h>
  46. #include "scsi_logging.h"
  47. #include "scsi_debug.h"
  48. #define SCSI_DEBUG_VERSION "1.80"
  49. static const char * scsi_debug_version_date = "20061018";
  50. /* Additional Sense Code (ASC) used */
  51. #define NO_ADDITIONAL_SENSE 0x0
  52. #define LOGICAL_UNIT_NOT_READY 0x4
  53. #define UNRECOVERED_READ_ERR 0x11
  54. #define PARAMETER_LIST_LENGTH_ERR 0x1a
  55. #define INVALID_OPCODE 0x20
  56. #define ADDR_OUT_OF_RANGE 0x21
  57. #define INVALID_FIELD_IN_CDB 0x24
  58. #define INVALID_FIELD_IN_PARAM_LIST 0x26
  59. #define POWERON_RESET 0x29
  60. #define SAVING_PARAMS_UNSUP 0x39
  61. #define THRESHOLD_EXCEEDED 0x5d
  62. #define LOW_POWER_COND_ON 0x5e
  63. #define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
  64. /* Default values for driver parameters */
  65. #define DEF_NUM_HOST 1
  66. #define DEF_NUM_TGTS 1
  67. #define DEF_MAX_LUNS 1
  68. /* With these defaults, this driver will make 1 host with 1 target
  69. * (id 0) containing 1 logical unit (lun 0). That is 1 device.
  70. */
  71. #define DEF_DELAY 1
  72. #define DEF_DEV_SIZE_MB 8
  73. #define DEF_EVERY_NTH 0
  74. #define DEF_NUM_PARTS 0
  75. #define DEF_OPTS 0
  76. #define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */
  77. #define DEF_PTYPE 0
  78. #define DEF_D_SENSE 0
  79. #define DEF_NO_LUN_0 0
  80. #define DEF_VIRTUAL_GB 0
  81. #define DEF_FAKE_RW 0
  82. #define DEF_VPD_USE_HOSTNO 1
  83. /* bit mask values for scsi_debug_opts */
  84. #define SCSI_DEBUG_OPT_NOISE 1
  85. #define SCSI_DEBUG_OPT_MEDIUM_ERR 2
  86. #define SCSI_DEBUG_OPT_TIMEOUT 4
  87. #define SCSI_DEBUG_OPT_RECOVERED_ERR 8
  88. /* When "every_nth" > 0 then modulo "every_nth" commands:
  89. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  90. * - a RECOVERED_ERROR is simulated on successful read and write
  91. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  92. *
  93. * When "every_nth" < 0 then after "- every_nth" commands:
  94. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  95. * - a RECOVERED_ERROR is simulated on successful read and write
  96. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  97. * This will continue until some other action occurs (e.g. the user
  98. * writing a new value (other than -1 or 1) to every_nth via sysfs).
  99. */
  100. /* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
  101. * sector on read commands: */
  102. #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
  103. /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
  104. * or "peripheral device" addressing (value 0) */
  105. #define SAM2_LUN_ADDRESS_METHOD 0
  106. #define SAM2_WLUN_REPORT_LUNS 0xc101
  107. static int scsi_debug_add_host = DEF_NUM_HOST;
  108. static int scsi_debug_delay = DEF_DELAY;
  109. static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
  110. static int scsi_debug_every_nth = DEF_EVERY_NTH;
  111. static int scsi_debug_max_luns = DEF_MAX_LUNS;
  112. static int scsi_debug_num_parts = DEF_NUM_PARTS;
  113. static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
  114. static int scsi_debug_opts = DEF_OPTS;
  115. static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
  116. static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
  117. static int scsi_debug_dsense = DEF_D_SENSE;
  118. static int scsi_debug_no_lun_0 = DEF_NO_LUN_0;
  119. static int scsi_debug_virtual_gb = DEF_VIRTUAL_GB;
  120. static int scsi_debug_fake_rw = DEF_FAKE_RW;
  121. static int scsi_debug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
  122. static int scsi_debug_cmnd_count = 0;
  123. #define DEV_READONLY(TGT) (0)
  124. #define DEV_REMOVEABLE(TGT) (0)
  125. static unsigned int sdebug_store_size; /* in bytes */
  126. static unsigned int sdebug_store_sectors;
  127. static sector_t sdebug_capacity; /* in sectors */
  128. /* old BIOS stuff, kernel may get rid of them but some mode sense pages
  129. may still need them */
  130. static int sdebug_heads; /* heads per disk */
  131. static int sdebug_cylinders_per; /* cylinders per surface */
  132. static int sdebug_sectors_per; /* sectors per cylinder */
  133. /* default sector size is 512 bytes, 2**9 bytes */
  134. #define POW2_SECT_SIZE 9
  135. #define SECT_SIZE (1 << POW2_SECT_SIZE)
  136. #define SECT_SIZE_PER(TGT) SECT_SIZE
  137. #define SDEBUG_MAX_PARTS 4
  138. #define SDEBUG_SENSE_LEN 32
  139. struct sdebug_dev_info {
  140. struct list_head dev_list;
  141. unsigned char sense_buff[SDEBUG_SENSE_LEN]; /* weak nexus */
  142. unsigned int channel;
  143. unsigned int target;
  144. unsigned int lun;
  145. struct sdebug_host_info *sdbg_host;
  146. unsigned int wlun;
  147. char reset;
  148. char stopped;
  149. char used;
  150. };
  151. struct sdebug_host_info {
  152. struct list_head host_list;
  153. struct Scsi_Host *shost;
  154. struct device dev;
  155. struct list_head dev_info_list;
  156. };
  157. #define to_sdebug_host(d) \
  158. container_of(d, struct sdebug_host_info, dev)
  159. static LIST_HEAD(sdebug_host_list);
  160. static DEFINE_SPINLOCK(sdebug_host_list_lock);
  161. typedef void (* done_funct_t) (struct scsi_cmnd *);
  162. struct sdebug_queued_cmd {
  163. int in_use;
  164. struct timer_list cmnd_timer;
  165. done_funct_t done_funct;
  166. struct scsi_cmnd * a_cmnd;
  167. int scsi_result;
  168. };
  169. static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
  170. static struct scsi_host_template sdebug_driver_template = {
  171. .proc_info = scsi_debug_proc_info,
  172. .name = "SCSI DEBUG",
  173. .info = scsi_debug_info,
  174. .slave_alloc = scsi_debug_slave_alloc,
  175. .slave_configure = scsi_debug_slave_configure,
  176. .slave_destroy = scsi_debug_slave_destroy,
  177. .ioctl = scsi_debug_ioctl,
  178. .queuecommand = scsi_debug_queuecommand,
  179. .eh_abort_handler = scsi_debug_abort,
  180. .eh_bus_reset_handler = scsi_debug_bus_reset,
  181. .eh_device_reset_handler = scsi_debug_device_reset,
  182. .eh_host_reset_handler = scsi_debug_host_reset,
  183. .bios_param = scsi_debug_biosparam,
  184. .can_queue = SCSI_DEBUG_CANQUEUE,
  185. .this_id = 7,
  186. .sg_tablesize = 256,
  187. .cmd_per_lun = 16,
  188. .max_sectors = 0xffff,
  189. .unchecked_isa_dma = 0,
  190. .use_clustering = ENABLE_CLUSTERING,
  191. .module = THIS_MODULE,
  192. };
  193. static unsigned char * fake_storep; /* ramdisk storage */
  194. static int num_aborts = 0;
  195. static int num_dev_resets = 0;
  196. static int num_bus_resets = 0;
  197. static int num_host_resets = 0;
  198. static DEFINE_SPINLOCK(queued_arr_lock);
  199. static DEFINE_RWLOCK(atomic_rw);
  200. static char sdebug_proc_name[] = "scsi_debug";
  201. static int sdebug_driver_probe(struct device *);
  202. static int sdebug_driver_remove(struct device *);
  203. static struct bus_type pseudo_lld_bus;
  204. static struct device_driver sdebug_driverfs_driver = {
  205. .name = sdebug_proc_name,
  206. .bus = &pseudo_lld_bus,
  207. };
  208. static const int check_condition_result =
  209. (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  210. static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  211. 0, 0, 0x2, 0x4b};
  212. static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  213. 0, 0, 0x0, 0x0};
  214. /* function declarations */
  215. static int resp_inquiry(struct scsi_cmnd * SCpnt, int target,
  216. struct sdebug_dev_info * devip);
  217. static int resp_requests(struct scsi_cmnd * SCpnt,
  218. struct sdebug_dev_info * devip);
  219. static int resp_start_stop(struct scsi_cmnd * scp,
  220. struct sdebug_dev_info * devip);
  221. static int resp_report_tgtpgs(struct scsi_cmnd * scp,
  222. struct sdebug_dev_info * devip);
  223. static int resp_readcap(struct scsi_cmnd * SCpnt,
  224. struct sdebug_dev_info * devip);
  225. static int resp_readcap16(struct scsi_cmnd * SCpnt,
  226. struct sdebug_dev_info * devip);
  227. static int resp_mode_sense(struct scsi_cmnd * scp, int target,
  228. struct sdebug_dev_info * devip);
  229. static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
  230. struct sdebug_dev_info * devip);
  231. static int resp_log_sense(struct scsi_cmnd * scp,
  232. struct sdebug_dev_info * devip);
  233. static int resp_read(struct scsi_cmnd * SCpnt, unsigned long long lba,
  234. unsigned int num, struct sdebug_dev_info * devip);
  235. static int resp_write(struct scsi_cmnd * SCpnt, unsigned long long lba,
  236. unsigned int num, struct sdebug_dev_info * devip);
  237. static int resp_report_luns(struct scsi_cmnd * SCpnt,
  238. struct sdebug_dev_info * devip);
  239. static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  240. int arr_len);
  241. static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  242. int max_arr_len);
  243. static void timer_intr_handler(unsigned long);
  244. static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev);
  245. static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
  246. int asc, int asq);
  247. static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
  248. struct sdebug_dev_info * devip);
  249. static int schedule_resp(struct scsi_cmnd * cmnd,
  250. struct sdebug_dev_info * devip,
  251. done_funct_t done, int scsi_result, int delta_jiff);
  252. static void __init sdebug_build_parts(unsigned char * ramp);
  253. static void __init init_all_queued(void);
  254. static void stop_all_queued(void);
  255. static int stop_queued_cmnd(struct scsi_cmnd * cmnd);
  256. static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
  257. int target_dev_id, int dev_id_num,
  258. const char * dev_id_str, int dev_id_str_len);
  259. static int inquiry_evpd_88(unsigned char * arr, int target_dev_id);
  260. static int do_create_driverfs_files(void);
  261. static void do_remove_driverfs_files(void);
  262. static int sdebug_add_adapter(void);
  263. static void sdebug_remove_adapter(void);
  264. static void sdebug_max_tgts_luns(void);
  265. static struct device pseudo_primary;
  266. static struct bus_type pseudo_lld_bus;
  267. static
  268. int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
  269. {
  270. unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
  271. int len, k, j;
  272. unsigned int num;
  273. unsigned long long lba;
  274. int errsts = 0;
  275. int target = SCpnt->device->id;
  276. struct sdebug_dev_info * devip = NULL;
  277. int inj_recovered = 0;
  278. int delay_override = 0;
  279. if (done == NULL)
  280. return 0; /* assume mid level reprocessing command */
  281. SCpnt->resid = 0;
  282. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) {
  283. printk(KERN_INFO "scsi_debug: cmd ");
  284. for (k = 0, len = SCpnt->cmd_len; k < len; ++k)
  285. printk("%02x ", (int)cmd[k]);
  286. printk("\n");
  287. }
  288. if(target == sdebug_driver_template.this_id) {
  289. printk(KERN_INFO "scsi_debug: initiator's id used as "
  290. "target!\n");
  291. return schedule_resp(SCpnt, NULL, done,
  292. DID_NO_CONNECT << 16, 0);
  293. }
  294. if ((SCpnt->device->lun >= scsi_debug_max_luns) &&
  295. (SCpnt->device->lun != SAM2_WLUN_REPORT_LUNS))
  296. return schedule_resp(SCpnt, NULL, done,
  297. DID_NO_CONNECT << 16, 0);
  298. devip = devInfoReg(SCpnt->device);
  299. if (NULL == devip)
  300. return schedule_resp(SCpnt, NULL, done,
  301. DID_NO_CONNECT << 16, 0);
  302. if ((scsi_debug_every_nth != 0) &&
  303. (++scsi_debug_cmnd_count >= abs(scsi_debug_every_nth))) {
  304. scsi_debug_cmnd_count = 0;
  305. if (scsi_debug_every_nth < -1)
  306. scsi_debug_every_nth = -1;
  307. if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
  308. return 0; /* ignore command causing timeout */
  309. else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
  310. inj_recovered = 1; /* to reads and writes below */
  311. }
  312. if (devip->wlun) {
  313. switch (*cmd) {
  314. case INQUIRY:
  315. case REQUEST_SENSE:
  316. case TEST_UNIT_READY:
  317. case REPORT_LUNS:
  318. break; /* only allowable wlun commands */
  319. default:
  320. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  321. printk(KERN_INFO "scsi_debug: Opcode: 0x%x "
  322. "not supported for wlun\n", *cmd);
  323. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  324. INVALID_OPCODE, 0);
  325. errsts = check_condition_result;
  326. return schedule_resp(SCpnt, devip, done, errsts,
  327. 0);
  328. }
  329. }
  330. switch (*cmd) {
  331. case INQUIRY: /* mandatory, ignore unit attention */
  332. delay_override = 1;
  333. errsts = resp_inquiry(SCpnt, target, devip);
  334. break;
  335. case REQUEST_SENSE: /* mandatory, ignore unit attention */
  336. delay_override = 1;
  337. errsts = resp_requests(SCpnt, devip);
  338. break;
  339. case REZERO_UNIT: /* actually this is REWIND for SSC */
  340. case START_STOP:
  341. errsts = resp_start_stop(SCpnt, devip);
  342. break;
  343. case ALLOW_MEDIUM_REMOVAL:
  344. if ((errsts = check_readiness(SCpnt, 1, devip)))
  345. break;
  346. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  347. printk(KERN_INFO "scsi_debug: Medium removal %s\n",
  348. cmd[4] ? "inhibited" : "enabled");
  349. break;
  350. case SEND_DIAGNOSTIC: /* mandatory */
  351. errsts = check_readiness(SCpnt, 1, devip);
  352. break;
  353. case TEST_UNIT_READY: /* mandatory */
  354. delay_override = 1;
  355. errsts = check_readiness(SCpnt, 0, devip);
  356. break;
  357. case RESERVE:
  358. errsts = check_readiness(SCpnt, 1, devip);
  359. break;
  360. case RESERVE_10:
  361. errsts = check_readiness(SCpnt, 1, devip);
  362. break;
  363. case RELEASE:
  364. errsts = check_readiness(SCpnt, 1, devip);
  365. break;
  366. case RELEASE_10:
  367. errsts = check_readiness(SCpnt, 1, devip);
  368. break;
  369. case READ_CAPACITY:
  370. errsts = resp_readcap(SCpnt, devip);
  371. break;
  372. case SERVICE_ACTION_IN:
  373. if (SAI_READ_CAPACITY_16 != cmd[1]) {
  374. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  375. INVALID_OPCODE, 0);
  376. errsts = check_condition_result;
  377. break;
  378. }
  379. errsts = resp_readcap16(SCpnt, devip);
  380. break;
  381. case MAINTENANCE_IN:
  382. if (MI_REPORT_TARGET_PGS != cmd[1]) {
  383. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  384. INVALID_OPCODE, 0);
  385. errsts = check_condition_result;
  386. break;
  387. }
  388. errsts = resp_report_tgtpgs(SCpnt, devip);
  389. break;
  390. case READ_16:
  391. case READ_12:
  392. case READ_10:
  393. case READ_6:
  394. if ((errsts = check_readiness(SCpnt, 0, devip)))
  395. break;
  396. if (scsi_debug_fake_rw)
  397. break;
  398. if ((*cmd) == READ_16) {
  399. for (lba = 0, j = 0; j < 8; ++j) {
  400. if (j > 0)
  401. lba <<= 8;
  402. lba += cmd[2 + j];
  403. }
  404. num = cmd[13] + (cmd[12] << 8) +
  405. (cmd[11] << 16) + (cmd[10] << 24);
  406. } else if ((*cmd) == READ_12) {
  407. lba = cmd[5] + (cmd[4] << 8) +
  408. (cmd[3] << 16) + (cmd[2] << 24);
  409. num = cmd[9] + (cmd[8] << 8) +
  410. (cmd[7] << 16) + (cmd[6] << 24);
  411. } else if ((*cmd) == READ_10) {
  412. lba = cmd[5] + (cmd[4] << 8) +
  413. (cmd[3] << 16) + (cmd[2] << 24);
  414. num = cmd[8] + (cmd[7] << 8);
  415. } else { /* READ (6) */
  416. lba = cmd[3] + (cmd[2] << 8) +
  417. ((cmd[1] & 0x1f) << 16);
  418. num = (0 == cmd[4]) ? 256 : cmd[4];
  419. }
  420. errsts = resp_read(SCpnt, lba, num, devip);
  421. if (inj_recovered && (0 == errsts)) {
  422. mk_sense_buffer(devip, RECOVERED_ERROR,
  423. THRESHOLD_EXCEEDED, 0);
  424. errsts = check_condition_result;
  425. }
  426. break;
  427. case REPORT_LUNS: /* mandatory, ignore unit attention */
  428. delay_override = 1;
  429. errsts = resp_report_luns(SCpnt, devip);
  430. break;
  431. case VERIFY: /* 10 byte SBC-2 command */
  432. errsts = check_readiness(SCpnt, 0, devip);
  433. break;
  434. case WRITE_16:
  435. case WRITE_12:
  436. case WRITE_10:
  437. case WRITE_6:
  438. if ((errsts = check_readiness(SCpnt, 0, devip)))
  439. break;
  440. if (scsi_debug_fake_rw)
  441. break;
  442. if ((*cmd) == WRITE_16) {
  443. for (lba = 0, j = 0; j < 8; ++j) {
  444. if (j > 0)
  445. lba <<= 8;
  446. lba += cmd[2 + j];
  447. }
  448. num = cmd[13] + (cmd[12] << 8) +
  449. (cmd[11] << 16) + (cmd[10] << 24);
  450. } else if ((*cmd) == WRITE_12) {
  451. lba = cmd[5] + (cmd[4] << 8) +
  452. (cmd[3] << 16) + (cmd[2] << 24);
  453. num = cmd[9] + (cmd[8] << 8) +
  454. (cmd[7] << 16) + (cmd[6] << 24);
  455. } else if ((*cmd) == WRITE_10) {
  456. lba = cmd[5] + (cmd[4] << 8) +
  457. (cmd[3] << 16) + (cmd[2] << 24);
  458. num = cmd[8] + (cmd[7] << 8);
  459. } else { /* WRITE (6) */
  460. lba = cmd[3] + (cmd[2] << 8) +
  461. ((cmd[1] & 0x1f) << 16);
  462. num = (0 == cmd[4]) ? 256 : cmd[4];
  463. }
  464. errsts = resp_write(SCpnt, lba, num, devip);
  465. if (inj_recovered && (0 == errsts)) {
  466. mk_sense_buffer(devip, RECOVERED_ERROR,
  467. THRESHOLD_EXCEEDED, 0);
  468. errsts = check_condition_result;
  469. }
  470. break;
  471. case MODE_SENSE:
  472. case MODE_SENSE_10:
  473. errsts = resp_mode_sense(SCpnt, target, devip);
  474. break;
  475. case MODE_SELECT:
  476. errsts = resp_mode_select(SCpnt, 1, devip);
  477. break;
  478. case MODE_SELECT_10:
  479. errsts = resp_mode_select(SCpnt, 0, devip);
  480. break;
  481. case LOG_SENSE:
  482. errsts = resp_log_sense(SCpnt, devip);
  483. break;
  484. case SYNCHRONIZE_CACHE:
  485. delay_override = 1;
  486. errsts = check_readiness(SCpnt, 0, devip);
  487. break;
  488. default:
  489. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  490. printk(KERN_INFO "scsi_debug: Opcode: 0x%x not "
  491. "supported\n", *cmd);
  492. if ((errsts = check_readiness(SCpnt, 1, devip)))
  493. break; /* Unit attention takes precedence */
  494. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
  495. errsts = check_condition_result;
  496. break;
  497. }
  498. return schedule_resp(SCpnt, devip, done, errsts,
  499. (delay_override ? 0 : scsi_debug_delay));
  500. }
  501. static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  502. {
  503. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  504. printk(KERN_INFO "scsi_debug: ioctl: cmd=0x%x\n", cmd);
  505. }
  506. return -EINVAL;
  507. /* return -ENOTTY; // correct return but upsets fdisk */
  508. }
  509. static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
  510. struct sdebug_dev_info * devip)
  511. {
  512. if (devip->reset) {
  513. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  514. printk(KERN_INFO "scsi_debug: Reporting Unit "
  515. "attention: power on reset\n");
  516. devip->reset = 0;
  517. mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0);
  518. return check_condition_result;
  519. }
  520. if ((0 == reset_only) && devip->stopped) {
  521. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  522. printk(KERN_INFO "scsi_debug: Reporting Not "
  523. "ready: initializing command required\n");
  524. mk_sense_buffer(devip, NOT_READY, LOGICAL_UNIT_NOT_READY,
  525. 0x2);
  526. return check_condition_result;
  527. }
  528. return 0;
  529. }
  530. /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
  531. static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  532. int arr_len)
  533. {
  534. int k, req_len, act_len, len, active;
  535. void * kaddr;
  536. void * kaddr_off;
  537. struct scatterlist * sgpnt;
  538. if (0 == scp->request_bufflen)
  539. return 0;
  540. if (NULL == scp->request_buffer)
  541. return (DID_ERROR << 16);
  542. if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
  543. (scp->sc_data_direction == DMA_FROM_DEVICE)))
  544. return (DID_ERROR << 16);
  545. if (0 == scp->use_sg) {
  546. req_len = scp->request_bufflen;
  547. act_len = (req_len < arr_len) ? req_len : arr_len;
  548. memcpy(scp->request_buffer, arr, act_len);
  549. if (scp->resid)
  550. scp->resid -= act_len;
  551. else
  552. scp->resid = req_len - act_len;
  553. return 0;
  554. }
  555. sgpnt = (struct scatterlist *)scp->request_buffer;
  556. active = 1;
  557. for (k = 0, req_len = 0, act_len = 0; k < scp->use_sg; ++k, ++sgpnt) {
  558. if (active) {
  559. kaddr = (unsigned char *)
  560. kmap_atomic(sgpnt->page, KM_USER0);
  561. if (NULL == kaddr)
  562. return (DID_ERROR << 16);
  563. kaddr_off = (unsigned char *)kaddr + sgpnt->offset;
  564. len = sgpnt->length;
  565. if ((req_len + len) > arr_len) {
  566. active = 0;
  567. len = arr_len - req_len;
  568. }
  569. memcpy(kaddr_off, arr + req_len, len);
  570. kunmap_atomic(kaddr, KM_USER0);
  571. act_len += len;
  572. }
  573. req_len += sgpnt->length;
  574. }
  575. if (scp->resid)
  576. scp->resid -= act_len;
  577. else
  578. scp->resid = req_len - act_len;
  579. return 0;
  580. }
  581. /* Returns number of bytes fetched into 'arr' or -1 if error. */
  582. static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  583. int max_arr_len)
  584. {
  585. int k, req_len, len, fin;
  586. void * kaddr;
  587. void * kaddr_off;
  588. struct scatterlist * sgpnt;
  589. if (0 == scp->request_bufflen)
  590. return 0;
  591. if (NULL == scp->request_buffer)
  592. return -1;
  593. if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
  594. (scp->sc_data_direction == DMA_TO_DEVICE)))
  595. return -1;
  596. if (0 == scp->use_sg) {
  597. req_len = scp->request_bufflen;
  598. len = (req_len < max_arr_len) ? req_len : max_arr_len;
  599. memcpy(arr, scp->request_buffer, len);
  600. return len;
  601. }
  602. sgpnt = (struct scatterlist *)scp->request_buffer;
  603. for (k = 0, req_len = 0, fin = 0; k < scp->use_sg; ++k, ++sgpnt) {
  604. kaddr = (unsigned char *)kmap_atomic(sgpnt->page, KM_USER0);
  605. if (NULL == kaddr)
  606. return -1;
  607. kaddr_off = (unsigned char *)kaddr + sgpnt->offset;
  608. len = sgpnt->length;
  609. if ((req_len + len) > max_arr_len) {
  610. len = max_arr_len - req_len;
  611. fin = 1;
  612. }
  613. memcpy(arr + req_len, kaddr_off, len);
  614. kunmap_atomic(kaddr, KM_USER0);
  615. if (fin)
  616. return req_len + len;
  617. req_len += sgpnt->length;
  618. }
  619. return req_len;
  620. }
  621. static const char * inq_vendor_id = "Linux ";
  622. static const char * inq_product_id = "scsi_debug ";
  623. static const char * inq_product_rev = "0004";
  624. static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
  625. int target_dev_id, int dev_id_num,
  626. const char * dev_id_str,
  627. int dev_id_str_len)
  628. {
  629. int num, port_a;
  630. char b[32];
  631. port_a = target_dev_id + 1;
  632. /* T10 vendor identifier field format (faked) */
  633. arr[0] = 0x2; /* ASCII */
  634. arr[1] = 0x1;
  635. arr[2] = 0x0;
  636. memcpy(&arr[4], inq_vendor_id, 8);
  637. memcpy(&arr[12], inq_product_id, 16);
  638. memcpy(&arr[28], dev_id_str, dev_id_str_len);
  639. num = 8 + 16 + dev_id_str_len;
  640. arr[3] = num;
  641. num += 4;
  642. if (dev_id_num >= 0) {
  643. /* NAA-5, Logical unit identifier (binary) */
  644. arr[num++] = 0x1; /* binary (not necessarily sas) */
  645. arr[num++] = 0x3; /* PIV=0, lu, naa */
  646. arr[num++] = 0x0;
  647. arr[num++] = 0x8;
  648. arr[num++] = 0x53; /* naa-5 ieee company id=0x333333 (fake) */
  649. arr[num++] = 0x33;
  650. arr[num++] = 0x33;
  651. arr[num++] = 0x30;
  652. arr[num++] = (dev_id_num >> 24);
  653. arr[num++] = (dev_id_num >> 16) & 0xff;
  654. arr[num++] = (dev_id_num >> 8) & 0xff;
  655. arr[num++] = dev_id_num & 0xff;
  656. /* Target relative port number */
  657. arr[num++] = 0x61; /* proto=sas, binary */
  658. arr[num++] = 0x94; /* PIV=1, target port, rel port */
  659. arr[num++] = 0x0; /* reserved */
  660. arr[num++] = 0x4; /* length */
  661. arr[num++] = 0x0; /* reserved */
  662. arr[num++] = 0x0; /* reserved */
  663. arr[num++] = 0x0;
  664. arr[num++] = 0x1; /* relative port A */
  665. }
  666. /* NAA-5, Target port identifier */
  667. arr[num++] = 0x61; /* proto=sas, binary */
  668. arr[num++] = 0x93; /* piv=1, target port, naa */
  669. arr[num++] = 0x0;
  670. arr[num++] = 0x8;
  671. arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
  672. arr[num++] = 0x22;
  673. arr[num++] = 0x22;
  674. arr[num++] = 0x20;
  675. arr[num++] = (port_a >> 24);
  676. arr[num++] = (port_a >> 16) & 0xff;
  677. arr[num++] = (port_a >> 8) & 0xff;
  678. arr[num++] = port_a & 0xff;
  679. /* NAA-5, Target port group identifier */
  680. arr[num++] = 0x61; /* proto=sas, binary */
  681. arr[num++] = 0x95; /* piv=1, target port group id */
  682. arr[num++] = 0x0;
  683. arr[num++] = 0x4;
  684. arr[num++] = 0;
  685. arr[num++] = 0;
  686. arr[num++] = (port_group_id >> 8) & 0xff;
  687. arr[num++] = port_group_id & 0xff;
  688. /* NAA-5, Target device identifier */
  689. arr[num++] = 0x61; /* proto=sas, binary */
  690. arr[num++] = 0xa3; /* piv=1, target device, naa */
  691. arr[num++] = 0x0;
  692. arr[num++] = 0x8;
  693. arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
  694. arr[num++] = 0x22;
  695. arr[num++] = 0x22;
  696. arr[num++] = 0x20;
  697. arr[num++] = (target_dev_id >> 24);
  698. arr[num++] = (target_dev_id >> 16) & 0xff;
  699. arr[num++] = (target_dev_id >> 8) & 0xff;
  700. arr[num++] = target_dev_id & 0xff;
  701. /* SCSI name string: Target device identifier */
  702. arr[num++] = 0x63; /* proto=sas, UTF-8 */
  703. arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
  704. arr[num++] = 0x0;
  705. arr[num++] = 24;
  706. memcpy(arr + num, "naa.52222220", 12);
  707. num += 12;
  708. snprintf(b, sizeof(b), "%08X", target_dev_id);
  709. memcpy(arr + num, b, 8);
  710. num += 8;
  711. memset(arr + num, 0, 4);
  712. num += 4;
  713. return num;
  714. }
  715. static unsigned char vpd84_data[] = {
  716. /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
  717. 0x22,0x22,0x22,0x0,0xbb,0x1,
  718. 0x22,0x22,0x22,0x0,0xbb,0x2,
  719. };
  720. static int inquiry_evpd_84(unsigned char * arr)
  721. {
  722. memcpy(arr, vpd84_data, sizeof(vpd84_data));
  723. return sizeof(vpd84_data);
  724. }
  725. static int inquiry_evpd_85(unsigned char * arr)
  726. {
  727. int num = 0;
  728. const char * na1 = "https://www.kernel.org/config";
  729. const char * na2 = "http://www.kernel.org/log";
  730. int plen, olen;
  731. arr[num++] = 0x1; /* lu, storage config */
  732. arr[num++] = 0x0; /* reserved */
  733. arr[num++] = 0x0;
  734. olen = strlen(na1);
  735. plen = olen + 1;
  736. if (plen % 4)
  737. plen = ((plen / 4) + 1) * 4;
  738. arr[num++] = plen; /* length, null termianted, padded */
  739. memcpy(arr + num, na1, olen);
  740. memset(arr + num + olen, 0, plen - olen);
  741. num += plen;
  742. arr[num++] = 0x4; /* lu, logging */
  743. arr[num++] = 0x0; /* reserved */
  744. arr[num++] = 0x0;
  745. olen = strlen(na2);
  746. plen = olen + 1;
  747. if (plen % 4)
  748. plen = ((plen / 4) + 1) * 4;
  749. arr[num++] = plen; /* length, null terminated, padded */
  750. memcpy(arr + num, na2, olen);
  751. memset(arr + num + olen, 0, plen - olen);
  752. num += plen;
  753. return num;
  754. }
  755. /* SCSI ports VPD page */
  756. static int inquiry_evpd_88(unsigned char * arr, int target_dev_id)
  757. {
  758. int num = 0;
  759. int port_a, port_b;
  760. port_a = target_dev_id + 1;
  761. port_b = port_a + 1;
  762. arr[num++] = 0x0; /* reserved */
  763. arr[num++] = 0x0; /* reserved */
  764. arr[num++] = 0x0;
  765. arr[num++] = 0x1; /* relative port 1 (primary) */
  766. memset(arr + num, 0, 6);
  767. num += 6;
  768. arr[num++] = 0x0;
  769. arr[num++] = 12; /* length tp descriptor */
  770. /* naa-5 target port identifier (A) */
  771. arr[num++] = 0x61; /* proto=sas, binary */
  772. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  773. arr[num++] = 0x0; /* reserved */
  774. arr[num++] = 0x8; /* length */
  775. arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
  776. arr[num++] = 0x22;
  777. arr[num++] = 0x22;
  778. arr[num++] = 0x20;
  779. arr[num++] = (port_a >> 24);
  780. arr[num++] = (port_a >> 16) & 0xff;
  781. arr[num++] = (port_a >> 8) & 0xff;
  782. arr[num++] = port_a & 0xff;
  783. arr[num++] = 0x0; /* reserved */
  784. arr[num++] = 0x0; /* reserved */
  785. arr[num++] = 0x0;
  786. arr[num++] = 0x2; /* relative port 2 (secondary) */
  787. memset(arr + num, 0, 6);
  788. num += 6;
  789. arr[num++] = 0x0;
  790. arr[num++] = 12; /* length tp descriptor */
  791. /* naa-5 target port identifier (B) */
  792. arr[num++] = 0x61; /* proto=sas, binary */
  793. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  794. arr[num++] = 0x0; /* reserved */
  795. arr[num++] = 0x8; /* length */
  796. arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
  797. arr[num++] = 0x22;
  798. arr[num++] = 0x22;
  799. arr[num++] = 0x20;
  800. arr[num++] = (port_b >> 24);
  801. arr[num++] = (port_b >> 16) & 0xff;
  802. arr[num++] = (port_b >> 8) & 0xff;
  803. arr[num++] = port_b & 0xff;
  804. return num;
  805. }
  806. static unsigned char vpd89_data[] = {
  807. /* from 4th byte */ 0,0,0,0,
  808. 'l','i','n','u','x',' ',' ',' ',
  809. 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
  810. '1','2','3','4',
  811. 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  812. 0xec,0,0,0,
  813. 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
  814. 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
  815. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
  816. 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
  817. 0x53,0x41,
  818. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  819. 0x20,0x20,
  820. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  821. 0x10,0x80,
  822. 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
  823. 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
  824. 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
  825. 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
  826. 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
  827. 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
  828. 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
  829. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  830. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  831. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  832. 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
  833. 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
  834. 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
  835. 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
  836. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  837. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  838. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  839. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  840. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  841. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  842. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  843. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  844. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  845. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  846. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  847. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
  848. };
  849. static int inquiry_evpd_89(unsigned char * arr)
  850. {
  851. memcpy(arr, vpd89_data, sizeof(vpd89_data));
  852. return sizeof(vpd89_data);
  853. }
  854. static unsigned char vpdb0_data[] = {
  855. /* from 4th byte */ 0,0,0,4,
  856. 0,0,0x4,0,
  857. 0,0,0,64,
  858. };
  859. static int inquiry_evpd_b0(unsigned char * arr)
  860. {
  861. memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
  862. if (sdebug_store_sectors > 0x400) {
  863. arr[4] = (sdebug_store_sectors >> 24) & 0xff;
  864. arr[5] = (sdebug_store_sectors >> 16) & 0xff;
  865. arr[6] = (sdebug_store_sectors >> 8) & 0xff;
  866. arr[7] = sdebug_store_sectors & 0xff;
  867. }
  868. return sizeof(vpdb0_data);
  869. }
  870. #define SDEBUG_LONG_INQ_SZ 96
  871. #define SDEBUG_MAX_INQ_ARR_SZ 584
  872. static int resp_inquiry(struct scsi_cmnd * scp, int target,
  873. struct sdebug_dev_info * devip)
  874. {
  875. unsigned char pq_pdt;
  876. unsigned char * arr;
  877. unsigned char *cmd = (unsigned char *)scp->cmnd;
  878. int alloc_len, n, ret;
  879. alloc_len = (cmd[3] << 8) + cmd[4];
  880. arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_KERNEL);
  881. if (devip->wlun)
  882. pq_pdt = 0x1e; /* present, wlun */
  883. else if (scsi_debug_no_lun_0 && (0 == devip->lun))
  884. pq_pdt = 0x7f; /* not present, no device type */
  885. else
  886. pq_pdt = (scsi_debug_ptype & 0x1f);
  887. arr[0] = pq_pdt;
  888. if (0x2 & cmd[1]) { /* CMDDT bit set */
  889. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  890. 0);
  891. kfree(arr);
  892. return check_condition_result;
  893. } else if (0x1 & cmd[1]) { /* EVPD bit set */
  894. int lu_id_num, port_group_id, target_dev_id, len;
  895. char lu_id_str[6];
  896. int host_no = devip->sdbg_host->shost->host_no;
  897. port_group_id = (((host_no + 1) & 0x7f) << 8) +
  898. (devip->channel & 0x7f);
  899. if (0 == scsi_debug_vpd_use_hostno)
  900. host_no = 0;
  901. lu_id_num = devip->wlun ? -1 : (((host_no + 1) * 2000) +
  902. (devip->target * 1000) + devip->lun);
  903. target_dev_id = ((host_no + 1) * 2000) +
  904. (devip->target * 1000) - 3;
  905. len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
  906. if (0 == cmd[2]) { /* supported vital product data pages */
  907. arr[1] = cmd[2]; /*sanity */
  908. n = 4;
  909. arr[n++] = 0x0; /* this page */
  910. arr[n++] = 0x80; /* unit serial number */
  911. arr[n++] = 0x83; /* device identification */
  912. arr[n++] = 0x84; /* software interface ident. */
  913. arr[n++] = 0x85; /* management network addresses */
  914. arr[n++] = 0x86; /* extended inquiry */
  915. arr[n++] = 0x87; /* mode page policy */
  916. arr[n++] = 0x88; /* SCSI ports */
  917. arr[n++] = 0x89; /* ATA information */
  918. arr[n++] = 0xb0; /* Block limits (SBC) */
  919. arr[3] = n - 4; /* number of supported VPD pages */
  920. } else if (0x80 == cmd[2]) { /* unit serial number */
  921. arr[1] = cmd[2]; /*sanity */
  922. arr[3] = len;
  923. memcpy(&arr[4], lu_id_str, len);
  924. } else if (0x83 == cmd[2]) { /* device identification */
  925. arr[1] = cmd[2]; /*sanity */
  926. arr[3] = inquiry_evpd_83(&arr[4], port_group_id,
  927. target_dev_id, lu_id_num,
  928. lu_id_str, len);
  929. } else if (0x84 == cmd[2]) { /* Software interface ident. */
  930. arr[1] = cmd[2]; /*sanity */
  931. arr[3] = inquiry_evpd_84(&arr[4]);
  932. } else if (0x85 == cmd[2]) { /* Management network addresses */
  933. arr[1] = cmd[2]; /*sanity */
  934. arr[3] = inquiry_evpd_85(&arr[4]);
  935. } else if (0x86 == cmd[2]) { /* extended inquiry */
  936. arr[1] = cmd[2]; /*sanity */
  937. arr[3] = 0x3c; /* number of following entries */
  938. arr[4] = 0x0; /* no protection stuff */
  939. arr[5] = 0x7; /* head of q, ordered + simple q's */
  940. } else if (0x87 == cmd[2]) { /* mode page policy */
  941. arr[1] = cmd[2]; /*sanity */
  942. arr[3] = 0x8; /* number of following entries */
  943. arr[4] = 0x2; /* disconnect-reconnect mp */
  944. arr[6] = 0x80; /* mlus, shared */
  945. arr[8] = 0x18; /* protocol specific lu */
  946. arr[10] = 0x82; /* mlus, per initiator port */
  947. } else if (0x88 == cmd[2]) { /* SCSI Ports */
  948. arr[1] = cmd[2]; /*sanity */
  949. arr[3] = inquiry_evpd_88(&arr[4], target_dev_id);
  950. } else if (0x89 == cmd[2]) { /* ATA information */
  951. arr[1] = cmd[2]; /*sanity */
  952. n = inquiry_evpd_89(&arr[4]);
  953. arr[2] = (n >> 8);
  954. arr[3] = (n & 0xff);
  955. } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */
  956. arr[1] = cmd[2]; /*sanity */
  957. arr[3] = inquiry_evpd_b0(&arr[4]);
  958. } else {
  959. /* Illegal request, invalid field in cdb */
  960. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  961. INVALID_FIELD_IN_CDB, 0);
  962. kfree(arr);
  963. return check_condition_result;
  964. }
  965. len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
  966. ret = fill_from_dev_buffer(scp, arr,
  967. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  968. kfree(arr);
  969. return ret;
  970. }
  971. /* drops through here for a standard inquiry */
  972. arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */
  973. arr[2] = scsi_debug_scsi_level;
  974. arr[3] = 2; /* response_data_format==2 */
  975. arr[4] = SDEBUG_LONG_INQ_SZ - 5;
  976. if (0 == scsi_debug_vpd_use_hostno)
  977. arr[5] = 0x10; /* claim: implicit TGPS */
  978. arr[6] = 0x10; /* claim: MultiP */
  979. /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
  980. arr[7] = 0xa; /* claim: LINKED + CMDQUE */
  981. memcpy(&arr[8], inq_vendor_id, 8);
  982. memcpy(&arr[16], inq_product_id, 16);
  983. memcpy(&arr[32], inq_product_rev, 4);
  984. /* version descriptors (2 bytes each) follow */
  985. arr[58] = 0x0; arr[59] = 0x77; /* SAM-3 ANSI */
  986. arr[60] = 0x3; arr[61] = 0x14; /* SPC-3 ANSI */
  987. n = 62;
  988. if (scsi_debug_ptype == 0) {
  989. arr[n++] = 0x3; arr[n++] = 0x3d; /* SBC-2 ANSI */
  990. } else if (scsi_debug_ptype == 1) {
  991. arr[n++] = 0x3; arr[n++] = 0x60; /* SSC-2 no version */
  992. }
  993. arr[n++] = 0xc; arr[n++] = 0xf; /* SAS-1.1 rev 10 */
  994. ret = fill_from_dev_buffer(scp, arr,
  995. min(alloc_len, SDEBUG_LONG_INQ_SZ));
  996. kfree(arr);
  997. return ret;
  998. }
  999. static int resp_requests(struct scsi_cmnd * scp,
  1000. struct sdebug_dev_info * devip)
  1001. {
  1002. unsigned char * sbuff;
  1003. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1004. unsigned char arr[SDEBUG_SENSE_LEN];
  1005. int want_dsense;
  1006. int len = 18;
  1007. memset(arr, 0, sizeof(arr));
  1008. if (devip->reset == 1)
  1009. mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
  1010. want_dsense = !!(cmd[1] & 1) || scsi_debug_dsense;
  1011. sbuff = devip->sense_buff;
  1012. if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
  1013. if (want_dsense) {
  1014. arr[0] = 0x72;
  1015. arr[1] = 0x0; /* NO_SENSE in sense_key */
  1016. arr[2] = THRESHOLD_EXCEEDED;
  1017. arr[3] = 0xff; /* TEST set and MRIE==6 */
  1018. } else {
  1019. arr[0] = 0x70;
  1020. arr[2] = 0x0; /* NO_SENSE in sense_key */
  1021. arr[7] = 0xa; /* 18 byte sense buffer */
  1022. arr[12] = THRESHOLD_EXCEEDED;
  1023. arr[13] = 0xff; /* TEST set and MRIE==6 */
  1024. }
  1025. } else {
  1026. memcpy(arr, sbuff, SDEBUG_SENSE_LEN);
  1027. if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
  1028. /* DESC bit set and sense_buff in fixed format */
  1029. memset(arr, 0, sizeof(arr));
  1030. arr[0] = 0x72;
  1031. arr[1] = sbuff[2]; /* sense key */
  1032. arr[2] = sbuff[12]; /* asc */
  1033. arr[3] = sbuff[13]; /* ascq */
  1034. len = 8;
  1035. }
  1036. }
  1037. mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
  1038. return fill_from_dev_buffer(scp, arr, len);
  1039. }
  1040. static int resp_start_stop(struct scsi_cmnd * scp,
  1041. struct sdebug_dev_info * devip)
  1042. {
  1043. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1044. int power_cond, errsts, start;
  1045. if ((errsts = check_readiness(scp, 1, devip)))
  1046. return errsts;
  1047. power_cond = (cmd[4] & 0xf0) >> 4;
  1048. if (power_cond) {
  1049. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1050. 0);
  1051. return check_condition_result;
  1052. }
  1053. start = cmd[4] & 1;
  1054. if (start == devip->stopped)
  1055. devip->stopped = !start;
  1056. return 0;
  1057. }
  1058. #define SDEBUG_READCAP_ARR_SZ 8
  1059. static int resp_readcap(struct scsi_cmnd * scp,
  1060. struct sdebug_dev_info * devip)
  1061. {
  1062. unsigned char arr[SDEBUG_READCAP_ARR_SZ];
  1063. unsigned int capac;
  1064. int errsts;
  1065. if ((errsts = check_readiness(scp, 1, devip)))
  1066. return errsts;
  1067. /* following just in case virtual_gb changed */
  1068. if (scsi_debug_virtual_gb > 0) {
  1069. sdebug_capacity = 2048 * 1024;
  1070. sdebug_capacity *= scsi_debug_virtual_gb;
  1071. } else
  1072. sdebug_capacity = sdebug_store_sectors;
  1073. memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
  1074. if (sdebug_capacity < 0xffffffff) {
  1075. capac = (unsigned int)sdebug_capacity - 1;
  1076. arr[0] = (capac >> 24);
  1077. arr[1] = (capac >> 16) & 0xff;
  1078. arr[2] = (capac >> 8) & 0xff;
  1079. arr[3] = capac & 0xff;
  1080. } else {
  1081. arr[0] = 0xff;
  1082. arr[1] = 0xff;
  1083. arr[2] = 0xff;
  1084. arr[3] = 0xff;
  1085. }
  1086. arr[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  1087. arr[7] = SECT_SIZE_PER(target) & 0xff;
  1088. return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
  1089. }
  1090. #define SDEBUG_READCAP16_ARR_SZ 32
  1091. static int resp_readcap16(struct scsi_cmnd * scp,
  1092. struct sdebug_dev_info * devip)
  1093. {
  1094. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1095. unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
  1096. unsigned long long capac;
  1097. int errsts, k, alloc_len;
  1098. if ((errsts = check_readiness(scp, 1, devip)))
  1099. return errsts;
  1100. alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8)
  1101. + cmd[13]);
  1102. /* following just in case virtual_gb changed */
  1103. if (scsi_debug_virtual_gb > 0) {
  1104. sdebug_capacity = 2048 * 1024;
  1105. sdebug_capacity *= scsi_debug_virtual_gb;
  1106. } else
  1107. sdebug_capacity = sdebug_store_sectors;
  1108. memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
  1109. capac = sdebug_capacity - 1;
  1110. for (k = 0; k < 8; ++k, capac >>= 8)
  1111. arr[7 - k] = capac & 0xff;
  1112. arr[8] = (SECT_SIZE_PER(target) >> 24) & 0xff;
  1113. arr[9] = (SECT_SIZE_PER(target) >> 16) & 0xff;
  1114. arr[10] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  1115. arr[11] = SECT_SIZE_PER(target) & 0xff;
  1116. return fill_from_dev_buffer(scp, arr,
  1117. min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
  1118. }
  1119. #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
  1120. static int resp_report_tgtpgs(struct scsi_cmnd * scp,
  1121. struct sdebug_dev_info * devip)
  1122. {
  1123. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1124. unsigned char * arr;
  1125. int host_no = devip->sdbg_host->shost->host_no;
  1126. int n, ret, alen, rlen;
  1127. int port_group_a, port_group_b, port_a, port_b;
  1128. alen = ((cmd[6] << 24) + (cmd[7] << 16) + (cmd[8] << 8)
  1129. + cmd[9]);
  1130. arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_KERNEL);
  1131. /*
  1132. * EVPD page 0x88 states we have two ports, one
  1133. * real and a fake port with no device connected.
  1134. * So we create two port groups with one port each
  1135. * and set the group with port B to unavailable.
  1136. */
  1137. port_a = 0x1; /* relative port A */
  1138. port_b = 0x2; /* relative port B */
  1139. port_group_a = (((host_no + 1) & 0x7f) << 8) +
  1140. (devip->channel & 0x7f);
  1141. port_group_b = (((host_no + 1) & 0x7f) << 8) +
  1142. (devip->channel & 0x7f) + 0x80;
  1143. /*
  1144. * The asymmetric access state is cycled according to the host_id.
  1145. */
  1146. n = 4;
  1147. if (0 == scsi_debug_vpd_use_hostno) {
  1148. arr[n++] = host_no % 3; /* Asymm access state */
  1149. arr[n++] = 0x0F; /* claim: all states are supported */
  1150. } else {
  1151. arr[n++] = 0x0; /* Active/Optimized path */
  1152. arr[n++] = 0x01; /* claim: only support active/optimized paths */
  1153. }
  1154. arr[n++] = (port_group_a >> 8) & 0xff;
  1155. arr[n++] = port_group_a & 0xff;
  1156. arr[n++] = 0; /* Reserved */
  1157. arr[n++] = 0; /* Status code */
  1158. arr[n++] = 0; /* Vendor unique */
  1159. arr[n++] = 0x1; /* One port per group */
  1160. arr[n++] = 0; /* Reserved */
  1161. arr[n++] = 0; /* Reserved */
  1162. arr[n++] = (port_a >> 8) & 0xff;
  1163. arr[n++] = port_a & 0xff;
  1164. arr[n++] = 3; /* Port unavailable */
  1165. arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
  1166. arr[n++] = (port_group_b >> 8) & 0xff;
  1167. arr[n++] = port_group_b & 0xff;
  1168. arr[n++] = 0; /* Reserved */
  1169. arr[n++] = 0; /* Status code */
  1170. arr[n++] = 0; /* Vendor unique */
  1171. arr[n++] = 0x1; /* One port per group */
  1172. arr[n++] = 0; /* Reserved */
  1173. arr[n++] = 0; /* Reserved */
  1174. arr[n++] = (port_b >> 8) & 0xff;
  1175. arr[n++] = port_b & 0xff;
  1176. rlen = n - 4;
  1177. arr[0] = (rlen >> 24) & 0xff;
  1178. arr[1] = (rlen >> 16) & 0xff;
  1179. arr[2] = (rlen >> 8) & 0xff;
  1180. arr[3] = rlen & 0xff;
  1181. /*
  1182. * Return the smallest value of either
  1183. * - The allocated length
  1184. * - The constructed command length
  1185. * - The maximum array size
  1186. */
  1187. rlen = min(alen,n);
  1188. ret = fill_from_dev_buffer(scp, arr,
  1189. min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
  1190. kfree(arr);
  1191. return ret;
  1192. }
  1193. /* <<Following mode page info copied from ST318451LW>> */
  1194. static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
  1195. { /* Read-Write Error Recovery page for mode_sense */
  1196. unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
  1197. 5, 0, 0xff, 0xff};
  1198. memcpy(p, err_recov_pg, sizeof(err_recov_pg));
  1199. if (1 == pcontrol)
  1200. memset(p + 2, 0, sizeof(err_recov_pg) - 2);
  1201. return sizeof(err_recov_pg);
  1202. }
  1203. static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
  1204. { /* Disconnect-Reconnect page for mode_sense */
  1205. unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
  1206. 0, 0, 0, 0, 0, 0, 0, 0};
  1207. memcpy(p, disconnect_pg, sizeof(disconnect_pg));
  1208. if (1 == pcontrol)
  1209. memset(p + 2, 0, sizeof(disconnect_pg) - 2);
  1210. return sizeof(disconnect_pg);
  1211. }
  1212. static int resp_format_pg(unsigned char * p, int pcontrol, int target)
  1213. { /* Format device page for mode_sense */
  1214. unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
  1215. 0, 0, 0, 0, 0, 0, 0, 0,
  1216. 0, 0, 0, 0, 0x40, 0, 0, 0};
  1217. memcpy(p, format_pg, sizeof(format_pg));
  1218. p[10] = (sdebug_sectors_per >> 8) & 0xff;
  1219. p[11] = sdebug_sectors_per & 0xff;
  1220. p[12] = (SECT_SIZE >> 8) & 0xff;
  1221. p[13] = SECT_SIZE & 0xff;
  1222. if (DEV_REMOVEABLE(target))
  1223. p[20] |= 0x20; /* should agree with INQUIRY */
  1224. if (1 == pcontrol)
  1225. memset(p + 2, 0, sizeof(format_pg) - 2);
  1226. return sizeof(format_pg);
  1227. }
  1228. static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
  1229. { /* Caching page for mode_sense */
  1230. unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  1231. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
  1232. memcpy(p, caching_pg, sizeof(caching_pg));
  1233. if (1 == pcontrol)
  1234. memset(p + 2, 0, sizeof(caching_pg) - 2);
  1235. return sizeof(caching_pg);
  1236. }
  1237. static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
  1238. { /* Control mode page for mode_sense */
  1239. unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
  1240. 0, 0, 0, 0};
  1241. unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  1242. 0, 0, 0x2, 0x4b};
  1243. if (scsi_debug_dsense)
  1244. ctrl_m_pg[2] |= 0x4;
  1245. else
  1246. ctrl_m_pg[2] &= ~0x4;
  1247. memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
  1248. if (1 == pcontrol)
  1249. memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
  1250. else if (2 == pcontrol)
  1251. memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
  1252. return sizeof(ctrl_m_pg);
  1253. }
  1254. static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
  1255. { /* Informational Exceptions control mode page for mode_sense */
  1256. unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
  1257. 0, 0, 0x0, 0x0};
  1258. unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1259. 0, 0, 0x0, 0x0};
  1260. memcpy(p, iec_m_pg, sizeof(iec_m_pg));
  1261. if (1 == pcontrol)
  1262. memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
  1263. else if (2 == pcontrol)
  1264. memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
  1265. return sizeof(iec_m_pg);
  1266. }
  1267. static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target)
  1268. { /* SAS SSP mode page - short format for mode_sense */
  1269. unsigned char sas_sf_m_pg[] = {0x19, 0x6,
  1270. 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
  1271. memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
  1272. if (1 == pcontrol)
  1273. memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
  1274. return sizeof(sas_sf_m_pg);
  1275. }
  1276. static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target,
  1277. int target_dev_id)
  1278. { /* SAS phy control and discover mode page for mode_sense */
  1279. unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
  1280. 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1281. 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
  1282. 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
  1283. 0x2, 0, 0, 0, 0, 0, 0, 0,
  1284. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1285. 0, 0, 0, 0, 0, 0, 0, 0,
  1286. 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1287. 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
  1288. 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
  1289. 0x3, 0, 0, 0, 0, 0, 0, 0,
  1290. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1291. 0, 0, 0, 0, 0, 0, 0, 0,
  1292. };
  1293. int port_a, port_b;
  1294. port_a = target_dev_id + 1;
  1295. port_b = port_a + 1;
  1296. memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
  1297. p[20] = (port_a >> 24);
  1298. p[21] = (port_a >> 16) & 0xff;
  1299. p[22] = (port_a >> 8) & 0xff;
  1300. p[23] = port_a & 0xff;
  1301. p[48 + 20] = (port_b >> 24);
  1302. p[48 + 21] = (port_b >> 16) & 0xff;
  1303. p[48 + 22] = (port_b >> 8) & 0xff;
  1304. p[48 + 23] = port_b & 0xff;
  1305. if (1 == pcontrol)
  1306. memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
  1307. return sizeof(sas_pcd_m_pg);
  1308. }
  1309. static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
  1310. { /* SAS SSP shared protocol specific port mode subpage */
  1311. unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
  1312. 0, 0, 0, 0, 0, 0, 0, 0,
  1313. };
  1314. memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
  1315. if (1 == pcontrol)
  1316. memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
  1317. return sizeof(sas_sha_m_pg);
  1318. }
  1319. #define SDEBUG_MAX_MSENSE_SZ 256
  1320. static int resp_mode_sense(struct scsi_cmnd * scp, int target,
  1321. struct sdebug_dev_info * devip)
  1322. {
  1323. unsigned char dbd, llbaa;
  1324. int pcontrol, pcode, subpcode, bd_len;
  1325. unsigned char dev_spec;
  1326. int k, alloc_len, msense_6, offset, len, errsts, target_dev_id;
  1327. unsigned char * ap;
  1328. unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
  1329. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1330. if ((errsts = check_readiness(scp, 1, devip)))
  1331. return errsts;
  1332. dbd = !!(cmd[1] & 0x8);
  1333. pcontrol = (cmd[2] & 0xc0) >> 6;
  1334. pcode = cmd[2] & 0x3f;
  1335. subpcode = cmd[3];
  1336. msense_6 = (MODE_SENSE == cmd[0]);
  1337. llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10);
  1338. if ((0 == scsi_debug_ptype) && (0 == dbd))
  1339. bd_len = llbaa ? 16 : 8;
  1340. else
  1341. bd_len = 0;
  1342. alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
  1343. memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
  1344. if (0x3 == pcontrol) { /* Saving values not supported */
  1345. mk_sense_buffer(devip, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP,
  1346. 0);
  1347. return check_condition_result;
  1348. }
  1349. target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
  1350. (devip->target * 1000) - 3;
  1351. /* set DPOFUA bit for disks */
  1352. if (0 == scsi_debug_ptype)
  1353. dev_spec = (DEV_READONLY(target) ? 0x80 : 0x0) | 0x10;
  1354. else
  1355. dev_spec = 0x0;
  1356. if (msense_6) {
  1357. arr[2] = dev_spec;
  1358. arr[3] = bd_len;
  1359. offset = 4;
  1360. } else {
  1361. arr[3] = dev_spec;
  1362. if (16 == bd_len)
  1363. arr[4] = 0x1; /* set LONGLBA bit */
  1364. arr[7] = bd_len; /* assume 255 or less */
  1365. offset = 8;
  1366. }
  1367. ap = arr + offset;
  1368. if ((bd_len > 0) && (0 == sdebug_capacity)) {
  1369. if (scsi_debug_virtual_gb > 0) {
  1370. sdebug_capacity = 2048 * 1024;
  1371. sdebug_capacity *= scsi_debug_virtual_gb;
  1372. } else
  1373. sdebug_capacity = sdebug_store_sectors;
  1374. }
  1375. if (8 == bd_len) {
  1376. if (sdebug_capacity > 0xfffffffe) {
  1377. ap[0] = 0xff;
  1378. ap[1] = 0xff;
  1379. ap[2] = 0xff;
  1380. ap[3] = 0xff;
  1381. } else {
  1382. ap[0] = (sdebug_capacity >> 24) & 0xff;
  1383. ap[1] = (sdebug_capacity >> 16) & 0xff;
  1384. ap[2] = (sdebug_capacity >> 8) & 0xff;
  1385. ap[3] = sdebug_capacity & 0xff;
  1386. }
  1387. ap[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  1388. ap[7] = SECT_SIZE_PER(target) & 0xff;
  1389. offset += bd_len;
  1390. ap = arr + offset;
  1391. } else if (16 == bd_len) {
  1392. unsigned long long capac = sdebug_capacity;
  1393. for (k = 0; k < 8; ++k, capac >>= 8)
  1394. ap[7 - k] = capac & 0xff;
  1395. ap[12] = (SECT_SIZE_PER(target) >> 24) & 0xff;
  1396. ap[13] = (SECT_SIZE_PER(target) >> 16) & 0xff;
  1397. ap[14] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  1398. ap[15] = SECT_SIZE_PER(target) & 0xff;
  1399. offset += bd_len;
  1400. ap = arr + offset;
  1401. }
  1402. if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
  1403. /* TODO: Control Extension page */
  1404. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1405. 0);
  1406. return check_condition_result;
  1407. }
  1408. switch (pcode) {
  1409. case 0x1: /* Read-Write error recovery page, direct access */
  1410. len = resp_err_recov_pg(ap, pcontrol, target);
  1411. offset += len;
  1412. break;
  1413. case 0x2: /* Disconnect-Reconnect page, all devices */
  1414. len = resp_disconnect_pg(ap, pcontrol, target);
  1415. offset += len;
  1416. break;
  1417. case 0x3: /* Format device page, direct access */
  1418. len = resp_format_pg(ap, pcontrol, target);
  1419. offset += len;
  1420. break;
  1421. case 0x8: /* Caching page, direct access */
  1422. len = resp_caching_pg(ap, pcontrol, target);
  1423. offset += len;
  1424. break;
  1425. case 0xa: /* Control Mode page, all devices */
  1426. len = resp_ctrl_m_pg(ap, pcontrol, target);
  1427. offset += len;
  1428. break;
  1429. case 0x19: /* if spc==1 then sas phy, control+discover */
  1430. if ((subpcode > 0x2) && (subpcode < 0xff)) {
  1431. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1432. INVALID_FIELD_IN_CDB, 0);
  1433. return check_condition_result;
  1434. }
  1435. len = 0;
  1436. if ((0x0 == subpcode) || (0xff == subpcode))
  1437. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  1438. if ((0x1 == subpcode) || (0xff == subpcode))
  1439. len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
  1440. target_dev_id);
  1441. if ((0x2 == subpcode) || (0xff == subpcode))
  1442. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  1443. offset += len;
  1444. break;
  1445. case 0x1c: /* Informational Exceptions Mode page, all devices */
  1446. len = resp_iec_m_pg(ap, pcontrol, target);
  1447. offset += len;
  1448. break;
  1449. case 0x3f: /* Read all Mode pages */
  1450. if ((0 == subpcode) || (0xff == subpcode)) {
  1451. len = resp_err_recov_pg(ap, pcontrol, target);
  1452. len += resp_disconnect_pg(ap + len, pcontrol, target);
  1453. len += resp_format_pg(ap + len, pcontrol, target);
  1454. len += resp_caching_pg(ap + len, pcontrol, target);
  1455. len += resp_ctrl_m_pg(ap + len, pcontrol, target);
  1456. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  1457. if (0xff == subpcode) {
  1458. len += resp_sas_pcd_m_spg(ap + len, pcontrol,
  1459. target, target_dev_id);
  1460. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  1461. }
  1462. len += resp_iec_m_pg(ap + len, pcontrol, target);
  1463. } else {
  1464. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1465. INVALID_FIELD_IN_CDB, 0);
  1466. return check_condition_result;
  1467. }
  1468. offset += len;
  1469. break;
  1470. default:
  1471. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1472. 0);
  1473. return check_condition_result;
  1474. }
  1475. if (msense_6)
  1476. arr[0] = offset - 1;
  1477. else {
  1478. arr[0] = ((offset - 2) >> 8) & 0xff;
  1479. arr[1] = (offset - 2) & 0xff;
  1480. }
  1481. return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
  1482. }
  1483. #define SDEBUG_MAX_MSELECT_SZ 512
  1484. static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
  1485. struct sdebug_dev_info * devip)
  1486. {
  1487. int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
  1488. int param_len, res, errsts, mpage;
  1489. unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
  1490. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1491. if ((errsts = check_readiness(scp, 1, devip)))
  1492. return errsts;
  1493. memset(arr, 0, sizeof(arr));
  1494. pf = cmd[1] & 0x10;
  1495. sp = cmd[1] & 0x1;
  1496. param_len = mselect6 ? cmd[4] : ((cmd[7] << 8) + cmd[8]);
  1497. if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
  1498. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1499. INVALID_FIELD_IN_CDB, 0);
  1500. return check_condition_result;
  1501. }
  1502. res = fetch_to_dev_buffer(scp, arr, param_len);
  1503. if (-1 == res)
  1504. return (DID_ERROR << 16);
  1505. else if ((res < param_len) &&
  1506. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  1507. printk(KERN_INFO "scsi_debug: mode_select: cdb indicated=%d, "
  1508. " IO sent=%d bytes\n", param_len, res);
  1509. md_len = mselect6 ? (arr[0] + 1) : ((arr[0] << 8) + arr[1] + 2);
  1510. bd_len = mselect6 ? arr[3] : ((arr[6] << 8) + arr[7]);
  1511. if (md_len > 2) {
  1512. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1513. INVALID_FIELD_IN_PARAM_LIST, 0);
  1514. return check_condition_result;
  1515. }
  1516. off = bd_len + (mselect6 ? 4 : 8);
  1517. mpage = arr[off] & 0x3f;
  1518. ps = !!(arr[off] & 0x80);
  1519. if (ps) {
  1520. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1521. INVALID_FIELD_IN_PARAM_LIST, 0);
  1522. return check_condition_result;
  1523. }
  1524. spf = !!(arr[off] & 0x40);
  1525. pg_len = spf ? ((arr[off + 2] << 8) + arr[off + 3] + 4) :
  1526. (arr[off + 1] + 2);
  1527. if ((pg_len + off) > param_len) {
  1528. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1529. PARAMETER_LIST_LENGTH_ERR, 0);
  1530. return check_condition_result;
  1531. }
  1532. switch (mpage) {
  1533. case 0xa: /* Control Mode page */
  1534. if (ctrl_m_pg[1] == arr[off + 1]) {
  1535. memcpy(ctrl_m_pg + 2, arr + off + 2,
  1536. sizeof(ctrl_m_pg) - 2);
  1537. scsi_debug_dsense = !!(ctrl_m_pg[2] & 0x4);
  1538. return 0;
  1539. }
  1540. break;
  1541. case 0x1c: /* Informational Exceptions Mode page */
  1542. if (iec_m_pg[1] == arr[off + 1]) {
  1543. memcpy(iec_m_pg + 2, arr + off + 2,
  1544. sizeof(iec_m_pg) - 2);
  1545. return 0;
  1546. }
  1547. break;
  1548. default:
  1549. break;
  1550. }
  1551. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1552. INVALID_FIELD_IN_PARAM_LIST, 0);
  1553. return check_condition_result;
  1554. }
  1555. static int resp_temp_l_pg(unsigned char * arr)
  1556. {
  1557. unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
  1558. 0x0, 0x1, 0x3, 0x2, 0x0, 65,
  1559. };
  1560. memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
  1561. return sizeof(temp_l_pg);
  1562. }
  1563. static int resp_ie_l_pg(unsigned char * arr)
  1564. {
  1565. unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
  1566. };
  1567. memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
  1568. if (iec_m_pg[2] & 0x4) { /* TEST bit set */
  1569. arr[4] = THRESHOLD_EXCEEDED;
  1570. arr[5] = 0xff;
  1571. }
  1572. return sizeof(ie_l_pg);
  1573. }
  1574. #define SDEBUG_MAX_LSENSE_SZ 512
  1575. static int resp_log_sense(struct scsi_cmnd * scp,
  1576. struct sdebug_dev_info * devip)
  1577. {
  1578. int ppc, sp, pcontrol, pcode, subpcode, alloc_len, errsts, len, n;
  1579. unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
  1580. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1581. if ((errsts = check_readiness(scp, 1, devip)))
  1582. return errsts;
  1583. memset(arr, 0, sizeof(arr));
  1584. ppc = cmd[1] & 0x2;
  1585. sp = cmd[1] & 0x1;
  1586. if (ppc || sp) {
  1587. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1588. INVALID_FIELD_IN_CDB, 0);
  1589. return check_condition_result;
  1590. }
  1591. pcontrol = (cmd[2] & 0xc0) >> 6;
  1592. pcode = cmd[2] & 0x3f;
  1593. subpcode = cmd[3] & 0xff;
  1594. alloc_len = (cmd[7] << 8) + cmd[8];
  1595. arr[0] = pcode;
  1596. if (0 == subpcode) {
  1597. switch (pcode) {
  1598. case 0x0: /* Supported log pages log page */
  1599. n = 4;
  1600. arr[n++] = 0x0; /* this page */
  1601. arr[n++] = 0xd; /* Temperature */
  1602. arr[n++] = 0x2f; /* Informational exceptions */
  1603. arr[3] = n - 4;
  1604. break;
  1605. case 0xd: /* Temperature log page */
  1606. arr[3] = resp_temp_l_pg(arr + 4);
  1607. break;
  1608. case 0x2f: /* Informational exceptions log page */
  1609. arr[3] = resp_ie_l_pg(arr + 4);
  1610. break;
  1611. default:
  1612. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1613. INVALID_FIELD_IN_CDB, 0);
  1614. return check_condition_result;
  1615. }
  1616. } else if (0xff == subpcode) {
  1617. arr[0] |= 0x40;
  1618. arr[1] = subpcode;
  1619. switch (pcode) {
  1620. case 0x0: /* Supported log pages and subpages log page */
  1621. n = 4;
  1622. arr[n++] = 0x0;
  1623. arr[n++] = 0x0; /* 0,0 page */
  1624. arr[n++] = 0x0;
  1625. arr[n++] = 0xff; /* this page */
  1626. arr[n++] = 0xd;
  1627. arr[n++] = 0x0; /* Temperature */
  1628. arr[n++] = 0x2f;
  1629. arr[n++] = 0x0; /* Informational exceptions */
  1630. arr[3] = n - 4;
  1631. break;
  1632. case 0xd: /* Temperature subpages */
  1633. n = 4;
  1634. arr[n++] = 0xd;
  1635. arr[n++] = 0x0; /* Temperature */
  1636. arr[3] = n - 4;
  1637. break;
  1638. case 0x2f: /* Informational exceptions subpages */
  1639. n = 4;
  1640. arr[n++] = 0x2f;
  1641. arr[n++] = 0x0; /* Informational exceptions */
  1642. arr[3] = n - 4;
  1643. break;
  1644. default:
  1645. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1646. INVALID_FIELD_IN_CDB, 0);
  1647. return check_condition_result;
  1648. }
  1649. } else {
  1650. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1651. INVALID_FIELD_IN_CDB, 0);
  1652. return check_condition_result;
  1653. }
  1654. len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
  1655. return fill_from_dev_buffer(scp, arr,
  1656. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  1657. }
  1658. static int resp_read(struct scsi_cmnd * SCpnt, unsigned long long lba,
  1659. unsigned int num, struct sdebug_dev_info * devip)
  1660. {
  1661. unsigned long iflags;
  1662. unsigned int block, from_bottom;
  1663. unsigned long long u;
  1664. int ret;
  1665. if (lba + num > sdebug_capacity) {
  1666. mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
  1667. 0);
  1668. return check_condition_result;
  1669. }
  1670. /* transfer length excessive (tie in to block limits VPD page) */
  1671. if (num > sdebug_store_sectors) {
  1672. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1673. 0);
  1674. return check_condition_result;
  1675. }
  1676. if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
  1677. (lba <= OPT_MEDIUM_ERR_ADDR) &&
  1678. ((lba + num) > OPT_MEDIUM_ERR_ADDR)) {
  1679. /* claim unrecoverable read error */
  1680. mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR,
  1681. 0);
  1682. /* set info field and valid bit for fixed descriptor */
  1683. if (0x70 == (devip->sense_buff[0] & 0x7f)) {
  1684. devip->sense_buff[0] |= 0x80; /* Valid bit */
  1685. ret = OPT_MEDIUM_ERR_ADDR;
  1686. devip->sense_buff[3] = (ret >> 24) & 0xff;
  1687. devip->sense_buff[4] = (ret >> 16) & 0xff;
  1688. devip->sense_buff[5] = (ret >> 8) & 0xff;
  1689. devip->sense_buff[6] = ret & 0xff;
  1690. }
  1691. return check_condition_result;
  1692. }
  1693. read_lock_irqsave(&atomic_rw, iflags);
  1694. if ((lba + num) <= sdebug_store_sectors)
  1695. ret = fill_from_dev_buffer(SCpnt,
  1696. fake_storep + (lba * SECT_SIZE),
  1697. num * SECT_SIZE);
  1698. else {
  1699. /* modulo when one arg is 64 bits needs do_div() */
  1700. u = lba;
  1701. block = do_div(u, sdebug_store_sectors);
  1702. from_bottom = 0;
  1703. if ((block + num) > sdebug_store_sectors)
  1704. from_bottom = (block + num) - sdebug_store_sectors;
  1705. ret = fill_from_dev_buffer(SCpnt,
  1706. fake_storep + (block * SECT_SIZE),
  1707. (num - from_bottom) * SECT_SIZE);
  1708. if ((0 == ret) && (from_bottom > 0))
  1709. ret = fill_from_dev_buffer(SCpnt, fake_storep,
  1710. from_bottom * SECT_SIZE);
  1711. }
  1712. read_unlock_irqrestore(&atomic_rw, iflags);
  1713. return ret;
  1714. }
  1715. static int resp_write(struct scsi_cmnd * SCpnt, unsigned long long lba,
  1716. unsigned int num, struct sdebug_dev_info * devip)
  1717. {
  1718. unsigned long iflags;
  1719. unsigned int block, to_bottom;
  1720. unsigned long long u;
  1721. int res;
  1722. if (lba + num > sdebug_capacity) {
  1723. mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
  1724. 0);
  1725. return check_condition_result;
  1726. }
  1727. /* transfer length excessive (tie in to block limits VPD page) */
  1728. if (num > sdebug_store_sectors) {
  1729. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1730. 0);
  1731. return check_condition_result;
  1732. }
  1733. write_lock_irqsave(&atomic_rw, iflags);
  1734. if ((lba + num) <= sdebug_store_sectors)
  1735. res = fetch_to_dev_buffer(SCpnt,
  1736. fake_storep + (lba * SECT_SIZE),
  1737. num * SECT_SIZE);
  1738. else {
  1739. /* modulo when one arg is 64 bits needs do_div() */
  1740. u = lba;
  1741. block = do_div(u, sdebug_store_sectors);
  1742. to_bottom = 0;
  1743. if ((block + num) > sdebug_store_sectors)
  1744. to_bottom = (block + num) - sdebug_store_sectors;
  1745. res = fetch_to_dev_buffer(SCpnt,
  1746. fake_storep + (block * SECT_SIZE),
  1747. (num - to_bottom) * SECT_SIZE);
  1748. if ((0 == res) && (to_bottom > 0))
  1749. res = fetch_to_dev_buffer(SCpnt, fake_storep,
  1750. to_bottom * SECT_SIZE);
  1751. }
  1752. write_unlock_irqrestore(&atomic_rw, iflags);
  1753. if (-1 == res)
  1754. return (DID_ERROR << 16);
  1755. else if ((res < (num * SECT_SIZE)) &&
  1756. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  1757. printk(KERN_INFO "scsi_debug: write: cdb indicated=%u, "
  1758. " IO sent=%d bytes\n", num * SECT_SIZE, res);
  1759. return 0;
  1760. }
  1761. #define SDEBUG_RLUN_ARR_SZ 256
  1762. static int resp_report_luns(struct scsi_cmnd * scp,
  1763. struct sdebug_dev_info * devip)
  1764. {
  1765. unsigned int alloc_len;
  1766. int lun_cnt, i, upper, num, n, wlun, lun;
  1767. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1768. int select_report = (int)cmd[2];
  1769. struct scsi_lun *one_lun;
  1770. unsigned char arr[SDEBUG_RLUN_ARR_SZ];
  1771. unsigned char * max_addr;
  1772. alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
  1773. if ((alloc_len < 4) || (select_report > 2)) {
  1774. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1775. 0);
  1776. return check_condition_result;
  1777. }
  1778. /* can produce response with up to 16k luns (lun 0 to lun 16383) */
  1779. memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
  1780. lun_cnt = scsi_debug_max_luns;
  1781. if (1 == select_report)
  1782. lun_cnt = 0;
  1783. else if (scsi_debug_no_lun_0 && (lun_cnt > 0))
  1784. --lun_cnt;
  1785. wlun = (select_report > 0) ? 1 : 0;
  1786. num = lun_cnt + wlun;
  1787. arr[2] = ((sizeof(struct scsi_lun) * num) >> 8) & 0xff;
  1788. arr[3] = (sizeof(struct scsi_lun) * num) & 0xff;
  1789. n = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
  1790. sizeof(struct scsi_lun)), num);
  1791. if (n < num) {
  1792. wlun = 0;
  1793. lun_cnt = n;
  1794. }
  1795. one_lun = (struct scsi_lun *) &arr[8];
  1796. max_addr = arr + SDEBUG_RLUN_ARR_SZ;
  1797. for (i = 0, lun = (scsi_debug_no_lun_0 ? 1 : 0);
  1798. ((i < lun_cnt) && ((unsigned char *)(one_lun + i) < max_addr));
  1799. i++, lun++) {
  1800. upper = (lun >> 8) & 0x3f;
  1801. if (upper)
  1802. one_lun[i].scsi_lun[0] =
  1803. (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
  1804. one_lun[i].scsi_lun[1] = lun & 0xff;
  1805. }
  1806. if (wlun) {
  1807. one_lun[i].scsi_lun[0] = (SAM2_WLUN_REPORT_LUNS >> 8) & 0xff;
  1808. one_lun[i].scsi_lun[1] = SAM2_WLUN_REPORT_LUNS & 0xff;
  1809. i++;
  1810. }
  1811. alloc_len = (unsigned char *)(one_lun + i) - arr;
  1812. return fill_from_dev_buffer(scp, arr,
  1813. min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
  1814. }
  1815. /* When timer goes off this function is called. */
  1816. static void timer_intr_handler(unsigned long indx)
  1817. {
  1818. struct sdebug_queued_cmd * sqcp;
  1819. unsigned long iflags;
  1820. if (indx >= SCSI_DEBUG_CANQUEUE) {
  1821. printk(KERN_ERR "scsi_debug:timer_intr_handler: indx too "
  1822. "large\n");
  1823. return;
  1824. }
  1825. spin_lock_irqsave(&queued_arr_lock, iflags);
  1826. sqcp = &queued_arr[(int)indx];
  1827. if (! sqcp->in_use) {
  1828. printk(KERN_ERR "scsi_debug:timer_intr_handler: Unexpected "
  1829. "interrupt\n");
  1830. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1831. return;
  1832. }
  1833. sqcp->in_use = 0;
  1834. if (sqcp->done_funct) {
  1835. sqcp->a_cmnd->result = sqcp->scsi_result;
  1836. sqcp->done_funct(sqcp->a_cmnd); /* callback to mid level */
  1837. }
  1838. sqcp->done_funct = NULL;
  1839. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1840. }
  1841. static int scsi_debug_slave_alloc(struct scsi_device * sdp)
  1842. {
  1843. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1844. printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %u>\n",
  1845. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  1846. return 0;
  1847. }
  1848. static int scsi_debug_slave_configure(struct scsi_device * sdp)
  1849. {
  1850. struct sdebug_dev_info * devip;
  1851. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1852. printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %u>\n",
  1853. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  1854. if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
  1855. sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
  1856. devip = devInfoReg(sdp);
  1857. sdp->hostdata = devip;
  1858. if (sdp->host->cmd_per_lun)
  1859. scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
  1860. sdp->host->cmd_per_lun);
  1861. blk_queue_max_segment_size(sdp->request_queue, 256 * 1024);
  1862. return 0;
  1863. }
  1864. static void scsi_debug_slave_destroy(struct scsi_device * sdp)
  1865. {
  1866. struct sdebug_dev_info * devip =
  1867. (struct sdebug_dev_info *)sdp->hostdata;
  1868. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1869. printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %u>\n",
  1870. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  1871. if (devip) {
  1872. /* make this slot avaliable for re-use */
  1873. devip->used = 0;
  1874. sdp->hostdata = NULL;
  1875. }
  1876. }
  1877. static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
  1878. {
  1879. struct sdebug_host_info * sdbg_host;
  1880. struct sdebug_dev_info * open_devip = NULL;
  1881. struct sdebug_dev_info * devip =
  1882. (struct sdebug_dev_info *)sdev->hostdata;
  1883. if (devip)
  1884. return devip;
  1885. sdbg_host = *(struct sdebug_host_info **) sdev->host->hostdata;
  1886. if(! sdbg_host) {
  1887. printk(KERN_ERR "Host info NULL\n");
  1888. return NULL;
  1889. }
  1890. list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
  1891. if ((devip->used) && (devip->channel == sdev->channel) &&
  1892. (devip->target == sdev->id) &&
  1893. (devip->lun == sdev->lun))
  1894. return devip;
  1895. else {
  1896. if ((!devip->used) && (!open_devip))
  1897. open_devip = devip;
  1898. }
  1899. }
  1900. if (NULL == open_devip) { /* try and make a new one */
  1901. open_devip = kzalloc(sizeof(*open_devip),GFP_KERNEL);
  1902. if (NULL == open_devip) {
  1903. printk(KERN_ERR "%s: out of memory at line %d\n",
  1904. __FUNCTION__, __LINE__);
  1905. return NULL;
  1906. }
  1907. open_devip->sdbg_host = sdbg_host;
  1908. list_add_tail(&open_devip->dev_list,
  1909. &sdbg_host->dev_info_list);
  1910. }
  1911. if (open_devip) {
  1912. open_devip->channel = sdev->channel;
  1913. open_devip->target = sdev->id;
  1914. open_devip->lun = sdev->lun;
  1915. open_devip->sdbg_host = sdbg_host;
  1916. open_devip->reset = 1;
  1917. open_devip->used = 1;
  1918. memset(open_devip->sense_buff, 0, SDEBUG_SENSE_LEN);
  1919. if (scsi_debug_dsense)
  1920. open_devip->sense_buff[0] = 0x72;
  1921. else {
  1922. open_devip->sense_buff[0] = 0x70;
  1923. open_devip->sense_buff[7] = 0xa;
  1924. }
  1925. if (sdev->lun == SAM2_WLUN_REPORT_LUNS)
  1926. open_devip->wlun = SAM2_WLUN_REPORT_LUNS & 0xff;
  1927. return open_devip;
  1928. }
  1929. return NULL;
  1930. }
  1931. static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
  1932. int asc, int asq)
  1933. {
  1934. unsigned char * sbuff;
  1935. sbuff = devip->sense_buff;
  1936. memset(sbuff, 0, SDEBUG_SENSE_LEN);
  1937. if (scsi_debug_dsense) {
  1938. sbuff[0] = 0x72; /* descriptor, current */
  1939. sbuff[1] = key;
  1940. sbuff[2] = asc;
  1941. sbuff[3] = asq;
  1942. } else {
  1943. sbuff[0] = 0x70; /* fixed, current */
  1944. sbuff[2] = key;
  1945. sbuff[7] = 0xa; /* implies 18 byte sense buffer */
  1946. sbuff[12] = asc;
  1947. sbuff[13] = asq;
  1948. }
  1949. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1950. printk(KERN_INFO "scsi_debug: [sense_key,asc,ascq]: "
  1951. "[0x%x,0x%x,0x%x]\n", key, asc, asq);
  1952. }
  1953. static int scsi_debug_abort(struct scsi_cmnd * SCpnt)
  1954. {
  1955. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1956. printk(KERN_INFO "scsi_debug: abort\n");
  1957. ++num_aborts;
  1958. stop_queued_cmnd(SCpnt);
  1959. return SUCCESS;
  1960. }
  1961. static int scsi_debug_biosparam(struct scsi_device *sdev,
  1962. struct block_device * bdev, sector_t capacity, int *info)
  1963. {
  1964. int res;
  1965. unsigned char *buf;
  1966. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1967. printk(KERN_INFO "scsi_debug: biosparam\n");
  1968. buf = scsi_bios_ptable(bdev);
  1969. if (buf) {
  1970. res = scsi_partsize(buf, capacity,
  1971. &info[2], &info[0], &info[1]);
  1972. kfree(buf);
  1973. if (! res)
  1974. return res;
  1975. }
  1976. info[0] = sdebug_heads;
  1977. info[1] = sdebug_sectors_per;
  1978. info[2] = sdebug_cylinders_per;
  1979. return 0;
  1980. }
  1981. static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
  1982. {
  1983. struct sdebug_dev_info * devip;
  1984. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1985. printk(KERN_INFO "scsi_debug: device_reset\n");
  1986. ++num_dev_resets;
  1987. if (SCpnt) {
  1988. devip = devInfoReg(SCpnt->device);
  1989. if (devip)
  1990. devip->reset = 1;
  1991. }
  1992. return SUCCESS;
  1993. }
  1994. static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
  1995. {
  1996. struct sdebug_host_info *sdbg_host;
  1997. struct sdebug_dev_info * dev_info;
  1998. struct scsi_device * sdp;
  1999. struct Scsi_Host * hp;
  2000. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2001. printk(KERN_INFO "scsi_debug: bus_reset\n");
  2002. ++num_bus_resets;
  2003. if (SCpnt && ((sdp = SCpnt->device)) && ((hp = sdp->host))) {
  2004. sdbg_host = *(struct sdebug_host_info **) hp->hostdata;
  2005. if (sdbg_host) {
  2006. list_for_each_entry(dev_info,
  2007. &sdbg_host->dev_info_list,
  2008. dev_list)
  2009. dev_info->reset = 1;
  2010. }
  2011. }
  2012. return SUCCESS;
  2013. }
  2014. static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
  2015. {
  2016. struct sdebug_host_info * sdbg_host;
  2017. struct sdebug_dev_info * dev_info;
  2018. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2019. printk(KERN_INFO "scsi_debug: host_reset\n");
  2020. ++num_host_resets;
  2021. spin_lock(&sdebug_host_list_lock);
  2022. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  2023. list_for_each_entry(dev_info, &sdbg_host->dev_info_list,
  2024. dev_list)
  2025. dev_info->reset = 1;
  2026. }
  2027. spin_unlock(&sdebug_host_list_lock);
  2028. stop_all_queued();
  2029. return SUCCESS;
  2030. }
  2031. /* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
  2032. static int stop_queued_cmnd(struct scsi_cmnd * cmnd)
  2033. {
  2034. unsigned long iflags;
  2035. int k;
  2036. struct sdebug_queued_cmd * sqcp;
  2037. spin_lock_irqsave(&queued_arr_lock, iflags);
  2038. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2039. sqcp = &queued_arr[k];
  2040. if (sqcp->in_use && (cmnd == sqcp->a_cmnd)) {
  2041. del_timer_sync(&sqcp->cmnd_timer);
  2042. sqcp->in_use = 0;
  2043. sqcp->a_cmnd = NULL;
  2044. break;
  2045. }
  2046. }
  2047. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2048. return (k < SCSI_DEBUG_CANQUEUE) ? 1 : 0;
  2049. }
  2050. /* Deletes (stops) timers of all queued commands */
  2051. static void stop_all_queued(void)
  2052. {
  2053. unsigned long iflags;
  2054. int k;
  2055. struct sdebug_queued_cmd * sqcp;
  2056. spin_lock_irqsave(&queued_arr_lock, iflags);
  2057. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2058. sqcp = &queued_arr[k];
  2059. if (sqcp->in_use && sqcp->a_cmnd) {
  2060. del_timer_sync(&sqcp->cmnd_timer);
  2061. sqcp->in_use = 0;
  2062. sqcp->a_cmnd = NULL;
  2063. }
  2064. }
  2065. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2066. }
  2067. /* Initializes timers in queued array */
  2068. static void __init init_all_queued(void)
  2069. {
  2070. unsigned long iflags;
  2071. int k;
  2072. struct sdebug_queued_cmd * sqcp;
  2073. spin_lock_irqsave(&queued_arr_lock, iflags);
  2074. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2075. sqcp = &queued_arr[k];
  2076. init_timer(&sqcp->cmnd_timer);
  2077. sqcp->in_use = 0;
  2078. sqcp->a_cmnd = NULL;
  2079. }
  2080. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2081. }
  2082. static void __init sdebug_build_parts(unsigned char * ramp)
  2083. {
  2084. struct partition * pp;
  2085. int starts[SDEBUG_MAX_PARTS + 2];
  2086. int sectors_per_part, num_sectors, k;
  2087. int heads_by_sects, start_sec, end_sec;
  2088. /* assume partition table already zeroed */
  2089. if ((scsi_debug_num_parts < 1) || (sdebug_store_size < 1048576))
  2090. return;
  2091. if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
  2092. scsi_debug_num_parts = SDEBUG_MAX_PARTS;
  2093. printk(KERN_WARNING "scsi_debug:build_parts: reducing "
  2094. "partitions to %d\n", SDEBUG_MAX_PARTS);
  2095. }
  2096. num_sectors = (int)sdebug_store_sectors;
  2097. sectors_per_part = (num_sectors - sdebug_sectors_per)
  2098. / scsi_debug_num_parts;
  2099. heads_by_sects = sdebug_heads * sdebug_sectors_per;
  2100. starts[0] = sdebug_sectors_per;
  2101. for (k = 1; k < scsi_debug_num_parts; ++k)
  2102. starts[k] = ((k * sectors_per_part) / heads_by_sects)
  2103. * heads_by_sects;
  2104. starts[scsi_debug_num_parts] = num_sectors;
  2105. starts[scsi_debug_num_parts + 1] = 0;
  2106. ramp[510] = 0x55; /* magic partition markings */
  2107. ramp[511] = 0xAA;
  2108. pp = (struct partition *)(ramp + 0x1be);
  2109. for (k = 0; starts[k + 1]; ++k, ++pp) {
  2110. start_sec = starts[k];
  2111. end_sec = starts[k + 1] - 1;
  2112. pp->boot_ind = 0;
  2113. pp->cyl = start_sec / heads_by_sects;
  2114. pp->head = (start_sec - (pp->cyl * heads_by_sects))
  2115. / sdebug_sectors_per;
  2116. pp->sector = (start_sec % sdebug_sectors_per) + 1;
  2117. pp->end_cyl = end_sec / heads_by_sects;
  2118. pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
  2119. / sdebug_sectors_per;
  2120. pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
  2121. pp->start_sect = start_sec;
  2122. pp->nr_sects = end_sec - start_sec + 1;
  2123. pp->sys_ind = 0x83; /* plain Linux partition */
  2124. }
  2125. }
  2126. static int schedule_resp(struct scsi_cmnd * cmnd,
  2127. struct sdebug_dev_info * devip,
  2128. done_funct_t done, int scsi_result, int delta_jiff)
  2129. {
  2130. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmnd) {
  2131. if (scsi_result) {
  2132. struct scsi_device * sdp = cmnd->device;
  2133. printk(KERN_INFO "scsi_debug: <%u %u %u %u> "
  2134. "non-zero result=0x%x\n", sdp->host->host_no,
  2135. sdp->channel, sdp->id, sdp->lun, scsi_result);
  2136. }
  2137. }
  2138. if (cmnd && devip) {
  2139. /* simulate autosense by this driver */
  2140. if (SAM_STAT_CHECK_CONDITION == (scsi_result & 0xff))
  2141. memcpy(cmnd->sense_buffer, devip->sense_buff,
  2142. (SCSI_SENSE_BUFFERSIZE > SDEBUG_SENSE_LEN) ?
  2143. SDEBUG_SENSE_LEN : SCSI_SENSE_BUFFERSIZE);
  2144. }
  2145. if (delta_jiff <= 0) {
  2146. if (cmnd)
  2147. cmnd->result = scsi_result;
  2148. if (done)
  2149. done(cmnd);
  2150. return 0;
  2151. } else {
  2152. unsigned long iflags;
  2153. int k;
  2154. struct sdebug_queued_cmd * sqcp = NULL;
  2155. spin_lock_irqsave(&queued_arr_lock, iflags);
  2156. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2157. sqcp = &queued_arr[k];
  2158. if (! sqcp->in_use)
  2159. break;
  2160. }
  2161. if (k >= SCSI_DEBUG_CANQUEUE) {
  2162. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2163. printk(KERN_WARNING "scsi_debug: can_queue exceeded\n");
  2164. return 1; /* report busy to mid level */
  2165. }
  2166. sqcp->in_use = 1;
  2167. sqcp->a_cmnd = cmnd;
  2168. sqcp->scsi_result = scsi_result;
  2169. sqcp->done_funct = done;
  2170. sqcp->cmnd_timer.function = timer_intr_handler;
  2171. sqcp->cmnd_timer.data = k;
  2172. sqcp->cmnd_timer.expires = jiffies + delta_jiff;
  2173. add_timer(&sqcp->cmnd_timer);
  2174. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2175. if (cmnd)
  2176. cmnd->result = 0;
  2177. return 0;
  2178. }
  2179. }
  2180. /* Note: The following macros create attribute files in the
  2181. /sys/module/scsi_debug/parameters directory. Unfortunately this
  2182. driver is unaware of a change and cannot trigger auxiliary actions
  2183. as it can when the corresponding attribute in the
  2184. /sys/bus/pseudo/drivers/scsi_debug directory is changed.
  2185. */
  2186. module_param_named(add_host, scsi_debug_add_host, int, S_IRUGO | S_IWUSR);
  2187. module_param_named(delay, scsi_debug_delay, int, S_IRUGO | S_IWUSR);
  2188. module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, S_IRUGO);
  2189. module_param_named(dsense, scsi_debug_dsense, int, S_IRUGO | S_IWUSR);
  2190. module_param_named(every_nth, scsi_debug_every_nth, int, S_IRUGO | S_IWUSR);
  2191. module_param_named(fake_rw, scsi_debug_fake_rw, int, S_IRUGO | S_IWUSR);
  2192. module_param_named(max_luns, scsi_debug_max_luns, int, S_IRUGO | S_IWUSR);
  2193. module_param_named(no_lun_0, scsi_debug_no_lun_0, int, S_IRUGO | S_IWUSR);
  2194. module_param_named(num_parts, scsi_debug_num_parts, int, S_IRUGO);
  2195. module_param_named(num_tgts, scsi_debug_num_tgts, int, S_IRUGO | S_IWUSR);
  2196. module_param_named(opts, scsi_debug_opts, int, S_IRUGO | S_IWUSR);
  2197. module_param_named(ptype, scsi_debug_ptype, int, S_IRUGO | S_IWUSR);
  2198. module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO);
  2199. module_param_named(virtual_gb, scsi_debug_virtual_gb, int, S_IRUGO | S_IWUSR);
  2200. module_param_named(vpd_use_hostno, scsi_debug_vpd_use_hostno, int,
  2201. S_IRUGO | S_IWUSR);
  2202. MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
  2203. MODULE_DESCRIPTION("SCSI debug adapter driver");
  2204. MODULE_LICENSE("GPL");
  2205. MODULE_VERSION(SCSI_DEBUG_VERSION);
  2206. MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
  2207. MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
  2208. MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs(def=8)");
  2209. MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
  2210. MODULE_PARM_DESC(every_nth, "timeout every nth command(def=100)");
  2211. MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
  2212. MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
  2213. MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
  2214. MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
  2215. MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
  2216. MODULE_PARM_DESC(opts, "1->noise, 2->medium_error, 4->... (def=0)");
  2217. MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
  2218. MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])");
  2219. MODULE_PARM_DESC(virtual_gb, "virtual gigabyte size (def=0 -> use dev_size_mb)");
  2220. MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
  2221. static char sdebug_info[256];
  2222. static const char * scsi_debug_info(struct Scsi_Host * shp)
  2223. {
  2224. sprintf(sdebug_info, "scsi_debug, version %s [%s], "
  2225. "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
  2226. scsi_debug_version_date, scsi_debug_dev_size_mb,
  2227. scsi_debug_opts);
  2228. return sdebug_info;
  2229. }
  2230. /* scsi_debug_proc_info
  2231. * Used if the driver currently has no own support for /proc/scsi
  2232. */
  2233. static int scsi_debug_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  2234. int length, int inout)
  2235. {
  2236. int len, pos, begin;
  2237. int orig_length;
  2238. orig_length = length;
  2239. if (inout == 1) {
  2240. char arr[16];
  2241. int minLen = length > 15 ? 15 : length;
  2242. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2243. return -EACCES;
  2244. memcpy(arr, buffer, minLen);
  2245. arr[minLen] = '\0';
  2246. if (1 != sscanf(arr, "%d", &pos))
  2247. return -EINVAL;
  2248. scsi_debug_opts = pos;
  2249. if (scsi_debug_every_nth != 0)
  2250. scsi_debug_cmnd_count = 0;
  2251. return length;
  2252. }
  2253. begin = 0;
  2254. pos = len = sprintf(buffer, "scsi_debug adapter driver, version "
  2255. "%s [%s]\n"
  2256. "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
  2257. "every_nth=%d(curr:%d)\n"
  2258. "delay=%d, max_luns=%d, scsi_level=%d\n"
  2259. "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
  2260. "number of aborts=%d, device_reset=%d, bus_resets=%d, "
  2261. "host_resets=%d\n",
  2262. SCSI_DEBUG_VERSION, scsi_debug_version_date, scsi_debug_num_tgts,
  2263. scsi_debug_dev_size_mb, scsi_debug_opts, scsi_debug_every_nth,
  2264. scsi_debug_cmnd_count, scsi_debug_delay,
  2265. scsi_debug_max_luns, scsi_debug_scsi_level,
  2266. SECT_SIZE, sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
  2267. num_aborts, num_dev_resets, num_bus_resets, num_host_resets);
  2268. if (pos < offset) {
  2269. len = 0;
  2270. begin = pos;
  2271. }
  2272. *start = buffer + (offset - begin); /* Start of wanted data */
  2273. len -= (offset - begin);
  2274. if (len > length)
  2275. len = length;
  2276. return len;
  2277. }
  2278. static ssize_t sdebug_delay_show(struct device_driver * ddp, char * buf)
  2279. {
  2280. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
  2281. }
  2282. static ssize_t sdebug_delay_store(struct device_driver * ddp,
  2283. const char * buf, size_t count)
  2284. {
  2285. int delay;
  2286. char work[20];
  2287. if (1 == sscanf(buf, "%10s", work)) {
  2288. if ((1 == sscanf(work, "%d", &delay)) && (delay >= 0)) {
  2289. scsi_debug_delay = delay;
  2290. return count;
  2291. }
  2292. }
  2293. return -EINVAL;
  2294. }
  2295. DRIVER_ATTR(delay, S_IRUGO | S_IWUSR, sdebug_delay_show,
  2296. sdebug_delay_store);
  2297. static ssize_t sdebug_opts_show(struct device_driver * ddp, char * buf)
  2298. {
  2299. return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
  2300. }
  2301. static ssize_t sdebug_opts_store(struct device_driver * ddp,
  2302. const char * buf, size_t count)
  2303. {
  2304. int opts;
  2305. char work[20];
  2306. if (1 == sscanf(buf, "%10s", work)) {
  2307. if (0 == strnicmp(work,"0x", 2)) {
  2308. if (1 == sscanf(&work[2], "%x", &opts))
  2309. goto opts_done;
  2310. } else {
  2311. if (1 == sscanf(work, "%d", &opts))
  2312. goto opts_done;
  2313. }
  2314. }
  2315. return -EINVAL;
  2316. opts_done:
  2317. scsi_debug_opts = opts;
  2318. scsi_debug_cmnd_count = 0;
  2319. return count;
  2320. }
  2321. DRIVER_ATTR(opts, S_IRUGO | S_IWUSR, sdebug_opts_show,
  2322. sdebug_opts_store);
  2323. static ssize_t sdebug_ptype_show(struct device_driver * ddp, char * buf)
  2324. {
  2325. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
  2326. }
  2327. static ssize_t sdebug_ptype_store(struct device_driver * ddp,
  2328. const char * buf, size_t count)
  2329. {
  2330. int n;
  2331. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2332. scsi_debug_ptype = n;
  2333. return count;
  2334. }
  2335. return -EINVAL;
  2336. }
  2337. DRIVER_ATTR(ptype, S_IRUGO | S_IWUSR, sdebug_ptype_show, sdebug_ptype_store);
  2338. static ssize_t sdebug_dsense_show(struct device_driver * ddp, char * buf)
  2339. {
  2340. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
  2341. }
  2342. static ssize_t sdebug_dsense_store(struct device_driver * ddp,
  2343. const char * buf, size_t count)
  2344. {
  2345. int n;
  2346. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2347. scsi_debug_dsense = n;
  2348. return count;
  2349. }
  2350. return -EINVAL;
  2351. }
  2352. DRIVER_ATTR(dsense, S_IRUGO | S_IWUSR, sdebug_dsense_show,
  2353. sdebug_dsense_store);
  2354. static ssize_t sdebug_fake_rw_show(struct device_driver * ddp, char * buf)
  2355. {
  2356. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_fake_rw);
  2357. }
  2358. static ssize_t sdebug_fake_rw_store(struct device_driver * ddp,
  2359. const char * buf, size_t count)
  2360. {
  2361. int n;
  2362. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2363. scsi_debug_fake_rw = n;
  2364. return count;
  2365. }
  2366. return -EINVAL;
  2367. }
  2368. DRIVER_ATTR(fake_rw, S_IRUGO | S_IWUSR, sdebug_fake_rw_show,
  2369. sdebug_fake_rw_store);
  2370. static ssize_t sdebug_no_lun_0_show(struct device_driver * ddp, char * buf)
  2371. {
  2372. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_lun_0);
  2373. }
  2374. static ssize_t sdebug_no_lun_0_store(struct device_driver * ddp,
  2375. const char * buf, size_t count)
  2376. {
  2377. int n;
  2378. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2379. scsi_debug_no_lun_0 = n;
  2380. return count;
  2381. }
  2382. return -EINVAL;
  2383. }
  2384. DRIVER_ATTR(no_lun_0, S_IRUGO | S_IWUSR, sdebug_no_lun_0_show,
  2385. sdebug_no_lun_0_store);
  2386. static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf)
  2387. {
  2388. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
  2389. }
  2390. static ssize_t sdebug_num_tgts_store(struct device_driver * ddp,
  2391. const char * buf, size_t count)
  2392. {
  2393. int n;
  2394. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2395. scsi_debug_num_tgts = n;
  2396. sdebug_max_tgts_luns();
  2397. return count;
  2398. }
  2399. return -EINVAL;
  2400. }
  2401. DRIVER_ATTR(num_tgts, S_IRUGO | S_IWUSR, sdebug_num_tgts_show,
  2402. sdebug_num_tgts_store);
  2403. static ssize_t sdebug_dev_size_mb_show(struct device_driver * ddp, char * buf)
  2404. {
  2405. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
  2406. }
  2407. DRIVER_ATTR(dev_size_mb, S_IRUGO, sdebug_dev_size_mb_show, NULL);
  2408. static ssize_t sdebug_num_parts_show(struct device_driver * ddp, char * buf)
  2409. {
  2410. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
  2411. }
  2412. DRIVER_ATTR(num_parts, S_IRUGO, sdebug_num_parts_show, NULL);
  2413. static ssize_t sdebug_every_nth_show(struct device_driver * ddp, char * buf)
  2414. {
  2415. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
  2416. }
  2417. static ssize_t sdebug_every_nth_store(struct device_driver * ddp,
  2418. const char * buf, size_t count)
  2419. {
  2420. int nth;
  2421. if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
  2422. scsi_debug_every_nth = nth;
  2423. scsi_debug_cmnd_count = 0;
  2424. return count;
  2425. }
  2426. return -EINVAL;
  2427. }
  2428. DRIVER_ATTR(every_nth, S_IRUGO | S_IWUSR, sdebug_every_nth_show,
  2429. sdebug_every_nth_store);
  2430. static ssize_t sdebug_max_luns_show(struct device_driver * ddp, char * buf)
  2431. {
  2432. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
  2433. }
  2434. static ssize_t sdebug_max_luns_store(struct device_driver * ddp,
  2435. const char * buf, size_t count)
  2436. {
  2437. int n;
  2438. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2439. scsi_debug_max_luns = n;
  2440. sdebug_max_tgts_luns();
  2441. return count;
  2442. }
  2443. return -EINVAL;
  2444. }
  2445. DRIVER_ATTR(max_luns, S_IRUGO | S_IWUSR, sdebug_max_luns_show,
  2446. sdebug_max_luns_store);
  2447. static ssize_t sdebug_scsi_level_show(struct device_driver * ddp, char * buf)
  2448. {
  2449. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
  2450. }
  2451. DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL);
  2452. static ssize_t sdebug_virtual_gb_show(struct device_driver * ddp, char * buf)
  2453. {
  2454. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb);
  2455. }
  2456. static ssize_t sdebug_virtual_gb_store(struct device_driver * ddp,
  2457. const char * buf, size_t count)
  2458. {
  2459. int n;
  2460. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2461. scsi_debug_virtual_gb = n;
  2462. if (scsi_debug_virtual_gb > 0) {
  2463. sdebug_capacity = 2048 * 1024;
  2464. sdebug_capacity *= scsi_debug_virtual_gb;
  2465. } else
  2466. sdebug_capacity = sdebug_store_sectors;
  2467. return count;
  2468. }
  2469. return -EINVAL;
  2470. }
  2471. DRIVER_ATTR(virtual_gb, S_IRUGO | S_IWUSR, sdebug_virtual_gb_show,
  2472. sdebug_virtual_gb_store);
  2473. static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf)
  2474. {
  2475. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
  2476. }
  2477. static ssize_t sdebug_add_host_store(struct device_driver * ddp,
  2478. const char * buf, size_t count)
  2479. {
  2480. int delta_hosts;
  2481. char work[20];
  2482. if (1 != sscanf(buf, "%10s", work))
  2483. return -EINVAL;
  2484. { /* temporary hack around sscanf() problem with -ve nums */
  2485. int neg = 0;
  2486. if ('-' == *work)
  2487. neg = 1;
  2488. if (1 != sscanf(work + neg, "%d", &delta_hosts))
  2489. return -EINVAL;
  2490. if (neg)
  2491. delta_hosts = -delta_hosts;
  2492. }
  2493. if (delta_hosts > 0) {
  2494. do {
  2495. sdebug_add_adapter();
  2496. } while (--delta_hosts);
  2497. } else if (delta_hosts < 0) {
  2498. do {
  2499. sdebug_remove_adapter();
  2500. } while (++delta_hosts);
  2501. }
  2502. return count;
  2503. }
  2504. DRIVER_ATTR(add_host, S_IRUGO | S_IWUSR, sdebug_add_host_show,
  2505. sdebug_add_host_store);
  2506. static ssize_t sdebug_vpd_use_hostno_show(struct device_driver * ddp,
  2507. char * buf)
  2508. {
  2509. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_vpd_use_hostno);
  2510. }
  2511. static ssize_t sdebug_vpd_use_hostno_store(struct device_driver * ddp,
  2512. const char * buf, size_t count)
  2513. {
  2514. int n;
  2515. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2516. scsi_debug_vpd_use_hostno = n;
  2517. return count;
  2518. }
  2519. return -EINVAL;
  2520. }
  2521. DRIVER_ATTR(vpd_use_hostno, S_IRUGO | S_IWUSR, sdebug_vpd_use_hostno_show,
  2522. sdebug_vpd_use_hostno_store);
  2523. /* Note: The following function creates attribute files in the
  2524. /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
  2525. files (over those found in the /sys/module/scsi_debug/parameters
  2526. directory) is that auxiliary actions can be triggered when an attribute
  2527. is changed. For example see: sdebug_add_host_store() above.
  2528. */
  2529. static int do_create_driverfs_files(void)
  2530. {
  2531. int ret;
  2532. ret = driver_create_file(&sdebug_driverfs_driver, &driver_attr_add_host);
  2533. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_delay);
  2534. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
  2535. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_dsense);
  2536. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
  2537. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_fake_rw);
  2538. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
  2539. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_no_lun_0);
  2540. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
  2541. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
  2542. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_ptype);
  2543. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_opts);
  2544. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
  2545. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_virtual_gb);
  2546. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_vpd_use_hostno);
  2547. return ret;
  2548. }
  2549. static void do_remove_driverfs_files(void)
  2550. {
  2551. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_vpd_use_hostno);
  2552. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_virtual_gb);
  2553. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
  2554. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_opts);
  2555. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_ptype);
  2556. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
  2557. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
  2558. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_no_lun_0);
  2559. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
  2560. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_fake_rw);
  2561. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
  2562. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dsense);
  2563. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
  2564. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_delay);
  2565. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_add_host);
  2566. }
  2567. static int __init scsi_debug_init(void)
  2568. {
  2569. unsigned int sz;
  2570. int host_to_add;
  2571. int k;
  2572. int ret;
  2573. if (scsi_debug_dev_size_mb < 1)
  2574. scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
  2575. sdebug_store_size = (unsigned int)scsi_debug_dev_size_mb * 1048576;
  2576. sdebug_store_sectors = sdebug_store_size / SECT_SIZE;
  2577. if (scsi_debug_virtual_gb > 0) {
  2578. sdebug_capacity = 2048 * 1024;
  2579. sdebug_capacity *= scsi_debug_virtual_gb;
  2580. } else
  2581. sdebug_capacity = sdebug_store_sectors;
  2582. /* play around with geometry, don't waste too much on track 0 */
  2583. sdebug_heads = 8;
  2584. sdebug_sectors_per = 32;
  2585. if (scsi_debug_dev_size_mb >= 16)
  2586. sdebug_heads = 32;
  2587. else if (scsi_debug_dev_size_mb >= 256)
  2588. sdebug_heads = 64;
  2589. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  2590. (sdebug_sectors_per * sdebug_heads);
  2591. if (sdebug_cylinders_per >= 1024) {
  2592. /* other LLDs do this; implies >= 1GB ram disk ... */
  2593. sdebug_heads = 255;
  2594. sdebug_sectors_per = 63;
  2595. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  2596. (sdebug_sectors_per * sdebug_heads);
  2597. }
  2598. sz = sdebug_store_size;
  2599. fake_storep = vmalloc(sz);
  2600. if (NULL == fake_storep) {
  2601. printk(KERN_ERR "scsi_debug_init: out of memory, 1\n");
  2602. return -ENOMEM;
  2603. }
  2604. memset(fake_storep, 0, sz);
  2605. if (scsi_debug_num_parts > 0)
  2606. sdebug_build_parts(fake_storep);
  2607. ret = device_register(&pseudo_primary);
  2608. if (ret < 0) {
  2609. printk(KERN_WARNING "scsi_debug: device_register error: %d\n",
  2610. ret);
  2611. goto free_vm;
  2612. }
  2613. ret = bus_register(&pseudo_lld_bus);
  2614. if (ret < 0) {
  2615. printk(KERN_WARNING "scsi_debug: bus_register error: %d\n",
  2616. ret);
  2617. goto dev_unreg;
  2618. }
  2619. ret = driver_register(&sdebug_driverfs_driver);
  2620. if (ret < 0) {
  2621. printk(KERN_WARNING "scsi_debug: driver_register error: %d\n",
  2622. ret);
  2623. goto bus_unreg;
  2624. }
  2625. ret = do_create_driverfs_files();
  2626. if (ret < 0) {
  2627. printk(KERN_WARNING "scsi_debug: driver_create_file error: %d\n",
  2628. ret);
  2629. goto del_files;
  2630. }
  2631. init_all_queued();
  2632. sdebug_driver_template.proc_name = (char *)sdebug_proc_name;
  2633. host_to_add = scsi_debug_add_host;
  2634. scsi_debug_add_host = 0;
  2635. for (k = 0; k < host_to_add; k++) {
  2636. if (sdebug_add_adapter()) {
  2637. printk(KERN_ERR "scsi_debug_init: "
  2638. "sdebug_add_adapter failed k=%d\n", k);
  2639. break;
  2640. }
  2641. }
  2642. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  2643. printk(KERN_INFO "scsi_debug_init: built %d host(s)\n",
  2644. scsi_debug_add_host);
  2645. }
  2646. return 0;
  2647. del_files:
  2648. do_remove_driverfs_files();
  2649. driver_unregister(&sdebug_driverfs_driver);
  2650. bus_unreg:
  2651. bus_unregister(&pseudo_lld_bus);
  2652. dev_unreg:
  2653. device_unregister(&pseudo_primary);
  2654. free_vm:
  2655. vfree(fake_storep);
  2656. return ret;
  2657. }
  2658. static void __exit scsi_debug_exit(void)
  2659. {
  2660. int k = scsi_debug_add_host;
  2661. stop_all_queued();
  2662. for (; k; k--)
  2663. sdebug_remove_adapter();
  2664. do_remove_driverfs_files();
  2665. driver_unregister(&sdebug_driverfs_driver);
  2666. bus_unregister(&pseudo_lld_bus);
  2667. device_unregister(&pseudo_primary);
  2668. vfree(fake_storep);
  2669. }
  2670. device_initcall(scsi_debug_init);
  2671. module_exit(scsi_debug_exit);
  2672. static void pseudo_0_release(struct device * dev)
  2673. {
  2674. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2675. printk(KERN_INFO "scsi_debug: pseudo_0_release() called\n");
  2676. }
  2677. static struct device pseudo_primary = {
  2678. .bus_id = "pseudo_0",
  2679. .release = pseudo_0_release,
  2680. };
  2681. static int pseudo_lld_bus_match(struct device *dev,
  2682. struct device_driver *dev_driver)
  2683. {
  2684. return 1;
  2685. }
  2686. static struct bus_type pseudo_lld_bus = {
  2687. .name = "pseudo",
  2688. .match = pseudo_lld_bus_match,
  2689. .probe = sdebug_driver_probe,
  2690. .remove = sdebug_driver_remove,
  2691. };
  2692. static void sdebug_release_adapter(struct device * dev)
  2693. {
  2694. struct sdebug_host_info *sdbg_host;
  2695. sdbg_host = to_sdebug_host(dev);
  2696. kfree(sdbg_host);
  2697. }
  2698. static int sdebug_add_adapter(void)
  2699. {
  2700. int k, devs_per_host;
  2701. int error = 0;
  2702. struct sdebug_host_info *sdbg_host;
  2703. struct sdebug_dev_info *sdbg_devinfo;
  2704. struct list_head *lh, *lh_sf;
  2705. sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL);
  2706. if (NULL == sdbg_host) {
  2707. printk(KERN_ERR "%s: out of memory at line %d\n",
  2708. __FUNCTION__, __LINE__);
  2709. return -ENOMEM;
  2710. }
  2711. INIT_LIST_HEAD(&sdbg_host->dev_info_list);
  2712. devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
  2713. for (k = 0; k < devs_per_host; k++) {
  2714. sdbg_devinfo = kzalloc(sizeof(*sdbg_devinfo),GFP_KERNEL);
  2715. if (NULL == sdbg_devinfo) {
  2716. printk(KERN_ERR "%s: out of memory at line %d\n",
  2717. __FUNCTION__, __LINE__);
  2718. error = -ENOMEM;
  2719. goto clean;
  2720. }
  2721. sdbg_devinfo->sdbg_host = sdbg_host;
  2722. list_add_tail(&sdbg_devinfo->dev_list,
  2723. &sdbg_host->dev_info_list);
  2724. }
  2725. spin_lock(&sdebug_host_list_lock);
  2726. list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
  2727. spin_unlock(&sdebug_host_list_lock);
  2728. sdbg_host->dev.bus = &pseudo_lld_bus;
  2729. sdbg_host->dev.parent = &pseudo_primary;
  2730. sdbg_host->dev.release = &sdebug_release_adapter;
  2731. sprintf(sdbg_host->dev.bus_id, "adapter%d", scsi_debug_add_host);
  2732. error = device_register(&sdbg_host->dev);
  2733. if (error)
  2734. goto clean;
  2735. ++scsi_debug_add_host;
  2736. return error;
  2737. clean:
  2738. list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
  2739. sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
  2740. dev_list);
  2741. list_del(&sdbg_devinfo->dev_list);
  2742. kfree(sdbg_devinfo);
  2743. }
  2744. kfree(sdbg_host);
  2745. return error;
  2746. }
  2747. static void sdebug_remove_adapter(void)
  2748. {
  2749. struct sdebug_host_info * sdbg_host = NULL;
  2750. spin_lock(&sdebug_host_list_lock);
  2751. if (!list_empty(&sdebug_host_list)) {
  2752. sdbg_host = list_entry(sdebug_host_list.prev,
  2753. struct sdebug_host_info, host_list);
  2754. list_del(&sdbg_host->host_list);
  2755. }
  2756. spin_unlock(&sdebug_host_list_lock);
  2757. if (!sdbg_host)
  2758. return;
  2759. device_unregister(&sdbg_host->dev);
  2760. --scsi_debug_add_host;
  2761. }
  2762. static int sdebug_driver_probe(struct device * dev)
  2763. {
  2764. int error = 0;
  2765. struct sdebug_host_info *sdbg_host;
  2766. struct Scsi_Host *hpnt;
  2767. sdbg_host = to_sdebug_host(dev);
  2768. hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
  2769. if (NULL == hpnt) {
  2770. printk(KERN_ERR "%s: scsi_register failed\n", __FUNCTION__);
  2771. error = -ENODEV;
  2772. return error;
  2773. }
  2774. sdbg_host->shost = hpnt;
  2775. *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
  2776. if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
  2777. hpnt->max_id = scsi_debug_num_tgts + 1;
  2778. else
  2779. hpnt->max_id = scsi_debug_num_tgts;
  2780. hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* = scsi_debug_max_luns; */
  2781. error = scsi_add_host(hpnt, &sdbg_host->dev);
  2782. if (error) {
  2783. printk(KERN_ERR "%s: scsi_add_host failed\n", __FUNCTION__);
  2784. error = -ENODEV;
  2785. scsi_host_put(hpnt);
  2786. } else
  2787. scsi_scan_host(hpnt);
  2788. return error;
  2789. }
  2790. static int sdebug_driver_remove(struct device * dev)
  2791. {
  2792. struct list_head *lh, *lh_sf;
  2793. struct sdebug_host_info *sdbg_host;
  2794. struct sdebug_dev_info *sdbg_devinfo;
  2795. sdbg_host = to_sdebug_host(dev);
  2796. if (!sdbg_host) {
  2797. printk(KERN_ERR "%s: Unable to locate host info\n",
  2798. __FUNCTION__);
  2799. return -ENODEV;
  2800. }
  2801. scsi_remove_host(sdbg_host->shost);
  2802. list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
  2803. sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
  2804. dev_list);
  2805. list_del(&sdbg_devinfo->dev_list);
  2806. kfree(sdbg_devinfo);
  2807. }
  2808. scsi_host_put(sdbg_host->shost);
  2809. return 0;
  2810. }
  2811. static void sdebug_max_tgts_luns(void)
  2812. {
  2813. struct sdebug_host_info * sdbg_host;
  2814. struct Scsi_Host *hpnt;
  2815. spin_lock(&sdebug_host_list_lock);
  2816. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  2817. hpnt = sdbg_host->shost;
  2818. if ((hpnt->this_id >= 0) &&
  2819. (scsi_debug_num_tgts > hpnt->this_id))
  2820. hpnt->max_id = scsi_debug_num_tgts + 1;
  2821. else
  2822. hpnt->max_id = scsi_debug_num_tgts;
  2823. hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* scsi_debug_max_luns; */
  2824. }
  2825. spin_unlock(&sdebug_host_list_lock);
  2826. }