scsi_debug.c 95 KB

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