blob: 3ded9daaf4a01c21732f7e7d4b3fb67d5e19fda4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9 *
10 * This version is more generic, simulating a variable number of disk
11 * (or disk like devices) sharing a common amount of RAM
12 *
13 *
14 * For documentation see http://www.torque.net/sg/sdebug26.html
15 *
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]
25 */
26
27#include <linux/config.h>
28#include <linux/module.h>
29
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>
43
44#include <linux/blkdev.h>
45#include "scsi.h"
46#include <scsi/scsi_host.h>
47#include <scsi/scsicam.h>
48
49#include <linux/stat.h>
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "scsi_logging.h"
52#include "scsi_debug.h"
53
54#define SCSI_DEBUG_VERSION "1.75"
55static const char * scsi_debug_version_date = "20050113";
56
57/* Additional Sense Code (ASC) used */
58#define NO_ADDED_SENSE 0x0
59#define UNRECOVERED_READ_ERR 0x11
60#define INVALID_OPCODE 0x20
61#define ADDR_OUT_OF_RANGE 0x21
62#define INVALID_FIELD_IN_CDB 0x24
63#define POWERON_RESET 0x29
64#define SAVING_PARAMS_UNSUP 0x39
65#define THRESHHOLD_EXCEEDED 0x5d
66
67#define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
68
69/* Default values for driver parameters */
70#define DEF_NUM_HOST 1
71#define DEF_NUM_TGTS 1
72#define DEF_MAX_LUNS 1
73/* With these defaults, this driver will make 1 host with 1 target
74 * (id 0) containing 1 logical unit (lun 0). That is 1 device.
75 */
76#define DEF_DELAY 1
77#define DEF_DEV_SIZE_MB 8
78#define DEF_EVERY_NTH 0
79#define DEF_NUM_PARTS 0
80#define DEF_OPTS 0
81#define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */
82#define DEF_PTYPE 0
83#define DEF_D_SENSE 0
84
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/* When "every_nth" > 0 then modulo "every_nth" commands:
91 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
92 * - a RECOVERED_ERROR is simulated on successful read and write
93 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
94 *
95 * When "every_nth" < 0 then after "- every_nth" commands:
96 * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
97 * - a RECOVERED_ERROR is simulated on successful read and write
98 * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
99 * This will continue until some other action occurs (e.g. the user
100 * writing a new value (other than -1 or 1) to every_nth via sysfs).
101 */
102
103/* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
104 * sector on read commands: */
105#define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
106
107/* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
108 * or "peripheral device" addressing (value 0) */
109#define SAM2_LUN_ADDRESS_METHOD 0
110
111static int scsi_debug_add_host = DEF_NUM_HOST;
112static int scsi_debug_delay = DEF_DELAY;
113static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
114static int scsi_debug_every_nth = DEF_EVERY_NTH;
115static int scsi_debug_max_luns = DEF_MAX_LUNS;
116static int scsi_debug_num_parts = DEF_NUM_PARTS;
117static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
118static int scsi_debug_opts = DEF_OPTS;
119static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
120static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
121static int scsi_debug_dsense = DEF_D_SENSE;
122
123static int scsi_debug_cmnd_count = 0;
124
125#define DEV_READONLY(TGT) (0)
126#define DEV_REMOVEABLE(TGT) (0)
127
128static unsigned long sdebug_store_size; /* in bytes */
129static sector_t sdebug_capacity; /* in sectors */
130
131/* old BIOS stuff, kernel may get rid of them but some mode sense pages
132 may still need them */
133static int sdebug_heads; /* heads per disk */
134static int sdebug_cylinders_per; /* cylinders per surface */
135static int sdebug_sectors_per; /* sectors per cylinder */
136
137/* default sector size is 512 bytes, 2**9 bytes */
138#define POW2_SECT_SIZE 9
139#define SECT_SIZE (1 << POW2_SECT_SIZE)
140#define SECT_SIZE_PER(TGT) SECT_SIZE
141
142#define SDEBUG_MAX_PARTS 4
143
144#define SDEBUG_SENSE_LEN 32
145
146struct 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 char reset;
154 char used;
155};
156
157struct sdebug_host_info {
158 struct list_head host_list;
159 struct Scsi_Host *shost;
160 struct device dev;
161 struct list_head dev_info_list;
162};
163
164#define to_sdebug_host(d) \
165 container_of(d, struct sdebug_host_info, dev)
166
167static LIST_HEAD(sdebug_host_list);
168static DEFINE_SPINLOCK(sdebug_host_list_lock);
169
170typedef void (* done_funct_t) (struct scsi_cmnd *);
171
172struct sdebug_queued_cmd {
173 int in_use;
174 struct timer_list cmnd_timer;
175 done_funct_t done_funct;
176 struct scsi_cmnd * a_cmnd;
177 int scsi_result;
178};
179static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
180
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100181static struct scsi_host_template sdebug_driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .proc_info = scsi_debug_proc_info,
183 .name = "SCSI DEBUG",
184 .info = scsi_debug_info,
185 .slave_alloc = scsi_debug_slave_alloc,
186 .slave_configure = scsi_debug_slave_configure,
187 .slave_destroy = scsi_debug_slave_destroy,
188 .ioctl = scsi_debug_ioctl,
189 .queuecommand = scsi_debug_queuecommand,
190 .eh_abort_handler = scsi_debug_abort,
191 .eh_bus_reset_handler = scsi_debug_bus_reset,
192 .eh_device_reset_handler = scsi_debug_device_reset,
193 .eh_host_reset_handler = scsi_debug_host_reset,
194 .bios_param = scsi_debug_biosparam,
195 .can_queue = SCSI_DEBUG_CANQUEUE,
196 .this_id = 7,
197 .sg_tablesize = 64,
198 .cmd_per_lun = 3,
199 .max_sectors = 4096,
200 .unchecked_isa_dma = 0,
201 .use_clustering = DISABLE_CLUSTERING,
202 .module = THIS_MODULE,
203};
204
205static unsigned char * fake_storep; /* ramdisk storage */
206
207static int num_aborts = 0;
208static int num_dev_resets = 0;
209static int num_bus_resets = 0;
210static int num_host_resets = 0;
211
212static DEFINE_SPINLOCK(queued_arr_lock);
213static DEFINE_RWLOCK(atomic_rw);
214
215static char sdebug_proc_name[] = "scsi_debug";
216
217static int sdebug_driver_probe(struct device *);
218static int sdebug_driver_remove(struct device *);
219static struct bus_type pseudo_lld_bus;
220
221static struct device_driver sdebug_driverfs_driver = {
222 .name = sdebug_proc_name,
223 .bus = &pseudo_lld_bus,
224 .probe = sdebug_driver_probe,
225 .remove = sdebug_driver_remove,
226};
227
228static const int check_condition_result =
229 (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
230
231/* function declarations */
232static int resp_inquiry(struct scsi_cmnd * SCpnt, int target,
233 struct sdebug_dev_info * devip);
234static int resp_requests(struct scsi_cmnd * SCpnt,
235 struct sdebug_dev_info * devip);
236static int resp_readcap(struct scsi_cmnd * SCpnt,
237 struct sdebug_dev_info * devip);
238static int resp_mode_sense(struct scsi_cmnd * SCpnt, int target,
239 struct sdebug_dev_info * devip);
240static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block,
241 int num, struct sdebug_dev_info * devip);
242static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block,
243 int num, struct sdebug_dev_info * devip);
244static int resp_report_luns(struct scsi_cmnd * SCpnt,
245 struct sdebug_dev_info * devip);
246static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
247 int arr_len);
248static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
249 int max_arr_len);
250static void timer_intr_handler(unsigned long);
251static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev);
252static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
253 int asc, int asq);
254static int check_reset(struct scsi_cmnd * SCpnt,
255 struct sdebug_dev_info * devip);
256static int schedule_resp(struct scsi_cmnd * cmnd,
257 struct sdebug_dev_info * devip,
258 done_funct_t done, int scsi_result, int delta_jiff);
259static void __init sdebug_build_parts(unsigned char * ramp);
260static void __init init_all_queued(void);
261static void stop_all_queued(void);
262static int stop_queued_cmnd(struct scsi_cmnd * cmnd);
263static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
264 const char * dev_id_str, int dev_id_str_len);
265static void do_create_driverfs_files(void);
266static void do_remove_driverfs_files(void);
267
268static int sdebug_add_adapter(void);
269static void sdebug_remove_adapter(void);
270static void sdebug_max_tgts_luns(void);
271
272static struct device pseudo_primary;
273static struct bus_type pseudo_lld_bus;
274
275
276static
277int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
278{
279 unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
280 int block, upper_blk, num, k;
281 int errsts = 0;
Jeff Garzik422c0d62005-10-24 18:05:09 -0400282 int target = scmd_id(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct sdebug_dev_info * devip = NULL;
284 int inj_recovered = 0;
285
286 if (done == NULL)
287 return 0; /* assume mid level reprocessing command */
288
289 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) {
290 printk(KERN_INFO "scsi_debug: cmd ");
291 for (k = 0, num = SCpnt->cmd_len; k < num; ++k)
292 printk("%02x ", (int)cmd[k]);
293 printk("\n");
294 }
295 if(target == sdebug_driver_template.this_id) {
296 printk(KERN_INFO "scsi_debug: initiator's id used as "
297 "target!\n");
298 return schedule_resp(SCpnt, NULL, done,
299 DID_NO_CONNECT << 16, 0);
300 }
301
302 if (SCpnt->device->lun >= scsi_debug_max_luns)
303 return schedule_resp(SCpnt, NULL, done,
304 DID_NO_CONNECT << 16, 0);
305 devip = devInfoReg(SCpnt->device);
306 if (NULL == devip)
307 return schedule_resp(SCpnt, NULL, done,
308 DID_NO_CONNECT << 16, 0);
309
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 }
320
321 switch (*cmd) {
322 case INQUIRY: /* mandatory, ignore unit attention */
323 errsts = resp_inquiry(SCpnt, target, devip);
324 break;
325 case REQUEST_SENSE: /* mandatory, ignore unit attention */
326 errsts = resp_requests(SCpnt, devip);
327 break;
328 case REZERO_UNIT: /* actually this is REWIND for SSC */
329 case START_STOP:
330 errsts = check_reset(SCpnt, devip);
331 break;
332 case ALLOW_MEDIUM_REMOVAL:
333 if ((errsts = check_reset(SCpnt, devip)))
334 break;
335 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
336 printk(KERN_INFO "scsi_debug: Medium removal %s\n",
337 cmd[4] ? "inhibited" : "enabled");
338 break;
339 case SEND_DIAGNOSTIC: /* mandatory */
340 errsts = check_reset(SCpnt, devip);
341 break;
342 case TEST_UNIT_READY: /* mandatory */
343 errsts = check_reset(SCpnt, devip);
344 break;
345 case RESERVE:
346 errsts = check_reset(SCpnt, devip);
347 break;
348 case RESERVE_10:
349 errsts = check_reset(SCpnt, devip);
350 break;
351 case RELEASE:
352 errsts = check_reset(SCpnt, devip);
353 break;
354 case RELEASE_10:
355 errsts = check_reset(SCpnt, devip);
356 break;
357 case READ_CAPACITY:
358 errsts = resp_readcap(SCpnt, devip);
359 break;
360 case READ_16:
361 case READ_12:
362 case READ_10:
363 case READ_6:
364 if ((errsts = check_reset(SCpnt, devip)))
365 break;
366 upper_blk = 0;
367 if ((*cmd) == READ_16) {
368 upper_blk = cmd[5] + (cmd[4] << 8) +
369 (cmd[3] << 16) + (cmd[2] << 24);
370 block = cmd[9] + (cmd[8] << 8) +
371 (cmd[7] << 16) + (cmd[6] << 24);
372 num = cmd[13] + (cmd[12] << 8) +
373 (cmd[11] << 16) + (cmd[10] << 24);
374 } else if ((*cmd) == READ_12) {
375 block = cmd[5] + (cmd[4] << 8) +
376 (cmd[3] << 16) + (cmd[2] << 24);
377 num = cmd[9] + (cmd[8] << 8) +
378 (cmd[7] << 16) + (cmd[6] << 24);
379 } else if ((*cmd) == READ_10) {
380 block = cmd[5] + (cmd[4] << 8) +
381 (cmd[3] << 16) + (cmd[2] << 24);
382 num = cmd[8] + (cmd[7] << 8);
383 } else {
384 block = cmd[3] + (cmd[2] << 8) +
385 ((cmd[1] & 0x1f) << 16);
386 num = cmd[4];
387 }
388 errsts = resp_read(SCpnt, upper_blk, block, num, devip);
389 if (inj_recovered && (0 == errsts)) {
390 mk_sense_buffer(devip, RECOVERED_ERROR,
391 THRESHHOLD_EXCEEDED, 0);
392 errsts = check_condition_result;
393 }
394 break;
395 case REPORT_LUNS: /* mandatory, ignore unit attention */
396 errsts = resp_report_luns(SCpnt, devip);
397 break;
398 case VERIFY: /* 10 byte SBC-2 command */
399 errsts = check_reset(SCpnt, devip);
400 break;
401 case WRITE_16:
402 case WRITE_12:
403 case WRITE_10:
404 case WRITE_6:
405 if ((errsts = check_reset(SCpnt, devip)))
406 break;
407 upper_blk = 0;
408 if ((*cmd) == WRITE_16) {
409 upper_blk = cmd[5] + (cmd[4] << 8) +
410 (cmd[3] << 16) + (cmd[2] << 24);
411 block = cmd[9] + (cmd[8] << 8) +
412 (cmd[7] << 16) + (cmd[6] << 24);
413 num = cmd[13] + (cmd[12] << 8) +
414 (cmd[11] << 16) + (cmd[10] << 24);
415 } else if ((*cmd) == WRITE_12) {
416 block = cmd[5] + (cmd[4] << 8) +
417 (cmd[3] << 16) + (cmd[2] << 24);
418 num = cmd[9] + (cmd[8] << 8) +
419 (cmd[7] << 16) + (cmd[6] << 24);
420 } else if ((*cmd) == WRITE_10) {
421 block = cmd[5] + (cmd[4] << 8) +
422 (cmd[3] << 16) + (cmd[2] << 24);
423 num = cmd[8] + (cmd[7] << 8);
424 } else {
425 block = cmd[3] + (cmd[2] << 8) +
426 ((cmd[1] & 0x1f) << 16);
427 num = cmd[4];
428 }
429 errsts = resp_write(SCpnt, upper_blk, block, num, devip);
430 if (inj_recovered && (0 == errsts)) {
431 mk_sense_buffer(devip, RECOVERED_ERROR,
432 THRESHHOLD_EXCEEDED, 0);
433 errsts = check_condition_result;
434 }
435 break;
436 case MODE_SENSE:
437 case MODE_SENSE_10:
438 errsts = resp_mode_sense(SCpnt, target, devip);
439 break;
440 case SYNCHRONIZE_CACHE:
441 errsts = check_reset(SCpnt, devip);
442 break;
443 default:
444 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
445 printk(KERN_INFO "scsi_debug: Opcode: 0x%x not "
446 "supported\n", *cmd);
447 if ((errsts = check_reset(SCpnt, devip)))
448 break; /* Unit attention takes precedence */
449 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
450 errsts = check_condition_result;
451 break;
452 }
453 return schedule_resp(SCpnt, devip, done, errsts, scsi_debug_delay);
454}
455
456static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
457{
458 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
459 printk(KERN_INFO "scsi_debug: ioctl: cmd=0x%x\n", cmd);
460 }
461 return -EINVAL;
462 /* return -ENOTTY; // correct return but upsets fdisk */
463}
464
465static int check_reset(struct scsi_cmnd * SCpnt, struct sdebug_dev_info * devip)
466{
467 if (devip->reset) {
468 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
469 printk(KERN_INFO "scsi_debug: Reporting Unit "
470 "attention: power on reset\n");
471 devip->reset = 0;
472 mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0);
473 return check_condition_result;
474 }
475 return 0;
476}
477
478/* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
479static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
480 int arr_len)
481{
482 int k, req_len, act_len, len, active;
483 void * kaddr;
484 void * kaddr_off;
485 struct scatterlist * sgpnt;
486
487 if (0 == scp->request_bufflen)
488 return 0;
489 if (NULL == scp->request_buffer)
490 return (DID_ERROR << 16);
491 if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
492 (scp->sc_data_direction == DMA_FROM_DEVICE)))
493 return (DID_ERROR << 16);
494 if (0 == scp->use_sg) {
495 req_len = scp->request_bufflen;
496 act_len = (req_len < arr_len) ? req_len : arr_len;
497 memcpy(scp->request_buffer, arr, act_len);
498 scp->resid = req_len - act_len;
499 return 0;
500 }
501 sgpnt = (struct scatterlist *)scp->request_buffer;
502 active = 1;
503 for (k = 0, req_len = 0, act_len = 0; k < scp->use_sg; ++k, ++sgpnt) {
504 if (active) {
505 kaddr = (unsigned char *)
506 kmap_atomic(sgpnt->page, KM_USER0);
507 if (NULL == kaddr)
508 return (DID_ERROR << 16);
509 kaddr_off = (unsigned char *)kaddr + sgpnt->offset;
510 len = sgpnt->length;
511 if ((req_len + len) > arr_len) {
512 active = 0;
513 len = arr_len - req_len;
514 }
515 memcpy(kaddr_off, arr + req_len, len);
516 kunmap_atomic(kaddr, KM_USER0);
517 act_len += len;
518 }
519 req_len += sgpnt->length;
520 }
521 scp->resid = req_len - act_len;
522 return 0;
523}
524
525/* Returns number of bytes fetched into 'arr' or -1 if error. */
526static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
527 int max_arr_len)
528{
529 int k, req_len, len, fin;
530 void * kaddr;
531 void * kaddr_off;
532 struct scatterlist * sgpnt;
533
534 if (0 == scp->request_bufflen)
535 return 0;
536 if (NULL == scp->request_buffer)
537 return -1;
538 if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
539 (scp->sc_data_direction == DMA_TO_DEVICE)))
540 return -1;
541 if (0 == scp->use_sg) {
542 req_len = scp->request_bufflen;
543 len = (req_len < max_arr_len) ? req_len : max_arr_len;
544 memcpy(arr, scp->request_buffer, len);
545 return len;
546 }
547 sgpnt = (struct scatterlist *)scp->request_buffer;
548 for (k = 0, req_len = 0, fin = 0; k < scp->use_sg; ++k, ++sgpnt) {
549 kaddr = (unsigned char *)kmap_atomic(sgpnt->page, KM_USER0);
550 if (NULL == kaddr)
551 return -1;
552 kaddr_off = (unsigned char *)kaddr + sgpnt->offset;
553 len = sgpnt->length;
554 if ((req_len + len) > max_arr_len) {
555 len = max_arr_len - req_len;
556 fin = 1;
557 }
558 memcpy(arr + req_len, kaddr_off, len);
559 kunmap_atomic(kaddr, KM_USER0);
560 if (fin)
561 return req_len + len;
562 req_len += sgpnt->length;
563 }
564 return req_len;
565}
566
567
568static const char * inq_vendor_id = "Linux ";
569static const char * inq_product_id = "scsi_debug ";
570static const char * inq_product_rev = "0004";
571
572static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
573 const char * dev_id_str, int dev_id_str_len)
574{
575 int num;
576
577 /* Two identification descriptors: */
578 /* T10 vendor identifier field format (faked) */
579 arr[0] = 0x2; /* ASCII */
580 arr[1] = 0x1;
581 arr[2] = 0x0;
582 memcpy(&arr[4], inq_vendor_id, 8);
583 memcpy(&arr[12], inq_product_id, 16);
584 memcpy(&arr[28], dev_id_str, dev_id_str_len);
585 num = 8 + 16 + dev_id_str_len;
586 arr[3] = num;
587 num += 4;
588 /* NAA IEEE registered identifier (faked) */
589 arr[num] = 0x1; /* binary */
590 arr[num + 1] = 0x3;
591 arr[num + 2] = 0x0;
592 arr[num + 3] = 0x8;
593 arr[num + 4] = 0x51; /* ieee company id=0x123456 (faked) */
594 arr[num + 5] = 0x23;
595 arr[num + 6] = 0x45;
596 arr[num + 7] = 0x60;
597 arr[num + 8] = (dev_id_num >> 24);
598 arr[num + 9] = (dev_id_num >> 16) & 0xff;
599 arr[num + 10] = (dev_id_num >> 8) & 0xff;
600 arr[num + 11] = dev_id_num & 0xff;
601 return num + 12;
602}
603
604
605#define SDEBUG_LONG_INQ_SZ 96
606#define SDEBUG_MAX_INQ_ARR_SZ 128
607
608static int resp_inquiry(struct scsi_cmnd * scp, int target,
609 struct sdebug_dev_info * devip)
610{
611 unsigned char pq_pdt;
612 unsigned char arr[SDEBUG_MAX_INQ_ARR_SZ];
613 unsigned char *cmd = (unsigned char *)scp->cmnd;
614 int alloc_len;
615
616 alloc_len = (cmd[3] << 8) + cmd[4];
617 memset(arr, 0, SDEBUG_MAX_INQ_ARR_SZ);
618 pq_pdt = (scsi_debug_ptype & 0x1f);
619 arr[0] = pq_pdt;
620 if (0x2 & cmd[1]) { /* CMDDT bit set */
621 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
622 0);
623 return check_condition_result;
624 } else if (0x1 & cmd[1]) { /* EVPD bit set */
625 int dev_id_num, len;
626 char dev_id_str[6];
627
628 dev_id_num = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
629 (devip->target * 1000) + devip->lun;
630 len = scnprintf(dev_id_str, 6, "%d", dev_id_num);
631 if (0 == cmd[2]) { /* supported vital product data pages */
632 arr[3] = 3;
633 arr[4] = 0x0; /* this page */
634 arr[5] = 0x80; /* unit serial number */
635 arr[6] = 0x83; /* device identification */
636 } else if (0x80 == cmd[2]) { /* unit serial number */
637 arr[1] = 0x80;
638 arr[3] = len;
639 memcpy(&arr[4], dev_id_str, len);
640 } else if (0x83 == cmd[2]) { /* device identification */
641 arr[1] = 0x83;
642 arr[3] = inquiry_evpd_83(&arr[4], dev_id_num,
643 dev_id_str, len);
644 } else {
645 /* Illegal request, invalid field in cdb */
646 mk_sense_buffer(devip, ILLEGAL_REQUEST,
647 INVALID_FIELD_IN_CDB, 0);
648 return check_condition_result;
649 }
650 return fill_from_dev_buffer(scp, arr,
651 min(alloc_len, SDEBUG_MAX_INQ_ARR_SZ));
652 }
653 /* drops through here for a standard inquiry */
654 arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */
655 arr[2] = scsi_debug_scsi_level;
656 arr[3] = 2; /* response_data_format==2 */
657 arr[4] = SDEBUG_LONG_INQ_SZ - 5;
658 arr[6] = 0x1; /* claim: ADDR16 */
659 /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
660 arr[7] = 0x3a; /* claim: WBUS16, SYNC, LINKED + CMDQUE */
661 memcpy(&arr[8], inq_vendor_id, 8);
662 memcpy(&arr[16], inq_product_id, 16);
663 memcpy(&arr[32], inq_product_rev, 4);
664 /* version descriptors (2 bytes each) follow */
665 arr[58] = 0x0; arr[59] = 0x40; /* SAM-2 */
666 arr[60] = 0x3; arr[61] = 0x0; /* SPC-3 */
667 if (scsi_debug_ptype == 0) {
668 arr[62] = 0x1; arr[63] = 0x80; /* SBC */
669 } else if (scsi_debug_ptype == 1) {
670 arr[62] = 0x2; arr[63] = 0x00; /* SSC */
671 }
672 return fill_from_dev_buffer(scp, arr,
673 min(alloc_len, SDEBUG_LONG_INQ_SZ));
674}
675
676static int resp_requests(struct scsi_cmnd * scp,
677 struct sdebug_dev_info * devip)
678{
679 unsigned char * sbuff;
680 unsigned char *cmd = (unsigned char *)scp->cmnd;
681 unsigned char arr[SDEBUG_SENSE_LEN];
682 int len = 18;
683
684 memset(arr, 0, SDEBUG_SENSE_LEN);
685 if (devip->reset == 1)
686 mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0);
687 sbuff = devip->sense_buff;
688 if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
689 /* DESC bit set and sense_buff in fixed format */
690 arr[0] = 0x72;
691 arr[1] = sbuff[2]; /* sense key */
692 arr[2] = sbuff[12]; /* asc */
693 arr[3] = sbuff[13]; /* ascq */
694 len = 8;
695 } else
696 memcpy(arr, sbuff, SDEBUG_SENSE_LEN);
697 mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0);
698 return fill_from_dev_buffer(scp, arr, len);
699}
700
701#define SDEBUG_READCAP_ARR_SZ 8
702static int resp_readcap(struct scsi_cmnd * scp,
703 struct sdebug_dev_info * devip)
704{
705 unsigned char arr[SDEBUG_READCAP_ARR_SZ];
706 unsigned long capac;
707 int errsts;
708
709 if ((errsts = check_reset(scp, devip)))
710 return errsts;
711 memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
712 capac = (unsigned long)sdebug_capacity - 1;
713 arr[0] = (capac >> 24);
714 arr[1] = (capac >> 16) & 0xff;
715 arr[2] = (capac >> 8) & 0xff;
716 arr[3] = capac & 0xff;
717 arr[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
718 arr[7] = SECT_SIZE_PER(target) & 0xff;
719 return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
720}
721
722/* <<Following mode page info copied from ST318451LW>> */
723
724static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
725{ /* Read-Write Error Recovery page for mode_sense */
726 unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
727 5, 0, 0xff, 0xff};
728
729 memcpy(p, err_recov_pg, sizeof(err_recov_pg));
730 if (1 == pcontrol)
731 memset(p + 2, 0, sizeof(err_recov_pg) - 2);
732 return sizeof(err_recov_pg);
733}
734
735static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
736{ /* Disconnect-Reconnect page for mode_sense */
737 unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
738 0, 0, 0, 0, 0, 0, 0, 0};
739
740 memcpy(p, disconnect_pg, sizeof(disconnect_pg));
741 if (1 == pcontrol)
742 memset(p + 2, 0, sizeof(disconnect_pg) - 2);
743 return sizeof(disconnect_pg);
744}
745
746static int resp_format_pg(unsigned char * p, int pcontrol, int target)
747{ /* Format device page for mode_sense */
748 unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
749 0, 0, 0, 0, 0, 0, 0, 0,
750 0, 0, 0, 0, 0x40, 0, 0, 0};
751
752 memcpy(p, format_pg, sizeof(format_pg));
753 p[10] = (sdebug_sectors_per >> 8) & 0xff;
754 p[11] = sdebug_sectors_per & 0xff;
755 p[12] = (SECT_SIZE >> 8) & 0xff;
756 p[13] = SECT_SIZE & 0xff;
757 if (DEV_REMOVEABLE(target))
758 p[20] |= 0x20; /* should agree with INQUIRY */
759 if (1 == pcontrol)
760 memset(p + 2, 0, sizeof(format_pg) - 2);
761 return sizeof(format_pg);
762}
763
764static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
765{ /* Caching page for mode_sense */
766 unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
767 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
768
769 memcpy(p, caching_pg, sizeof(caching_pg));
770 if (1 == pcontrol)
771 memset(p + 2, 0, sizeof(caching_pg) - 2);
772 return sizeof(caching_pg);
773}
774
775static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
776{ /* Control mode page for mode_sense */
777 unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
778 0, 0, 0x2, 0x4b};
779
780 if (scsi_debug_dsense)
781 ctrl_m_pg[2] |= 0x4;
782 memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
783 if (1 == pcontrol)
784 memset(p + 2, 0, sizeof(ctrl_m_pg) - 2);
785 return sizeof(ctrl_m_pg);
786}
787
788static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
789{ /* Informational Exceptions control mode page for mode_sense */
790 unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
791 0, 0, 0x0, 0x0};
792 memcpy(p, iec_m_pg, sizeof(iec_m_pg));
793 if (1 == pcontrol)
794 memset(p + 2, 0, sizeof(iec_m_pg) - 2);
795 return sizeof(iec_m_pg);
796}
797
798#define SDEBUG_MAX_MSENSE_SZ 256
799
800static int resp_mode_sense(struct scsi_cmnd * scp, int target,
801 struct sdebug_dev_info * devip)
802{
803 unsigned char dbd;
804 int pcontrol, pcode, subpcode;
805 unsigned char dev_spec;
806 int alloc_len, msense_6, offset, len, errsts;
807 unsigned char * ap;
808 unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
809 unsigned char *cmd = (unsigned char *)scp->cmnd;
810
811 if ((errsts = check_reset(scp, devip)))
812 return errsts;
813 dbd = cmd[1] & 0x8;
814 pcontrol = (cmd[2] & 0xc0) >> 6;
815 pcode = cmd[2] & 0x3f;
816 subpcode = cmd[3];
817 msense_6 = (MODE_SENSE == cmd[0]);
818 alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
819 memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
820 if (0x3 == pcontrol) { /* Saving values not supported */
821 mk_sense_buffer(devip, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP,
822 0);
823 return check_condition_result;
824 }
825 dev_spec = DEV_READONLY(target) ? 0x80 : 0x0;
826 if (msense_6) {
827 arr[2] = dev_spec;
828 offset = 4;
829 } else {
830 arr[3] = dev_spec;
831 offset = 8;
832 }
833 ap = arr + offset;
834
835 if (0 != subpcode) { /* TODO: Control Extension page */
836 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
837 0);
838 return check_condition_result;
839 }
840 switch (pcode) {
841 case 0x1: /* Read-Write error recovery page, direct access */
842 len = resp_err_recov_pg(ap, pcontrol, target);
843 offset += len;
844 break;
845 case 0x2: /* Disconnect-Reconnect page, all devices */
846 len = resp_disconnect_pg(ap, pcontrol, target);
847 offset += len;
848 break;
849 case 0x3: /* Format device page, direct access */
850 len = resp_format_pg(ap, pcontrol, target);
851 offset += len;
852 break;
853 case 0x8: /* Caching page, direct access */
854 len = resp_caching_pg(ap, pcontrol, target);
855 offset += len;
856 break;
857 case 0xa: /* Control Mode page, all devices */
858 len = resp_ctrl_m_pg(ap, pcontrol, target);
859 offset += len;
860 break;
861 case 0x1c: /* Informational Exceptions Mode page, all devices */
862 len = resp_iec_m_pg(ap, pcontrol, target);
863 offset += len;
864 break;
865 case 0x3f: /* Read all Mode pages */
866 len = resp_err_recov_pg(ap, pcontrol, target);
867 len += resp_disconnect_pg(ap + len, pcontrol, target);
868 len += resp_format_pg(ap + len, pcontrol, target);
869 len += resp_caching_pg(ap + len, pcontrol, target);
870 len += resp_ctrl_m_pg(ap + len, pcontrol, target);
871 len += resp_iec_m_pg(ap + len, pcontrol, target);
872 offset += len;
873 break;
874 default:
875 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
876 0);
877 return check_condition_result;
878 }
879 if (msense_6)
880 arr[0] = offset - 1;
881 else {
882 arr[0] = ((offset - 2) >> 8) & 0xff;
883 arr[1] = (offset - 2) & 0xff;
884 }
885 return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
886}
887
888static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block,
889 int num, struct sdebug_dev_info * devip)
890{
891 unsigned long iflags;
892 int ret;
893
894 if (upper_blk || (block + num > sdebug_capacity)) {
895 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
896 0);
897 return check_condition_result;
898 }
899 if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
900 (block <= OPT_MEDIUM_ERR_ADDR) &&
901 ((block + num) > OPT_MEDIUM_ERR_ADDR)) {
902 mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR,
903 0);
904 /* claim unrecoverable read error */
905 return check_condition_result;
906 }
907 read_lock_irqsave(&atomic_rw, iflags);
908 ret = fill_from_dev_buffer(SCpnt, fake_storep + (block * SECT_SIZE),
909 num * SECT_SIZE);
910 read_unlock_irqrestore(&atomic_rw, iflags);
911 return ret;
912}
913
914static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block,
915 int num, struct sdebug_dev_info * devip)
916{
917 unsigned long iflags;
918 int res;
919
920 if (upper_blk || (block + num > sdebug_capacity)) {
921 mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
922 0);
923 return check_condition_result;
924 }
925
926 write_lock_irqsave(&atomic_rw, iflags);
927 res = fetch_to_dev_buffer(SCpnt, fake_storep + (block * SECT_SIZE),
928 num * SECT_SIZE);
929 write_unlock_irqrestore(&atomic_rw, iflags);
930 if (-1 == res)
931 return (DID_ERROR << 16);
932 else if ((res < (num * SECT_SIZE)) &&
933 (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
934 printk(KERN_INFO "scsi_debug: write: cdb indicated=%d, "
935 " IO sent=%d bytes\n", num * SECT_SIZE, res);
936 return 0;
937}
938
939#define SDEBUG_RLUN_ARR_SZ 128
940
941static int resp_report_luns(struct scsi_cmnd * scp,
942 struct sdebug_dev_info * devip)
943{
944 unsigned int alloc_len;
945 int lun_cnt, i, upper;
946 unsigned char *cmd = (unsigned char *)scp->cmnd;
947 int select_report = (int)cmd[2];
948 struct scsi_lun *one_lun;
949 unsigned char arr[SDEBUG_RLUN_ARR_SZ];
950
951 alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
952 if ((alloc_len < 16) || (select_report > 2)) {
953 mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
954 0);
955 return check_condition_result;
956 }
957 /* can produce response with up to 16k luns (lun 0 to lun 16383) */
958 memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
959 lun_cnt = scsi_debug_max_luns;
960 arr[2] = ((sizeof(struct scsi_lun) * lun_cnt) >> 8) & 0xff;
961 arr[3] = (sizeof(struct scsi_lun) * lun_cnt) & 0xff;
962 lun_cnt = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
963 sizeof(struct scsi_lun)), lun_cnt);
964 one_lun = (struct scsi_lun *) &arr[8];
965 for (i = 0; i < lun_cnt; i++) {
966 upper = (i >> 8) & 0x3f;
967 if (upper)
968 one_lun[i].scsi_lun[0] =
969 (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
970 one_lun[i].scsi_lun[1] = i & 0xff;
971 }
972 return fill_from_dev_buffer(scp, arr,
973 min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
974}
975
976/* When timer goes off this function is called. */
977static void timer_intr_handler(unsigned long indx)
978{
979 struct sdebug_queued_cmd * sqcp;
980 unsigned long iflags;
981
982 if (indx >= SCSI_DEBUG_CANQUEUE) {
983 printk(KERN_ERR "scsi_debug:timer_intr_handler: indx too "
984 "large\n");
985 return;
986 }
987 spin_lock_irqsave(&queued_arr_lock, iflags);
988 sqcp = &queued_arr[(int)indx];
989 if (! sqcp->in_use) {
990 printk(KERN_ERR "scsi_debug:timer_intr_handler: Unexpected "
991 "interrupt\n");
992 spin_unlock_irqrestore(&queued_arr_lock, iflags);
993 return;
994 }
995 sqcp->in_use = 0;
996 if (sqcp->done_funct) {
997 sqcp->a_cmnd->result = sqcp->scsi_result;
998 sqcp->done_funct(sqcp->a_cmnd); /* callback to mid level */
999 }
1000 sqcp->done_funct = NULL;
1001 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1002}
1003
1004static int scsi_debug_slave_alloc(struct scsi_device * sdp)
1005{
1006 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001007 sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_alloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return 0;
1009}
1010
1011static int scsi_debug_slave_configure(struct scsi_device * sdp)
1012{
1013 struct sdebug_dev_info * devip;
1014
1015 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001016 sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_configure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
1018 sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
1019 devip = devInfoReg(sdp);
1020 sdp->hostdata = devip;
1021 if (sdp->host->cmd_per_lun)
1022 scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
1023 sdp->host->cmd_per_lun);
1024 return 0;
1025}
1026
1027static void scsi_debug_slave_destroy(struct scsi_device * sdp)
1028{
1029 struct sdebug_dev_info * devip =
1030 (struct sdebug_dev_info *)sdp->hostdata;
1031
1032 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001033 sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_destroy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (devip) {
1035 /* make this slot avaliable for re-use */
1036 devip->used = 0;
1037 sdp->hostdata = NULL;
1038 }
1039}
1040
1041static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
1042{
1043 struct sdebug_host_info * sdbg_host;
1044 struct sdebug_dev_info * open_devip = NULL;
1045 struct sdebug_dev_info * devip =
1046 (struct sdebug_dev_info *)sdev->hostdata;
1047
1048 if (devip)
1049 return devip;
1050 sdbg_host = *(struct sdebug_host_info **) sdev->host->hostdata;
1051 if(! sdbg_host) {
1052 printk(KERN_ERR "Host info NULL\n");
1053 return NULL;
1054 }
1055 list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
1056 if ((devip->used) && (devip->channel == sdev->channel) &&
1057 (devip->target == sdev->id) &&
1058 (devip->lun == sdev->lun))
1059 return devip;
1060 else {
1061 if ((!devip->used) && (!open_devip))
1062 open_devip = devip;
1063 }
1064 }
1065 if (NULL == open_devip) { /* try and make a new one */
1066 open_devip = kmalloc(sizeof(*open_devip),GFP_KERNEL);
1067 if (NULL == open_devip) {
1068 printk(KERN_ERR "%s: out of memory at line %d\n",
1069 __FUNCTION__, __LINE__);
1070 return NULL;
1071 }
1072 memset(open_devip, 0, sizeof(*open_devip));
1073 open_devip->sdbg_host = sdbg_host;
1074 list_add_tail(&open_devip->dev_list,
1075 &sdbg_host->dev_info_list);
1076 }
1077 if (open_devip) {
1078 open_devip->channel = sdev->channel;
1079 open_devip->target = sdev->id;
1080 open_devip->lun = sdev->lun;
1081 open_devip->sdbg_host = sdbg_host;
1082 open_devip->reset = 1;
1083 open_devip->used = 1;
1084 memset(open_devip->sense_buff, 0, SDEBUG_SENSE_LEN);
1085 if (scsi_debug_dsense)
1086 open_devip->sense_buff[0] = 0x72;
1087 else {
1088 open_devip->sense_buff[0] = 0x70;
1089 open_devip->sense_buff[7] = 0xa;
1090 }
1091 return open_devip;
1092 }
1093 return NULL;
1094}
1095
1096static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
1097 int asc, int asq)
1098{
1099 unsigned char * sbuff;
1100
1101 sbuff = devip->sense_buff;
1102 memset(sbuff, 0, SDEBUG_SENSE_LEN);
1103 if (scsi_debug_dsense) {
1104 sbuff[0] = 0x72; /* descriptor, current */
1105 sbuff[1] = key;
1106 sbuff[2] = asc;
1107 sbuff[3] = asq;
1108 } else {
1109 sbuff[0] = 0x70; /* fixed, current */
1110 sbuff[2] = key;
1111 sbuff[7] = 0xa; /* implies 18 byte sense buffer */
1112 sbuff[12] = asc;
1113 sbuff[13] = asq;
1114 }
1115 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1116 printk(KERN_INFO "scsi_debug: [sense_key,asc,ascq]: "
1117 "[0x%x,0x%x,0x%x]\n", key, asc, asq);
1118}
1119
1120static int scsi_debug_abort(struct scsi_cmnd * SCpnt)
1121{
1122 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1123 printk(KERN_INFO "scsi_debug: abort\n");
1124 ++num_aborts;
1125 stop_queued_cmnd(SCpnt);
1126 return SUCCESS;
1127}
1128
1129static int scsi_debug_biosparam(struct scsi_device *sdev,
1130 struct block_device * bdev, sector_t capacity, int *info)
1131{
1132 int res;
1133 unsigned char *buf;
1134
1135 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1136 printk(KERN_INFO "scsi_debug: biosparam\n");
1137 buf = scsi_bios_ptable(bdev);
1138 if (buf) {
1139 res = scsi_partsize(buf, capacity,
1140 &info[2], &info[0], &info[1]);
1141 kfree(buf);
1142 if (! res)
1143 return res;
1144 }
1145 info[0] = sdebug_heads;
1146 info[1] = sdebug_sectors_per;
1147 info[2] = sdebug_cylinders_per;
1148 return 0;
1149}
1150
1151static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
1152{
1153 struct sdebug_dev_info * devip;
1154
1155 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1156 printk(KERN_INFO "scsi_debug: device_reset\n");
1157 ++num_dev_resets;
1158 if (SCpnt) {
1159 devip = devInfoReg(SCpnt->device);
1160 if (devip)
1161 devip->reset = 1;
1162 }
1163 return SUCCESS;
1164}
1165
1166static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
1167{
1168 struct sdebug_host_info *sdbg_host;
1169 struct sdebug_dev_info * dev_info;
1170 struct scsi_device * sdp;
1171 struct Scsi_Host * hp;
1172
1173 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1174 printk(KERN_INFO "scsi_debug: bus_reset\n");
1175 ++num_bus_resets;
1176 if (SCpnt && ((sdp = SCpnt->device)) && ((hp = sdp->host))) {
1177 sdbg_host = *(struct sdebug_host_info **) hp->hostdata;
1178 if (sdbg_host) {
1179 list_for_each_entry(dev_info,
1180 &sdbg_host->dev_info_list,
1181 dev_list)
1182 dev_info->reset = 1;
1183 }
1184 }
1185 return SUCCESS;
1186}
1187
1188static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
1189{
1190 struct sdebug_host_info * sdbg_host;
1191 struct sdebug_dev_info * dev_info;
1192
1193 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1194 printk(KERN_INFO "scsi_debug: host_reset\n");
1195 ++num_host_resets;
1196 spin_lock(&sdebug_host_list_lock);
1197 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
1198 list_for_each_entry(dev_info, &sdbg_host->dev_info_list,
1199 dev_list)
1200 dev_info->reset = 1;
1201 }
1202 spin_unlock(&sdebug_host_list_lock);
1203 stop_all_queued();
1204 return SUCCESS;
1205}
1206
1207/* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
1208static int stop_queued_cmnd(struct scsi_cmnd * cmnd)
1209{
1210 unsigned long iflags;
1211 int k;
1212 struct sdebug_queued_cmd * sqcp;
1213
1214 spin_lock_irqsave(&queued_arr_lock, iflags);
1215 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1216 sqcp = &queued_arr[k];
1217 if (sqcp->in_use && (cmnd == sqcp->a_cmnd)) {
1218 del_timer_sync(&sqcp->cmnd_timer);
1219 sqcp->in_use = 0;
1220 sqcp->a_cmnd = NULL;
1221 break;
1222 }
1223 }
1224 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1225 return (k < SCSI_DEBUG_CANQUEUE) ? 1 : 0;
1226}
1227
1228/* Deletes (stops) timers of all queued commands */
1229static void stop_all_queued(void)
1230{
1231 unsigned long iflags;
1232 int k;
1233 struct sdebug_queued_cmd * sqcp;
1234
1235 spin_lock_irqsave(&queued_arr_lock, iflags);
1236 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1237 sqcp = &queued_arr[k];
1238 if (sqcp->in_use && sqcp->a_cmnd) {
1239 del_timer_sync(&sqcp->cmnd_timer);
1240 sqcp->in_use = 0;
1241 sqcp->a_cmnd = NULL;
1242 }
1243 }
1244 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1245}
1246
1247/* Initializes timers in queued array */
1248static void __init init_all_queued(void)
1249{
1250 unsigned long iflags;
1251 int k;
1252 struct sdebug_queued_cmd * sqcp;
1253
1254 spin_lock_irqsave(&queued_arr_lock, iflags);
1255 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1256 sqcp = &queued_arr[k];
1257 init_timer(&sqcp->cmnd_timer);
1258 sqcp->in_use = 0;
1259 sqcp->a_cmnd = NULL;
1260 }
1261 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1262}
1263
1264static void __init sdebug_build_parts(unsigned char * ramp)
1265{
1266 struct partition * pp;
1267 int starts[SDEBUG_MAX_PARTS + 2];
1268 int sectors_per_part, num_sectors, k;
1269 int heads_by_sects, start_sec, end_sec;
1270
1271 /* assume partition table already zeroed */
1272 if ((scsi_debug_num_parts < 1) || (sdebug_store_size < 1048576))
1273 return;
1274 if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
1275 scsi_debug_num_parts = SDEBUG_MAX_PARTS;
1276 printk(KERN_WARNING "scsi_debug:build_parts: reducing "
1277 "partitions to %d\n", SDEBUG_MAX_PARTS);
1278 }
1279 num_sectors = (int)(sdebug_store_size / SECT_SIZE);
1280 sectors_per_part = (num_sectors - sdebug_sectors_per)
1281 / scsi_debug_num_parts;
1282 heads_by_sects = sdebug_heads * sdebug_sectors_per;
1283 starts[0] = sdebug_sectors_per;
1284 for (k = 1; k < scsi_debug_num_parts; ++k)
1285 starts[k] = ((k * sectors_per_part) / heads_by_sects)
1286 * heads_by_sects;
1287 starts[scsi_debug_num_parts] = num_sectors;
1288 starts[scsi_debug_num_parts + 1] = 0;
1289
1290 ramp[510] = 0x55; /* magic partition markings */
1291 ramp[511] = 0xAA;
1292 pp = (struct partition *)(ramp + 0x1be);
1293 for (k = 0; starts[k + 1]; ++k, ++pp) {
1294 start_sec = starts[k];
1295 end_sec = starts[k + 1] - 1;
1296 pp->boot_ind = 0;
1297
1298 pp->cyl = start_sec / heads_by_sects;
1299 pp->head = (start_sec - (pp->cyl * heads_by_sects))
1300 / sdebug_sectors_per;
1301 pp->sector = (start_sec % sdebug_sectors_per) + 1;
1302
1303 pp->end_cyl = end_sec / heads_by_sects;
1304 pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
1305 / sdebug_sectors_per;
1306 pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
1307
1308 pp->start_sect = start_sec;
1309 pp->nr_sects = end_sec - start_sec + 1;
1310 pp->sys_ind = 0x83; /* plain Linux partition */
1311 }
1312}
1313
1314static int schedule_resp(struct scsi_cmnd * cmnd,
1315 struct sdebug_dev_info * devip,
1316 done_funct_t done, int scsi_result, int delta_jiff)
1317{
1318 if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmnd) {
1319 if (scsi_result) {
1320 struct scsi_device * sdp = cmnd->device;
1321
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001322 sdev_printk(KERN_INFO, sdp,
1323 "non-zero result=0x%x\n",
1324 scsi_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 }
1327 if (cmnd && devip) {
1328 /* simulate autosense by this driver */
1329 if (SAM_STAT_CHECK_CONDITION == (scsi_result & 0xff))
1330 memcpy(cmnd->sense_buffer, devip->sense_buff,
1331 (SCSI_SENSE_BUFFERSIZE > SDEBUG_SENSE_LEN) ?
1332 SDEBUG_SENSE_LEN : SCSI_SENSE_BUFFERSIZE);
1333 }
1334 if (delta_jiff <= 0) {
1335 if (cmnd)
1336 cmnd->result = scsi_result;
1337 if (done)
1338 done(cmnd);
1339 return 0;
1340 } else {
1341 unsigned long iflags;
1342 int k;
1343 struct sdebug_queued_cmd * sqcp = NULL;
1344
1345 spin_lock_irqsave(&queued_arr_lock, iflags);
1346 for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
1347 sqcp = &queued_arr[k];
1348 if (! sqcp->in_use)
1349 break;
1350 }
1351 if (k >= SCSI_DEBUG_CANQUEUE) {
1352 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1353 printk(KERN_WARNING "scsi_debug: can_queue exceeded\n");
1354 return 1; /* report busy to mid level */
1355 }
1356 sqcp->in_use = 1;
1357 sqcp->a_cmnd = cmnd;
1358 sqcp->scsi_result = scsi_result;
1359 sqcp->done_funct = done;
1360 sqcp->cmnd_timer.function = timer_intr_handler;
1361 sqcp->cmnd_timer.data = k;
1362 sqcp->cmnd_timer.expires = jiffies + delta_jiff;
1363 add_timer(&sqcp->cmnd_timer);
1364 spin_unlock_irqrestore(&queued_arr_lock, iflags);
1365 if (cmnd)
1366 cmnd->result = 0;
1367 return 0;
1368 }
1369}
1370
1371/* Set 'perm' (4th argument) to 0 to disable module_param's definition
1372 * of sysfs parameters (which module_param doesn't yet support).
1373 * Sysfs parameters defined explicitly below.
1374 */
1375module_param_named(add_host, scsi_debug_add_host, int, 0); /* perm=0644 */
1376module_param_named(delay, scsi_debug_delay, int, 0); /* perm=0644 */
1377module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, 0);
1378module_param_named(dsense, scsi_debug_dsense, int, 0);
1379module_param_named(every_nth, scsi_debug_every_nth, int, 0);
1380module_param_named(max_luns, scsi_debug_max_luns, int, 0);
1381module_param_named(num_parts, scsi_debug_num_parts, int, 0);
1382module_param_named(num_tgts, scsi_debug_num_tgts, int, 0);
1383module_param_named(opts, scsi_debug_opts, int, 0); /* perm=0644 */
1384module_param_named(ptype, scsi_debug_ptype, int, 0);
1385module_param_named(scsi_level, scsi_debug_scsi_level, int, 0);
1386
1387MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
1388MODULE_DESCRIPTION("SCSI debug adapter driver");
1389MODULE_LICENSE("GPL");
1390MODULE_VERSION(SCSI_DEBUG_VERSION);
1391
1392MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
1393MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
1394MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs");
1395MODULE_PARM_DESC(dsense, "use descriptor sense format(def: fixed)");
1396MODULE_PARM_DESC(every_nth, "timeout every nth command(def=100)");
1397MODULE_PARM_DESC(max_luns, "number of SCSI LUNs per target to simulate");
1398MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
1399MODULE_PARM_DESC(num_tgts, "number of SCSI targets per host to simulate");
1400MODULE_PARM_DESC(opts, "1->noise, 2->medium_error, 4->...");
1401MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
1402MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])");
1403
1404
1405static char sdebug_info[256];
1406
1407static const char * scsi_debug_info(struct Scsi_Host * shp)
1408{
1409 sprintf(sdebug_info, "scsi_debug, version %s [%s], "
1410 "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
1411 scsi_debug_version_date, scsi_debug_dev_size_mb,
1412 scsi_debug_opts);
1413 return sdebug_info;
1414}
1415
1416/* scsi_debug_proc_info
1417 * Used if the driver currently has no own support for /proc/scsi
1418 */
1419static int scsi_debug_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
1420 int length, int inout)
1421{
1422 int len, pos, begin;
1423 int orig_length;
1424
1425 orig_length = length;
1426
1427 if (inout == 1) {
1428 char arr[16];
1429 int minLen = length > 15 ? 15 : length;
1430
1431 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1432 return -EACCES;
1433 memcpy(arr, buffer, minLen);
1434 arr[minLen] = '\0';
1435 if (1 != sscanf(arr, "%d", &pos))
1436 return -EINVAL;
1437 scsi_debug_opts = pos;
1438 if (scsi_debug_every_nth != 0)
1439 scsi_debug_cmnd_count = 0;
1440 return length;
1441 }
1442 begin = 0;
1443 pos = len = sprintf(buffer, "scsi_debug adapter driver, version "
1444 "%s [%s]\n"
1445 "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
1446 "every_nth=%d(curr:%d)\n"
1447 "delay=%d, max_luns=%d, scsi_level=%d\n"
1448 "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
1449 "number of aborts=%d, device_reset=%d, bus_resets=%d, "
1450 "host_resets=%d\n",
1451 SCSI_DEBUG_VERSION, scsi_debug_version_date, scsi_debug_num_tgts,
1452 scsi_debug_dev_size_mb, scsi_debug_opts, scsi_debug_every_nth,
1453 scsi_debug_cmnd_count, scsi_debug_delay,
1454 scsi_debug_max_luns, scsi_debug_scsi_level,
1455 SECT_SIZE, sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
1456 num_aborts, num_dev_resets, num_bus_resets, num_host_resets);
1457 if (pos < offset) {
1458 len = 0;
1459 begin = pos;
1460 }
1461 *start = buffer + (offset - begin); /* Start of wanted data */
1462 len -= (offset - begin);
1463 if (len > length)
1464 len = length;
1465 return len;
1466}
1467
1468static ssize_t sdebug_delay_show(struct device_driver * ddp, char * buf)
1469{
1470 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
1471}
1472
1473static ssize_t sdebug_delay_store(struct device_driver * ddp,
1474 const char * buf, size_t count)
1475{
1476 int delay;
1477 char work[20];
1478
1479 if (1 == sscanf(buf, "%10s", work)) {
1480 if ((1 == sscanf(work, "%d", &delay)) && (delay >= 0)) {
1481 scsi_debug_delay = delay;
1482 return count;
1483 }
1484 }
1485 return -EINVAL;
1486}
1487DRIVER_ATTR(delay, S_IRUGO | S_IWUSR, sdebug_delay_show,
1488 sdebug_delay_store);
1489
1490static ssize_t sdebug_opts_show(struct device_driver * ddp, char * buf)
1491{
1492 return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
1493}
1494
1495static ssize_t sdebug_opts_store(struct device_driver * ddp,
1496 const char * buf, size_t count)
1497{
1498 int opts;
1499 char work[20];
1500
1501 if (1 == sscanf(buf, "%10s", work)) {
1502 if (0 == strnicmp(work,"0x", 2)) {
1503 if (1 == sscanf(&work[2], "%x", &opts))
1504 goto opts_done;
1505 } else {
1506 if (1 == sscanf(work, "%d", &opts))
1507 goto opts_done;
1508 }
1509 }
1510 return -EINVAL;
1511opts_done:
1512 scsi_debug_opts = opts;
1513 scsi_debug_cmnd_count = 0;
1514 return count;
1515}
1516DRIVER_ATTR(opts, S_IRUGO | S_IWUSR, sdebug_opts_show,
1517 sdebug_opts_store);
1518
1519static ssize_t sdebug_ptype_show(struct device_driver * ddp, char * buf)
1520{
1521 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
1522}
1523static ssize_t sdebug_ptype_store(struct device_driver * ddp,
1524 const char * buf, size_t count)
1525{
1526 int n;
1527
1528 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1529 scsi_debug_ptype = n;
1530 return count;
1531 }
1532 return -EINVAL;
1533}
1534DRIVER_ATTR(ptype, S_IRUGO | S_IWUSR, sdebug_ptype_show, sdebug_ptype_store);
1535
1536static ssize_t sdebug_dsense_show(struct device_driver * ddp, char * buf)
1537{
1538 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
1539}
1540static ssize_t sdebug_dsense_store(struct device_driver * ddp,
1541 const char * buf, size_t count)
1542{
1543 int n;
1544
1545 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1546 scsi_debug_dsense = n;
1547 return count;
1548 }
1549 return -EINVAL;
1550}
1551DRIVER_ATTR(dsense, S_IRUGO | S_IWUSR, sdebug_dsense_show,
1552 sdebug_dsense_store);
1553
1554static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf)
1555{
1556 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
1557}
1558static ssize_t sdebug_num_tgts_store(struct device_driver * ddp,
1559 const char * buf, size_t count)
1560{
1561 int n;
1562
1563 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1564 scsi_debug_num_tgts = n;
1565 sdebug_max_tgts_luns();
1566 return count;
1567 }
1568 return -EINVAL;
1569}
1570DRIVER_ATTR(num_tgts, S_IRUGO | S_IWUSR, sdebug_num_tgts_show,
1571 sdebug_num_tgts_store);
1572
1573static ssize_t sdebug_dev_size_mb_show(struct device_driver * ddp, char * buf)
1574{
1575 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
1576}
1577DRIVER_ATTR(dev_size_mb, S_IRUGO, sdebug_dev_size_mb_show, NULL);
1578
1579static ssize_t sdebug_num_parts_show(struct device_driver * ddp, char * buf)
1580{
1581 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
1582}
1583DRIVER_ATTR(num_parts, S_IRUGO, sdebug_num_parts_show, NULL);
1584
1585static ssize_t sdebug_every_nth_show(struct device_driver * ddp, char * buf)
1586{
1587 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
1588}
1589static ssize_t sdebug_every_nth_store(struct device_driver * ddp,
1590 const char * buf, size_t count)
1591{
1592 int nth;
1593
1594 if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
1595 scsi_debug_every_nth = nth;
1596 scsi_debug_cmnd_count = 0;
1597 return count;
1598 }
1599 return -EINVAL;
1600}
1601DRIVER_ATTR(every_nth, S_IRUGO | S_IWUSR, sdebug_every_nth_show,
1602 sdebug_every_nth_store);
1603
1604static ssize_t sdebug_max_luns_show(struct device_driver * ddp, char * buf)
1605{
1606 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
1607}
1608static ssize_t sdebug_max_luns_store(struct device_driver * ddp,
1609 const char * buf, size_t count)
1610{
1611 int n;
1612
1613 if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
1614 scsi_debug_max_luns = n;
1615 sdebug_max_tgts_luns();
1616 return count;
1617 }
1618 return -EINVAL;
1619}
1620DRIVER_ATTR(max_luns, S_IRUGO | S_IWUSR, sdebug_max_luns_show,
1621 sdebug_max_luns_store);
1622
1623static ssize_t sdebug_scsi_level_show(struct device_driver * ddp, char * buf)
1624{
1625 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
1626}
1627DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL);
1628
1629static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf)
1630{
1631 return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
1632}
1633
1634static ssize_t sdebug_add_host_store(struct device_driver * ddp,
1635 const char * buf, size_t count)
1636{
1637 int delta_hosts;
1638 char work[20];
1639
1640 if (1 != sscanf(buf, "%10s", work))
1641 return -EINVAL;
1642 { /* temporary hack around sscanf() problem with -ve nums */
1643 int neg = 0;
1644
1645 if ('-' == *work)
1646 neg = 1;
1647 if (1 != sscanf(work + neg, "%d", &delta_hosts))
1648 return -EINVAL;
1649 if (neg)
1650 delta_hosts = -delta_hosts;
1651 }
1652 if (delta_hosts > 0) {
1653 do {
1654 sdebug_add_adapter();
1655 } while (--delta_hosts);
1656 } else if (delta_hosts < 0) {
1657 do {
1658 sdebug_remove_adapter();
1659 } while (++delta_hosts);
1660 }
1661 return count;
1662}
1663DRIVER_ATTR(add_host, S_IRUGO | S_IWUSR, sdebug_add_host_show,
1664 sdebug_add_host_store);
1665
1666static void do_create_driverfs_files(void)
1667{
1668 driver_create_file(&sdebug_driverfs_driver, &driver_attr_add_host);
1669 driver_create_file(&sdebug_driverfs_driver, &driver_attr_delay);
1670 driver_create_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
1671 driver_create_file(&sdebug_driverfs_driver, &driver_attr_dsense);
1672 driver_create_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
1673 driver_create_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
1674 driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
1675 driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
1676 driver_create_file(&sdebug_driverfs_driver, &driver_attr_ptype);
1677 driver_create_file(&sdebug_driverfs_driver, &driver_attr_opts);
1678 driver_create_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
1679}
1680
1681static void do_remove_driverfs_files(void)
1682{
1683 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
1684 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_opts);
1685 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_ptype);
1686 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
1687 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
1688 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
1689 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
1690 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dsense);
1691 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
1692 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_delay);
1693 driver_remove_file(&sdebug_driverfs_driver, &driver_attr_add_host);
1694}
1695
1696static int __init scsi_debug_init(void)
1697{
1698 unsigned long sz;
1699 int host_to_add;
1700 int k;
1701
1702 if (scsi_debug_dev_size_mb < 1)
1703 scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
1704 sdebug_store_size = (unsigned long)scsi_debug_dev_size_mb * 1048576;
1705 sdebug_capacity = sdebug_store_size / SECT_SIZE;
1706
1707 /* play around with geometry, don't waste too much on track 0 */
1708 sdebug_heads = 8;
1709 sdebug_sectors_per = 32;
1710 if (scsi_debug_dev_size_mb >= 16)
1711 sdebug_heads = 32;
1712 else if (scsi_debug_dev_size_mb >= 256)
1713 sdebug_heads = 64;
1714 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
1715 (sdebug_sectors_per * sdebug_heads);
1716 if (sdebug_cylinders_per >= 1024) {
1717 /* other LLDs do this; implies >= 1GB ram disk ... */
1718 sdebug_heads = 255;
1719 sdebug_sectors_per = 63;
1720 sdebug_cylinders_per = (unsigned long)sdebug_capacity /
1721 (sdebug_sectors_per * sdebug_heads);
1722 }
1723
1724 sz = sdebug_store_size;
1725 fake_storep = vmalloc(sz);
1726 if (NULL == fake_storep) {
1727 printk(KERN_ERR "scsi_debug_init: out of memory, 1\n");
1728 return -ENOMEM;
1729 }
1730 memset(fake_storep, 0, sz);
1731 if (scsi_debug_num_parts > 0)
1732 sdebug_build_parts(fake_storep);
1733
1734 init_all_queued();
1735
1736 device_register(&pseudo_primary);
1737 bus_register(&pseudo_lld_bus);
1738 driver_register(&sdebug_driverfs_driver);
1739 do_create_driverfs_files();
1740
1741 sdebug_driver_template.proc_name = (char *)sdebug_proc_name;
1742
1743 host_to_add = scsi_debug_add_host;
1744 scsi_debug_add_host = 0;
1745
1746 for (k = 0; k < host_to_add; k++) {
1747 if (sdebug_add_adapter()) {
1748 printk(KERN_ERR "scsi_debug_init: "
1749 "sdebug_add_adapter failed k=%d\n", k);
1750 break;
1751 }
1752 }
1753
1754 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
1755 printk(KERN_INFO "scsi_debug_init: built %d host(s)\n",
1756 scsi_debug_add_host);
1757 }
1758 return 0;
1759}
1760
1761static void __exit scsi_debug_exit(void)
1762{
1763 int k = scsi_debug_add_host;
1764
1765 stop_all_queued();
1766 for (; k; k--)
1767 sdebug_remove_adapter();
1768 do_remove_driverfs_files();
1769 driver_unregister(&sdebug_driverfs_driver);
1770 bus_unregister(&pseudo_lld_bus);
1771 device_unregister(&pseudo_primary);
1772
1773 vfree(fake_storep);
1774}
1775
1776device_initcall(scsi_debug_init);
1777module_exit(scsi_debug_exit);
1778
Adrian Bunk52c1da32005-06-23 22:05:33 -07001779static void pseudo_0_release(struct device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
1782 printk(KERN_INFO "scsi_debug: pseudo_0_release() called\n");
1783}
1784
1785static struct device pseudo_primary = {
1786 .bus_id = "pseudo_0",
1787 .release = pseudo_0_release,
1788};
1789
1790static int pseudo_lld_bus_match(struct device *dev,
1791 struct device_driver *dev_driver)
1792{
1793 return 1;
1794}
1795
1796static struct bus_type pseudo_lld_bus = {
1797 .name = "pseudo",
1798 .match = pseudo_lld_bus_match,
1799};
1800
1801static void sdebug_release_adapter(struct device * dev)
1802{
1803 struct sdebug_host_info *sdbg_host;
1804
1805 sdbg_host = to_sdebug_host(dev);
1806 kfree(sdbg_host);
1807}
1808
1809static int sdebug_add_adapter(void)
1810{
1811 int k, devs_per_host;
1812 int error = 0;
1813 struct sdebug_host_info *sdbg_host;
1814 struct sdebug_dev_info *sdbg_devinfo;
1815 struct list_head *lh, *lh_sf;
1816
1817 sdbg_host = kmalloc(sizeof(*sdbg_host),GFP_KERNEL);
1818
1819 if (NULL == sdbg_host) {
1820 printk(KERN_ERR "%s: out of memory at line %d\n",
1821 __FUNCTION__, __LINE__);
1822 return -ENOMEM;
1823 }
1824
1825 memset(sdbg_host, 0, sizeof(*sdbg_host));
1826 INIT_LIST_HEAD(&sdbg_host->dev_info_list);
1827
1828 devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
1829 for (k = 0; k < devs_per_host; k++) {
1830 sdbg_devinfo = kmalloc(sizeof(*sdbg_devinfo),GFP_KERNEL);
1831 if (NULL == sdbg_devinfo) {
1832 printk(KERN_ERR "%s: out of memory at line %d\n",
1833 __FUNCTION__, __LINE__);
1834 error = -ENOMEM;
1835 goto clean;
1836 }
1837 memset(sdbg_devinfo, 0, sizeof(*sdbg_devinfo));
1838 sdbg_devinfo->sdbg_host = sdbg_host;
1839 list_add_tail(&sdbg_devinfo->dev_list,
1840 &sdbg_host->dev_info_list);
1841 }
1842
1843 spin_lock(&sdebug_host_list_lock);
1844 list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
1845 spin_unlock(&sdebug_host_list_lock);
1846
1847 sdbg_host->dev.bus = &pseudo_lld_bus;
1848 sdbg_host->dev.parent = &pseudo_primary;
1849 sdbg_host->dev.release = &sdebug_release_adapter;
1850 sprintf(sdbg_host->dev.bus_id, "adapter%d", scsi_debug_add_host);
1851
1852 error = device_register(&sdbg_host->dev);
1853
1854 if (error)
1855 goto clean;
1856
1857 ++scsi_debug_add_host;
1858 return error;
1859
1860clean:
1861 list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
1862 sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
1863 dev_list);
1864 list_del(&sdbg_devinfo->dev_list);
1865 kfree(sdbg_devinfo);
1866 }
1867
1868 kfree(sdbg_host);
1869 return error;
1870}
1871
1872static void sdebug_remove_adapter(void)
1873{
1874 struct sdebug_host_info * sdbg_host = NULL;
1875
1876 spin_lock(&sdebug_host_list_lock);
1877 if (!list_empty(&sdebug_host_list)) {
1878 sdbg_host = list_entry(sdebug_host_list.prev,
1879 struct sdebug_host_info, host_list);
1880 list_del(&sdbg_host->host_list);
1881 }
1882 spin_unlock(&sdebug_host_list_lock);
1883
1884 if (!sdbg_host)
1885 return;
1886
1887 device_unregister(&sdbg_host->dev);
1888 --scsi_debug_add_host;
1889}
1890
1891static int sdebug_driver_probe(struct device * dev)
1892{
1893 int error = 0;
1894 struct sdebug_host_info *sdbg_host;
1895 struct Scsi_Host *hpnt;
1896
1897 sdbg_host = to_sdebug_host(dev);
1898
1899 hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
1900 if (NULL == hpnt) {
1901 printk(KERN_ERR "%s: scsi_register failed\n", __FUNCTION__);
1902 error = -ENODEV;
1903 return error;
1904 }
1905
1906 sdbg_host->shost = hpnt;
1907 *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
1908 if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
1909 hpnt->max_id = scsi_debug_num_tgts + 1;
1910 else
1911 hpnt->max_id = scsi_debug_num_tgts;
1912 hpnt->max_lun = scsi_debug_max_luns;
1913
1914 error = scsi_add_host(hpnt, &sdbg_host->dev);
1915 if (error) {
1916 printk(KERN_ERR "%s: scsi_add_host failed\n", __FUNCTION__);
1917 error = -ENODEV;
1918 scsi_host_put(hpnt);
1919 } else
1920 scsi_scan_host(hpnt);
1921
1922
1923 return error;
1924}
1925
1926static int sdebug_driver_remove(struct device * dev)
1927{
1928 struct list_head *lh, *lh_sf;
1929 struct sdebug_host_info *sdbg_host;
1930 struct sdebug_dev_info *sdbg_devinfo;
1931
1932 sdbg_host = to_sdebug_host(dev);
1933
1934 if (!sdbg_host) {
1935 printk(KERN_ERR "%s: Unable to locate host info\n",
1936 __FUNCTION__);
1937 return -ENODEV;
1938 }
1939
1940 scsi_remove_host(sdbg_host->shost);
1941
1942 list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
1943 sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
1944 dev_list);
1945 list_del(&sdbg_devinfo->dev_list);
1946 kfree(sdbg_devinfo);
1947 }
1948
1949 scsi_host_put(sdbg_host->shost);
1950 return 0;
1951}
1952
1953static void sdebug_max_tgts_luns(void)
1954{
1955 struct sdebug_host_info * sdbg_host;
1956 struct Scsi_Host *hpnt;
1957
1958 spin_lock(&sdebug_host_list_lock);
1959 list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
1960 hpnt = sdbg_host->shost;
1961 if ((hpnt->this_id >= 0) &&
1962 (scsi_debug_num_tgts > hpnt->this_id))
1963 hpnt->max_id = scsi_debug_num_tgts + 1;
1964 else
1965 hpnt->max_id = scsi_debug_num_tgts;
1966 hpnt->max_lun = scsi_debug_max_luns;
1967 }
1968 spin_unlock(&sdebug_host_list_lock);
1969}