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.h>
60 #include <scsi/scsi_cmnd.h>
61 #include <scsi/scsi_dbg.h>
62 #include <scsi/scsi_device.h>
63 #include <scsi/scsi_eh.h>
64 #include <scsi/scsi_host.h>
65 #include <scsi/scsi_tcq.h>
66 #include <scsi/scsi_request.h>
68 #include "scsi_priv.h"
69 #include "scsi_logging.h"
71 static void scsi_done(struct scsi_cmnd *cmd);
72 static int scsi_retry_command(struct scsi_cmnd *cmd);
75 * Definitions and constants.
78 #define MIN_RESET_DELAY (2*HZ)
80 /* Do not call reset on error if we just did a reset within 15 sec. */
81 #define MIN_RESET_PERIOD (15*HZ)
84 * Macro to determine the size of SCSI command. This macro takes vendor
85 * unique commands into account. SCSI commands in groups 6 and 7 are
86 * vendor unique and we will depend upon the command length being
87 * supplied correctly in cmd_len.
89 #define CDB_SIZE(cmd) (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \
90 COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len)
93 * Note - the initial logging level can be set here to log events at boot time.
94 * After the system is up, you may enable logging via the /proc interface.
96 unsigned int scsi_logging_level;
97 #if defined(CONFIG_SCSI_LOGGING)
98 EXPORT_SYMBOL(scsi_logging_level);
101 const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] = {
118 EXPORT_SYMBOL(scsi_device_types);
121 * Function: scsi_allocate_request
123 * Purpose: Allocate a request descriptor.
125 * Arguments: device - device for which we want a request
126 * gfp_mask - allocation flags passed to kmalloc
128 * Lock status: No locks assumed to be held. This function is SMP-safe.
130 * Returns: Pointer to request block.
132 struct scsi_request *scsi_allocate_request(struct scsi_device *sdev,
135 const int offset = ALIGN(sizeof(struct scsi_request), 4);
136 const int size = offset + sizeof(struct request);
137 struct scsi_request *sreq;
139 sreq = kmalloc(size, gfp_mask);
140 if (likely(sreq != NULL)) {
141 memset(sreq, 0, size);
142 sreq->sr_request = (struct request *)(((char *)sreq) + offset);
143 sreq->sr_device = sdev;
144 sreq->sr_host = sdev->host;
145 sreq->sr_magic = SCSI_REQ_MAGIC;
146 sreq->sr_data_direction = DMA_BIDIRECTIONAL;
151 EXPORT_SYMBOL(scsi_allocate_request);
153 void __scsi_release_request(struct scsi_request *sreq)
155 struct request *req = sreq->sr_request;
157 /* unlikely because the tag was usually ended earlier by the
158 * mid-layer. However, for layering reasons ULD's don't end
159 * the tag of commands they generate. */
160 if (unlikely(blk_rq_tagged(req))) {
162 struct request_queue *q = req->q;
164 spin_lock_irqsave(q->queue_lock, flags);
165 blk_queue_end_tag(q, req);
166 spin_unlock_irqrestore(q->queue_lock, flags);
170 if (likely(sreq->sr_command != NULL)) {
171 struct scsi_cmnd *cmd = sreq->sr_command;
173 sreq->sr_command = NULL;
174 scsi_next_command(cmd);
179 * Function: scsi_release_request
181 * Purpose: Release a request descriptor.
183 * Arguments: sreq - request to release
185 * Lock status: No locks assumed to be held. This function is SMP-safe.
187 void scsi_release_request(struct scsi_request *sreq)
189 __scsi_release_request(sreq);
192 EXPORT_SYMBOL(scsi_release_request);
194 struct scsi_host_cmd_pool {
198 unsigned int slab_flags;
199 unsigned int gfp_mask;
202 static struct scsi_host_cmd_pool scsi_cmd_pool = {
203 .name = "scsi_cmd_cache",
204 .slab_flags = SLAB_HWCACHE_ALIGN,
207 static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
208 .name = "scsi_cmd_cache(DMA)",
209 .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
210 .gfp_mask = __GFP_DMA,
213 static DECLARE_MUTEX(host_cmd_pool_mutex);
215 static struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost,
218 struct scsi_cmnd *cmd;
220 cmd = kmem_cache_alloc(shost->cmd_pool->slab,
221 gfp_mask | shost->cmd_pool->gfp_mask);
223 if (unlikely(!cmd)) {
226 spin_lock_irqsave(&shost->free_list_lock, flags);
227 if (likely(!list_empty(&shost->free_list))) {
228 cmd = list_entry(shost->free_list.next,
229 struct scsi_cmnd, list);
230 list_del_init(&cmd->list);
232 spin_unlock_irqrestore(&shost->free_list_lock, flags);
239 * Function: scsi_get_command()
241 * Purpose: Allocate and setup a scsi command block
243 * Arguments: dev - parent scsi device
244 * gfp_mask- allocator flags
246 * Returns: The allocated scsi command structure.
248 struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, int gfp_mask)
250 struct scsi_cmnd *cmd;
252 /* Bail if we can't get a reference to the device */
253 if (!get_device(&dev->sdev_gendev))
256 cmd = __scsi_get_command(dev->host, gfp_mask);
258 if (likely(cmd != NULL)) {
261 memset(cmd, 0, sizeof(*cmd));
263 init_timer(&cmd->eh_timeout);
264 INIT_LIST_HEAD(&cmd->list);
265 spin_lock_irqsave(&dev->list_lock, flags);
266 list_add_tail(&cmd->list, &dev->cmd_list);
267 spin_unlock_irqrestore(&dev->list_lock, flags);
269 put_device(&dev->sdev_gendev);
271 cmd->jiffies_at_alloc = jiffies;
274 EXPORT_SYMBOL(scsi_get_command);
277 * Function: scsi_put_command()
279 * Purpose: Free a scsi command block
281 * Arguments: cmd - command block to free
285 * Notes: The command must not belong to any lists.
287 void scsi_put_command(struct scsi_cmnd *cmd)
289 struct scsi_device *sdev = cmd->device;
290 struct Scsi_Host *shost = sdev->host;
293 /* serious error if the command hasn't come from a device list */
294 spin_lock_irqsave(&cmd->device->list_lock, flags);
295 BUG_ON(list_empty(&cmd->list));
296 list_del_init(&cmd->list);
297 spin_unlock(&cmd->device->list_lock);
298 /* changing locks here, don't need to restore the irq state */
299 spin_lock(&shost->free_list_lock);
300 if (unlikely(list_empty(&shost->free_list))) {
301 list_add(&cmd->list, &shost->free_list);
304 spin_unlock_irqrestore(&shost->free_list_lock, flags);
306 if (likely(cmd != NULL))
307 kmem_cache_free(shost->cmd_pool->slab, cmd);
309 put_device(&sdev->sdev_gendev);
311 EXPORT_SYMBOL(scsi_put_command);
314 * Function: scsi_setup_command_freelist()
316 * Purpose: Setup the command freelist for a scsi host.
318 * Arguments: shost - host to allocate the freelist for.
322 int scsi_setup_command_freelist(struct Scsi_Host *shost)
324 struct scsi_host_cmd_pool *pool;
325 struct scsi_cmnd *cmd;
327 spin_lock_init(&shost->free_list_lock);
328 INIT_LIST_HEAD(&shost->free_list);
331 * Select a command slab for this host and create it if not
334 down(&host_cmd_pool_mutex);
335 pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool);
337 pool->slab = kmem_cache_create(pool->name,
338 sizeof(struct scsi_cmnd), 0,
339 pool->slab_flags, NULL, NULL);
345 shost->cmd_pool = pool;
346 up(&host_cmd_pool_mutex);
349 * Get one backup command for this host.
351 cmd = kmem_cache_alloc(shost->cmd_pool->slab,
352 GFP_KERNEL | shost->cmd_pool->gfp_mask);
355 list_add(&cmd->list, &shost->free_list);
360 kmem_cache_destroy(pool->slab);
363 up(&host_cmd_pool_mutex);
369 * Function: scsi_destroy_command_freelist()
371 * Purpose: Release the command freelist for a scsi host.
373 * Arguments: shost - host that's freelist is going to be destroyed
375 void scsi_destroy_command_freelist(struct Scsi_Host *shost)
377 while (!list_empty(&shost->free_list)) {
378 struct scsi_cmnd *cmd;
380 cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
381 list_del_init(&cmd->list);
382 kmem_cache_free(shost->cmd_pool->slab, cmd);
385 down(&host_cmd_pool_mutex);
386 if (!--shost->cmd_pool->users)
387 kmem_cache_destroy(shost->cmd_pool->slab);
388 up(&host_cmd_pool_mutex);
391 #ifdef CONFIG_SCSI_LOGGING
392 void scsi_log_send(struct scsi_cmnd *cmd)
395 struct scsi_device *sdev;
398 * If ML QUEUE log level is greater than or equal to:
400 * 1: nothing (match completion)
402 * 2: log opcode + command of all commands
404 * 3: same as 2 plus dump cmd address
406 * 4: same as 3 plus dump extra junk
408 if (unlikely(scsi_logging_level)) {
409 level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
410 SCSI_LOG_MLQUEUE_BITS);
413 printk(KERN_INFO "scsi <%d:%d:%d:%d> send ",
414 sdev->host->host_no, sdev->channel, sdev->id,
417 printk("0x%p ", cmd);
419 * spaces to match disposition and cmd->result
420 * output in scsi_log_completion.
423 scsi_print_command(cmd);
425 printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
426 " done = 0x%p, queuecommand 0x%p\n",
427 cmd->buffer, cmd->bufflen,
429 sdev->host->hostt->queuecommand);
436 void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
439 struct scsi_device *sdev;
442 * If ML COMPLETE log level is greater than or equal to:
444 * 1: log disposition, result, opcode + command, and conditionally
445 * sense data for failures or non SUCCESS dispositions.
447 * 2: same as 1 but for all command completions.
449 * 3: same as 2 plus dump cmd address
451 * 4: same as 3 plus dump extra junk
453 if (unlikely(scsi_logging_level)) {
454 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
455 SCSI_LOG_MLCOMPLETE_BITS);
456 if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
459 printk(KERN_INFO "scsi <%d:%d:%d:%d> done ",
460 sdev->host->host_no, sdev->channel, sdev->id,
463 printk("0x%p ", cmd);
465 * Dump truncated values, so we usually fit within
468 switch (disposition) {
483 * If called via scsi_times_out.
490 printk(" %8x ", cmd->result);
491 scsi_print_command(cmd);
492 if (status_byte(cmd->result) & CHECK_CONDITION) {
494 * XXX The scsi_print_sense formatting/prefix
495 * doesn't match this function.
497 scsi_print_sense("", cmd);
500 printk(KERN_INFO "scsi host busy %d failed %d\n",
501 sdev->host->host_busy,
502 sdev->host->host_failed);
510 * Assign a serial number and pid to the request for error recovery
511 * and debugging purposes. Protected by the Host_Lock of host.
513 static inline void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
515 cmd->serial_number = host->cmd_serial_number++;
516 if (cmd->serial_number == 0)
517 cmd->serial_number = host->cmd_serial_number++;
519 cmd->pid = host->cmd_pid++;
521 cmd->pid = host->cmd_pid++;
525 * Function: scsi_dispatch_command
527 * Purpose: Dispatch a command to the low-level driver.
529 * Arguments: cmd - command block we are dispatching.
533 int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
535 struct Scsi_Host *host = cmd->device->host;
536 unsigned long flags = 0;
537 unsigned long timeout;
540 /* check if the device is still usable */
541 if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
542 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
543 * returns an immediate error upwards, and signals
544 * that the device is no longer present */
545 cmd->result = DID_NO_CONNECT << 16;
546 atomic_inc(&cmd->device->iorequest_cnt);
548 /* return 0 (because the command has been processed) */
552 /* Check to see if the scsi lld put this device into state SDEV_BLOCK. */
553 if (unlikely(cmd->device->sdev_state == SDEV_BLOCK)) {
555 * in SDEV_BLOCK, the command is just put back on the device
556 * queue. The suspend state has already blocked the queue so
557 * future requests should not occur until the device
558 * transitions out of the suspend state.
560 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
562 SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n"));
565 * NOTE: rtn is still zero here because we don't need the
566 * queue to be plugged on return (it's already stopped)
572 * If SCSI-2 or lower, store the LUN value in cmnd.
574 if (cmd->device->scsi_level <= SCSI_2) {
575 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
576 (cmd->device->lun << 5 & 0xe0);
580 * We will wait MIN_RESET_DELAY clock ticks after the last reset so
581 * we can avoid the drive not being ready.
583 timeout = host->last_reset + MIN_RESET_DELAY;
585 if (host->resetting && time_before(jiffies, timeout)) {
586 int ticks_remaining = timeout - jiffies;
588 * NOTE: This may be executed from within an interrupt
589 * handler! This is bad, but for now, it'll do. The irq
590 * level of the interrupt handler has been masked out by the
591 * platform dependent interrupt handling code already, so the
592 * sti() here will not cause another call to the SCSI host's
593 * interrupt handler (assuming there is one irq-level per
596 while (--ticks_remaining >= 0)
597 mdelay(1 + 999 / HZ);
602 * AK: unlikely race here: for some reason the timer could
603 * expire before the serial number is set up below.
605 scsi_add_timer(cmd, cmd->timeout_per_command, scsi_times_out);
610 * We will use a queued command if possible, otherwise we will
611 * emulate the queuing and calling of completion function ourselves.
613 atomic_inc(&cmd->device->iorequest_cnt);
616 * Before we queue this command, check if the command
617 * length exceeds what the host adapter can handle.
619 if (CDB_SIZE(cmd) > cmd->device->host->max_cmd_len) {
621 printk("queuecommand : command too long.\n"));
622 cmd->result = (DID_ABORT << 16);
628 spin_lock_irqsave(host->host_lock, flags);
629 scsi_cmd_get_serial(host, cmd);
631 if (unlikely(host->shost_state == SHOST_DEL)) {
632 cmd->result = (DID_NO_CONNECT << 16);
635 rtn = host->hostt->queuecommand(cmd, scsi_done);
637 spin_unlock_irqrestore(host->host_lock, flags);
639 if (scsi_delete_timer(cmd)) {
640 atomic_inc(&cmd->device->iodone_cnt);
641 scsi_queue_insert(cmd,
642 (rtn == SCSI_MLQUEUE_DEVICE_BUSY) ?
643 rtn : SCSI_MLQUEUE_HOST_BUSY);
646 printk("queuecommand : request rejected\n"));
650 SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
655 * Function: scsi_init_cmd_from_req
657 * Purpose: Queue a SCSI command
658 * Purpose: Initialize a struct scsi_cmnd from a struct scsi_request
660 * Arguments: cmd - command descriptor.
661 * sreq - Request from the queue.
663 * Lock status: None needed.
667 * Notes: Mainly transfer data from the request structure to the
668 * command structure. The request structure is allocated
669 * using the normal memory allocator, and requests can pile
670 * up to more or less any depth. The command structure represents
671 * a consumable resource, as these are allocated into a pool
672 * when the SCSI subsystem initializes. The preallocation is
673 * required so that in low-memory situations a disk I/O request
674 * won't cause the memory manager to try and write out a page.
675 * The request structure is generally used by ioctls and character
678 void scsi_init_cmd_from_req(struct scsi_cmnd *cmd, struct scsi_request *sreq)
680 sreq->sr_command = cmd;
682 cmd->cmd_len = sreq->sr_cmd_len;
683 cmd->use_sg = sreq->sr_use_sg;
685 cmd->request = sreq->sr_request;
686 memcpy(cmd->data_cmnd, sreq->sr_cmnd, sizeof(cmd->data_cmnd));
687 cmd->serial_number = 0;
688 cmd->bufflen = sreq->sr_bufflen;
689 cmd->buffer = sreq->sr_buffer;
691 cmd->allowed = sreq->sr_allowed;
692 cmd->done = sreq->sr_done;
693 cmd->timeout_per_command = sreq->sr_timeout_per_command;
694 cmd->sc_data_direction = sreq->sr_data_direction;
695 cmd->sglist_len = sreq->sr_sglist_len;
696 cmd->underflow = sreq->sr_underflow;
697 cmd->sc_request = sreq;
698 memcpy(cmd->cmnd, sreq->sr_cmnd, sizeof(sreq->sr_cmnd));
701 * Zero the sense buffer. Some host adapters automatically request
702 * sense on error. 0 is not a valid sense code.
704 memset(cmd->sense_buffer, 0, sizeof(sreq->sr_sense_buffer));
705 cmd->request_buffer = sreq->sr_buffer;
706 cmd->request_bufflen = sreq->sr_bufflen;
707 cmd->old_use_sg = cmd->use_sg;
708 if (cmd->cmd_len == 0)
709 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
710 cmd->old_cmd_len = cmd->cmd_len;
711 cmd->sc_old_data_direction = cmd->sc_data_direction;
712 cmd->old_underflow = cmd->underflow;
715 * Start the timer ticking.
719 SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_init_cmd_from_req()\n"));
723 * Per-CPU I/O completion queue.
725 static DEFINE_PER_CPU(struct list_head, scsi_done_q);
728 * scsi_done - Enqueue the finished SCSI command into the done queue.
729 * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
730 * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
732 * This function is the mid-level's (SCSI Core) interrupt routine, which
733 * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues
734 * the command to the done queue for further processing.
736 * This is the producer of the done queue who enqueues at the tail.
738 * This function is interrupt context safe.
740 static void scsi_done(struct scsi_cmnd *cmd)
743 * We don't have to worry about this one timing out any more.
744 * If we are unable to remove the timer, then the command
745 * has already timed out. In which case, we have no choice but to
746 * let the timeout function run, as we have no idea where in fact
747 * that function could really be. It might be on another processor,
750 if (!scsi_delete_timer(cmd))
755 /* Private entry to scsi_done() to complete a command when the timer
756 * isn't running --- used by scsi_times_out */
757 void __scsi_done(struct scsi_cmnd *cmd)
762 * Set the serial numbers back to zero
764 cmd->serial_number = 0;
766 atomic_inc(&cmd->device->iodone_cnt);
768 atomic_inc(&cmd->device->ioerr_cnt);
771 * Next, enqueue the command into the done queue.
772 * It is a per-CPU queue, so we just disable local interrupts
773 * and need no spinlock.
775 local_irq_save(flags);
776 list_add_tail(&cmd->eh_entry, &__get_cpu_var(scsi_done_q));
777 raise_softirq_irqoff(SCSI_SOFTIRQ);
778 local_irq_restore(flags);
782 * scsi_softirq - Perform post-interrupt processing of finished SCSI commands.
784 * This is the consumer of the done queue.
786 * This is called with all interrupts enabled. This should reduce
787 * interrupt latency, stack depth, and reentrancy of the low-level
790 static void scsi_softirq(struct softirq_action *h)
796 list_splice_init(&__get_cpu_var(scsi_done_q), &local_q);
799 while (!list_empty(&local_q)) {
800 struct scsi_cmnd *cmd = list_entry(local_q.next,
801 struct scsi_cmnd, eh_entry);
802 /* The longest time any command should be outstanding is the
803 * per command timeout multiplied by the number of retries.
805 * For a typical command, this is 2.5 minutes */
806 unsigned long wait_for
807 = cmd->allowed * cmd->timeout_per_command;
808 list_del_init(&cmd->eh_entry);
810 disposition = scsi_decide_disposition(cmd);
811 if (disposition != SUCCESS &&
812 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
813 dev_printk(KERN_ERR, &cmd->device->sdev_gendev,
814 "timing out command, waited %lus\n",
816 disposition = SUCCESS;
819 scsi_log_completion(cmd, disposition);
820 switch (disposition) {
822 scsi_finish_command(cmd);
825 scsi_retry_command(cmd);
828 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
831 if (!scsi_eh_scmd_add(cmd, 0))
832 scsi_finish_command(cmd);
838 * Function: scsi_retry_command
840 * Purpose: Send a command back to the low level to be retried.
842 * Notes: This command is always executed in the context of the
843 * bottom half handler, or the error handler thread. Low
844 * level drivers should not become re-entrant as a result of
847 static int scsi_retry_command(struct scsi_cmnd *cmd)
850 * Restore the SCSI command state.
852 scsi_setup_cmd_retry(cmd);
855 * Zero the sense information from the last time we tried
858 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
860 return scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
864 * Function: scsi_finish_command
866 * Purpose: Pass command off to upper layer for finishing of I/O
867 * request, waking processes that are waiting on results,
870 void scsi_finish_command(struct scsi_cmnd *cmd)
872 struct scsi_device *sdev = cmd->device;
873 struct Scsi_Host *shost = sdev->host;
874 struct scsi_request *sreq;
876 scsi_device_unbusy(sdev);
879 * Clear the flags which say that the device/host is no longer
880 * capable of accepting new commands. These are set in scsi_queue.c
881 * for both the queue full condition on a device, and for a
882 * host full condition on the host.
884 * XXX(hch): What about locking?
886 shost->host_blocked = 0;
887 sdev->device_blocked = 0;
890 * If we have valid sense information, then some kind of recovery
891 * must have taken place. Make a note of this.
893 if (SCSI_SENSE_VALID(cmd))
894 cmd->result |= (DRIVER_SENSE << 24);
896 SCSI_LOG_MLCOMPLETE(4, printk("Notifying upper driver of completion "
897 "for device %d %x\n", sdev->id, cmd->result));
900 * We can get here with use_sg=0, causing a panic in the upper level
902 cmd->use_sg = cmd->old_use_sg;
905 * If there is an associated request structure, copy the data over
906 * before we call the completion function.
908 sreq = cmd->sc_request;
910 sreq->sr_result = sreq->sr_command->result;
911 if (sreq->sr_result) {
912 memcpy(sreq->sr_sense_buffer,
913 sreq->sr_command->sense_buffer,
914 sizeof(sreq->sr_sense_buffer));
920 EXPORT_SYMBOL(scsi_finish_command);
923 * Function: scsi_adjust_queue_depth()
925 * Purpose: Allow low level drivers to tell us to change the queue depth
926 * on a specific SCSI device
928 * Arguments: sdev - SCSI Device in question
929 * tagged - Do we use tagged queueing (non-0) or do we treat
930 * this device as an untagged device (0)
931 * tags - Number of tags allowed if tagged queueing enabled,
932 * or number of commands the low level driver can
933 * queue up in non-tagged mode (as per cmd_per_lun).
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 depending on whether or not the device is
941 * currently active and whether or not it even has the
942 * command blocks built yet.
944 void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
949 * refuse to set tagged depth to an unworkable size
954 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
956 /* Check to see if the queue is managed by the block layer
957 * if it is, and we fail to adjust the depth, exit */
958 if (blk_queue_tagged(sdev->request_queue) &&
959 blk_queue_resize_tags(sdev->request_queue, tags) != 0)
962 sdev->queue_depth = tags;
964 case MSG_ORDERED_TAG:
965 sdev->ordered_tags = 1;
966 sdev->simple_tags = 1;
969 sdev->ordered_tags = 0;
970 sdev->simple_tags = 1;
973 printk(KERN_WARNING "(scsi%d:%d:%d:%d) "
974 "scsi_adjust_queue_depth, bad queue type, "
975 "disabled\n", sdev->host->host_no,
976 sdev->channel, sdev->id, sdev->lun);
978 sdev->ordered_tags = sdev->simple_tags = 0;
979 sdev->queue_depth = tags;
983 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
985 EXPORT_SYMBOL(scsi_adjust_queue_depth);
988 * Function: scsi_track_queue_full()
990 * Purpose: This function will track successive QUEUE_FULL events on a
991 * specific SCSI device to determine if and when there is a
992 * need to adjust the queue depth on the device.
994 * Arguments: sdev - SCSI Device in question
995 * depth - Current number of outstanding SCSI commands on
996 * this device, not counting the one returned as
999 * Returns: 0 - No change needed
1000 * >0 - Adjust queue depth to this new depth
1001 * -1 - Drop back to untagged operation using host->cmd_per_lun
1002 * as the untagged command depth
1004 * Lock Status: None held on entry
1006 * Notes: Low level drivers may call this at any time and we will do
1007 * "The Right Thing." We are interrupt context safe.
1009 int scsi_track_queue_full(struct scsi_device *sdev, int depth)
1011 if ((jiffies >> 4) == sdev->last_queue_full_time)
1014 sdev->last_queue_full_time = (jiffies >> 4);
1015 if (sdev->last_queue_full_depth != depth) {
1016 sdev->last_queue_full_count = 1;
1017 sdev->last_queue_full_depth = depth;
1019 sdev->last_queue_full_count++;
1022 if (sdev->last_queue_full_count <= 10)
1024 if (sdev->last_queue_full_depth < 8) {
1025 /* Drop back to untagged */
1026 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
1030 if (sdev->ordered_tags)
1031 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
1033 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
1036 EXPORT_SYMBOL(scsi_track_queue_full);
1039 * scsi_device_get - get an addition reference to a scsi_device
1040 * @sdev: device to get a reference to
1042 * Gets a reference to the scsi_device and increments the use count
1043 * of the underlying LLDD module. You must hold host_lock of the
1044 * parent Scsi_Host or already have a reference when calling this.
1046 int scsi_device_get(struct scsi_device *sdev)
1048 if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
1050 if (!get_device(&sdev->sdev_gendev))
1052 if (!try_module_get(sdev->host->hostt->module)) {
1053 put_device(&sdev->sdev_gendev);
1058 EXPORT_SYMBOL(scsi_device_get);
1061 * scsi_device_put - release a reference to a scsi_device
1062 * @sdev: device to release a reference on.
1064 * Release a reference to the scsi_device and decrements the use count
1065 * of the underlying LLDD module. The device is freed once the last
1068 void scsi_device_put(struct scsi_device *sdev)
1070 module_put(sdev->host->hostt->module);
1071 put_device(&sdev->sdev_gendev);
1073 EXPORT_SYMBOL(scsi_device_put);
1075 /* helper for shost_for_each_device, thus not documented */
1076 struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
1077 struct scsi_device *prev)
1079 struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
1080 struct scsi_device *next = NULL;
1081 unsigned long flags;
1083 spin_lock_irqsave(shost->host_lock, flags);
1084 while (list->next != &shost->__devices) {
1085 next = list_entry(list->next, struct scsi_device, siblings);
1086 /* skip devices that we can't get a reference to */
1087 if (!scsi_device_get(next))
1092 spin_unlock_irqrestore(shost->host_lock, flags);
1095 scsi_device_put(prev);
1098 EXPORT_SYMBOL(__scsi_iterate_devices);
1101 * starget_for_each_device - helper to walk all devices of a target
1102 * @starget: target whose devices we want to iterate over.
1104 * This traverses over each devices of @shost. The devices have
1105 * a reference that must be released by scsi_host_put when breaking
1108 void starget_for_each_device(struct scsi_target *starget, void * data,
1109 void (*fn)(struct scsi_device *, void *))
1111 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1112 struct scsi_device *sdev;
1114 shost_for_each_device(sdev, shost) {
1115 if ((sdev->channel == starget->channel) &&
1116 (sdev->id == starget->id))
1120 EXPORT_SYMBOL(starget_for_each_device);
1123 * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
1124 * @starget: SCSI target pointer
1125 * @lun: SCSI Logical Unit Number
1127 * Looks up the scsi_device with the specified @lun for a give
1128 * @starget. The returned scsi_device does not have an additional
1129 * reference. You must hold the host's host_lock over this call and
1130 * any access to the returned scsi_device.
1132 * Note: The only reason why drivers would want to use this is because
1133 * they're need to access the device list in irq context. Otherwise you
1134 * really want to use scsi_device_lookup_by_target instead.
1136 struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
1139 struct scsi_device *sdev;
1141 list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
1142 if (sdev->lun ==lun)
1148 EXPORT_SYMBOL(__scsi_device_lookup_by_target);
1151 * scsi_device_lookup_by_target - find a device given the target
1152 * @starget: SCSI target pointer
1153 * @lun: SCSI Logical Unit Number
1155 * Looks up the scsi_device with the specified @channel, @id, @lun for a
1156 * give host. The returned scsi_device has an additional reference that
1157 * needs to be release with scsi_host_put once you're done with it.
1159 struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
1162 struct scsi_device *sdev;
1163 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1164 unsigned long flags;
1166 spin_lock_irqsave(shost->host_lock, flags);
1167 sdev = __scsi_device_lookup_by_target(starget, lun);
1168 if (sdev && scsi_device_get(sdev))
1170 spin_unlock_irqrestore(shost->host_lock, flags);
1174 EXPORT_SYMBOL(scsi_device_lookup_by_target);
1177 * scsi_device_lookup - find a device given the host (UNLOCKED)
1178 * @shost: SCSI host pointer
1179 * @channel: SCSI channel (zero if only one channel)
1180 * @pun: SCSI target number (physical unit number)
1181 * @lun: SCSI Logical Unit Number
1183 * Looks up the scsi_device with the specified @channel, @id, @lun for a
1184 * give host. The returned scsi_device does not have an additional reference.
1185 * You must hold the host's host_lock over this call and any access to the
1186 * returned scsi_device.
1188 * Note: The only reason why drivers would want to use this is because
1189 * they're need to access the device list in irq context. Otherwise you
1190 * really want to use scsi_device_lookup instead.
1192 struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
1193 uint channel, uint id, uint lun)
1195 struct scsi_device *sdev;
1197 list_for_each_entry(sdev, &shost->__devices, siblings) {
1198 if (sdev->channel == channel && sdev->id == id &&
1205 EXPORT_SYMBOL(__scsi_device_lookup);
1208 * scsi_device_lookup - find a device given the host
1209 * @shost: SCSI host pointer
1210 * @channel: SCSI channel (zero if only one channel)
1211 * @id: SCSI target number (physical unit number)
1212 * @lun: SCSI Logical Unit Number
1214 * Looks up the scsi_device with the specified @channel, @id, @lun for a
1215 * give host. The returned scsi_device has an additional reference that
1216 * needs to be release with scsi_host_put once you're done with it.
1218 struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
1219 uint channel, uint id, uint lun)
1221 struct scsi_device *sdev;
1222 unsigned long flags;
1224 spin_lock_irqsave(shost->host_lock, flags);
1225 sdev = __scsi_device_lookup(shost, channel, id, lun);
1226 if (sdev && scsi_device_get(sdev))
1228 spin_unlock_irqrestore(shost->host_lock, flags);
1232 EXPORT_SYMBOL(scsi_device_lookup);
1235 * scsi_device_cancel - cancel outstanding IO to this device
1236 * @sdev: Pointer to struct scsi_device
1237 * @recovery: Boolean instructing function to recover device or not.
1240 int scsi_device_cancel(struct scsi_device *sdev, int recovery)
1242 struct scsi_cmnd *scmd;
1243 LIST_HEAD(active_list);
1244 struct list_head *lh, *lh_sf;
1245 unsigned long flags;
1247 scsi_device_set_state(sdev, SDEV_CANCEL);
1249 spin_lock_irqsave(&sdev->list_lock, flags);
1250 list_for_each_entry(scmd, &sdev->cmd_list, list) {
1251 if (scmd->request && scmd->request->rq_status != RQ_INACTIVE) {
1253 * If we are unable to remove the timer, it means
1254 * that the command has already timed out or
1257 if (!scsi_delete_timer(scmd))
1259 list_add_tail(&scmd->eh_entry, &active_list);
1262 spin_unlock_irqrestore(&sdev->list_lock, flags);
1264 if (!list_empty(&active_list)) {
1265 list_for_each_safe(lh, lh_sf, &active_list) {
1266 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1269 scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD);
1271 scmd->result = (DID_ABORT << 16);
1272 scsi_finish_command(scmd);
1279 EXPORT_SYMBOL(scsi_device_cancel);
1281 #ifdef CONFIG_HOTPLUG_CPU
1282 static int scsi_cpu_notify(struct notifier_block *self,
1283 unsigned long action, void *hcpu)
1285 int cpu = (unsigned long)hcpu;
1289 /* Drain scsi_done_q. */
1290 local_irq_disable();
1291 list_splice_init(&per_cpu(scsi_done_q, cpu),
1292 &__get_cpu_var(scsi_done_q));
1293 raise_softirq_irqoff(SCSI_SOFTIRQ);
1302 static struct notifier_block __devinitdata scsi_cpu_nb = {
1303 .notifier_call = scsi_cpu_notify,
1306 #define register_scsi_cpu() register_cpu_notifier(&scsi_cpu_nb)
1307 #define unregister_scsi_cpu() unregister_cpu_notifier(&scsi_cpu_nb)
1309 #define register_scsi_cpu()
1310 #define unregister_scsi_cpu()
1311 #endif /* CONFIG_HOTPLUG_CPU */
1313 MODULE_DESCRIPTION("SCSI core");
1314 MODULE_LICENSE("GPL");
1316 module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
1317 MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
1319 static int __init init_scsi(void)
1323 error = scsi_init_queue();
1326 error = scsi_init_procfs();
1329 error = scsi_init_devinfo();
1331 goto cleanup_procfs;
1332 error = scsi_init_hosts();
1334 goto cleanup_devlist;
1335 error = scsi_init_sysctl();
1338 error = scsi_sysfs_register();
1340 goto cleanup_sysctl;
1342 for (i = 0; i < NR_CPUS; i++)
1343 INIT_LIST_HEAD(&per_cpu(scsi_done_q, i));
1345 devfs_mk_dir("scsi");
1346 open_softirq(SCSI_SOFTIRQ, scsi_softirq, NULL);
1347 register_scsi_cpu();
1348 printk(KERN_NOTICE "SCSI subsystem initialized\n");
1356 scsi_exit_devinfo();
1361 printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
1366 static void __exit exit_scsi(void)
1368 scsi_sysfs_unregister();
1371 scsi_exit_devinfo();
1372 devfs_remove("scsi");
1375 unregister_scsi_cpu();
1378 subsys_initcall(init_scsi);
1379 module_exit(exit_scsi);