scsi.c 40.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 *  scsi.c Copyright (C) 1992 Drew Eckhardt
 *         Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
 *
 *  generic mid-level SCSI driver
 *      Initial versions: Drew Eckhardt
 *      Subsequent revisions: Eric Youngdale
 *
 *  <drew@colorado.edu>
 *
 *  Bug correction thanks go to :
 *      Rik Faith <faith@cs.unc.edu>
 *      Tommy Thorn <tthorn>
 *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
 *
 *  Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to
 *  add scatter-gather, multiple outstanding request, and other
 *  enhancements.
 *
 *  Native multichannel, wide scsi, /proc/scsi and hot plugging
 *  support added by Michael Neuffer <mike@i-connect.net>
 *
 *  Added request_module("scsi_hostadapter") for kerneld:
 *  (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modules.conf)
 *  Bjorn Ekwall  <bj0rn@blox.se>
 *  (changed to kmod)
 *
 *  Major improvements to the timeout, abort, and reset processing,
 *  as well as performance modifications for large queue depths by
 *  Leonard N. Zubkoff <lnz@dandelion.com>
 *
 *  Converted cli() code to spinlocks, Ingo Molnar
 *
 *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
 *
 *  out_of_space hacks, D. Gilbert (dpg) 990608
 */

#include <linux/config.h>
#include <linux/module.h>

#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
Linus Torvalds's avatar
Linus Torvalds committed
45
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51 52
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/blk.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/init.h>
Linus Torvalds's avatar
Linus Torvalds committed
53
#include <linux/smp_lock.h>
Linus Torvalds's avatar
Linus Torvalds committed
54
#include <linux/completion.h>
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

#define __KERNEL_SYSCALLS__

#include <linux/unistd.h>
#include <linux/spinlock.h>

#include <asm/system.h>
#include <asm/irq.h>
#include <asm/dma.h>
#include <asm/uaccess.h>

#include "scsi.h"
#include "hosts.h"

#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#endif


/*
 * Definitions and constants.
 */

#define MIN_RESET_DELAY (2*HZ)

/* Do not call reset on error if we just did a reset within 15 sec. */
#define MIN_RESET_PERIOD (15*HZ)

Linus Torvalds's avatar
Linus Torvalds committed
83 84 85 86 87 88 89 90
/*
 * Macro to determine the size of SCSI command. This macro takes vendor
 * unique commands into account. SCSI commands in groups 6 and 7 are
 * vendor unique and we will depend upon the command length being
 * supplied correctly in cmd_len.
 */
#define CDB_SIZE(SCpnt)	((((SCpnt->cmnd[0] >> 5) & 7) < 6) ? \
				COMMAND_SIZE(SCpnt->cmnd[0]) : SCpnt->cmd_len)
Linus Torvalds's avatar
Linus Torvalds committed
91 92 93 94 95 96 97

/*
 * Data declarations.
 */
unsigned long scsi_pid;
Scsi_Cmnd *last_cmnd;
static unsigned long serial_number;
Matthew Wilcox's avatar
Matthew Wilcox committed
98

99
static struct list_head done_q[NR_CPUS] __cacheline_aligned;
Linus Torvalds's avatar
Linus Torvalds committed
100

101 102 103
/*
 * List of all highlevel drivers.
 */
104
LIST_HEAD(scsi_devicelist);
105 106
static DECLARE_RWSEM(scsi_devicelist_mutex);

Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/*
 * Note - the initial logging level can be set here to log events at boot time.
 * After the system is up, you may enable logging via the /proc interface.
 */
unsigned int scsi_logging_level;

const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
{
	"Direct-Access    ",
	"Sequential-Access",
	"Printer          ",
	"Processor        ",
	"WORM             ",
	"CD-ROM           ",
	"Scanner          ",
	"Optical Device   ",
	"Medium Changer   ",
	"Communications   ",
	"Unknown          ",
	"Unknown          ",
	"Unknown          ",
	"Enclosure        ",
};

131 132 133
static const char * const spaces = "                "; /* 16 of them */

static unsigned scsi_default_dev_flags;
134
LIST_HEAD(scsi_dev_info_list);
135

Linus Torvalds's avatar
Linus Torvalds committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/* 
 * Function prototypes.
 */
extern void scsi_times_out(Scsi_Cmnd * SCpnt);

#ifdef MODULE
MODULE_PARM(scsi_logging_level, "i");
MODULE_PARM_DESC(scsi_logging_level, "SCSI logging level; should be zero or nonzero");

#else

static int __init scsi_logging_setup(char *str)
{
	int tmp;

	if (get_option(&str, &tmp) == 1) {
		scsi_logging_level = (tmp ? ~0 : 0);
		return 1;
	} else {
		printk(KERN_INFO "scsi_logging_setup : usage scsi_logging_level=n "
		       "(n should be 0 or non-zero)\n");
		return 0;
	}
}

__setup("scsi_logging=", scsi_logging_setup);

#endif

/*
 * Function:    scsi_allocate_request
 *
 * Purpose:     Allocate a request descriptor.
 *
 * Arguments:   device    - device for which we want a request
 *
 * Lock status: No locks assumed to be held.  This function is SMP-safe.
 *
 * Returns:     Pointer to request block.
 *
 * Notes:       With the new queueing code, it becomes important
 *              to track the difference between a command and a
 *              request.  A request is a pending item in the queue that
 *              has not yet reached the top of the queue.
 */

Scsi_Request *scsi_allocate_request(Scsi_Device * device)
{
  	Scsi_Request *SRpnt = NULL;
185 186
        const int offset = ALIGN(sizeof(Scsi_Request), 4);
        const int size = offset + sizeof(struct request);
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190
  
  	if (!device)
  		panic("No device passed to scsi_allocate_request().\n");
  
191
        SRpnt = (Scsi_Request *) kmalloc(size, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
192 193 194 195
	if( SRpnt == NULL )
	{
		return NULL;
	}
196 197
	memset(SRpnt, 0, size);
        SRpnt->sr_request = (struct request *)(((char *)SRpnt) + offset);
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	SRpnt->sr_device = device;
	SRpnt->sr_host = device->host;
	SRpnt->sr_magic = SCSI_REQ_MAGIC;
	SRpnt->sr_data_direction = SCSI_DATA_UNKNOWN;

	return SRpnt;
}

/*
 * Function:    scsi_release_request
 *
 * Purpose:     Release a request descriptor.
 *
 * Arguments:   device    - device for which we want a request
 *
 * Lock status: No locks assumed to be held.  This function is SMP-safe.
 *
 * Returns:     Pointer to request block.
 *
 * Notes:       With the new queueing code, it becomes important
 *              to track the difference between a command and a
 *              request.  A request is a pending item in the queue that
 *              has not yet reached the top of the queue.  We still need
 *              to free a request when we are done with it, of course.
 */
void scsi_release_request(Scsi_Request * req)
{
	if( req->sr_command != NULL )
	{
Luben Tuikov's avatar
Luben Tuikov committed
227
		scsi_put_command(req->sr_command);
Linus Torvalds's avatar
Linus Torvalds committed
228 229 230 231 232 233
		req->sr_command = NULL;
	}

	kfree(req);
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
struct scsi_host_cmd_pool {
	kmem_cache_t	*slab;
	unsigned int	users;
	char		*name;
	unsigned int	slab_flags;
	unsigned int	gfp_mask;
};

static struct scsi_host_cmd_pool scsi_cmd_pool = {
	.name		= "scsi_cmd_cache",
	.slab_flags	= SLAB_HWCACHE_ALIGN,
};

static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
	.name		= "scsi_cmd_cache(DMA)",
	.slab_flags	= SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
	.gfp_mask	= __GFP_DMA,
};

static DECLARE_MUTEX(host_cmd_pool_mutex);

static struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost,
					    int gfp_mask)
{
	struct scsi_cmnd *cmd;

	cmd = kmem_cache_alloc(shost->cmd_pool->slab,
			gfp_mask | shost->cmd_pool->gfp_mask);

	if (unlikely(!cmd)) {
		unsigned long flags;

		spin_lock_irqsave(&shost->free_list_lock, flags);
		if (likely(!list_empty(&shost->free_list))) {
			cmd = list_entry(shost->free_list.next,
					 struct scsi_cmnd, list);
			list_del_init(&cmd->list);
		}
		spin_unlock_irqrestore(&shost->free_list_lock, flags);
	}

	return cmd;
}

/*
 * Function:	scsi_get_command()
 *
 * Purpose:	Allocate and setup a scsi command block
 *
 * Arguments:	dev	- parent scsi device
 *		gfp_mask- allocator flags
 *
 * Returns:	The allocated scsi command structure.
 */
struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, int gfp_mask)
{
	struct scsi_cmnd *cmd = __scsi_get_command(dev->host, gfp_mask);

292
	if (likely(cmd != NULL)) {
293 294
		unsigned long flags;

295 296 297 298 299 300
		memset(cmd, 0, sizeof(*cmd));
		cmd->device = dev;
		cmd->state = SCSI_STATE_UNUSED;
		cmd->owner = SCSI_OWNER_NOBODY;
		init_timer(&cmd->eh_timeout);
		INIT_LIST_HEAD(&cmd->list);
301
		spin_lock_irqsave(&dev->list_lock, flags);
302
		list_add_tail(&cmd->list, &dev->cmd_list);
303
		spin_unlock_irqrestore(&dev->list_lock, flags);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	}

	return cmd;
}				

/*
 * Function:	scsi_put_command()
 *
 * Purpose:	Free a scsi command block
 *
 * Arguments:	cmd	- command block to free
 *
 * Returns:	Nothing.
 *
 * Notes:	The command must not belong to any lists.
 */
void scsi_put_command(struct scsi_cmnd *cmd)
{
	struct Scsi_Host *shost = cmd->device->host;
	unsigned long flags;
	
325 326 327 328 329 330 331
	/* serious error if the command hasn't come from a device list */
	spin_lock_irqsave(&cmd->device->list_lock, flags);
	BUG_ON(list_empty(&cmd->list));
	list_del_init(&cmd->list);
	spin_unlock(&cmd->device->list_lock);
	/* changing locks here, don't need to restore the irq state */
	spin_lock(&shost->free_list_lock);
332 333 334 335 336 337
	if (unlikely(list_empty(&shost->free_list))) {
		list_add(&cmd->list, &shost->free_list);
		cmd = NULL;
	}
	spin_unlock_irqrestore(&shost->free_list_lock, flags);

338
	if (likely(cmd != NULL))
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
		kmem_cache_free(shost->cmd_pool->slab, cmd);
}

/*
 * Function:	scsi_setup_command_freelist()
 *
 * Purpose:	Setup the command freelist for a scsi host.
 *
 * Arguments:	shost	- host to allocate the freelist for.
 *
 * Returns:	Nothing.
 */
int scsi_setup_command_freelist(struct Scsi_Host *shost)
{
	struct scsi_host_cmd_pool *pool;
	struct scsi_cmnd *cmd;

	spin_lock_init(&shost->free_list_lock);
	INIT_LIST_HEAD(&shost->free_list);

	/*
	 * Select a command slab for this host and create it if not
	 * yet existant.
	 */
	down(&host_cmd_pool_mutex);
	pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool);
	if (!pool->users) {
		pool->slab = kmem_cache_create(pool->name,
				sizeof(struct scsi_cmnd), 0,
				pool->slab_flags, NULL, NULL);
		if (!pool->slab)
			goto fail;
	}

	pool->users++;
	shost->cmd_pool = pool;
	up(&host_cmd_pool_mutex);

	/*
	 * Get one backup command for this host.
	 */
	cmd = kmem_cache_alloc(shost->cmd_pool->slab,
			GFP_KERNEL | shost->cmd_pool->gfp_mask);
	if (!cmd)
		goto fail2;
	list_add(&cmd->list, &shost->free_list);		
	return 0;

 fail2:
	if (!--pool->users)
		kmem_cache_destroy(pool->slab);
	return -ENOMEM;
 fail:
	up(&host_cmd_pool_mutex);
	return -ENOMEM;

}

Linus Torvalds's avatar
Linus Torvalds committed
397
/*
398 399 400 401 402
 * Function:	scsi_destroy_command_freelist()
 *
 * Purpose:	Release the command freelist for a scsi host.
 *
 * Arguments:	shost	- host that's freelist is going to be destroyed
Linus Torvalds's avatar
Linus Torvalds committed
403
 */
404
void scsi_destroy_command_freelist(struct Scsi_Host *shost)
Linus Torvalds's avatar
Linus Torvalds committed
405
{
406 407 408 409 410 411 412 413 414 415 416 417
	while (!list_empty(&shost->free_list)) {
		struct scsi_cmnd *cmd;

		cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
		list_del_init(&cmd->list);
		kmem_cache_free(shost->cmd_pool->slab, cmd);
	}

	down(&host_cmd_pool_mutex);
	if (!--shost->cmd_pool->users)
		kmem_cache_destroy(shost->cmd_pool->slab);
	up(&host_cmd_pool_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
}

/*
 * Function:    scsi_dispatch_command
 *
 * Purpose:     Dispatch a command to the low-level driver.
 *
 * Arguments:   SCpnt - command block we are dispatching.
 *
 * Notes:
 */
int scsi_dispatch_cmd(Scsi_Cmnd * SCpnt)
{
#ifdef DEBUG_DELAY
	unsigned long clock;
#endif
	struct Scsi_Host *host;
	int rtn = 0;
	unsigned long flags = 0;
	unsigned long timeout;

#if DEBUG
	unsigned long *ret = 0;
#ifdef __mips__
	__asm__ __volatile__("move\t%0,$31":"=r"(ret));
#else
	ret = __builtin_return_address(0);
#endif
#endif

448
	host = SCpnt->device->host;
Linus Torvalds's avatar
Linus Torvalds committed
449 450 451 452 453

	/* Assign a unique nonzero serial_number. */
	if (++serial_number == 0)
		serial_number = 1;
	SCpnt->serial_number = serial_number;
Linus Torvalds's avatar
Linus Torvalds committed
454
	SCpnt->pid = scsi_pid++;
455 456 457 458 459
	/* 
	 * If SCSI-2 or lower, store the LUN value in cmnd.
	 */
	if (SCpnt->device->scsi_level <= SCSI_2)
		SCpnt->cmnd[1] = (SCpnt->cmnd[1] & 0x1f) |
460
			(SCpnt->device->lun << 5 & 0xe0);
Linus Torvalds's avatar
Linus Torvalds committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

	/*
	 * We will wait MIN_RESET_DELAY clock ticks after the last reset so
	 * we can avoid the drive not being ready.
	 */
	timeout = host->last_reset + MIN_RESET_DELAY;

	if (host->resetting && time_before(jiffies, timeout)) {
		int ticks_remaining = timeout - jiffies;
		/*
		 * NOTE: This may be executed from within an interrupt
		 * handler!  This is bad, but for now, it'll do.  The irq
		 * level of the interrupt handler has been masked out by the
		 * platform dependent interrupt handling code already, so the
		 * sti() here will not cause another call to the SCSI host's
		 * interrupt handler (assuming there is one irq-level per
		 * host).
		 */
		while (--ticks_remaining >= 0)
			mdelay(1 + 999 / HZ);
		host->resetting = 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
483 484

	scsi_add_timer(SCpnt, SCpnt->timeout_per_command, scsi_times_out);
Linus Torvalds's avatar
Linus Torvalds committed
485 486 487 488 489 490 491

	/*
	 * We will use a queued command if possible, otherwise we will emulate the
	 * queuing and calling of completion function ourselves.
	 */
	SCSI_LOG_MLQUEUE(3, printk("scsi_dispatch_cmnd (host = %d, channel = %d, target = %d, "
	       "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n",
492
	SCpnt->device->host->host_no, SCpnt->device->channel, SCpnt->device->id, SCpnt->cmnd,
Linus Torvalds's avatar
Linus Torvalds committed
493 494 495 496 497 498 499 500
			    SCpnt->buffer, SCpnt->bufflen, SCpnt->done));

	SCpnt->state = SCSI_STATE_QUEUED;
	SCpnt->owner = SCSI_OWNER_LOWLEVEL;
	if (host->can_queue) {
		SCSI_LOG_MLQUEUE(3, printk("queuecommand : routine at %p\n",
					   host->hostt->queuecommand));
		/*
Linus Torvalds's avatar
Linus Torvalds committed
501 502
		 * Before we queue this command, check if the command
		 * length exceeds what the host adapter can handle.
Linus Torvalds's avatar
Linus Torvalds committed
503
		 */
504
		if (CDB_SIZE(SCpnt) <= SCpnt->device->host->max_cmd_len) {
Linus Torvalds's avatar
Linus Torvalds committed
505
			spin_lock_irqsave(host->host_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
506
			rtn = host->hostt->queuecommand(SCpnt, scsi_done);
Linus Torvalds's avatar
Linus Torvalds committed
507
			spin_unlock_irqrestore(host->host_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
508
			if (rtn != 0) {
509
				scsi_queue_insert(SCpnt, rtn == SCSI_MLQUEUE_DEVICE_BUSY ? rtn : SCSI_MLQUEUE_HOST_BUSY);
Linus Torvalds's avatar
Linus Torvalds committed
510 511
				SCSI_LOG_MLQUEUE(3,
				   printk("queuecommand : request rejected\n"));                                
Linus Torvalds's avatar
Linus Torvalds committed
512 513
			}
		} else {
Linus Torvalds's avatar
Linus Torvalds committed
514 515 516
			SCSI_LOG_MLQUEUE(3,
				printk("queuecommand : command too long.\n"));
			SCpnt->result = (DID_ABORT << 16);
Linus Torvalds's avatar
Linus Torvalds committed
517
			spin_lock_irqsave(host->host_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
518
			scsi_done(SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
519
			spin_unlock_irqrestore(host->host_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
520
			rtn = 1;
Linus Torvalds's avatar
Linus Torvalds committed
521 522 523 524 525
		}
	} else {
		int temp;

		SCSI_LOG_MLQUEUE(3, printk("command() :  routine at %p\n", host->hostt->command));
Linus Torvalds's avatar
Linus Torvalds committed
526
                spin_lock_irqsave(host->host_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
527 528 529
		temp = host->hostt->command(SCpnt);
		SCpnt->result = temp;
#ifdef DEBUG_DELAY
Linus Torvalds's avatar
Linus Torvalds committed
530
                spin_unlock_irqrestore(host->host_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
531
		clock = jiffies + 4 * HZ;
Linus Torvalds's avatar
Linus Torvalds committed
532
		while (time_before(jiffies, clock)) {
Linus Torvalds's avatar
Linus Torvalds committed
533
			barrier();
Linus Torvalds's avatar
Linus Torvalds committed
534 535
			cpu_relax();
		}
Linus Torvalds's avatar
Linus Torvalds committed
536 537
		printk("done(host = %d, result = %04x) : routine at %p\n",
		       host->host_no, temp, host->hostt->command);
Linus Torvalds's avatar
Linus Torvalds committed
538
                spin_lock_irqsave(host->host_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
539
#endif
Linus Torvalds's avatar
Linus Torvalds committed
540
		scsi_done(SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
541
                spin_unlock_irqrestore(host->host_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
	}
	SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
	return rtn;
}

/*
 * Function:    scsi_init_cmd_from_req
 *
 * Purpose:     Queue a SCSI command
 * Purpose:     Initialize a Scsi_Cmnd from a Scsi_Request
 *
 * Arguments:   SCpnt     - command descriptor.
 *              SRpnt     - Request from the queue.
 *
 * Lock status: None needed.
 *
 * Returns:     Nothing.
 *
 * Notes:       Mainly transfer data from the request structure to the
 *              command structure.  The request structure is allocated
 *              using the normal memory allocator, and requests can pile
 *              up to more or less any depth.  The command structure represents
 *              a consumable resource, as these are allocated into a pool
 *              when the SCSI subsystem initializes.  The preallocation is
 *              required so that in low-memory situations a disk I/O request
 *              won't cause the memory manager to try and write out a page.
 *              The request structure is generally used by ioctls and character
 *              devices.
 */
void scsi_init_cmd_from_req(Scsi_Cmnd * SCpnt, Scsi_Request * SRpnt)
{
573
	struct Scsi_Host *host = SCpnt->device->host;
Linus Torvalds's avatar
Linus Torvalds committed
574 575 576 577 578 579 580 581 582 583 584

	SCpnt->owner = SCSI_OWNER_MIDLEVEL;
	SRpnt->sr_command = SCpnt;

	if (!host) {
		panic("Invalid or not present host.\n");
	}

	SCpnt->cmd_len = SRpnt->sr_cmd_len;
	SCpnt->use_sg = SRpnt->sr_use_sg;

585
        SCpnt->request = SRpnt->sr_request;
Linus Torvalds's avatar
Linus Torvalds committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
	memcpy((void *) SCpnt->data_cmnd, (const void *) SRpnt->sr_cmnd, 
	       sizeof(SCpnt->data_cmnd));
	SCpnt->reset_chain = NULL;
	SCpnt->serial_number = 0;
	SCpnt->serial_number_at_timeout = 0;
	SCpnt->bufflen = SRpnt->sr_bufflen;
	SCpnt->buffer = SRpnt->sr_buffer;
	SCpnt->flags = 0;
	SCpnt->retries = 0;
	SCpnt->allowed = SRpnt->sr_allowed;
	SCpnt->done = SRpnt->sr_done;
	SCpnt->timeout_per_command = SRpnt->sr_timeout_per_command;

	SCpnt->sc_data_direction = SRpnt->sr_data_direction;

	SCpnt->sglist_len = SRpnt->sr_sglist_len;
	SCpnt->underflow = SRpnt->sr_underflow;

	SCpnt->sc_request = SRpnt;

	memcpy((void *) SCpnt->cmnd, (const void *) SRpnt->sr_cmnd, 
	       sizeof(SCpnt->cmnd));
	/* Zero the sense buffer.  Some host adapters automatically request
	 * sense on error.  0 is not a valid sense code.
	 */
	memset((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
	SCpnt->request_buffer = SRpnt->sr_buffer;
	SCpnt->request_bufflen = SRpnt->sr_bufflen;
	SCpnt->old_use_sg = SCpnt->use_sg;
	if (SCpnt->cmd_len == 0)
		SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
	SCpnt->old_cmd_len = SCpnt->cmd_len;
	SCpnt->sc_old_data_direction = SCpnt->sc_data_direction;
	SCpnt->old_underflow = SCpnt->underflow;

	/* Start the timer ticking.  */

	SCpnt->internal_timeout = NORMAL_TIMEOUT;
	SCpnt->abort_reason = 0;
	SCpnt->result = 0;

Linus Torvalds's avatar
Linus Torvalds committed
627
	SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_init_cmd_from_req()\n"));
Linus Torvalds's avatar
Linus Torvalds committed
628 629
}

Matthew Wilcox's avatar
Matthew Wilcox committed
630
/**
631 632 633
 * scsi_done - Enqueue the finished SCSI command into the done queue.
 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
Linus Torvalds's avatar
Linus Torvalds committed
634
 *
635 636 637
 * This function is the mid-level's (SCSI Core) interrupt routine, which
 * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues
 * the command to the done queue for further processing.
Linus Torvalds's avatar
Linus Torvalds committed
638
 *
639
 * This is the producer of the done queue who enqueues at the tail.
Matthew Wilcox's avatar
Matthew Wilcox committed
640
 *
641
 * This function is interrupt context safe.
Linus Torvalds's avatar
Linus Torvalds committed
642
 */
643
void scsi_done(struct scsi_cmnd *cmd)
Linus Torvalds's avatar
Linus Torvalds committed
644
{
645
	int cpu;
Linus Torvalds's avatar
Linus Torvalds committed
646
	unsigned long flags;
647
	struct list_head *pdone_q;
Linus Torvalds's avatar
Linus Torvalds committed
648 649 650

	/*
	 * We don't have to worry about this one timing out any more.
651 652
	 * If we are unable to remove the timer, then the command
	 * has already timed out.  In which case, we have no choice but to
Linus Torvalds's avatar
Linus Torvalds committed
653 654 655 656
	 * let the timeout function run, as we have no idea where in fact
	 * that function could really be.  It might be on another processor,
	 * etc, etc.
	 */
657
	if (!scsi_delete_timer(cmd))
Linus Torvalds's avatar
Linus Torvalds committed
658
		return;
659

Linus Torvalds's avatar
Linus Torvalds committed
660
	/* Set the serial numbers back to zero */
661 662 663 664
	cmd->serial_number = 0;
	cmd->serial_number_at_timeout = 0;
	cmd->state = SCSI_STATE_BHQUEUE;
	cmd->owner = SCSI_OWNER_BH_HANDLER;
Linus Torvalds's avatar
Linus Torvalds committed
665 666

	/*
667 668
	 * Next, enqueue the command into the done queue.
	 * It is a per-CPU queue, so we just disable local interrupts
Matthew Wilcox's avatar
Matthew Wilcox committed
669
	 * and need no spinlock.
Linus Torvalds's avatar
Linus Torvalds committed
670
	 */
Matthew Wilcox's avatar
Matthew Wilcox committed
671 672 673
	local_irq_save(flags);

	cpu = smp_processor_id();
674 675
	pdone_q = &done_q[cpu];
	list_add_tail(&cmd->eh_entry, pdone_q);
Matthew Wilcox's avatar
Matthew Wilcox committed
676 677 678
	cpu_raise_softirq(cpu, SCSI_SOFTIRQ);

	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
679 680
}

Matthew Wilcox's avatar
Matthew Wilcox committed
681
/**
682 683 684
 * scsi_softirq - Perform post-interrupt processing of finished SCSI commands.
 *
 * This is the consumer of the done queue.
Matthew Wilcox's avatar
Matthew Wilcox committed
685 686 687 688
 *
 * This is called with all interrupts enabled.  This should reduce
 * interrupt latency, stack depth, and reentrancy of the low-level
 * drivers.
Linus Torvalds's avatar
Linus Torvalds committed
689
 */
Matthew Wilcox's avatar
Matthew Wilcox committed
690
static void scsi_softirq(struct softirq_action *h)
Linus Torvalds's avatar
Linus Torvalds committed
691
{
692
	LIST_HEAD(local_q);
Linus Torvalds's avatar
Linus Torvalds committed
693

694 695 696
	local_irq_disable();
	list_splice_init(&done_q[smp_processor_id()], &local_q);
	local_irq_enable();
Linus Torvalds's avatar
Linus Torvalds committed
697

698 699 700 701
	while (!list_empty(&local_q)) {
		struct scsi_cmnd *cmd = list_entry(local_q.next,
						   struct scsi_cmnd, eh_entry);
		list_del_init(&cmd->eh_entry);
Linus Torvalds's avatar
Linus Torvalds committed
702

703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
		switch (scsi_decide_disposition(cmd)) {
		case SUCCESS:
			/*
			 * Add to BH queue.
			 */
			SCSI_LOG_MLCOMPLETE(3,
					    printk("Command finished %d %d "
						   "0x%x\n",
					   cmd->device->host->host_busy,
					   cmd->device->host->host_failed,
						   cmd->result));

			scsi_finish_command(cmd);
			break;
		case NEEDS_RETRY:
			/*
			 * We only come in here if we want to retry a
			 * command.  The test to see whether the
			 * command should be retried should be keeping
			 * track of the number of tries, so we don't
			 * end up looping, of course.
			 */
			SCSI_LOG_MLCOMPLETE(3, printk("Command needs retry "
						      "%d %d 0x%x\n",
					      cmd->device->host->host_busy,
					      cmd->device->host->host_failed,
						      cmd->result));
Linus Torvalds's avatar
Linus Torvalds committed
730

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
			scsi_retry_command(cmd);
			break;
		case ADD_TO_MLQUEUE:
			/* 
			 * This typically happens for a QUEUE_FULL
			 * message - typically only when the queue
			 * depth is only approximate for a given
			 * device.  Adding a command to the queue for
			 * the device will prevent further commands
			 * from being sent to the device, so we
			 * shouldn't end up with tons of things being
			 * sent down that shouldn't be.
			 */
			SCSI_LOG_MLCOMPLETE(3, printk("Command rejected as "
						      "device queue full, "
						      "put on ml queue %p\n",
						      cmd));
			scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
			break;
		default:
			/*
			 * Here we have a fatal error of some sort.
			 * Turn it over to the error handler.
			 */
			SCSI_LOG_MLCOMPLETE(3,
					    printk("Command failed %p %x "
						   "busy=%d failed=%d\n",
						   cmd, cmd->result,
					   cmd->device->host->host_busy,
					   cmd->device->host->host_failed));
Linus Torvalds's avatar
Linus Torvalds committed
761

762 763 764 765 766 767
			/*
			 * Dump the sense information too.
			 */
			if ((status_byte(cmd->result)&CHECK_CONDITION) != 0) {
				SCSI_LOG_MLCOMPLETE(3, print_sense("bh", cmd));
			}
Linus Torvalds's avatar
Linus Torvalds committed
768

769
			if (!scsi_eh_scmd_add(cmd, 0)) {
Linus Torvalds's avatar
Linus Torvalds committed
770
				/*
771 772
				 * We only get here if the error
				 * recovery thread has died.
Linus Torvalds's avatar
Linus Torvalds committed
773
				 */
774 775 776 777
				scsi_finish_command(cmd);
			}
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791
}

/*
 * Function:    scsi_retry_command
 *
 * Purpose:     Send a command back to the low level to be retried.
 *
 * Notes:       This command is always executed in the context of the
 *              bottom half handler, or the error handler thread. Low
 *              level drivers should not become re-entrant as a result of
 *              this.
 */
int scsi_retry_command(Scsi_Cmnd * SCpnt)
{
792 793 794 795
	/*
	 * Restore the SCSI command state.
	 */
	scsi_setup_cmd_retry(SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

        /*
         * Zero the sense information from the last time we tried
         * this command.
         */
	memset((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);

	return scsi_dispatch_cmd(SCpnt);
}

/*
 * Function:    scsi_finish_command
 *
 * Purpose:     Pass command off to upper layer for finishing of I/O
 *              request, waking processes that are waiting on results,
 *              etc.
 */
void scsi_finish_command(Scsi_Cmnd * SCpnt)
{
	struct Scsi_Host *host;
	Scsi_Device *device;
	Scsi_Request * SRpnt;
818
	unsigned int flags;
Linus Torvalds's avatar
Linus Torvalds committed
819

820
	host = SCpnt->device->host;
Linus Torvalds's avatar
Linus Torvalds committed
821 822 823 824 825 826 827 828 829
	device = SCpnt->device;

        /*
         * We need to protect the decrement, as otherwise a race condition
         * would exist.  Fiddling with SCpnt isn't a problem as the
         * design only allows a single SCpnt to be active in only
         * one execution context, but the device and host structures are
         * shared.
         */
830
	scsi_host_busy_dec_and_test(host, device);
831 832 833
	spin_lock_irqsave(SCpnt->device->request_queue->queue_lock, flags);
	SCpnt->device->device_busy--;
	spin_unlock_irqrestore(SCpnt->device->request_queue->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
834 835 836 837 838 839 840

        /*
         * Clear the flags which say that the device/host is no longer
         * capable of accepting new commands.  These are set in scsi_queue.c
         * for both the queue full condition on a device, and for a
         * host full condition on the host.
         */
841
        host->host_blocked = 0;
842
        device->device_blocked = 0;
Linus Torvalds's avatar
Linus Torvalds committed
843 844 845 846 847

	/*
	 * If we have valid sense information, then some kind of recovery
	 * must have taken place.  Make a note of this.
	 */
848
	if (SCSI_SENSE_VALID(SCpnt)) {
Linus Torvalds's avatar
Linus Torvalds committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
		SCpnt->result |= (DRIVER_SENSE << 24);
	}
	SCSI_LOG_MLCOMPLETE(3, printk("Notifying upper driver of completion for device %d %x\n",
				      SCpnt->device->id, SCpnt->result));

	SCpnt->owner = SCSI_OWNER_HIGHLEVEL;
	SCpnt->state = SCSI_STATE_FINISHED;

	/* We can get here with use_sg=0, causing a panic in the upper level (DB) */
	SCpnt->use_sg = SCpnt->old_use_sg;

       /*
	* If there is an associated request structure, copy the data over before we call the
	* completion function.
	*/
	SRpnt = SCpnt->sc_request;
	if( SRpnt != NULL ) {
	       SRpnt->sr_result = SRpnt->sr_command->result;
	       if( SRpnt->sr_result != 0 ) {
		       memcpy(SRpnt->sr_sense_buffer,
			      SRpnt->sr_command->sense_buffer,
			      sizeof(SRpnt->sr_sense_buffer));
	       }
	}

	SCpnt->done(SCpnt);
}

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
/*
 * Function:	scsi_adjust_queue_depth()
 *
 * Purpose:	Allow low level drivers to tell us to change the queue depth
 * 		on a specific SCSI device
 *
 * Arguments:	SDpnt	- SCSI Device in question
 * 		tagged	- Do we use tagged queueing (non-0) or do we treat
 * 			  this device as an untagged device (0)
 * 		tags	- Number of tags allowed if tagged queueing enabled,
 * 			  or number of commands the low level driver can
 * 			  queue up in non-tagged mode (as per cmd_per_lun).
 *
 * Returns:	Nothing
 *
 * Lock Status:	None held on entry
 *
 * Notes:	Low level drivers may call this at any time and we will do
 * 		the right thing depending on whether or not the device is
 * 		currently active and whether or not it even has the
 * 		command blocks built yet.
 */
void scsi_adjust_queue_depth(Scsi_Device *SDpnt, int tagged, int tags)
{
901
	static spinlock_t device_request_lock = SPIN_LOCK_UNLOCKED;
902
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
903

904 905 906
	/*
	 * refuse to set tagged depth to an unworkable size
	 */
907
	if(tags <= 0)
908
		return;
909 910 911 912 913 914 915 916
	/*
	 * Limit max queue depth on a single lun to 256 for now.  Remember,
	 * we allocate a struct scsi_command for each of these and keep it
	 * around forever.  Too deep of a depth just wastes memory.
	 */
	if(tags > 256)
		return;

917
	spin_lock_irqsave(&device_request_lock, flags);
918
	SDpnt->queue_depth = tags;
919 920 921 922 923 924 925 926 927 928 929 930 931
	switch(tagged) {
		case MSG_ORDERED_TAG:
			SDpnt->ordered_tags = 1;
			SDpnt->simple_tags = 1;
			break;
		case MSG_SIMPLE_TAG:
			SDpnt->ordered_tags = 0;
			SDpnt->simple_tags = 1;
			break;
		default:
			printk(KERN_WARNING "(scsi%d:%d:%d:%d) "
				"scsi_adjust_queue_depth, bad queue type, "
				"disabled\n", SDpnt->host->host_no,
932
				SDpnt->channel, SDpnt->id, SDpnt->lun); 
933 934
		case 0:
			SDpnt->ordered_tags = SDpnt->simple_tags = 0;
935
			SDpnt->queue_depth = tags;
936 937
			break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
938 939 940
	spin_unlock_irqrestore(&device_request_lock, flags);
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
/*
 * Function:	scsi_track_queue_full()
 *
 * Purpose:	This function will track successive QUEUE_FULL events on a
 * 		specific SCSI device to determine if and when there is a
 * 		need to adjust the queue depth on the device.
 *
 * Arguments:	SDpnt	- SCSI Device in question
 * 		depth	- Current number of outstanding SCSI commands on
 * 			  this device, not counting the one returned as
 * 			  QUEUE_FULL.
 *
 * Returns:	0 - No change needed
 * 		>0 - Adjust queue depth to this new depth
 * 		-1 - Drop back to untagged operation using host->cmd_per_lun
 * 			as the untagged command depth
 *
 * Lock Status:	None held on entry
 *
 * Notes:	Low level drivers may call this at any time and we will do
 * 		"The Right Thing."  We are interrupt context safe.
 */
int scsi_track_queue_full(Scsi_Device *SDptr, int depth)
{
	if((jiffies >> 4) != SDptr->last_queue_full_time) {
		SDptr->last_queue_full_time = (jiffies >> 4);
		if(SDptr->last_queue_full_depth == depth)
			SDptr->last_queue_full_count++;
		else {
			SDptr->last_queue_full_count = 1;
			SDptr->last_queue_full_depth = depth;
		}
		if(SDptr->last_queue_full_count > 10) {
			if(SDptr->last_queue_full_depth < 8) {
				/* Drop back to untagged */
				scsi_adjust_queue_depth(SDptr, 0 /* untagged */,
						SDptr->host->cmd_per_lun);
				return -1;
			}
			if(SDptr->ordered_tags)
				scsi_adjust_queue_depth(SDptr, MSG_ORDERED_TAG,
						depth);
			else
				scsi_adjust_queue_depth(SDptr, MSG_SIMPLE_TAG,
						depth);
			return depth;
		}
	}
	return 0;
}


993 994 995 996 997 998 999 1000 1001 1002 1003
/*
 * scsi_strcpy_devinfo: called from scsi_dev_info_list_add to copy into
 * devinfo vendor and model strings.
 */
static void scsi_strcpy_devinfo(char *name, char *to, size_t to_length,
				char *from, int compatible)
{
	size_t from_length;

	from_length = strlen(from);
	strncpy(to, from, min(to_length, from_length));
1004
	if (from_length < to_length) {
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		if (compatible) {
			/*
			 * NUL terminate the string if it is short.
			 */
			to[from_length] = '\0';
		} else {
			/* 
			 * space pad the string if it is short. 
			 */
			strncpy(&to[from_length], spaces,
				to_length - from_length);
		}
1017
	}
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	if (from_length > to_length)
		 printk(KERN_WARNING "%s: %s string '%s' is too long\n",
			__FUNCTION__, name, from);
}

/**
 * scsi_dev_info_list_add: add one dev_info list entry.
 * @vendor:	vendor string
 * @model:	model (product) string
 * @strflags:	integer string
 * @flag:	if strflags NULL, use this flag value
 *
 * Description:
 * 	Create and add one dev_info entry for @vendor, @model, @strflags or
 * 	@flag. If @compatible, add to the tail of the list, do not space
 * 	pad, and set devinfo->compatible. The scsi_static_device_list entries
 * 	are added with @compatible 1 and @clfags NULL.
 *
 * Returns: 0 OK, -error on failure.
 **/
static int scsi_dev_info_list_add(int compatible, char *vendor, char *model,
			    char *strflags, int flags)
{
	struct scsi_dev_info_list *devinfo;

	devinfo = kmalloc(sizeof(*devinfo), GFP_KERNEL);
	if (!devinfo) {
		printk(KERN_ERR "%s: no memory\n", __FUNCTION__);
		return -ENOMEM;
	}

	scsi_strcpy_devinfo("vendor", devinfo->vendor, sizeof(devinfo->vendor),
			    vendor, compatible);
	scsi_strcpy_devinfo("model", devinfo->model, sizeof(devinfo->model),
			    model, compatible);

	if (strflags)
		devinfo->flags = simple_strtoul(strflags, NULL, 0);
	else
		devinfo->flags = flags;

	devinfo->compatible = compatible;

	if (compatible)
		list_add_tail(&devinfo->dev_info_list, &scsi_dev_info_list);
	else
		list_add(&devinfo->dev_info_list, &scsi_dev_info_list);

	return 0;
}

/**
 * scsi_dev_info_list_add_str: parse dev_list and add to the
 * scsi_dev_info_list.
 * @dev_list:	string of device flags to add
 *
 * Description:
 * 	Parse dev_list, and add entries to the scsi_dev_info_list.
 * 	dev_list is of the form "vendor:product:flag,vendor:product:flag".
 * 	dev_list is modified via strsep. Can be called for command line
 * 	addition, for proc or mabye a sysfs interface.
 *
 * Returns: 0 if OK, -error on failure.
 **/
1082
int scsi_dev_info_list_add_str (char *dev_list)
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
{
	char *vendor, *model, *strflags, *next;
	char *next_check;
	int res = 0;

	next = dev_list;
	if (next && next[0] == '"') {
		/*
		 * Ignore both the leading and trailing quote.
		 */
		next++;
		next_check = ",\"";
	} else {
		next_check = ",";
	}

	/*
	 * For the leading and trailing '"' case, the for loop comes
	 * through the last time with vendor[0] == '\0'.
	 */
	for (vendor = strsep(&next, ":"); vendor && (vendor[0] != '\0')
	     && (res == 0); vendor = strsep(&next, ":")) {
		strflags = NULL;
		model = strsep(&next, ":");
		if (model)
			strflags = strsep(&next, next_check);
		if (!model || !strflags) {
			printk(KERN_ERR "%s: bad dev info string '%s' '%s'"
			       " '%s'\n", __FUNCTION__, vendor, model,
			       strflags);
			res = -EINVAL;
		} else
			res = scsi_dev_info_list_add(0 /* compatible */, vendor,
						     model, strflags, 0);
	}
	return res;
}

/**
 * scsi_dev_info_list_delete: called from scsi.c:exit_scsi to remove
 * 	the scsi_dev_info_list.
 **/
static void scsi_dev_info_list_delete (void)
{
	struct list_head *lh, *lh_next;
	struct scsi_dev_info_list *devinfo;

	list_for_each_safe(lh, lh_next, &scsi_dev_info_list) {
		devinfo = list_entry(lh, struct scsi_dev_info_list,
				     dev_info_list);
		kfree(devinfo);
	}
}

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
/**
 * scsi_dev_list_init: set up the dynamic device list.
 * @dev_list:	string of device flags to add
 *
 * Description:
 * 	Add command line @dev_list entries, then add
 * 	scsi_static_device_list entries to the scsi device info list.
 **/
static int scsi_dev_info_list_init (char *dev_list)
{
	int error, i;

	error = scsi_dev_info_list_add_str(dev_list);
	if (error)
		return error;

	for (i = 0; scsi_static_device_list[i].vendor != NULL; i++) {
		error = scsi_dev_info_list_add(1 /* compatibile */,
				scsi_static_device_list[i].vendor,
				scsi_static_device_list[i].model,
				NULL,
				scsi_static_device_list[i].flags);
		if (error)
			break;
	}

	if (error)
		scsi_dev_info_list_delete();
	return error;
}

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
/**
 * get_device_flags - get device specific flags from the dynamic device
 * list. Called during scan time.
 * @vendor:	vendor name
 * @model:	model name
 *
 * Description:
 *     Search the scsi_dev_info_list for an entry matching @vendor and
 *     @model, if found, return the matching flags value, else return
 *     scsi_default_dev_flags.
 **/
int scsi_get_device_flags(unsigned char *vendor, unsigned char *model)
{
	struct scsi_dev_info_list *devinfo;

	list_for_each_entry(devinfo, &scsi_dev_info_list, dev_info_list) {
		if (devinfo->compatible) {
			/*
			 * Behave like the older version of get_device_flags.
			 */
			size_t max;
			/*
			 * XXX why skip leading spaces? If an odd INQUIRY
			 * value, that should have been part of the
			 * scsi_static_device_list[] entry, such as "  FOO"
			 * rather than "FOO". Since this code is already
			 * here, and we don't know what device it is
			 * trying to work with, leave it as-is.
			 */
			max = 8;	/* max length of vendor */
			while ((max > 0) && *vendor == ' ') {
				max--;
				vendor++;
			}
			/*
			 * XXX removing the following strlen() would be
			 * good, using it means that for a an entry not in
			 * the list, we scan every byte of every vendor
			 * listed in scsi_static_device_list[], and never match
			 * a single one (and still have to compare at
			 * least the first byte of each vendor).
			 */
			if (memcmp(devinfo->vendor, vendor,
				    min(max, strlen(devinfo->vendor))))
				continue;
			/*
			 * Skip spaces again.
			 */
			max = 16;	/* max length of model */
			while ((max > 0) && *model == ' ') {
				max--;
				model++;
			}
			if (memcmp(devinfo->model, model,
				   min(max, strlen(devinfo->model))))
				continue;
			return devinfo->flags;
		} else {
			if (!memcmp(devinfo->vendor, vendor,
				     sizeof(devinfo->vendor)) &&
			     !memcmp(devinfo->model, model,
				      sizeof(devinfo->model)))
				return devinfo->flags;
		}
	}
	return scsi_default_dev_flags;
}

1236 1237 1238 1239 1240
int scsi_attach_device(struct scsi_device *sdev)
{
	struct Scsi_Device_Template *sdt;

	down_read(&scsi_devicelist_mutex);
1241 1242 1243 1244 1245 1246
	list_for_each_entry(sdt, &scsi_devicelist, list) {
		if (!try_module_get(sdt->module))
			continue;
		(*sdt->attach)(sdev);
		module_put(sdt->module);
	}
1247 1248 1249 1250 1251 1252 1253 1254 1255
	up_read(&scsi_devicelist_mutex);
	return 0;
}

void scsi_detach_device(struct scsi_device *sdev)
{
	struct Scsi_Device_Template *sdt;

	down_read(&scsi_devicelist_mutex);
1256 1257 1258 1259 1260 1261
	list_for_each_entry(sdt, &scsi_devicelist, list) {
		if (!try_module_get(sdt->module))
			continue;
		(*sdt->detach)(sdev);
		module_put(sdt->module);
	}
1262 1263 1264
	up_read(&scsi_devicelist_mutex);
}

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
void scsi_rescan_device(struct scsi_device *sdev)
{
	struct Scsi_Device_Template *sdt;

	down_read(&scsi_devicelist_mutex);
	list_for_each_entry(sdt, &scsi_devicelist, list) {
		if (!try_module_get(sdt->module))
			continue;
		if (*sdt->rescan)
			(*sdt->rescan)(sdev);
		module_put(sdt->module);
	}
	up_read(&scsi_devicelist_mutex);
}

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
int scsi_device_get(struct scsi_device *sdev)
{
	if (!try_module_get(sdev->host->hostt->module))
		return -ENXIO;

	sdev->access_count++;
	return 0;
}

void scsi_device_put(struct scsi_device *sdev)
{
	sdev->access_count--;
	module_put(sdev->host->hostt->module);
}

1295 1296 1297 1298 1299 1300 1301 1302 1303
/**
 * scsi_set_device_offline - set scsi_device offline
 * @sdev:	pointer to struct scsi_device to offline. 
 *
 * Locks:	host_lock held on entry.
 **/
void scsi_set_device_offline(struct scsi_device *sdev)
{
	struct scsi_cmnd *scmd;
1304 1305
	LIST_HEAD(active_list);
	struct list_head *lh, *lh_sf;
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	unsigned long flags;

	sdev->online = FALSE;

	spin_lock_irqsave(&sdev->list_lock, flags);
	list_for_each_entry(scmd, &sdev->cmd_list, list) {
		if (scmd->request && scmd->request->rq_status != RQ_INACTIVE) {
			/*
			 * If we are unable to remove the timer, it means
			 * that the command has already timed out or
			 * finished.
			 */
			if (!scsi_delete_timer(scmd)) {
				continue;
			}
1321
			list_add_tail(&scmd->eh_entry, &active_list);
1322 1323 1324 1325
		}
	}
	spin_unlock_irqrestore(&sdev->list_lock, flags);

1326 1327 1328 1329 1330 1331
	if (!list_empty(&active_list)) {
		list_for_each_safe(lh, lh_sf, &active_list) {
			scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
			scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD);
		}
	} else {
1332 1333 1334 1335
		/* FIXME: Send online state change hotplug event */
	}
}

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
/*
 * Function:	scsi_slave_attach()
 *
 * Purpose:	Called from the upper level driver attach to handle common
 * 		attach code.
 *
 * Arguments:	sdev - scsi_device to attach
 *
 * Returns:	1 on error, 0 on succes
 *
 * Lock Status:	Protected via scsi_devicelist_mutex.
 */
int scsi_slave_attach(struct scsi_device *sdev)
{
1350
	sdev->attached++;
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
	return 0;
}

/*
 * Function:	scsi_slave_detach()
 *
 * Purpose:	Called from the upper level driver attach to handle common
 * 		detach code.
 *
 * Arguments:	sdev - struct scsi_device to detach
 *
 * Lock Status:	Protected via scsi_devicelist_mutex.
 */
void scsi_slave_detach(struct scsi_device *sdev)
{
1366
	sdev->attached--;
1367
}
Linus Torvalds's avatar
Linus Torvalds committed
1368 1369 1370
/*
 * This entry point should be called by a loadable module if it is trying
 * add a high level scsi driver to the system.
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
 *
 * This entry point is called from the upper level module's module_init()
 * routine.  That implies that when this function is called, the
 * scsi_mod module is locked down because of upper module layering and
 * that the high level driver module is locked down by being in it's
 * init routine.  So, the *only* thing we have to do to protect adds 
 * we perform in this function is to make sure that all call's
 * to the high level driver's attach() and detach() call in points, other
 * than via scsi_register_device and scsi_unregister_device which are in
 * the module_init and module_exit code respectively and therefore already
 * locked down by the kernel module loader, are wrapped by try_module_get()
 * and module_put() to avoid races on device adds and removes.
Linus Torvalds's avatar
Linus Torvalds committed
1383
 */
Linus Torvalds's avatar
Linus Torvalds committed
1384
int scsi_register_device(struct Scsi_Device_Template *tpnt)
Linus Torvalds's avatar
Linus Torvalds committed
1385 1386 1387 1388
{
	Scsi_Device *SDpnt;
	struct Scsi_Host *shpnt;

Linus Torvalds's avatar
Linus Torvalds committed
1389
#ifdef CONFIG_KMOD
1390
	if (scsi_host_get_next(NULL) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
1391 1392 1393
		request_module("scsi_hostadapter");
#endif

1394
	if (!list_empty(&tpnt->list))
Linus Torvalds's avatar
Linus Torvalds committed
1395 1396
		return 1;

1397
	down_write(&scsi_devicelist_mutex);
1398
	list_add_tail(&tpnt->list, &scsi_devicelist);
1399
	up_write(&scsi_devicelist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1400

Mike Anderson's avatar
Mike Anderson committed
1401
	scsi_upper_driver_register(tpnt);
1402

1403
	for (shpnt = scsi_host_get_next(NULL); shpnt;
1404 1405 1406
	     shpnt = scsi_host_get_next(shpnt)) 
		list_for_each_entry (SDpnt, &shpnt->my_devices, siblings)
			(*tpnt->attach) (SDpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1407

1408
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1409 1410
}

Linus Torvalds's avatar
Linus Torvalds committed
1411
int scsi_unregister_device(struct Scsi_Device_Template *tpnt)
Linus Torvalds's avatar
Linus Torvalds committed
1412 1413 1414
{
	Scsi_Device *SDpnt;
	struct Scsi_Host *shpnt;
1415

Linus Torvalds's avatar
Linus Torvalds committed
1416 1417 1418
	/*
	 * Next, detach the devices from the driver.
	 */
1419 1420
	for (shpnt = scsi_host_get_next(NULL); shpnt;
	     shpnt = scsi_host_get_next(shpnt)) {
1421 1422
		list_for_each_entry(SDpnt, &shpnt->my_devices, siblings)
			(*tpnt->detach) (SDpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1423
	}
1424

Linus Torvalds's avatar
Linus Torvalds committed
1425 1426 1427
	/*
	 * Extract the template from the linked list.
	 */
1428
	down_write(&scsi_devicelist_mutex);
1429
	list_del(&tpnt->list);
1430
	up_write(&scsi_devicelist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1431

Mike Anderson's avatar
Mike Anderson committed
1432
	scsi_upper_driver_unregister(tpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1433 1434 1435
	return 0;
}

1436 1437 1438 1439 1440 1441 1442 1443 1444
static char *scsi_dev_flags;
MODULE_PARM(scsi_dev_flags, "s");
MODULE_PARM_DESC(scsi_dev_flags,
	 "Given scsi_dev_flags=vendor:model:flags, add a black/white list"
	 " entry for vendor and model with an integer value of flags"
	 " to the scsi device info list");
MODULE_PARM(scsi_default_dev_flags, "i");
MODULE_PARM_DESC(scsi_default_dev_flags,
		 "scsi default device flag integer value");
Linus Torvalds's avatar
Linus Torvalds committed
1445
MODULE_DESCRIPTION("SCSI core");
Linus Torvalds's avatar
Linus Torvalds committed
1446
MODULE_LICENSE("GPL");
Linus Torvalds's avatar
Linus Torvalds committed
1447 1448

#ifndef MODULE
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472

int __init setup_scsi_dev_flags(char *str)
{
	scsi_dev_flags = str;
	return 1;
}
__setup("scsi_dev_flags=", setup_scsi_dev_flags);

static int __init setup_scsi_default_dev_flags(char *str)
{
	unsigned int tmp;
	if (get_option(&str, &tmp) == 1) {
		scsi_default_dev_flags = tmp;
		printk(KERN_WARNING "%s %d\n", __FUNCTION__,
		       scsi_default_dev_flags);
		return 1;
	} else {
		printk(KERN_WARNING "%s: usage scsi_default_dev_flags=intr\n",
		       __FUNCTION__);
		return 0;
	}
}
__setup("scsi_default_dev_flags=", setup_scsi_default_dev_flags);

Linus Torvalds's avatar
Linus Torvalds committed
1473
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1474

Linus Torvalds's avatar
Linus Torvalds committed
1475 1476
static int __init init_scsi(void)
{
1477
	int error, i;
1478 1479 1480 1481 1482 1483 1484 1485 1486

	error = scsi_init_queue();
	if (error)
		return error;
	error = scsi_init_procfs();
	if (error)
		goto cleanup_queue;
	error = scsi_dev_info_list_init(scsi_dev_flags);
	if (error)
1487
		goto cleanup_procfs;
1488 1489 1490 1491
	error = scsi_sysfs_register();
	if (error)
		goto cleanup_devlist;

1492 1493 1494
	for (i = 0; i < NR_CPUS; i++)
		INIT_LIST_HEAD(&done_q[i]);

Patrick Mansfield's avatar
Patrick Mansfield committed
1495
	scsi_host_init();
1496
	devfs_mk_dir("scsi");
Matthew Wilcox's avatar
Matthew Wilcox committed
1497
	open_softirq(SCSI_SOFTIRQ, scsi_softirq, NULL);
1498
	printk(KERN_NOTICE "SCSI subsystem initialized\n");
Linus Torvalds's avatar
Linus Torvalds committed
1499
	return 0;
1500 1501 1502 1503 1504 1505 1506

cleanup_devlist:
	scsi_dev_info_list_delete();
cleanup_procfs:
	scsi_exit_procfs();
cleanup_queue:
	scsi_exit_queue();
1507 1508
	printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
	       -error);
1509
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
1510 1511 1512 1513
}

static void __exit exit_scsi(void)
{
Mike Anderson's avatar
Mike Anderson committed
1514
	scsi_sysfs_unregister();
1515
	scsi_dev_info_list_delete();
Alexander Viro's avatar
Alexander Viro committed
1516
	devfs_remove("scsi");
1517
	scsi_exit_procfs();
1518
	scsi_exit_queue();
Linus Torvalds's avatar
Linus Torvalds committed
1519 1520
}

Mike Anderson's avatar
Mike Anderson committed
1521
subsys_initcall(init_scsi);
Linus Torvalds's avatar
Linus Torvalds committed
1522
module_exit(exit_scsi);