[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / scsi / scsi_debug.c
blob322b5a41a36ff10046021f4e289eac7ddb630568
1 /*
2 * linux/kernel/scsi_debug.c
3 * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
4 * Copyright (C) 1992 Eric Youngdale
5 * Simulate a host adapter with 2 disks attached. Do a lot of checking
6 * to make sure that we are not getting blocks mixed up, and PANIC if
7 * anything out of the ordinary is seen.
8 * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 * This version is more generic, simulating a variable number of disk
11 * (or disk like devices) sharing a common amount of RAM
14 * For documentation see http://www.torque.net/sg/sdebug26.html
16 * D. Gilbert (dpg) work for Magneto-Optical device test [20010421]
17 * dpg: work for devfs large number of disks [20010809]
18 * forked for lk 2.5 series [20011216, 20020101]
19 * use vmalloc() more inquiry+mode_sense [20020302]
20 * add timers for delayed responses [20020721]
21 * Patrick Mansfield <patmans@us.ibm.com> max_luns+scsi_level [20021031]
22 * Mike Anderson <andmike@us.ibm.com> sysfs work [20021118]
23 * dpg: change style of boot options to "scsi_debug.num_tgts=2" and
24 * module options to "modprobe scsi_debug num_tgts=2" [20021221]
27 #include <linux/config.h>
28 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/genhd.h>
37 #include <linux/fs.h>
38 #include <linux/init.h>
39 #include <linux/proc_fs.h>
40 #include <linux/smp_lock.h>
41 #include <linux/vmalloc.h>
42 #include <linux/moduleparam.h>
44 #include <linux/blkdev.h>
45 #include "scsi.h"
46 #include <scsi/scsi_host.h>
47 #include <scsi/scsicam.h>
49 #include <linux/stat.h>
51 #ifndef LINUX_VERSION_CODE
52 #include <linux/version.h>
53 #endif
55 #include "scsi_logging.h"
56 #include "scsi_debug.h"
58 #define SCSI_DEBUG_VERSION "1.75"
59 static const char * scsi_debug_version_date = "20050113";
61 /* Additional Sense Code (ASC) used */
62 #define NO_ADDED_SENSE 0x0
63 #define UNRECOVERED_READ_ERR 0x11
64 #define INVALID_OPCODE 0x20
65 #define ADDR_OUT_OF_RANGE 0x21
66 #define INVALID_FIELD_IN_CDB 0x24
67 #define POWERON_RESET 0x29
68 #define SAVING_PARAMS_UNSUP 0x39
69 #define THRESHHOLD_EXCEEDED 0x5d
71 #define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
73 /* Default values for driver parameters */
74 #define DEF_NUM_HOST 1
75 #define DEF_NUM_TGTS 1
76 #define DEF_MAX_LUNS 1
77 /* With these defaults, this driver will make 1 host with 1 target
78 * (id 0) containing 1 logical unit (lun 0). That is 1 device.
80 #define DEF_DELAY 1
81 #define DEF_DEV_SIZE_MB 8
82 #define DEF_EVERY_NTH 0
83 #define DEF_NUM_PARTS 0
84 #define DEF_OPTS 0
85 #define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */
86 #define DEF_PTYPE 0
87 #define DEF_D_SENSE 0
89 /* bit mask values for scsi_debug_opts */
90 #define SCSI_DEBUG_OPT_NOISE 1
91 #define SCSI_DEBUG_OPT_MEDIUM_ERR 2
92 #define SCSI_DEBUG_OPT_TIMEOUT 4
93 #define SCSI_DEBUG_OPT_RECOVERED_ERR 8
94 /* When "every_nth" > 0 then modulo "every_nth" commands:
95 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
96 * - a RECOVERED_ERROR is simulated on successful read and write
97 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
99 * When "every_nth" < 0 then after "- every_nth" commands:
100 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
101 * - a RECOVERED_ERROR is simulated on successful read and write
102 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
103 * This will continue until some other action occurs (e.g. the user
104 * writing a new value (other than -1 or 1) to every_nth via sysfs).
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 */
111 /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
112 * or "peripheral device" addressing (value 0) */
113 #define SAM2_LUN_ADDRESS_METHOD 0
115 static int scsi_debug_add_host = DEF_NUM_HOST;
116 static int scsi_debug_delay = DEF_DELAY;
117 static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
118 static int scsi_debug_every_nth = DEF_EVERY_NTH;
119 static int scsi_debug_max_luns = DEF_MAX_LUNS;
120 static int scsi_debug_num_parts = DEF_NUM_PARTS;
121 static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
122 static int scsi_debug_opts = DEF_OPTS;
123 static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
124 static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
125 static int scsi_debug_dsense = DEF_D_SENSE;
127 static int scsi_debug_cmnd_count = 0;
129 #define DEV_READONLY(TGT) (0)
130 #define DEV_REMOVEABLE(TGT) (0)
132 static unsigned long sdebug_store_size; /* in bytes */
133 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 */
141 /* default sector size is 512 bytes, 2**9 bytes */
142 #define POW2_SECT_SIZE 9
143 #define SECT_SIZE (1 << POW2_SECT_SIZE)
144 #define SECT_SIZE_PER(TGT) SECT_SIZE
146 #define SDEBUG_MAX_PARTS 4
148 #define SDEBUG_SENSE_LEN 32
150 struct sdebug_dev_info {
151 struct list_head dev_list;
152 unsigned char sense_buff[SDEBUG_SENSE_LEN]; /* weak nexus */
153 unsigned int channel;
154 unsigned int target;
155 unsigned int lun;
156 struct sdebug_host_info *sdbg_host;
157 char reset;
158 char used;
161 struct sdebug_host_info {
162 struct list_head host_list;
163 struct Scsi_Host *shost;
164 struct device dev;
165 struct list_head dev_info_list;
168 #define to_sdebug_host(d) \
169 container_of(d, struct sdebug_host_info, dev)
171 static LIST_HEAD(sdebug_host_list);
172 static DEFINE_SPINLOCK(sdebug_host_list_lock);
174 typedef void (* done_funct_t) (struct scsi_cmnd *);
176 struct sdebug_queued_cmd {
177 int in_use;
178 struct timer_list cmnd_timer;
179 done_funct_t done_funct;
180 struct scsi_cmnd * a_cmnd;
181 int scsi_result;
183 static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
185 static Scsi_Host_Template sdebug_driver_template = {
186 .proc_info = scsi_debug_proc_info,
187 .name = "SCSI DEBUG",
188 .info = scsi_debug_info,
189 .slave_alloc = scsi_debug_slave_alloc,
190 .slave_configure = scsi_debug_slave_configure,
191 .slave_destroy = scsi_debug_slave_destroy,
192 .ioctl = scsi_debug_ioctl,
193 .queuecommand = scsi_debug_queuecommand,
194 .eh_abort_handler = scsi_debug_abort,
195 .eh_bus_reset_handler = scsi_debug_bus_reset,
196 .eh_device_reset_handler = scsi_debug_device_reset,
197 .eh_host_reset_handler = scsi_debug_host_reset,
198 .bios_param = scsi_debug_biosparam,
199 .can_queue = SCSI_DEBUG_CANQUEUE,
200 .this_id = 7,
201 .sg_tablesize = 64,
202 .cmd_per_lun = 3,
203 .max_sectors = 4096,
204 .unchecked_isa_dma = 0,
205 .use_clustering = DISABLE_CLUSTERING,
206 .module = THIS_MODULE,
209 static unsigned char * fake_storep; /* ramdisk storage */
211 static int num_aborts = 0;
212 static int num_dev_resets = 0;
213 static int num_bus_resets = 0;
214 static int num_host_resets = 0;
216 static DEFINE_SPINLOCK(queued_arr_lock);
217 static DEFINE_RWLOCK(atomic_rw);
219 static char sdebug_proc_name[] = "scsi_debug";
221 static int sdebug_driver_probe(struct device *);
222 static int sdebug_driver_remove(struct device *);
223 static struct bus_type pseudo_lld_bus;
225 static struct device_driver sdebug_driverfs_driver = {
226 .name = sdebug_proc_name,
227 .bus = &pseudo_lld_bus,
228 .probe = sdebug_driver_probe,
229 .remove = sdebug_driver_remove,
232 static const int check_condition_result =
233 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
235 /* function declarations */
236 static int resp_inquiry(struct scsi_cmnd * SCpnt, int target,
237 struct sdebug_dev_info * devip);
238 static int resp_requests(struct scsi_cmnd * SCpnt,
239 struct sdebug_dev_info * devip);
240 static int resp_readcap(struct scsi_cmnd * SCpnt,
241 struct sdebug_dev_info * devip);
242 static int resp_mode_sense(struct scsi_cmnd * SCpnt, int target,
243 struct sdebug_dev_info * devip);
244 static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block,
245 int num, struct sdebug_dev_info * devip);
246 static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block,
247 int num, struct sdebug_dev_info * devip);
248 static int resp_report_luns(struct scsi_cmnd * SCpnt,
249 struct sdebug_dev_info * devip);
250 static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
251 int arr_len);
252 static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
253 int max_arr_len);
254 static void timer_intr_handler(unsigned long);
255 static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev);
256 static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
257 int asc, int asq);
258 static int check_reset(struct scsi_cmnd * SCpnt,
259 struct sdebug_dev_info * devip);
260 static int schedule_resp(struct scsi_cmnd * cmnd,
261 struct sdebug_dev_info * devip,
262 done_funct_t done, int scsi_result, int delta_jiff);
263 static void __init sdebug_build_parts(unsigned char * ramp);
264 static void __init init_all_queued(void);
265 static void stop_all_queued(void);
266 static int stop_queued_cmnd(struct scsi_cmnd * cmnd);
267 static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
268 const char * dev_id_str, int dev_id_str_len);
269 static void do_create_driverfs_files(void);
270 static void do_remove_driverfs_files(void);
272 static int sdebug_add_adapter(void);
273 static void sdebug_remove_adapter(void);
274 static void sdebug_max_tgts_luns(void);
276 static struct device pseudo_primary;
277 static struct bus_type pseudo_lld_bus;
280 static
281 int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
283 unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
284 int block, upper_blk, num, k;
285 int errsts = 0;
286 int target = SCpnt->device->id;
287 struct sdebug_dev_info * devip = NULL;
288 int inj_recovered = 0;
290 if (done == NULL)
291 return 0; /* assume mid level reprocessing command */
293 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) {
294 printk(KERN_INFO "scsi_debug: cmd ");
295 for (k = 0, num = SCpnt->cmd_len; k < num; ++k)
296 printk("%02x ", (int)cmd[k]);
297 printk("\n");
299 if(target == sdebug_driver_template.this_id) {
300 printk(KERN_INFO "scsi_debug: initiator's id used as "
301 "target!\n");
302 return schedule_resp(SCpnt, NULL, done,
303 DID_NO_CONNECT << 16, 0);
306 if (SCpnt->device->lun >= scsi_debug_max_luns)
307 return schedule_resp(SCpnt, NULL, done,
308 DID_NO_CONNECT << 16, 0);
309 devip = devInfoReg(SCpnt->device);
310 if (NULL == devip)
311 return schedule_resp(SCpnt, NULL, done,
312 DID_NO_CONNECT << 16, 0);
314 if ((scsi_debug_every_nth != 0) &&
315 (++scsi_debug_cmnd_count >= abs(scsi_debug_every_nth))) {
316 scsi_debug_cmnd_count = 0;
317 if (scsi_debug_every_nth < -1)
318 scsi_debug_every_nth = -1;
319 if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
320 return 0; /* ignore command causing timeout */
321 else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
322 inj_recovered = 1; /* to reads and writes below */
325 switch (*cmd) {
326 case INQUIRY: /* mandatory, ignore unit attention */
327 errsts = resp_inquiry(SCpnt, target, devip);
328 break;
329 case REQUEST_SENSE: /* mandatory, ignore unit attention */
330 errsts = resp_requests(SCpnt, devip);
331 break;
332 case REZERO_UNIT: /* actually this is REWIND for SSC */
333 case START_STOP:
334 errsts = check_reset(SCpnt, devip);
335 break;
336 case ALLOW_MEDIUM_REMOVAL:
337 if ((errsts = check_reset(SCpnt, devip)))
338 break;
339 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
340 printk(KERN_INFO "scsi_debug: Medium removal %s\n",
341 cmd[4] ? "inhibited" : "enabled");
342 break;
343 case SEND_DIAGNOSTIC: /* mandatory */
344 errsts = check_reset(SCpnt, devip);
345 break;
346 case TEST_UNIT_READY: /* mandatory */
347 errsts = check_reset(SCpnt, devip);
348 break;
349 case RESERVE:
350 errsts = check_reset(SCpnt, devip);
351 break;
352 case RESERVE_10:
353 errsts = check_reset(SCpnt, devip);
354 break;
355 case RELEASE:
356 errsts = check_reset(SCpnt, devip);
357 break;
358 case RELEASE_10:
359 errsts = check_reset(SCpnt, devip);
360 break;
361 case READ_CAPACITY:
362 errsts = resp_readcap(SCpnt, devip);
363 break;
364 case READ_16:
365 case READ_12:
366 case READ_10:
367 case READ_6:
368 if ((errsts = check_reset(SCpnt, devip)))
369 break;
370 upper_blk = 0;
371 if ((*cmd) == READ_16) {
372 upper_blk = cmd[5] + (cmd[4] << 8) +
373 (cmd[3] << 16) + (cmd[2] << 24);
374 block = cmd[9] + (cmd[8] << 8) +
375 (cmd[7] << 16) + (cmd[6] << 24);
376 num = cmd[13] + (cmd[12] << 8) +
377 (cmd[11] << 16) + (cmd[10] << 24);
378 } else if ((*cmd) == READ_12) {
379 block = cmd[5] + (cmd[4] << 8) +
380 (cmd[3] << 16) + (cmd[2] << 24);
381 num = cmd[9] + (cmd[8] << 8) +
382 (cmd[7] << 16) + (cmd[6] << 24);
383 } else if ((*cmd) == READ_10) {
384 block = cmd[5] + (cmd[4] << 8) +
385 (cmd[3] << 16) + (cmd[2] << 24);
386 num = cmd[8] + (cmd[7] << 8);
387 } else {
388 block = cmd[3] + (cmd[2] << 8) +
389 ((cmd[1] & 0x1f) << 16);
390 num = cmd[4];
392 errsts = resp_read(SCpnt, upper_blk, block, num, devip);
393 if (inj_recovered && (0 == errsts)) {
394 mk_sense_buffer(devip, RECOVERED_ERROR,
395 THRESHHOLD_EXCEEDED, 0);
396 errsts = check_condition_result;
398 break;
399 case REPORT_LUNS: /* mandatory, ignore unit attention */
400 errsts = resp_report_luns(SCpnt, devip);
401 break;
402 case VERIFY: /* 10 byte SBC-2 command */
403 errsts = check_reset(SCpnt, devip);
404 break;
405 case WRITE_16:
406 case WRITE_12:
407 case WRITE_10:
408 case WRITE_6:
409 if ((errsts = check_reset(SCpnt, devip)))
410 break;
411 upper_blk = 0;
412 if ((*cmd) == WRITE_16) {
413 upper_blk = cmd[5] + (cmd[4] << 8) +
414 (cmd[3] << 16) + (cmd[2] << 24);
415 block = cmd[9] + (cmd[8] << 8) +
416 (cmd[7] << 16) + (cmd[6] << 24);
417 num = cmd[13] + (cmd[12] << 8) +
418 (cmd[11] << 16) + (cmd[10] << 24);
419 } else if ((*cmd) == WRITE_12) {
420 block = cmd[5] + (cmd[4] << 8) +
421 (cmd[3] << 16) + (cmd[2] << 24);
422 num = cmd[9] + (cmd[8] << 8) +
423 (cmd[7] << 16) + (cmd[6] << 24);
424 } else if ((*cmd) == WRITE_10) {
425 block = cmd[5] + (cmd[4] << 8) +
426 (cmd[3] << 16) + (cmd[2] << 24);
427 num = cmd[8] + (cmd[7] << 8);
428 } else {
429 block = cmd[3] + (cmd[2] << 8) +
430 ((cmd[1] & 0x1f) << 16);
431 num = cmd[4];
433 errsts = resp_write(SCpnt, upper_blk, block, num, devip);
434 if (inj_recovered && (0 == errsts)) {
435 mk_sense_buffer(devip, RECOVERED_ERROR,
436 THRESHHOLD_EXCEEDED, 0);
437 errsts = check_condition_result;
439 break;
440 case MODE_SENSE:
441 case MODE_SENSE_10:
442 errsts = resp_mode_sense(SCpnt, target, devip);
443 break;
444 case SYNCHRONIZE_CACHE:
445 errsts = check_reset(SCpnt, devip);
446 break;
447 default:
448 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
449 printk(KERN_INFO "scsi_debug: Opcode: 0x%x not "
450 "supported\n", *cmd);
451 if ((errsts = check_reset(SCpnt, devip)))
452 break; /* Unit attention takes precedence */
453 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
454 errsts = check_condition_result;
455 break;
457 return schedule_resp(SCpnt, devip, done, errsts, scsi_debug_delay);
460 static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
462 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
463 printk(KERN_INFO "scsi_debug: ioctl: cmd=0x%x\n", cmd);
465 return -EINVAL;
466 /* return -ENOTTY; // correct return but upsets fdisk */
469 static int check_reset(struct scsi_cmnd * SCpnt, struct sdebug_dev_info * devip)
471 if (devip->reset) {
472 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
473 printk(KERN_INFO "scsi_debug: Reporting Unit "
474 "attention: power on reset\n");
475 devip->reset = 0;
476 mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0);
477 return check_condition_result;
479 return 0;
482 /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
483 static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
484 int arr_len)
486 int k, req_len, act_len, len, active;
487 void * kaddr;
488 void * kaddr_off;
489 struct scatterlist * sgpnt;
491 if (0 == scp->request_bufflen)
492 return 0;
493 if (NULL == scp->request_buffer)
494 return (DID_ERROR << 16);
495 if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
496 (scp->sc_data_direction == DMA_FROM_DEVICE)))
497 return (DID_ERROR << 16);
498 if (0 == scp->use_sg) {
499 req_len = scp->request_bufflen;
500 act_len = (req_len < arr_len) ? req_len : arr_len;
501 memcpy(scp->request_buffer, arr, act_len);
502 scp->resid = req_len - act_len;
503 return 0;
505 sgpnt = (struct scatterlist *)scp->request_buffer;
506 active = 1;
507 for (k = 0, req_len = 0, act_len = 0; k < scp->use_sg; ++k, ++sgpnt) {
508 if (active) {
509 kaddr = (unsigned char *)
510 kmap_atomic(sgpnt->page, KM_USER0);
511 if (NULL == kaddr)
512 return (DID_ERROR << 16);
513 kaddr_off = (unsigned char *)kaddr + sgpnt->offset;
514 len = sgpnt->length;
515 if ((req_len + len) > arr_len) {
516 active = 0;
517 len = arr_len - req_len;
519 memcpy(kaddr_off, arr + req_len, len);
520 kunmap_atomic(kaddr, KM_USER0);
521 act_len += len;
523 req_len += sgpnt->length;
525 scp->resid = req_len - act_len;
526 return 0;
529 /* Returns number of bytes fetched into 'arr' or -1 if error. */
530 static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
531 int max_arr_len)
533 int k, req_len, len, fin;
534 void * kaddr;
535 void * kaddr_off;
536 struct scatterlist * sgpnt;
538 if (0 == scp->request_bufflen)
539 return 0;
540 if (NULL == scp->request_buffer)
541 return -1;
542 if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
543 (scp->sc_data_direction == DMA_TO_DEVICE)))
544 return -1;
545 if (0 == scp->use_sg) {
546 req_len = scp->request_bufflen;
547 len = (req_len < max_arr_len) ? req_len : max_arr_len;
548 memcpy(arr, scp->request_buffer, len);
549 return len;
551 sgpnt = (struct scatterlist *)scp->request_buffer;
552 for (k = 0, req_len = 0, fin = 0; k < scp->use_sg; ++k, ++sgpnt) {
553 kaddr = (unsigned char *)kmap_atomic(sgpnt->page, KM_USER0);
554 if (NULL == kaddr)
555 return -1;
556 kaddr_off = (unsigned char *)kaddr + sgpnt->offset;
557 len = sgpnt->length;
558 if ((req_len + len) > max_arr_len) {
559 len = max_arr_len - req_len;
560 fin = 1;
562 memcpy(arr + req_len, kaddr_off, len);
563 kunmap_atomic(kaddr, KM_USER0);
564 if (fin)
565 return req_len + len;
566 req_len += sgpnt->length;
568 return req_len;
572 static const char * inq_vendor_id = "Linux ";
573 static const char * inq_product_id = "scsi_debug ";
574 static const char * inq_product_rev = "0004";
576 static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
577 const char * dev_id_str, int dev_id_str_len)
579 int num;
581 /* Two identification descriptors: */
582 /* T10 vendor identifier field format (faked) */
583 arr[0] = 0x2; /* ASCII */
584 arr[1] = 0x1;
585 arr[2] = 0x0;
586 memcpy(&arr[4], inq_vendor_id, 8);
587 memcpy(&arr[12], inq_product_id, 16);
588 memcpy(&arr[28], dev_id_str, dev_id_str_len);
589 num = 8 + 16 + dev_id_str_len;
590 arr[3] = num;
591 num += 4;
592 /* NAA IEEE registered identifier (faked) */
593 arr[num] = 0x1; /* binary */
594 arr[num + 1] = 0x3;
595 arr[num + 2] = 0x0;
596 arr[num + 3] = 0x8;
597 arr[num + 4] = 0x51; /* ieee company id=0x123456 (faked) */
598 arr[num + 5] = 0x23;
599 arr[num + 6] = 0x45;
600 arr[num + 7] = 0x60;
601 arr[num + 8] = (dev_id_num >> 24);
602 arr[num + 9] = (dev_id_num >> 16) & 0xff;
603 arr[num + 10] = (dev_id_num >> 8) & 0xff;
604 arr[num + 11] = dev_id_num & 0xff;
605 return num + 12;
609 #define SDEBUG_LONG_INQ_SZ 96
610 #define SDEBUG_MAX_INQ_ARR_SZ 128
612 static int resp_inquiry(struct scsi_cmnd * scp, int target,
613 struct sdebug_dev_info * devip)
615 unsigned char pq_pdt;
616 unsigned char arr[SDEBUG_MAX_INQ_ARR_SZ];
617 unsigned char *cmd = (unsigned char *)scp->cmnd;
618 int alloc_len;
620 alloc_len = (cmd[3] << 8) + cmd[4];
621 memset(arr, 0, SDEBUG_MAX_INQ_ARR_SZ);
622 pq_pdt = (scsi_debug_ptype & 0x1f);
623 arr[0] = pq_pdt;
624 if (0x2 & cmd[1]) { /* CMDDT bit set */
625 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
627 return check_condition_result;
628 } else if (0x1 & cmd[1]) { /* EVPD bit set */
629 int dev_id_num, len;
630 char dev_id_str[6];
632 dev_id_num = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
633 (devip->target * 1000) + devip->lun;
634 len = scnprintf(dev_id_str, 6, "%d", dev_id_num);
635 if (0 == cmd[2]) { /* supported vital product data pages */
636 arr[3] = 3;
637 arr[4] = 0x0; /* this page */
638 arr[5] = 0x80; /* unit serial number */
639 arr[6] = 0x83; /* device identification */
640 } else if (0x80 == cmd[2]) { /* unit serial number */
641 arr[1] = 0x80;
642 arr[3] = len;
643 memcpy(&arr[4], dev_id_str, len);
644 } else if (0x83 == cmd[2]) { /* device identification */
645 arr[1] = 0x83;
646 arr[3] = inquiry_evpd_83(&arr[4], dev_id_num,
647 dev_id_str, len);
648 } else {
649 /* Illegal request, invalid field in cdb */
650 mk_sense_buffer(devip, ILLEGAL_REQUEST,
651 INVALID_FIELD_IN_CDB, 0);
652 return check_condition_result;
654 return fill_from_dev_buffer(scp, arr,
655 min(alloc_len, SDEBUG_MAX_INQ_ARR_SZ));
657 /* drops through here for a standard inquiry */
658 arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */
659 arr[2] = scsi_debug_scsi_level;
660 arr[3] = 2; /* response_data_format==2 */
661 arr[4] = SDEBUG_LONG_INQ_SZ - 5;
662 arr[6] = 0x1; /* claim: ADDR16 */
663 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
664 arr[7] = 0x3a; /* claim: WBUS16, SYNC, LINKED + CMDQUE */
665 memcpy(&arr[8], inq_vendor_id, 8);
666 memcpy(&arr[16], inq_product_id, 16);
667 memcpy(&arr[32], inq_product_rev, 4);
668 /* version descriptors (2 bytes each) follow */
669 arr[58] = 0x0; arr[59] = 0x40; /* SAM-2 */
670 arr[60] = 0x3; arr[61] = 0x0; /* SPC-3 */
671 if (scsi_debug_ptype == 0) {
672 arr[62] = 0x1; arr[63] = 0x80; /* SBC */
673 } else if (scsi_debug_ptype == 1) {
674 arr[62] = 0x2; arr[63] = 0x00; /* SSC */
676 return fill_from_dev_buffer(scp, arr,
677 min(alloc_len, SDEBUG_LONG_INQ_SZ));
680 static int resp_requests(struct scsi_cmnd * scp,
681 struct sdebug_dev_info * devip)
683 unsigned char * sbuff;
684 unsigned char *cmd = (unsigned char *)scp->cmnd;
685 unsigned char arr[SDEBUG_SENSE_LEN];
686 int len = 18;
688 memset(arr, 0, SDEBUG_SENSE_LEN);
689 if (devip->reset == 1)
690 mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0);
691 sbuff = devip->sense_buff;
692 if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
693 /* DESC bit set and sense_buff in fixed format */
694 arr[0] = 0x72;
695 arr[1] = sbuff[2]; /* sense key */
696 arr[2] = sbuff[12]; /* asc */
697 arr[3] = sbuff[13]; /* ascq */
698 len = 8;
699 } else
700 memcpy(arr, sbuff, SDEBUG_SENSE_LEN);
701 mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0);
702 return fill_from_dev_buffer(scp, arr, len);
705 #define SDEBUG_READCAP_ARR_SZ 8
706 static int resp_readcap(struct scsi_cmnd * scp,
707 struct sdebug_dev_info * devip)
709 unsigned char arr[SDEBUG_READCAP_ARR_SZ];
710 unsigned long capac;
711 int errsts;
713 if ((errsts = check_reset(scp, devip)))
714 return errsts;
715 memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
716 capac = (unsigned long)sdebug_capacity - 1;
717 arr[0] = (capac >> 24);
718 arr[1] = (capac >> 16) & 0xff;
719 arr[2] = (capac >> 8) & 0xff;
720 arr[3] = capac & 0xff;
721 arr[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
722 arr[7] = SECT_SIZE_PER(target) & 0xff;
723 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
726 /* <<Following mode page info copied from ST318451LW>> */
728 static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
729 { /* Read-Write Error Recovery page for mode_sense */
730 unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
731 5, 0, 0xff, 0xff};
733 memcpy(p, err_recov_pg, sizeof(err_recov_pg));
734 if (1 == pcontrol)
735 memset(p + 2, 0, sizeof(err_recov_pg) - 2);
736 return sizeof(err_recov_pg);
739 static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
740 { /* Disconnect-Reconnect page for mode_sense */
741 unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
742 0, 0, 0, 0, 0, 0, 0, 0};
744 memcpy(p, disconnect_pg, sizeof(disconnect_pg));
745 if (1 == pcontrol)
746 memset(p + 2, 0, sizeof(disconnect_pg) - 2);
747 return sizeof(disconnect_pg);
750 static int resp_format_pg(unsigned char * p, int pcontrol, int target)
751 { /* Format device page for mode_sense */
752 unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
753 0, 0, 0, 0, 0, 0, 0, 0,
754 0, 0, 0, 0, 0x40, 0, 0, 0};
756 memcpy(p, format_pg, sizeof(format_pg));
757 p[10] = (sdebug_sectors_per >> 8) & 0xff;
758 p[11] = sdebug_sectors_per & 0xff;
759 p[12] = (SECT_SIZE >> 8) & 0xff;
760 p[13] = SECT_SIZE & 0xff;
761 if (DEV_REMOVEABLE(target))
762 p[20] |= 0x20; /* should agree with INQUIRY */
763 if (1 == pcontrol)
764 memset(p + 2, 0, sizeof(format_pg) - 2);
765 return sizeof(format_pg);
768 static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
769 { /* Caching page for mode_sense */
770 unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
771 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
773 memcpy(p, caching_pg, sizeof(caching_pg));
774 if (1 == pcontrol)
775 memset(p + 2, 0, sizeof(caching_pg) - 2);
776 return sizeof(caching_pg);
779 static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
780 { /* Control mode page for mode_sense */
781 unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
782 0, 0, 0x2, 0x4b};
784 if (scsi_debug_dsense)
785 ctrl_m_pg[2] |= 0x4;
786 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
787 if (1 == pcontrol)
788 memset(p + 2, 0, sizeof(ctrl_m_pg) - 2);
789 return sizeof(ctrl_m_pg);
792 static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
793 { /* Informational Exceptions control mode page for mode_sense */
794 unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
795 0, 0, 0x0, 0x0};
796 memcpy(p, iec_m_pg, sizeof(iec_m_pg));
797 if (1 == pcontrol)
798 memset(p + 2, 0, sizeof(iec_m_pg) - 2);
799 return sizeof(iec_m_pg);
802 #define SDEBUG_MAX_MSENSE_SZ 256
804 static int resp_mode_sense(struct scsi_cmnd * scp, int target,
805 struct sdebug_dev_info * devip)
807 unsigned char dbd;
808 int pcontrol, pcode, subpcode;
809 unsigned char dev_spec;
810 int alloc_len, msense_6, offset, len, errsts;
811 unsigned char * ap;
812 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
813 unsigned char *cmd = (unsigned char *)scp->cmnd;
815 if ((errsts = check_reset(scp, devip)))
816 return errsts;
817 dbd = cmd[1] & 0x8;
818 pcontrol = (cmd[2] & 0xc0) >> 6;
819 pcode = cmd[2] & 0x3f;
820 subpcode = cmd[3];
821 msense_6 = (MODE_SENSE == cmd[0]);
822 alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
823 memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
824 if (0x3 == pcontrol) { /* Saving values not supported */
825 mk_sense_buffer(devip, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP,
827 return check_condition_result;
829 dev_spec = DEV_READONLY(target) ? 0x80 : 0x0;
830 if (msense_6) {
831 arr[2] = dev_spec;
832 offset = 4;
833 } else {
834 arr[3] = dev_spec;
835 offset = 8;
837 ap = arr + offset;
839 if (0 != subpcode) { /* TODO: Control Extension page */
840 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
842 return check_condition_result;
844 switch (pcode) {
845 case 0x1: /* Read-Write error recovery page, direct access */
846 len = resp_err_recov_pg(ap, pcontrol, target);
847 offset += len;
848 break;
849 case 0x2: /* Disconnect-Reconnect page, all devices */
850 len = resp_disconnect_pg(ap, pcontrol, target);
851 offset += len;
852 break;
853 case 0x3: /* Format device page, direct access */
854 len = resp_format_pg(ap, pcontrol, target);
855 offset += len;
856 break;
857 case 0x8: /* Caching page, direct access */
858 len = resp_caching_pg(ap, pcontrol, target);
859 offset += len;
860 break;
861 case 0xa: /* Control Mode page, all devices */
862 len = resp_ctrl_m_pg(ap, pcontrol, target);
863 offset += len;
864 break;
865 case 0x1c: /* Informational Exceptions Mode page, all devices */
866 len = resp_iec_m_pg(ap, pcontrol, target);
867 offset += len;
868 break;
869 case 0x3f: /* Read all Mode pages */
870 len = resp_err_recov_pg(ap, pcontrol, target);
871 len += resp_disconnect_pg(ap + len, pcontrol, target);
872 len += resp_format_pg(ap + len, pcontrol, target);
873 len += resp_caching_pg(ap + len, pcontrol, target);
874 len += resp_ctrl_m_pg(ap + len, pcontrol, target);
875 len += resp_iec_m_pg(ap + len, pcontrol, target);
876 offset += len;
877 break;
878 default:
879 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
881 return check_condition_result;
883 if (msense_6)
884 arr[0] = offset - 1;
885 else {
886 arr[0] = ((offset - 2) >> 8) & 0xff;
887 arr[1] = (offset - 2) & 0xff;
889 return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
892 static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block,
893 int num, struct sdebug_dev_info * devip)
895 unsigned long iflags;
896 int ret;
898 if (upper_blk || (block + num > sdebug_capacity)) {
899 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
901 return check_condition_result;
903 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
904 (block <= OPT_MEDIUM_ERR_ADDR) &&
905 ((block + num) > OPT_MEDIUM_ERR_ADDR)) {
906 mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR,
908 /* claim unrecoverable read error */
909 return check_condition_result;
911 read_lock_irqsave(&atomic_rw, iflags);
912 ret = fill_from_dev_buffer(SCpnt, fake_storep + (block * SECT_SIZE),
913 num * SECT_SIZE);
914 read_unlock_irqrestore(&atomic_rw, iflags);
915 return ret;
918 static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block,
919 int num, struct sdebug_dev_info * devip)
921 unsigned long iflags;
922 int res;
924 if (upper_blk || (block + num > sdebug_capacity)) {
925 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
927 return check_condition_result;
930 write_lock_irqsave(&atomic_rw, iflags);
931 res = fetch_to_dev_buffer(SCpnt, fake_storep + (block * SECT_SIZE),
932 num * SECT_SIZE);
933 write_unlock_irqrestore(&atomic_rw, iflags);
934 if (-1 == res)
935 return (DID_ERROR << 16);
936 else if ((res < (num * SECT_SIZE)) &&
937 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
938 printk(KERN_INFO "scsi_debug: write: cdb indicated=%d, "
939 " IO sent=%d bytes\n", num * SECT_SIZE, res);
940 return 0;
943 #define SDEBUG_RLUN_ARR_SZ 128
945 static int resp_report_luns(struct scsi_cmnd * scp,
946 struct sdebug_dev_info * devip)
948 unsigned int alloc_len;
949 int lun_cnt, i, upper;
950 unsigned char *cmd = (unsigned char *)scp->cmnd;
951 int select_report = (int)cmd[2];
952 struct scsi_lun *one_lun;
953 unsigned char arr[SDEBUG_RLUN_ARR_SZ];
955 alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
956 if ((alloc_len < 16) || (select_report > 2)) {
957 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
959 return check_condition_result;
961 /* can produce response with up to 16k luns (lun 0 to lun 16383) */
962 memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
963 lun_cnt = scsi_debug_max_luns;
964 arr[2] = ((sizeof(struct scsi_lun) * lun_cnt) >> 8) & 0xff;
965 arr[3] = (sizeof(struct scsi_lun) * lun_cnt) & 0xff;
966 lun_cnt = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
967 sizeof(struct scsi_lun)), lun_cnt);
968 one_lun = (struct scsi_lun *) &arr[8];
969 for (i = 0; i < lun_cnt; i++) {
970 upper = (i >> 8) & 0x3f;
971 if (upper)
972 one_lun[i].scsi_lun[0] =
973 (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
974 one_lun[i].scsi_lun[1] = i & 0xff;
976 return fill_from_dev_buffer(scp, arr,
977 min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
980 /* When timer goes off this function is called. */
981 static void timer_intr_handler(unsigned long indx)
983 struct sdebug_queued_cmd * sqcp;
984 unsigned long iflags;
986 if (indx >= SCSI_DEBUG_CANQUEUE) {
987 printk(KERN_ERR "scsi_debug:timer_intr_handler: indx too "
988 "large\n");
989 return;
991 spin_lock_irqsave(&queued_arr_lock, iflags);
992 sqcp = &queued_arr[(int)indx];
993 if (! sqcp->in_use) {
994 printk(KERN_ERR "scsi_debug:timer_intr_handler: Unexpected "
995 "interrupt\n");
996 spin_unlock_irqrestore(&queued_arr_lock, iflags);
997 return;
999 sqcp->in_use = 0;
1000 if (sqcp->done_funct) {
1001 sqcp->a_cmnd->result = sqcp->scsi_result;
1002 sqcp->done_funct(sqcp->a_cmnd); /* callback to mid level */
1004 sqcp->done_funct = NULL;
1005 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1008 static int scsi_debug_slave_alloc(struct scsi_device * sdp)
1010 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1011 printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %u>\n",
1012 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
1013 return 0;
1016 static int scsi_debug_slave_configure(struct scsi_device * sdp)
1018 struct sdebug_dev_info * devip;
1020 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1021 printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %u>\n",
1022 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
1023 if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
1024 sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
1025 devip = devInfoReg(sdp);
1026 sdp->hostdata = devip;
1027 if (sdp->host->cmd_per_lun)
1028 scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
1029 sdp->host->cmd_per_lun);
1030 return 0;
1033 static void scsi_debug_slave_destroy(struct scsi_device * sdp)
1035 struct sdebug_dev_info * devip =
1036 (struct sdebug_dev_info *)sdp->hostdata;
1038 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1039 printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %u>\n",
1040 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
1041 if (devip) {
1042 /* make this slot avaliable for re-use */
1043 devip->used = 0;
1044 sdp->hostdata = NULL;
1048 static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
1050 struct sdebug_host_info * sdbg_host;
1051 struct sdebug_dev_info * open_devip = NULL;
1052 struct sdebug_dev_info * devip =
1053 (struct sdebug_dev_info *)sdev->hostdata;
1055 if (devip)
1056 return devip;
1057 sdbg_host = *(struct sdebug_host_info **) sdev->host->hostdata;
1058 if(! sdbg_host) {
1059 printk(KERN_ERR "Host info NULL\n");
1060 return NULL;
1062 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
1063 if ((devip->used) && (devip->channel == sdev->channel) &&
1064 (devip->target == sdev->id) &&
1065 (devip->lun == sdev->lun))
1066 return devip;
1067 else {
1068 if ((!devip->used) && (!open_devip))
1069 open_devip = devip;
1072 if (NULL == open_devip) { /* try and make a new one */
1073 open_devip = kmalloc(sizeof(*open_devip),GFP_KERNEL);
1074 if (NULL == open_devip) {
1075 printk(KERN_ERR "%s: out of memory at line %d\n",
1076 __FUNCTION__, __LINE__);
1077 return NULL;
1079 memset(open_devip, 0, sizeof(*open_devip));
1080 open_devip->sdbg_host = sdbg_host;
1081 list_add_tail(&open_devip->dev_list,
1082 &sdbg_host->dev_info_list);
1084 if (open_devip) {
1085 open_devip->channel = sdev->channel;
1086 open_devip->target = sdev->id;
1087 open_devip->lun = sdev->lun;
1088 open_devip->sdbg_host = sdbg_host;
1089 open_devip->reset = 1;
1090 open_devip->used = 1;
1091 memset(open_devip->sense_buff, 0, SDEBUG_SENSE_LEN);
1092 if (scsi_debug_dsense)
1093 open_devip->sense_buff[0] = 0x72;
1094 else {
1095 open_devip->sense_buff[0] = 0x70;
1096 open_devip->sense_buff[7] = 0xa;
1098 return open_devip;
1100 return NULL;
1103 static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
1104 int asc, int asq)
1106 unsigned char * sbuff;
1108 sbuff = devip->sense_buff;
1109 memset(sbuff, 0, SDEBUG_SENSE_LEN);
1110 if (scsi_debug_dsense) {
1111 sbuff[0] = 0x72; /* descriptor, current */
1112 sbuff[1] = key;
1113 sbuff[2] = asc;
1114 sbuff[3] = asq;
1115 } else {
1116 sbuff[0] = 0x70; /* fixed, current */
1117 sbuff[2] = key;
1118 sbuff[7] = 0xa; /* implies 18 byte sense buffer */
1119 sbuff[12] = asc;
1120 sbuff[13] = asq;
1122 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1123 printk(KERN_INFO "scsi_debug: [sense_key,asc,ascq]: "
1124 "[0x%x,0x%x,0x%x]\n", key, asc, asq);
1127 static int scsi_debug_abort(struct scsi_cmnd * SCpnt)
1129 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1130 printk(KERN_INFO "scsi_debug: abort\n");
1131 ++num_aborts;
1132 stop_queued_cmnd(SCpnt);
1133 return SUCCESS;
1136 static int scsi_debug_biosparam(struct scsi_device *sdev,
1137 struct block_device * bdev, sector_t capacity, int *info)
1139 int res;
1140 unsigned char *buf;
1142 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1143 printk(KERN_INFO "scsi_debug: biosparam\n");
1144 buf = scsi_bios_ptable(bdev);
1145 if (buf) {
1146 res = scsi_partsize(buf, capacity,
1147 &info[2], &info[0], &info[1]);
1148 kfree(buf);
1149 if (! res)
1150 return res;
1152 info[0] = sdebug_heads;
1153 info[1] = sdebug_sectors_per;
1154 info[2] = sdebug_cylinders_per;
1155 return 0;
1158 static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
1160 struct sdebug_dev_info * devip;
1162 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1163 printk(KERN_INFO "scsi_debug: device_reset\n");
1164 ++num_dev_resets;
1165 if (SCpnt) {
1166 devip = devInfoReg(SCpnt->device);
1167 if (devip)
1168 devip->reset = 1;
1170 return SUCCESS;
1173 static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
1175 struct sdebug_host_info *sdbg_host;
1176 struct sdebug_dev_info * dev_info;
1177 struct scsi_device * sdp;
1178 struct Scsi_Host * hp;
1180 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1181 printk(KERN_INFO "scsi_debug: bus_reset\n");
1182 ++num_bus_resets;
1183 if (SCpnt && ((sdp = SCpnt->device)) && ((hp = sdp->host))) {
1184 sdbg_host = *(struct sdebug_host_info **) hp->hostdata;
1185 if (sdbg_host) {
1186 list_for_each_entry(dev_info,
1187 &sdbg_host->dev_info_list,
1188 dev_list)
1189 dev_info->reset = 1;
1192 return SUCCESS;
1195 static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
1197 struct sdebug_host_info * sdbg_host;
1198 struct sdebug_dev_info * dev_info;
1200 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1201 printk(KERN_INFO "scsi_debug: host_reset\n");
1202 ++num_host_resets;
1203 spin_lock(&sdebug_host_list_lock);
1204 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
1205 list_for_each_entry(dev_info, &sdbg_host->dev_info_list,
1206 dev_list)
1207 dev_info->reset = 1;
1209 spin_unlock(&sdebug_host_list_lock);
1210 stop_all_queued();
1211 return SUCCESS;
1214 /* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
1215 static int stop_queued_cmnd(struct scsi_cmnd * cmnd)
1217 unsigned long iflags;
1218 int k;
1219 struct sdebug_queued_cmd * sqcp;
1221 spin_lock_irqsave(&queued_arr_lock, iflags);
1222 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1223 sqcp = &queued_arr[k];
1224 if (sqcp->in_use && (cmnd == sqcp->a_cmnd)) {
1225 del_timer_sync(&sqcp->cmnd_timer);
1226 sqcp->in_use = 0;
1227 sqcp->a_cmnd = NULL;
1228 break;
1231 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1232 return (k < SCSI_DEBUG_CANQUEUE) ? 1 : 0;
1235 /* Deletes (stops) timers of all queued commands */
1236 static void stop_all_queued(void)
1238 unsigned long iflags;
1239 int k;
1240 struct sdebug_queued_cmd * sqcp;
1242 spin_lock_irqsave(&queued_arr_lock, iflags);
1243 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1244 sqcp = &queued_arr[k];
1245 if (sqcp->in_use && sqcp->a_cmnd) {
1246 del_timer_sync(&sqcp->cmnd_timer);
1247 sqcp->in_use = 0;
1248 sqcp->a_cmnd = NULL;
1251 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1254 /* Initializes timers in queued array */
1255 static void __init init_all_queued(void)
1257 unsigned long iflags;
1258 int k;
1259 struct sdebug_queued_cmd * sqcp;
1261 spin_lock_irqsave(&queued_arr_lock, iflags);
1262 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1263 sqcp = &queued_arr[k];
1264 init_timer(&sqcp->cmnd_timer);
1265 sqcp->in_use = 0;
1266 sqcp->a_cmnd = NULL;
1268 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1271 static void __init sdebug_build_parts(unsigned char * ramp)
1273 struct partition * pp;
1274 int starts[SDEBUG_MAX_PARTS + 2];
1275 int sectors_per_part, num_sectors, k;
1276 int heads_by_sects, start_sec, end_sec;
1278 /* assume partition table already zeroed */
1279 if ((scsi_debug_num_parts < 1) || (sdebug_store_size < 1048576))
1280 return;
1281 if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
1282 scsi_debug_num_parts = SDEBUG_MAX_PARTS;
1283 printk(KERN_WARNING "scsi_debug:build_parts: reducing "
1284 "partitions to %d\n", SDEBUG_MAX_PARTS);
1286 num_sectors = (int)(sdebug_store_size / SECT_SIZE);
1287 sectors_per_part = (num_sectors - sdebug_sectors_per)
1288 / scsi_debug_num_parts;
1289 heads_by_sects = sdebug_heads * sdebug_sectors_per;
1290 starts[0] = sdebug_sectors_per;
1291 for (k = 1; k < scsi_debug_num_parts; ++k)
1292 starts[k] = ((k * sectors_per_part) / heads_by_sects)
1293 * heads_by_sects;
1294 starts[scsi_debug_num_parts] = num_sectors;
1295 starts[scsi_debug_num_parts + 1] = 0;
1297 ramp[510] = 0x55; /* magic partition markings */
1298 ramp[511] = 0xAA;
1299 pp = (struct partition *)(ramp + 0x1be);
1300 for (k = 0; starts[k + 1]; ++k, ++pp) {
1301 start_sec = starts[k];
1302 end_sec = starts[k + 1] - 1;
1303 pp->boot_ind = 0;
1305 pp->cyl = start_sec / heads_by_sects;
1306 pp->head = (start_sec - (pp->cyl * heads_by_sects))
1307 / sdebug_sectors_per;
1308 pp->sector = (start_sec % sdebug_sectors_per) + 1;
1310 pp->end_cyl = end_sec / heads_by_sects;
1311 pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
1312 / sdebug_sectors_per;
1313 pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
1315 pp->start_sect = start_sec;
1316 pp->nr_sects = end_sec - start_sec + 1;
1317 pp->sys_ind = 0x83; /* plain Linux partition */
1321 static int schedule_resp(struct scsi_cmnd * cmnd,
1322 struct sdebug_dev_info * devip,
1323 done_funct_t done, int scsi_result, int delta_jiff)
1325 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmnd) {
1326 if (scsi_result) {
1327 struct scsi_device * sdp = cmnd->device;
1329 printk(KERN_INFO "scsi_debug: <%u %u %u %u> "
1330 "non-zero result=0x%x\n", sdp->host->host_no,
1331 sdp->channel, sdp->id, sdp->lun, scsi_result);
1334 if (cmnd && devip) {
1335 /* simulate autosense by this driver */
1336 if (SAM_STAT_CHECK_CONDITION == (scsi_result & 0xff))
1337 memcpy(cmnd->sense_buffer, devip->sense_buff,
1338 (SCSI_SENSE_BUFFERSIZE > SDEBUG_SENSE_LEN) ?
1339 SDEBUG_SENSE_LEN : SCSI_SENSE_BUFFERSIZE);
1341 if (delta_jiff <= 0) {
1342 if (cmnd)
1343 cmnd->result = scsi_result;
1344 if (done)
1345 done(cmnd);
1346 return 0;
1347 } else {
1348 unsigned long iflags;
1349 int k;
1350 struct sdebug_queued_cmd * sqcp = NULL;
1352 spin_lock_irqsave(&queued_arr_lock, iflags);
1353 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1354 sqcp = &queued_arr[k];
1355 if (! sqcp->in_use)
1356 break;
1358 if (k >= SCSI_DEBUG_CANQUEUE) {
1359 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1360 printk(KERN_WARNING "scsi_debug: can_queue exceeded\n");
1361 return 1; /* report busy to mid level */
1363 sqcp->in_use = 1;
1364 sqcp->a_cmnd = cmnd;
1365 sqcp->scsi_result = scsi_result;
1366 sqcp->done_funct = done;
1367 sqcp->cmnd_timer.function = timer_intr_handler;
1368 sqcp->cmnd_timer.data = k;
1369 sqcp->cmnd_timer.expires = jiffies + delta_jiff;
1370 add_timer(&sqcp->cmnd_timer);
1371 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1372 if (cmnd)
1373 cmnd->result = 0;
1374 return 0;
1378 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
1379 * of sysfs parameters (which module_param doesn't yet support).
1380 * Sysfs parameters defined explicitly below.
1382 module_param_named(add_host, scsi_debug_add_host, int, 0); /* perm=0644 */
1383 module_param_named(delay, scsi_debug_delay, int, 0); /* perm=0644 */
1384 module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, 0);
1385 module_param_named(dsense, scsi_debug_dsense, int, 0);
1386 module_param_named(every_nth, scsi_debug_every_nth, int, 0);
1387 module_param_named(max_luns, scsi_debug_max_luns, int, 0);
1388 module_param_named(num_parts, scsi_debug_num_parts, int, 0);
1389 module_param_named(num_tgts, scsi_debug_num_tgts, int, 0);
1390 module_param_named(opts, scsi_debug_opts, int, 0); /* perm=0644 */
1391 module_param_named(ptype, scsi_debug_ptype, int, 0);
1392 module_param_named(scsi_level, scsi_debug_scsi_level, int, 0);
1394 MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
1395 MODULE_DESCRIPTION("SCSI debug adapter driver");
1396 MODULE_LICENSE("GPL");
1397 MODULE_VERSION(SCSI_DEBUG_VERSION);
1399 MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
1400 MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
1401 MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs");
1402 MODULE_PARM_DESC(dsense, "use descriptor sense format(def: fixed)");
1403 MODULE_PARM_DESC(every_nth, "timeout every nth command(def=100)");
1404 MODULE_PARM_DESC(max_luns, "number of SCSI LUNs per target to simulate");
1405 MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
1406 MODULE_PARM_DESC(num_tgts, "number of SCSI targets per host to simulate");
1407 MODULE_PARM_DESC(opts, "1->noise, 2->medium_error, 4->...");
1408 MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
1409 MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])");
1412 static char sdebug_info[256];
1414 static const char * scsi_debug_info(struct Scsi_Host * shp)
1416 sprintf(sdebug_info, "scsi_debug, version %s [%s], "
1417 "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
1418 scsi_debug_version_date, scsi_debug_dev_size_mb,
1419 scsi_debug_opts);
1420 return sdebug_info;
1423 /* scsi_debug_proc_info
1424 * Used if the driver currently has no own support for /proc/scsi
1426 static int scsi_debug_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
1427 int length, int inout)
1429 int len, pos, begin;
1430 int orig_length;
1432 orig_length = length;
1434 if (inout == 1) {
1435 char arr[16];
1436 int minLen = length > 15 ? 15 : length;
1438 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1439 return -EACCES;
1440 memcpy(arr, buffer, minLen);
1441 arr[minLen] = '\0';
1442 if (1 != sscanf(arr, "%d", &pos))
1443 return -EINVAL;
1444 scsi_debug_opts = pos;
1445 if (scsi_debug_every_nth != 0)
1446 scsi_debug_cmnd_count = 0;
1447 return length;
1449 begin = 0;
1450 pos = len = sprintf(buffer, "scsi_debug adapter driver, version "
1451 "%s [%s]\n"
1452 "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
1453 "every_nth=%d(curr:%d)\n"
1454 "delay=%d, max_luns=%d, scsi_level=%d\n"
1455 "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
1456 "number of aborts=%d, device_reset=%d, bus_resets=%d, "
1457 "host_resets=%d\n",
1458 SCSI_DEBUG_VERSION, scsi_debug_version_date, scsi_debug_num_tgts,
1459 scsi_debug_dev_size_mb, scsi_debug_opts, scsi_debug_every_nth,
1460 scsi_debug_cmnd_count, scsi_debug_delay,
1461 scsi_debug_max_luns, scsi_debug_scsi_level,
1462 SECT_SIZE, sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
1463 num_aborts, num_dev_resets, num_bus_resets, num_host_resets);
1464 if (pos < offset) {
1465 len = 0;
1466 begin = pos;
1468 *start = buffer + (offset - begin); /* Start of wanted data */
1469 len -= (offset - begin);
1470 if (len > length)
1471 len = length;
1472 return len;
1475 static ssize_t sdebug_delay_show(struct device_driver * ddp, char * buf)
1477 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
1480 static ssize_t sdebug_delay_store(struct device_driver * ddp,
1481 const char * buf, size_t count)
1483 int delay;
1484 char work[20];
1486 if (1 == sscanf(buf, "%10s", work)) {
1487 if ((1 == sscanf(work, "%d", &delay)) && (delay >= 0)) {
1488 scsi_debug_delay = delay;
1489 return count;
1492 return -EINVAL;
1494 DRIVER_ATTR(delay, S_IRUGO | S_IWUSR, sdebug_delay_show,
1495 sdebug_delay_store);
1497 static ssize_t sdebug_opts_show(struct device_driver * ddp, char * buf)
1499 return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
1502 static ssize_t sdebug_opts_store(struct device_driver * ddp,
1503 const char * buf, size_t count)
1505 int opts;
1506 char work[20];
1508 if (1 == sscanf(buf, "%10s", work)) {
1509 if (0 == strnicmp(work,"0x", 2)) {
1510 if (1 == sscanf(&work[2], "%x", &opts))
1511 goto opts_done;
1512 } else {
1513 if (1 == sscanf(work, "%d", &opts))
1514 goto opts_done;
1517 return -EINVAL;
1518 opts_done:
1519 scsi_debug_opts = opts;
1520 scsi_debug_cmnd_count = 0;
1521 return count;
1523 DRIVER_ATTR(opts, S_IRUGO | S_IWUSR, sdebug_opts_show,
1524 sdebug_opts_store);
1526 static ssize_t sdebug_ptype_show(struct device_driver * ddp, char * buf)
1528 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
1530 static ssize_t sdebug_ptype_store(struct device_driver * ddp,
1531 const char * buf, size_t count)
1533 int n;
1535 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1536 scsi_debug_ptype = n;
1537 return count;
1539 return -EINVAL;
1541 DRIVER_ATTR(ptype, S_IRUGO | S_IWUSR, sdebug_ptype_show, sdebug_ptype_store);
1543 static ssize_t sdebug_dsense_show(struct device_driver * ddp, char * buf)
1545 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
1547 static ssize_t sdebug_dsense_store(struct device_driver * ddp,
1548 const char * buf, size_t count)
1550 int n;
1552 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1553 scsi_debug_dsense = n;
1554 return count;
1556 return -EINVAL;
1558 DRIVER_ATTR(dsense, S_IRUGO | S_IWUSR, sdebug_dsense_show,
1559 sdebug_dsense_store);
1561 static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf)
1563 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
1565 static ssize_t sdebug_num_tgts_store(struct device_driver * ddp,
1566 const char * buf, size_t count)
1568 int n;
1570 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1571 scsi_debug_num_tgts = n;
1572 sdebug_max_tgts_luns();
1573 return count;
1575 return -EINVAL;
1577 DRIVER_ATTR(num_tgts, S_IRUGO | S_IWUSR, sdebug_num_tgts_show,
1578 sdebug_num_tgts_store);
1580 static ssize_t sdebug_dev_size_mb_show(struct device_driver * ddp, char * buf)
1582 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
1584 DRIVER_ATTR(dev_size_mb, S_IRUGO, sdebug_dev_size_mb_show, NULL);
1586 static ssize_t sdebug_num_parts_show(struct device_driver * ddp, char * buf)
1588 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
1590 DRIVER_ATTR(num_parts, S_IRUGO, sdebug_num_parts_show, NULL);
1592 static ssize_t sdebug_every_nth_show(struct device_driver * ddp, char * buf)
1594 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
1596 static ssize_t sdebug_every_nth_store(struct device_driver * ddp,
1597 const char * buf, size_t count)
1599 int nth;
1601 if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
1602 scsi_debug_every_nth = nth;
1603 scsi_debug_cmnd_count = 0;
1604 return count;
1606 return -EINVAL;
1608 DRIVER_ATTR(every_nth, S_IRUGO | S_IWUSR, sdebug_every_nth_show,
1609 sdebug_every_nth_store);
1611 static ssize_t sdebug_max_luns_show(struct device_driver * ddp, char * buf)
1613 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
1615 static ssize_t sdebug_max_luns_store(struct device_driver * ddp,
1616 const char * buf, size_t count)
1618 int n;
1620 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1621 scsi_debug_max_luns = n;
1622 sdebug_max_tgts_luns();
1623 return count;
1625 return -EINVAL;
1627 DRIVER_ATTR(max_luns, S_IRUGO | S_IWUSR, sdebug_max_luns_show,
1628 sdebug_max_luns_store);
1630 static ssize_t sdebug_scsi_level_show(struct device_driver * ddp, char * buf)
1632 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
1634 DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL);
1636 static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf)
1638 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
1641 static ssize_t sdebug_add_host_store(struct device_driver * ddp,
1642 const char * buf, size_t count)
1644 int delta_hosts;
1645 char work[20];
1647 if (1 != sscanf(buf, "%10s", work))
1648 return -EINVAL;
1649 { /* temporary hack around sscanf() problem with -ve nums */
1650 int neg = 0;
1652 if ('-' == *work)
1653 neg = 1;
1654 if (1 != sscanf(work + neg, "%d", &delta_hosts))
1655 return -EINVAL;
1656 if (neg)
1657 delta_hosts = -delta_hosts;
1659 if (delta_hosts > 0) {
1660 do {
1661 sdebug_add_adapter();
1662 } while (--delta_hosts);
1663 } else if (delta_hosts < 0) {
1664 do {
1665 sdebug_remove_adapter();
1666 } while (++delta_hosts);
1668 return count;
1670 DRIVER_ATTR(add_host, S_IRUGO | S_IWUSR, sdebug_add_host_show,
1671 sdebug_add_host_store);
1673 static void do_create_driverfs_files(void)
1675 driver_create_file(&sdebug_driverfs_driver, &driver_attr_add_host);
1676 driver_create_file(&sdebug_driverfs_driver, &driver_attr_delay);
1677 driver_create_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
1678 driver_create_file(&sdebug_driverfs_driver, &driver_attr_dsense);
1679 driver_create_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
1680 driver_create_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
1681 driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
1682 driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
1683 driver_create_file(&sdebug_driverfs_driver, &driver_attr_ptype);
1684 driver_create_file(&sdebug_driverfs_driver, &driver_attr_opts);
1685 driver_create_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
1688 static void do_remove_driverfs_files(void)
1690 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
1691 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_opts);
1692 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_ptype);
1693 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
1694 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
1695 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
1696 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
1697 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dsense);
1698 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
1699 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_delay);
1700 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_add_host);
1703 static int __init scsi_debug_init(void)
1705 unsigned long sz;
1706 int host_to_add;
1707 int k;
1709 if (scsi_debug_dev_size_mb < 1)
1710 scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
1711 sdebug_store_size = (unsigned long)scsi_debug_dev_size_mb * 1048576;
1712 sdebug_capacity = sdebug_store_size / SECT_SIZE;
1714 /* play around with geometry, don't waste too much on track 0 */
1715 sdebug_heads = 8;
1716 sdebug_sectors_per = 32;
1717 if (scsi_debug_dev_size_mb >= 16)
1718 sdebug_heads = 32;
1719 else if (scsi_debug_dev_size_mb >= 256)
1720 sdebug_heads = 64;
1721 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
1722 (sdebug_sectors_per * sdebug_heads);
1723 if (sdebug_cylinders_per >= 1024) {
1724 /* other LLDs do this; implies >= 1GB ram disk ... */
1725 sdebug_heads = 255;
1726 sdebug_sectors_per = 63;
1727 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
1728 (sdebug_sectors_per * sdebug_heads);
1731 sz = sdebug_store_size;
1732 fake_storep = vmalloc(sz);
1733 if (NULL == fake_storep) {
1734 printk(KERN_ERR "scsi_debug_init: out of memory, 1\n");
1735 return -ENOMEM;
1737 memset(fake_storep, 0, sz);
1738 if (scsi_debug_num_parts > 0)
1739 sdebug_build_parts(fake_storep);
1741 init_all_queued();
1743 device_register(&pseudo_primary);
1744 bus_register(&pseudo_lld_bus);
1745 driver_register(&sdebug_driverfs_driver);
1746 do_create_driverfs_files();
1748 sdebug_driver_template.proc_name = (char *)sdebug_proc_name;
1750 host_to_add = scsi_debug_add_host;
1751 scsi_debug_add_host = 0;
1753 for (k = 0; k < host_to_add; k++) {
1754 if (sdebug_add_adapter()) {
1755 printk(KERN_ERR "scsi_debug_init: "
1756 "sdebug_add_adapter failed k=%d\n", k);
1757 break;
1761 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
1762 printk(KERN_INFO "scsi_debug_init: built %d host(s)\n",
1763 scsi_debug_add_host);
1765 return 0;
1768 static void __exit scsi_debug_exit(void)
1770 int k = scsi_debug_add_host;
1772 stop_all_queued();
1773 for (; k; k--)
1774 sdebug_remove_adapter();
1775 do_remove_driverfs_files();
1776 driver_unregister(&sdebug_driverfs_driver);
1777 bus_unregister(&pseudo_lld_bus);
1778 device_unregister(&pseudo_primary);
1780 vfree(fake_storep);
1783 device_initcall(scsi_debug_init);
1784 module_exit(scsi_debug_exit);
1786 static void pseudo_0_release(struct device * dev)
1788 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1789 printk(KERN_INFO "scsi_debug: pseudo_0_release() called\n");
1792 static struct device pseudo_primary = {
1793 .bus_id = "pseudo_0",
1794 .release = pseudo_0_release,
1797 static int pseudo_lld_bus_match(struct device *dev,
1798 struct device_driver *dev_driver)
1800 return 1;
1803 static struct bus_type pseudo_lld_bus = {
1804 .name = "pseudo",
1805 .match = pseudo_lld_bus_match,
1808 static void sdebug_release_adapter(struct device * dev)
1810 struct sdebug_host_info *sdbg_host;
1812 sdbg_host = to_sdebug_host(dev);
1813 kfree(sdbg_host);
1816 static int sdebug_add_adapter(void)
1818 int k, devs_per_host;
1819 int error = 0;
1820 struct sdebug_host_info *sdbg_host;
1821 struct sdebug_dev_info *sdbg_devinfo;
1822 struct list_head *lh, *lh_sf;
1824 sdbg_host = kmalloc(sizeof(*sdbg_host),GFP_KERNEL);
1826 if (NULL == sdbg_host) {
1827 printk(KERN_ERR "%s: out of memory at line %d\n",
1828 __FUNCTION__, __LINE__);
1829 return -ENOMEM;
1832 memset(sdbg_host, 0, sizeof(*sdbg_host));
1833 INIT_LIST_HEAD(&sdbg_host->dev_info_list);
1835 devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
1836 for (k = 0; k < devs_per_host; k++) {
1837 sdbg_devinfo = kmalloc(sizeof(*sdbg_devinfo),GFP_KERNEL);
1838 if (NULL == sdbg_devinfo) {
1839 printk(KERN_ERR "%s: out of memory at line %d\n",
1840 __FUNCTION__, __LINE__);
1841 error = -ENOMEM;
1842 goto clean;
1844 memset(sdbg_devinfo, 0, sizeof(*sdbg_devinfo));
1845 sdbg_devinfo->sdbg_host = sdbg_host;
1846 list_add_tail(&sdbg_devinfo->dev_list,
1847 &sdbg_host->dev_info_list);
1850 spin_lock(&sdebug_host_list_lock);
1851 list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
1852 spin_unlock(&sdebug_host_list_lock);
1854 sdbg_host->dev.bus = &pseudo_lld_bus;
1855 sdbg_host->dev.parent = &pseudo_primary;
1856 sdbg_host->dev.release = &sdebug_release_adapter;
1857 sprintf(sdbg_host->dev.bus_id, "adapter%d", scsi_debug_add_host);
1859 error = device_register(&sdbg_host->dev);
1861 if (error)
1862 goto clean;
1864 ++scsi_debug_add_host;
1865 return error;
1867 clean:
1868 list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
1869 sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
1870 dev_list);
1871 list_del(&sdbg_devinfo->dev_list);
1872 kfree(sdbg_devinfo);
1875 kfree(sdbg_host);
1876 return error;
1879 static void sdebug_remove_adapter(void)
1881 struct sdebug_host_info * sdbg_host = NULL;
1883 spin_lock(&sdebug_host_list_lock);
1884 if (!list_empty(&sdebug_host_list)) {
1885 sdbg_host = list_entry(sdebug_host_list.prev,
1886 struct sdebug_host_info, host_list);
1887 list_del(&sdbg_host->host_list);
1889 spin_unlock(&sdebug_host_list_lock);
1891 if (!sdbg_host)
1892 return;
1894 device_unregister(&sdbg_host->dev);
1895 --scsi_debug_add_host;
1898 static int sdebug_driver_probe(struct device * dev)
1900 int error = 0;
1901 struct sdebug_host_info *sdbg_host;
1902 struct Scsi_Host *hpnt;
1904 sdbg_host = to_sdebug_host(dev);
1906 hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
1907 if (NULL == hpnt) {
1908 printk(KERN_ERR "%s: scsi_register failed\n", __FUNCTION__);
1909 error = -ENODEV;
1910 return error;
1913 sdbg_host->shost = hpnt;
1914 *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
1915 if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
1916 hpnt->max_id = scsi_debug_num_tgts + 1;
1917 else
1918 hpnt->max_id = scsi_debug_num_tgts;
1919 hpnt->max_lun = scsi_debug_max_luns;
1921 error = scsi_add_host(hpnt, &sdbg_host->dev);
1922 if (error) {
1923 printk(KERN_ERR "%s: scsi_add_host failed\n", __FUNCTION__);
1924 error = -ENODEV;
1925 scsi_host_put(hpnt);
1926 } else
1927 scsi_scan_host(hpnt);
1930 return error;
1933 static int sdebug_driver_remove(struct device * dev)
1935 struct list_head *lh, *lh_sf;
1936 struct sdebug_host_info *sdbg_host;
1937 struct sdebug_dev_info *sdbg_devinfo;
1939 sdbg_host = to_sdebug_host(dev);
1941 if (!sdbg_host) {
1942 printk(KERN_ERR "%s: Unable to locate host info\n",
1943 __FUNCTION__);
1944 return -ENODEV;
1947 scsi_remove_host(sdbg_host->shost);
1949 list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
1950 sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
1951 dev_list);
1952 list_del(&sdbg_devinfo->dev_list);
1953 kfree(sdbg_devinfo);
1956 scsi_host_put(sdbg_host->shost);
1957 return 0;
1960 static void sdebug_max_tgts_luns(void)
1962 struct sdebug_host_info * sdbg_host;
1963 struct Scsi_Host *hpnt;
1965 spin_lock(&sdebug_host_list_lock);
1966 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
1967 hpnt = sdbg_host->shost;
1968 if ((hpnt->this_id >= 0) &&
1969 (scsi_debug_num_tgts > hpnt->this_id))
1970 hpnt->max_id = scsi_debug_num_tgts + 1;
1971 else
1972 hpnt->max_id = scsi_debug_num_tgts;
1973 hpnt->max_lun = scsi_debug_max_luns;
1975 spin_unlock(&sdebug_host_list_lock);