scsi_debug.c 91 KB

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