scsi_debug.c 85 KB

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