scsi_debug.c 91 KB

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