scsi_debug.c 95 KB

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