2 * scsi.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 * Copyright (C) 2002, 2003 Christoph Hellwig
6 * generic mid-level SCSI driver
7 * Initial versions: Drew Eckhardt
8 * Subsequent revisions: Eric Youngdale
12 * Bug correction thanks go to :
13 * Rik Faith <faith@cs.unc.edu>
14 * Tommy Thorn <tthorn>
15 * Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
17 * Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to
18 * add scatter-gather, multiple outstanding request, and other
21 * Native multichannel, wide scsi, /proc/scsi and hot plugging
22 * support added by Michael Neuffer <mike@i-connect.net>
24 * Added request_module("scsi_hostadapter") for kerneld:
25 * (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf)
26 * Bjorn Ekwall <bj0rn@blox.se>
29 * Major improvements to the timeout, abort, and reset processing,
30 * as well as performance modifications for large queue depths by
31 * Leonard N. Zubkoff <lnz@dandelion.com>
33 * Converted cli() code to spinlocks, Ingo Molnar
35 * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
37 * out_of_space hacks, D. Gilbert (dpg) 990608
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/timer.h>
45 #include <linux/string.h>
46 #include <linux/slab.h>
47 #include <linux/blkdev.h>
48 #include <linux/delay.h>
49 #include <linux/init.h>
50 #include <linux/completion.h>
51 #include <linux/devfs_fs_kernel.h>
52 #include <linux/unistd.h>
53 #include <linux/spinlock.h>
54 #include <linux/kmod.h>
55 #include <linux/interrupt.h>
56 #include <linux/notifier.h>
57 #include <linux/cpu.h>
59 #include <scsi/scsi_host.h>
62 #include "scsi_priv.h"
63 #include "scsi_logging.h"
67 * Definitions and constants.
70 #define MIN_RESET_DELAY (2*HZ)
72 /* Do not call reset on error if we just did a reset within 15 sec. */
73 #define MIN_RESET_PERIOD (15*HZ)
76 * Macro to determine the size of SCSI command. This macro takes vendor
77 * unique commands into account. SCSI commands in groups 6 and 7 are
78 * vendor unique and we will depend upon the command length being
79 * supplied correctly in cmd_len.
81 #define CDB_SIZE(cmd) (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \
82 COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len)
87 unsigned long scsi_pid;
88 static unsigned long serial_number;
91 * Note - the initial logging level can be set here to log events at boot time.
92 * After the system is up, you may enable logging via the /proc interface.
94 unsigned int scsi_logging_level;
96 const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] = {
114 * Function: scsi_allocate_request
116 * Purpose: Allocate a request descriptor.
118 * Arguments: device - device for which we want a request
119 * gfp_mask - allocation flags passed to kmalloc
121 * Lock status: No locks assumed to be held. This function is SMP-safe.
123 * Returns: Pointer to request block.
125 struct scsi_request *scsi_allocate_request(struct scsi_device *sdev,
128 const int offset = ALIGN(sizeof(struct scsi_request), 4);
129 const int size = offset + sizeof(struct request);
130 struct scsi_request *sreq;
132 sreq = kmalloc(size, gfp_mask);
133 if (likely(sreq != NULL)) {
134 memset(sreq, 0, size);
135 sreq->sr_request = (struct request *)(((char *)sreq) + offset);
136 sreq->sr_device = sdev;
137 sreq->sr_host = sdev->host;
138 sreq->sr_magic = SCSI_REQ_MAGIC;
139 sreq->sr_data_direction = DMA_BIDIRECTIONAL;
145 void __scsi_release_request(struct scsi_request *sreq)
147 struct request *req = sreq->sr_request;
149 /* unlikely because the tag was usually ended earlier by the
150 * mid-layer. However, for layering reasons ULD's don't end
151 * the tag of commands they generate. */
152 if (unlikely(blk_rq_tagged(req))) {
154 struct request_queue *q = req->q;
156 spin_lock_irqsave(q->queue_lock, flags);
157 blk_queue_end_tag(q, req);
158 spin_unlock_irqrestore(q->queue_lock, flags);
162 if (likely(sreq->sr_command != NULL)) {
163 struct scsi_cmnd *cmd = sreq->sr_command;
165 sreq->sr_command = NULL;
166 scsi_next_command(cmd);
171 * Function: scsi_release_request
173 * Purpose: Release a request descriptor.
175 * Arguments: sreq - request to release
177 * Lock status: No locks assumed to be held. This function is SMP-safe.
179 void scsi_release_request(struct scsi_request *sreq)
181 __scsi_release_request(sreq);
185 struct scsi_host_cmd_pool {
189 unsigned int slab_flags;
190 unsigned int gfp_mask;
193 static struct scsi_host_cmd_pool scsi_cmd_pool = {
194 .name = "scsi_cmd_cache",
195 .slab_flags = SLAB_HWCACHE_ALIGN,
198 static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
199 .name = "scsi_cmd_cache(DMA)",
200 .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
201 .gfp_mask = __GFP_DMA,
204 static DECLARE_MUTEX(host_cmd_pool_mutex);
206 static struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost,
209 struct scsi_cmnd *cmd;
211 cmd = kmem_cache_alloc(shost->cmd_pool->slab,
212 gfp_mask | shost->cmd_pool->gfp_mask);
214 if (unlikely(!cmd)) {
217 spin_lock_irqsave(&shost->free_list_lock, flags);
218 if (likely(!list_empty(&shost->free_list))) {
219 cmd = list_entry(shost->free_list.next,
220 struct scsi_cmnd, list);
221 list_del_init(&cmd->list);
223 spin_unlock_irqrestore(&shost->free_list_lock, flags);
230 * Function: scsi_get_command()
232 * Purpose: Allocate and setup a scsi command block
234 * Arguments: dev - parent scsi device
235 * gfp_mask- allocator flags
237 * Returns: The allocated scsi command structure.
239 struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, int gfp_mask)
241 struct scsi_cmnd *cmd = __scsi_get_command(dev->host, gfp_mask);
243 if (likely(cmd != NULL)) {
246 memset(cmd, 0, sizeof(*cmd));
248 cmd->state = SCSI_STATE_UNUSED;
249 cmd->owner = SCSI_OWNER_NOBODY;
250 init_timer(&cmd->eh_timeout);
251 INIT_LIST_HEAD(&cmd->list);
252 spin_lock_irqsave(&dev->list_lock, flags);
253 list_add_tail(&cmd->list, &dev->cmd_list);
254 spin_unlock_irqrestore(&dev->list_lock, flags);
261 * Function: scsi_put_command()
263 * Purpose: Free a scsi command block
265 * Arguments: cmd - command block to free
269 * Notes: The command must not belong to any lists.
271 void scsi_put_command(struct scsi_cmnd *cmd)
273 struct Scsi_Host *shost = cmd->device->host;
276 /* serious error if the command hasn't come from a device list */
277 spin_lock_irqsave(&cmd->device->list_lock, flags);
278 BUG_ON(list_empty(&cmd->list));
279 list_del_init(&cmd->list);
280 spin_unlock(&cmd->device->list_lock);
281 /* changing locks here, don't need to restore the irq state */
282 spin_lock(&shost->free_list_lock);
283 if (unlikely(list_empty(&shost->free_list))) {
284 list_add(&cmd->list, &shost->free_list);
287 spin_unlock_irqrestore(&shost->free_list_lock, flags);
289 if (likely(cmd != NULL))
290 kmem_cache_free(shost->cmd_pool->slab, cmd);
294 * Function: scsi_setup_command_freelist()
296 * Purpose: Setup the command freelist for a scsi host.
298 * Arguments: shost - host to allocate the freelist for.
302 int scsi_setup_command_freelist(struct Scsi_Host *shost)
304 struct scsi_host_cmd_pool *pool;
305 struct scsi_cmnd *cmd;
307 spin_lock_init(&shost->free_list_lock);
308 INIT_LIST_HEAD(&shost->free_list);
311 * Select a command slab for this host and create it if not
314 down(&host_cmd_pool_mutex);
315 pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool);
317 pool->slab = kmem_cache_create(pool->name,
318 sizeof(struct scsi_cmnd), 0,
319 pool->slab_flags, NULL, NULL);
325 shost->cmd_pool = pool;
326 up(&host_cmd_pool_mutex);
329 * Get one backup command for this host.
331 cmd = kmem_cache_alloc(shost->cmd_pool->slab,
332 GFP_KERNEL | shost->cmd_pool->gfp_mask);
335 list_add(&cmd->list, &shost->free_list);
340 kmem_cache_destroy(pool->slab);
343 up(&host_cmd_pool_mutex);
349 * Function: scsi_destroy_command_freelist()
351 * Purpose: Release the command freelist for a scsi host.
353 * Arguments: shost - host that's freelist is going to be destroyed
355 void scsi_destroy_command_freelist(struct Scsi_Host *shost)
357 while (!list_empty(&shost->free_list)) {
358 struct scsi_cmnd *cmd;
360 cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
361 list_del_init(&cmd->list);
362 kmem_cache_free(shost->cmd_pool->slab, cmd);
365 down(&host_cmd_pool_mutex);
366 if (!--shost->cmd_pool->users)
367 kmem_cache_destroy(shost->cmd_pool->slab);
368 up(&host_cmd_pool_mutex);
371 #ifdef CONFIG_SCSI_LOGGING
372 void scsi_log_send(struct scsi_cmnd *cmd)
375 struct scsi_device *sdev;
378 * If ML QUEUE log level is greater than or equal to:
380 * 1: nothing (match completion)
382 * 2: log opcode + command of all commands
384 * 3: same as 2 plus dump cmd address
386 * 4: same as 3 plus dump extra junk
388 if (unlikely(scsi_logging_level)) {
389 level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
390 SCSI_LOG_MLQUEUE_BITS);
393 printk(KERN_INFO "scsi <%d:%d:%d:%d> send ",
394 sdev->host->host_no, sdev->channel, sdev->id,
397 printk("0x%p ", cmd);
399 * spaces to match disposition and cmd->result
400 * output in scsi_log_completion.
403 print_command(cmd->cmnd);
405 printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
406 " done = 0x%p, queuecommand 0x%p\n",
407 cmd->buffer, cmd->bufflen,
409 sdev->host->hostt->queuecommand);
416 void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
419 struct scsi_device *sdev;
422 * If ML COMPLETE log level is greater than or equal to:
424 * 1: log disposition, result, opcode + command, and conditionally
425 * sense data for failures or non SUCCESS dispositions.
427 * 2: same as 1 but for all command completions.
429 * 3: same as 2 plus dump cmd address
431 * 4: same as 3 plus dump extra junk
433 if (unlikely(scsi_logging_level)) {
434 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
435 SCSI_LOG_MLCOMPLETE_BITS);
436 if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
439 printk(KERN_INFO "scsi <%d:%d:%d:%d> done ",
440 sdev->host->host_no, sdev->channel, sdev->id,
443 printk("0x%p ", cmd);
445 * Dump truncated values, so we usually fit within
448 switch (disposition) {
463 * If called via scsi_times_out.
470 printk(" %8x ", cmd->result);
471 print_command(cmd->cmnd);
472 if (status_byte(cmd->result) & CHECK_CONDITION) {
474 * XXX The print_sense formatting/prefix
475 * doesn't match this function.
477 print_sense("", cmd);
480 printk(KERN_INFO "scsi host busy %d failed %d\n",
481 sdev->host->host_busy,
482 sdev->host->host_failed);
490 * Function: scsi_dispatch_command
492 * Purpose: Dispatch a command to the low-level driver.
494 * Arguments: cmd - command block we are dispatching.
498 int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
500 struct Scsi_Host *host = cmd->device->host;
501 unsigned long flags = 0;
502 unsigned long timeout;
505 /* check if the device is still usable */
506 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
507 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
508 * returns an immediate error upwards, and signals
509 * that the device is no longer present */
510 cmd->result = DID_NO_CONNECT << 16;
512 /* return 0 (because the command has been processed) */
515 /* Assign a unique nonzero serial_number. */
516 /* XXX(hch): this is racy */
517 if (++serial_number == 0)
519 cmd->serial_number = serial_number;
520 cmd->pid = scsi_pid++;
523 * If SCSI-2 or lower, store the LUN value in cmnd.
525 if (cmd->device->scsi_level <= SCSI_2) {
526 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
527 (cmd->device->lun << 5 & 0xe0);
531 * We will wait MIN_RESET_DELAY clock ticks after the last reset so
532 * we can avoid the drive not being ready.
534 timeout = host->last_reset + MIN_RESET_DELAY;
536 if (host->resetting && time_before(jiffies, timeout)) {
537 int ticks_remaining = timeout - jiffies;
539 * NOTE: This may be executed from within an interrupt
540 * handler! This is bad, but for now, it'll do. The irq
541 * level of the interrupt handler has been masked out by the
542 * platform dependent interrupt handling code already, so the
543 * sti() here will not cause another call to the SCSI host's
544 * interrupt handler (assuming there is one irq-level per
547 while (--ticks_remaining >= 0)
548 mdelay(1 + 999 / HZ);
552 scsi_add_timer(cmd, cmd->timeout_per_command, scsi_times_out);
557 * We will use a queued command if possible, otherwise we will
558 * emulate the queuing and calling of completion function ourselves.
561 cmd->state = SCSI_STATE_QUEUED;
562 cmd->owner = SCSI_OWNER_LOWLEVEL;
565 * Before we queue this command, check if the command
566 * length exceeds what the host adapter can handle.
568 if (CDB_SIZE(cmd) > cmd->device->host->max_cmd_len) {
570 printk("queuecommand : command too long.\n"));
571 cmd->result = (DID_ABORT << 16);
573 spin_lock_irqsave(host->host_lock, flags);
575 spin_unlock_irqrestore(host->host_lock, flags);
579 spin_lock_irqsave(host->host_lock, flags);
580 if (unlikely(test_bit(SHOST_CANCEL, &host->shost_state))) {
581 cmd->result = (DID_NO_CONNECT << 16);
584 rtn = host->hostt->queuecommand(cmd, scsi_done);
586 spin_unlock_irqrestore(host->host_lock, flags);
588 scsi_queue_insert(cmd,
589 (rtn == SCSI_MLQUEUE_DEVICE_BUSY) ?
590 rtn : SCSI_MLQUEUE_HOST_BUSY);
592 printk("queuecommand : request rejected\n"));
596 SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
601 * Function: scsi_init_cmd_from_req
603 * Purpose: Queue a SCSI command
604 * Purpose: Initialize a struct scsi_cmnd from a struct scsi_request
606 * Arguments: cmd - command descriptor.
607 * sreq - Request from the queue.
609 * Lock status: None needed.
613 * Notes: Mainly transfer data from the request structure to the
614 * command structure. The request structure is allocated
615 * using the normal memory allocator, and requests can pile
616 * up to more or less any depth. The command structure represents
617 * a consumable resource, as these are allocated into a pool
618 * when the SCSI subsystem initializes. The preallocation is
619 * required so that in low-memory situations a disk I/O request
620 * won't cause the memory manager to try and write out a page.
621 * The request structure is generally used by ioctls and character
624 void scsi_init_cmd_from_req(struct scsi_cmnd *cmd, struct scsi_request *sreq)
626 sreq->sr_command = cmd;
628 cmd->owner = SCSI_OWNER_MIDLEVEL;
629 cmd->cmd_len = sreq->sr_cmd_len;
630 cmd->use_sg = sreq->sr_use_sg;
632 cmd->request = sreq->sr_request;
633 memcpy(cmd->data_cmnd, sreq->sr_cmnd, sizeof(cmd->data_cmnd));
634 cmd->serial_number = 0;
635 cmd->serial_number_at_timeout = 0;
636 cmd->bufflen = sreq->sr_bufflen;
637 cmd->buffer = sreq->sr_buffer;
639 cmd->allowed = sreq->sr_allowed;
640 cmd->done = sreq->sr_done;
641 cmd->timeout_per_command = sreq->sr_timeout_per_command;
642 cmd->sc_data_direction = sreq->sr_data_direction;
643 cmd->sglist_len = sreq->sr_sglist_len;
644 cmd->underflow = sreq->sr_underflow;
645 cmd->sc_request = sreq;
646 memcpy(cmd->cmnd, sreq->sr_cmnd, sizeof(sreq->sr_cmnd));
649 * Zero the sense buffer. Some host adapters automatically request
650 * sense on error. 0 is not a valid sense code.
652 memset(cmd->sense_buffer, 0, sizeof(sreq->sr_sense_buffer));
653 cmd->request_buffer = sreq->sr_buffer;
654 cmd->request_bufflen = sreq->sr_bufflen;
655 cmd->old_use_sg = cmd->use_sg;
656 if (cmd->cmd_len == 0)
657 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
658 cmd->old_cmd_len = cmd->cmd_len;
659 cmd->sc_old_data_direction = cmd->sc_data_direction;
660 cmd->old_underflow = cmd->underflow;
663 * Start the timer ticking.
665 cmd->internal_timeout = NORMAL_TIMEOUT;
666 cmd->abort_reason = 0;
669 SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_init_cmd_from_req()\n"));
673 * Per-CPU I/O completion queue.
675 static DEFINE_PER_CPU(struct list_head, scsi_done_q);
678 * scsi_done - Enqueue the finished SCSI command into the done queue.
679 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
680 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
682 * This function is the mid-level's (SCSI Core) interrupt routine, which
683 * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues
684 * the command to the done queue for further processing.
686 * This is the producer of the done queue who enqueues at the tail.
688 * This function is interrupt context safe.
690 void scsi_done(struct scsi_cmnd *cmd)
695 * We don't have to worry about this one timing out any more.
696 * If we are unable to remove the timer, then the command
697 * has already timed out. In which case, we have no choice but to
698 * let the timeout function run, as we have no idea where in fact
699 * that function could really be. It might be on another processor,
702 if (!scsi_delete_timer(cmd))
706 * Set the serial numbers back to zero
708 cmd->serial_number = 0;
709 cmd->serial_number_at_timeout = 0;
710 cmd->state = SCSI_STATE_BHQUEUE;
711 cmd->owner = SCSI_OWNER_BH_HANDLER;
714 * Next, enqueue the command into the done queue.
715 * It is a per-CPU queue, so we just disable local interrupts
716 * and need no spinlock.
718 local_irq_save(flags);
719 list_add_tail(&cmd->eh_entry, &__get_cpu_var(scsi_done_q));
720 raise_softirq_irqoff(SCSI_SOFTIRQ);
721 local_irq_restore(flags);
725 * scsi_softirq - Perform post-interrupt processing of finished SCSI commands.
727 * This is the consumer of the done queue.
729 * This is called with all interrupts enabled. This should reduce
730 * interrupt latency, stack depth, and reentrancy of the low-level
733 static void scsi_softirq(struct softirq_action *h)
739 list_splice_init(&__get_cpu_var(scsi_done_q), &local_q);
742 while (!list_empty(&local_q)) {
743 struct scsi_cmnd *cmd = list_entry(local_q.next,
744 struct scsi_cmnd, eh_entry);
745 list_del_init(&cmd->eh_entry);
747 disposition = scsi_decide_disposition(cmd);
748 scsi_log_completion(cmd, disposition);
749 switch (disposition) {
751 scsi_finish_command(cmd);
754 scsi_retry_command(cmd);
757 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
760 if (!scsi_eh_scmd_add(cmd, 0))
761 scsi_finish_command(cmd);
767 * Function: scsi_retry_command
769 * Purpose: Send a command back to the low level to be retried.
771 * Notes: This command is always executed in the context of the
772 * bottom half handler, or the error handler thread. Low
773 * level drivers should not become re-entrant as a result of
776 int scsi_retry_command(struct scsi_cmnd *cmd)
779 * Restore the SCSI command state.
781 scsi_setup_cmd_retry(cmd);
784 * Zero the sense information from the last time we tried
787 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
789 return scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
793 * Function: scsi_finish_command
795 * Purpose: Pass command off to upper layer for finishing of I/O
796 * request, waking processes that are waiting on results,
799 void scsi_finish_command(struct scsi_cmnd *cmd)
801 struct scsi_device *sdev = cmd->device;
802 struct Scsi_Host *shost = sdev->host;
803 struct scsi_request *sreq;
805 scsi_device_unbusy(sdev);
808 * Clear the flags which say that the device/host is no longer
809 * capable of accepting new commands. These are set in scsi_queue.c
810 * for both the queue full condition on a device, and for a
811 * host full condition on the host.
813 * XXX(hch): What about locking?
815 shost->host_blocked = 0;
816 sdev->device_blocked = 0;
819 * If we have valid sense information, then some kind of recovery
820 * must have taken place. Make a note of this.
822 if (SCSI_SENSE_VALID(cmd))
823 cmd->result |= (DRIVER_SENSE << 24);
825 SCSI_LOG_MLCOMPLETE(4, printk("Notifying upper driver of completion "
826 "for device %d %x\n", sdev->id, cmd->result));
828 cmd->owner = SCSI_OWNER_HIGHLEVEL;
829 cmd->state = SCSI_STATE_FINISHED;
832 * We can get here with use_sg=0, causing a panic in the upper level
834 cmd->use_sg = cmd->old_use_sg;
837 * If there is an associated request structure, copy the data over
838 * before we call the completion function.
840 sreq = cmd->sc_request;
842 sreq->sr_result = sreq->sr_command->result;
843 if (sreq->sr_result) {
844 memcpy(sreq->sr_sense_buffer,
845 sreq->sr_command->sense_buffer,
846 sizeof(sreq->sr_sense_buffer));
852 EXPORT_SYMBOL(scsi_finish_command);
855 * Function: scsi_adjust_queue_depth()
857 * Purpose: Allow low level drivers to tell us to change the queue depth
858 * on a specific SCSI device
860 * Arguments: sdev - SCSI Device in question
861 * tagged - Do we use tagged queueing (non-0) or do we treat
862 * this device as an untagged device (0)
863 * tags - Number of tags allowed if tagged queueing enabled,
864 * or number of commands the low level driver can
865 * queue up in non-tagged mode (as per cmd_per_lun).
869 * Lock Status: None held on entry
871 * Notes: Low level drivers may call this at any time and we will do
872 * the right thing depending on whether or not the device is
873 * currently active and whether or not it even has the
874 * command blocks built yet.
876 * XXX(hch): What exactly is device_request_lock trying to protect?
878 void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
880 static spinlock_t device_request_lock = SPIN_LOCK_UNLOCKED;
884 * refuse to set tagged depth to an unworkable size
889 * Limit max queue depth on a single lun to 256 for now. Remember,
890 * we allocate a struct scsi_command for each of these and keep it
891 * around forever. Too deep of a depth just wastes memory.
896 spin_lock_irqsave(&device_request_lock, flags);
897 sdev->queue_depth = tags;
899 case MSG_ORDERED_TAG:
900 sdev->ordered_tags = 1;
901 sdev->simple_tags = 1;
904 sdev->ordered_tags = 0;
905 sdev->simple_tags = 1;
908 printk(KERN_WARNING "(scsi%d:%d:%d:%d) "
909 "scsi_adjust_queue_depth, bad queue type, "
910 "disabled\n", sdev->host->host_no,
911 sdev->channel, sdev->id, sdev->lun);
913 sdev->ordered_tags = sdev->simple_tags = 0;
914 sdev->queue_depth = tags;
917 spin_unlock_irqrestore(&device_request_lock, flags);
921 * Function: scsi_track_queue_full()
923 * Purpose: This function will track successive QUEUE_FULL events on a
924 * specific SCSI device to determine if and when there is a
925 * need to adjust the queue depth on the device.
927 * Arguments: sdev - SCSI Device in question
928 * depth - Current number of outstanding SCSI commands on
929 * this device, not counting the one returned as
932 * Returns: 0 - No change needed
933 * >0 - Adjust queue depth to this new depth
934 * -1 - Drop back to untagged operation using host->cmd_per_lun
935 * as the untagged command depth
937 * Lock Status: None held on entry
939 * Notes: Low level drivers may call this at any time and we will do
940 * "The Right Thing." We are interrupt context safe.
942 int scsi_track_queue_full(struct scsi_device *sdev, int depth)
944 if ((jiffies >> 4) == sdev->last_queue_full_time)
947 sdev->last_queue_full_time = (jiffies >> 4);
948 if (sdev->last_queue_full_depth != depth) {
949 sdev->last_queue_full_count = 1;
950 sdev->last_queue_full_depth = depth;
952 sdev->last_queue_full_count++;
955 if (sdev->last_queue_full_count <= 10)
957 if (sdev->last_queue_full_depth < 8) {
958 /* Drop back to untagged */
959 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
963 if (sdev->ordered_tags)
964 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
966 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
971 * scsi_device_get - get an addition reference to a scsi_device
972 * @sdev: device to get a reference to
974 * Gets a reference to the scsi_device and increments the use count
975 * of the underlying LLDD module. You must hold host_lock of the
976 * parent Scsi_Host or already have a reference when calling this.
978 int scsi_device_get(struct scsi_device *sdev)
982 if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
984 if (!get_device(&sdev->sdev_gendev))
986 if (!try_module_get(sdev->host->hostt->module)) {
987 put_device(&sdev->sdev_gendev);
992 EXPORT_SYMBOL(scsi_device_get);
995 * scsi_device_put - release a reference to a scsi_device
996 * @sdev: device to release a reference on.
998 * Release a reference to the scsi_device and decrements the use count
999 * of the underlying LLDD module. The device is freed once the last
1002 void scsi_device_put(struct scsi_device *sdev)
1004 module_put(sdev->host->hostt->module);
1005 put_device(&sdev->sdev_gendev);
1007 EXPORT_SYMBOL(scsi_device_put);
1009 /* helper for shost_for_each_device, thus not documented */
1010 struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
1011 struct scsi_device *prev)
1013 struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
1014 struct scsi_device *next = NULL;
1015 unsigned long flags;
1017 spin_lock_irqsave(shost->host_lock, flags);
1018 while (list->next != &shost->__devices) {
1019 next = list_entry(list->next, struct scsi_device, siblings);
1020 /* skip devices that we can't get a reference to */
1021 if (!scsi_device_get(next))
1025 spin_unlock_irqrestore(shost->host_lock, flags);
1028 scsi_device_put(prev);
1031 EXPORT_SYMBOL(__scsi_iterate_devices);
1034 * scsi_device_lookup - find a device given the host (UNLOCKED)
1035 * @shost: SCSI host pointer
1036 * @channel: SCSI channel (zero if only one channel)
1037 * @pun: SCSI target number (physical unit number)
1038 * @lun: SCSI Logical Unit Number
1040 * Looks up the scsi_device with the specified @channel, @id, @lun for a
1041 * give host. The returned scsi_device does not have an additional reference.
1042 * You must hold the host's host_lock over this call and any access to the
1043 * returned scsi_device.
1045 * Note: The only reason why drivers would want to use this is because
1046 * they're need to access the device list in irq context. Otherwise you
1047 * really want to use scsi_device_lookup instead.
1049 struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
1050 uint channel, uint id, uint lun)
1052 struct scsi_device *sdev;
1054 list_for_each_entry(sdev, &shost->__devices, siblings) {
1055 if (sdev->channel == channel && sdev->id == id &&
1062 EXPORT_SYMBOL(__scsi_device_lookup);
1065 * scsi_device_lookup - find a device given the host
1066 * @shost: SCSI host pointer
1067 * @channel: SCSI channel (zero if only one channel)
1068 * @id: SCSI target number (physical unit number)
1069 * @lun: SCSI Logical Unit Number
1071 * Looks up the scsi_device with the specified @channel, @id, @lun for a
1072 * give host. The returned scsi_device has an additional reference that
1073 * needs to be release with scsi_host_put once you're done with it.
1075 struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
1076 uint channel, uint id, uint lun)
1078 struct scsi_device *sdev;
1079 unsigned long flags;
1081 spin_lock_irqsave(shost->host_lock, flags);
1082 sdev = __scsi_device_lookup(shost, channel, id, lun);
1083 if (sdev && scsi_device_get(sdev))
1085 spin_unlock_irqrestore(shost->host_lock, flags);
1089 EXPORT_SYMBOL(scsi_device_lookup);
1092 * scsi_device_cancel - cancel outstanding IO to this device
1093 * @sdev: pointer to struct scsi_device
1094 * @data: pointer to cancel value.
1097 int scsi_device_cancel(struct scsi_device *sdev, int recovery)
1099 struct scsi_cmnd *scmd;
1100 LIST_HEAD(active_list);
1101 struct list_head *lh, *lh_sf;
1102 unsigned long flags;
1104 scsi_device_set_state(sdev, SDEV_CANCEL);
1106 spin_lock_irqsave(&sdev->list_lock, flags);
1107 list_for_each_entry(scmd, &sdev->cmd_list, list) {
1108 if (scmd->request && scmd->request->rq_status != RQ_INACTIVE) {
1110 * If we are unable to remove the timer, it means
1111 * that the command has already timed out or
1114 if (!scsi_delete_timer(scmd))
1116 list_add_tail(&scmd->eh_entry, &active_list);
1119 spin_unlock_irqrestore(&sdev->list_lock, flags);
1121 if (!list_empty(&active_list)) {
1122 list_for_each_safe(lh, lh_sf, &active_list) {
1123 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1126 scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD);
1128 scmd->result = (DID_ABORT << 16);
1129 scsi_finish_command(scmd);
1137 #ifdef CONFIG_HOTPLUG_CPU
1138 static int scsi_cpu_notify(struct notifier_block *self,
1139 unsigned long action, void *hcpu)
1141 int cpu = (unsigned long)hcpu;
1145 /* Drain scsi_done_q. */
1146 local_irq_disable();
1147 list_splice_init(&per_cpu(scsi_done_q, cpu),
1148 &__get_cpu_var(scsi_done_q));
1149 raise_softirq_irqoff(SCSI_SOFTIRQ);
1158 static struct notifier_block __devinitdata scsi_cpu_nb = {
1159 .notifier_call = scsi_cpu_notify,
1162 #define register_scsi_cpu() register_cpu_notifier(&scsi_cpu_nb)
1163 #define unregister_scsi_cpu() unregister_cpu_notifier(&scsi_cpu_nb)
1165 #define register_scsi_cpu()
1166 #define unregister_scsi_cpu()
1167 #endif /* CONFIG_HOTPLUG_CPU */
1169 MODULE_DESCRIPTION("SCSI core");
1170 MODULE_LICENSE("GPL");
1172 module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
1173 MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
1175 static int __init init_scsi(void)
1179 error = scsi_init_queue();
1182 error = scsi_init_procfs();
1185 error = scsi_init_devinfo();
1187 goto cleanup_procfs;
1188 error = scsi_init_hosts();
1190 goto cleanup_devlist;
1191 error = scsi_init_sysctl();
1194 error = scsi_sysfs_register();
1196 goto cleanup_sysctl;
1198 for (i = 0; i < NR_CPUS; i++)
1199 INIT_LIST_HEAD(&per_cpu(scsi_done_q, i));
1201 devfs_mk_dir("scsi");
1202 open_softirq(SCSI_SOFTIRQ, scsi_softirq, NULL);
1203 register_scsi_cpu();
1204 printk(KERN_NOTICE "SCSI subsystem initialized\n");
1212 scsi_exit_devinfo();
1217 printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
1222 static void __exit exit_scsi(void)
1224 scsi_sysfs_unregister();
1227 scsi_exit_devinfo();
1228 devfs_remove("scsi");
1231 unregister_scsi_cpu();
1234 subsys_initcall(init_scsi);
1235 module_exit(exit_scsi);