2 * scsi_lib.c Copyright (C) 1999 Eric Youngdale
4 * SCSI queueing library.
5 * Initial versions: Eric Youngdale (eric@andante.org).
6 * Based upon conversations with large numbers
7 * of people at Linux Expo.
10 #include <linux/bio.h>
11 #include <linux/blkdev.h>
12 #include <linux/completion.h>
13 #include <linux/kernel.h>
14 #include <linux/mempool.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/delay.h>
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_dbg.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/scsi_driver.h>
24 #include <scsi/scsi_eh.h>
25 #include <scsi/scsi_host.h>
26 #include <scsi/scsi_request.h>
28 #include "scsi_priv.h"
29 #include "scsi_logging.h"
32 #define SG_MEMPOOL_NR (sizeof(scsi_sg_pools)/sizeof(struct scsi_host_sg_pool))
33 #define SG_MEMPOOL_SIZE 32
35 struct scsi_host_sg_pool {
42 #if (SCSI_MAX_PHYS_SEGMENTS < 32)
43 #error SCSI_MAX_PHYS_SEGMENTS is too small
46 #define SP(x) { x, "sgpool-" #x }
47 struct scsi_host_sg_pool scsi_sg_pools[] = {
51 #if (SCSI_MAX_PHYS_SEGMENTS > 32)
53 #if (SCSI_MAX_PHYS_SEGMENTS > 64)
55 #if (SCSI_MAX_PHYS_SEGMENTS > 128)
57 #if (SCSI_MAX_PHYS_SEGMENTS > 256)
58 #error SCSI_MAX_PHYS_SEGMENTS is too large
68 * Function: scsi_insert_special_req()
70 * Purpose: Insert pre-formed request into request queue.
72 * Arguments: sreq - request that is ready to be queued.
73 * at_head - boolean. True if we should insert at head
74 * of queue, false if we should insert at tail.
76 * Lock status: Assumed that lock is not held upon entry.
80 * Notes: This function is called from character device and from
81 * ioctl types of functions where the caller knows exactly
82 * what SCSI command needs to be issued. The idea is that
83 * we merely inject the command into the queue (at the head
84 * for now), and then call the queue request function to actually
87 int scsi_insert_special_req(struct scsi_request *sreq, int at_head)
90 * Because users of this function are apt to reuse requests with no
91 * modification, we have to sanitise the request flags here
93 sreq->sr_request->flags &= ~REQ_DONTPREP;
94 blk_insert_request(sreq->sr_device->request_queue, sreq->sr_request,
100 * Function: scsi_queue_insert()
102 * Purpose: Insert a command in the midlevel queue.
104 * Arguments: cmd - command that we are adding to queue.
105 * reason - why we are inserting command to queue.
107 * Lock status: Assumed that lock is not held upon entry.
111 * Notes: We do this for one of two cases. Either the host is busy
112 * and it cannot accept any more commands for the time being,
113 * or the device returned QUEUE_FULL and can accept no more
115 * Notes: This could be called either from an interrupt context or a
116 * normal process context.
118 int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
120 struct Scsi_Host *host = cmd->device->host;
121 struct scsi_device *device = cmd->device;
124 printk("Inserting command %p into mlqueue\n", cmd));
127 * We are inserting the command into the ml queue. First, we
128 * cancel the timer, so it doesn't time out.
130 scsi_delete_timer(cmd);
133 * Next, set the appropriate busy bit for the device/host.
135 * If the host/device isn't busy, assume that something actually
136 * completed, and that we should be able to queue a command now.
138 * Note that the prior mid-layer assumption that any host could
139 * always queue at least one command is now broken. The mid-layer
140 * will implement a user specifiable stall (see
141 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
142 * if a command is requeued with no other commands outstanding
143 * either for the device or for the host.
145 if (reason == SCSI_MLQUEUE_HOST_BUSY)
146 host->host_blocked = host->max_host_blocked;
147 else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
148 device->device_blocked = device->max_device_blocked;
151 * Register the fact that we own the thing for now.
153 cmd->state = SCSI_STATE_MLQUEUE;
154 cmd->owner = SCSI_OWNER_MIDLEVEL;
157 * Decrement the counters, since these commands are no longer
158 * active on the host/device.
160 scsi_device_unbusy(device);
163 * Insert this command at the head of the queue for it's device.
164 * It will go before all other commands that are already in the queue.
166 * NOTE: there is magic here about the way the queue is plugged if
167 * we have no outstanding commands.
169 * Although this *doesn't* plug the queue, it does call the request
170 * function. The SCSI request function detects the blocked condition
171 * and plugs the queue appropriately.
173 blk_insert_request(device->request_queue, cmd->request, 1, cmd, 1);
178 * Function: scsi_do_req
180 * Purpose: Queue a SCSI request
182 * Arguments: sreq - command descriptor.
183 * cmnd - actual SCSI command to be performed.
184 * buffer - data buffer.
185 * bufflen - size of data buffer.
186 * done - completion function to be run.
187 * timeout - how long to let it run before timeout.
188 * retries - number of retries we allow.
190 * Lock status: No locks held upon entry.
194 * Notes: This function is only used for queueing requests for things
195 * like ioctls and character device requests - this is because
196 * we essentially just inject a request into the queue for the
199 * In order to support the scsi_device_quiesce function, we
200 * now inject requests on the *head* of the device queue
201 * rather than the tail.
203 void scsi_do_req(struct scsi_request *sreq, const void *cmnd,
204 void *buffer, unsigned bufflen,
205 void (*done)(struct scsi_cmnd *),
206 int timeout, int retries)
209 * If the upper level driver is reusing these things, then
210 * we should release the low-level block now. Another one will
211 * be allocated later when this request is getting queued.
213 __scsi_release_request(sreq);
216 * Our own function scsi_done (which marks the host as not busy,
217 * disables the timeout counter, etc) will be called by us or by the
218 * scsi_hosts[host].queuecommand() function needs to also call
219 * the completion function for the high level driver.
221 memcpy(sreq->sr_cmnd, cmnd, sizeof(sreq->sr_cmnd));
222 sreq->sr_bufflen = bufflen;
223 sreq->sr_buffer = buffer;
224 sreq->sr_allowed = retries;
225 sreq->sr_done = done;
226 sreq->sr_timeout_per_command = timeout;
228 if (sreq->sr_cmd_len == 0)
229 sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
232 * head injection *required* here otherwise quiesce won't work
234 scsi_insert_special_req(sreq, 1);
236 EXPORT_SYMBOL(scsi_do_req);
238 static void scsi_wait_done(struct scsi_cmnd *cmd)
240 struct request *req = cmd->request;
241 struct request_queue *q = cmd->device->request_queue;
244 req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
246 spin_lock_irqsave(q->queue_lock, flags);
247 if (blk_rq_tagged(req))
248 blk_queue_end_tag(q, req);
249 spin_unlock_irqrestore(q->queue_lock, flags);
252 complete(req->waiting);
255 /* This is the end routine we get to if a command was never attached
256 * to the request. Simply complete the request without changing
257 * rq_status; this will cause a DRIVER_ERROR. */
258 static void scsi_wait_req_end_io(struct request *req)
260 BUG_ON(!req->waiting);
262 complete(req->waiting);
265 void scsi_wait_req(struct scsi_request *sreq, const void *cmnd, void *buffer,
266 unsigned bufflen, int timeout, int retries)
268 DECLARE_COMPLETION(wait);
270 sreq->sr_request->waiting = &wait;
271 sreq->sr_request->rq_status = RQ_SCSI_BUSY;
272 sreq->sr_request->end_io = scsi_wait_req_end_io;
273 scsi_do_req(sreq, cmnd, buffer, bufflen, scsi_wait_done,
275 wait_for_completion(&wait);
276 sreq->sr_request->waiting = NULL;
277 if (sreq->sr_request->rq_status != RQ_SCSI_DONE)
278 sreq->sr_result |= (DRIVER_ERROR << 24);
280 __scsi_release_request(sreq);
282 EXPORT_SYMBOL(scsi_wait_req);
285 * Function: scsi_init_cmd_errh()
287 * Purpose: Initialize cmd fields related to error handling.
289 * Arguments: cmd - command that is ready to be queued.
293 * Notes: This function has the job of initializing a number of
294 * fields related to error handling. Typically this will
295 * be called once for each command, as required.
297 static int scsi_init_cmd_errh(struct scsi_cmnd *cmd)
299 cmd->owner = SCSI_OWNER_MIDLEVEL;
300 cmd->serial_number = 0;
301 cmd->abort_reason = 0;
303 memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
305 if (cmd->cmd_len == 0)
306 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
309 * We need saved copies of a number of fields - this is because
310 * error handling may need to overwrite these with different values
311 * to run different commands, and once error handling is complete,
312 * we will need to restore these values prior to running the actual
315 cmd->old_use_sg = cmd->use_sg;
316 cmd->old_cmd_len = cmd->cmd_len;
317 cmd->sc_old_data_direction = cmd->sc_data_direction;
318 cmd->old_underflow = cmd->underflow;
319 memcpy(cmd->data_cmnd, cmd->cmnd, sizeof(cmd->cmnd));
320 cmd->buffer = cmd->request_buffer;
321 cmd->bufflen = cmd->request_bufflen;
322 cmd->abort_reason = 0;
328 * Function: scsi_setup_cmd_retry()
330 * Purpose: Restore the command state for a retry
332 * Arguments: cmd - command to be restored
336 * Notes: Immediately prior to retrying a command, we need
337 * to restore certain fields that we saved above.
339 void scsi_setup_cmd_retry(struct scsi_cmnd *cmd)
341 memcpy(cmd->cmnd, cmd->data_cmnd, sizeof(cmd->data_cmnd));
342 cmd->request_buffer = cmd->buffer;
343 cmd->request_bufflen = cmd->bufflen;
344 cmd->use_sg = cmd->old_use_sg;
345 cmd->cmd_len = cmd->old_cmd_len;
346 cmd->sc_data_direction = cmd->sc_old_data_direction;
347 cmd->underflow = cmd->old_underflow;
350 void scsi_device_unbusy(struct scsi_device *sdev)
352 struct Scsi_Host *shost = sdev->host;
355 spin_lock_irqsave(shost->host_lock, flags);
357 if (unlikely(test_bit(SHOST_RECOVERY, &shost->shost_state) &&
359 scsi_eh_wakeup(shost);
360 spin_unlock(shost->host_lock);
361 spin_lock(sdev->request_queue->queue_lock);
363 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
367 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
368 * and call blk_run_queue for all the scsi_devices on the target -
369 * including current_sdev first.
371 * Called with *no* scsi locks held.
373 static void scsi_single_lun_run(struct scsi_device *current_sdev)
375 struct Scsi_Host *shost = current_sdev->host;
376 struct scsi_device *sdev, *tmp;
377 struct scsi_target *starget = scsi_target(current_sdev);
380 spin_lock_irqsave(shost->host_lock, flags);
381 starget->starget_sdev_user = NULL;
382 spin_unlock_irqrestore(shost->host_lock, flags);
385 * Call blk_run_queue for all LUNs on the target, starting with
386 * current_sdev. We race with others (to set starget_sdev_user),
387 * but in most cases, we will be first. Ideally, each LU on the
388 * target would get some limited time or requests on the target.
390 blk_run_queue(current_sdev->request_queue);
392 spin_lock_irqsave(shost->host_lock, flags);
393 if (starget->starget_sdev_user)
395 list_for_each_entry_safe(sdev, tmp, &starget->devices,
396 same_target_siblings) {
397 if (sdev == current_sdev)
399 if (scsi_device_get(sdev))
402 spin_unlock_irqrestore(shost->host_lock, flags);
403 blk_run_queue(sdev->request_queue);
404 spin_lock_irqsave(shost->host_lock, flags);
406 scsi_device_put(sdev);
409 spin_unlock_irqrestore(shost->host_lock, flags);
413 * Function: scsi_run_queue()
415 * Purpose: Select a proper request queue to serve next
417 * Arguments: q - last request's queue
421 * Notes: The previous command was completely finished, start
422 * a new one if possible.
424 static void scsi_run_queue(struct request_queue *q)
426 struct scsi_device *sdev = q->queuedata;
427 struct Scsi_Host *shost = sdev->host;
430 if (sdev->single_lun)
431 scsi_single_lun_run(sdev);
433 spin_lock_irqsave(shost->host_lock, flags);
434 while (!list_empty(&shost->starved_list) &&
435 !shost->host_blocked && !shost->host_self_blocked &&
436 !((shost->can_queue > 0) &&
437 (shost->host_busy >= shost->can_queue))) {
439 * As long as shost is accepting commands and we have
440 * starved queues, call blk_run_queue. scsi_request_fn
441 * drops the queue_lock and can add us back to the
444 * host_lock protects the starved_list and starved_entry.
445 * scsi_request_fn must get the host_lock before checking
446 * or modifying starved_list or starved_entry.
448 sdev = list_entry(shost->starved_list.next,
449 struct scsi_device, starved_entry);
450 list_del_init(&sdev->starved_entry);
451 spin_unlock_irqrestore(shost->host_lock, flags);
453 blk_run_queue(sdev->request_queue);
455 spin_lock_irqsave(shost->host_lock, flags);
456 if (unlikely(!list_empty(&sdev->starved_entry)))
458 * sdev lost a race, and was put back on the
459 * starved list. This is unlikely but without this
460 * in theory we could loop forever.
464 spin_unlock_irqrestore(shost->host_lock, flags);
470 * Function: scsi_requeue_command()
472 * Purpose: Handle post-processing of completed commands.
474 * Arguments: q - queue to operate on
475 * cmd - command that may need to be requeued.
479 * Notes: After command completion, there may be blocks left
480 * over which weren't finished by the previous command
481 * this can be for a number of reasons - the main one is
482 * I/O errors in the middle of the request, in which case
483 * we need to request the blocks that come after the bad
486 static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
490 cmd->request->flags &= ~REQ_DONTPREP;
492 spin_lock_irqsave(q->queue_lock, flags);
493 blk_requeue_request(q, cmd->request);
494 spin_unlock_irqrestore(q->queue_lock, flags);
499 void scsi_next_command(struct scsi_cmnd *cmd)
501 struct request_queue *q = cmd->device->request_queue;
503 scsi_put_command(cmd);
507 void scsi_run_host_queues(struct Scsi_Host *shost)
509 struct scsi_device *sdev;
511 shost_for_each_device(sdev, shost)
512 scsi_run_queue(sdev->request_queue);
516 * Function: scsi_end_request()
518 * Purpose: Post-processing of completed commands (usually invoked at end
519 * of upper level post-processing and scsi_io_completion).
521 * Arguments: cmd - command that is complete.
522 * uptodate - 1 if I/O indicates success, <= 0 for I/O error.
523 * bytes - number of bytes of completed I/O
524 * requeue - indicates whether we should requeue leftovers.
526 * Lock status: Assumed that lock is not held upon entry.
528 * Returns: cmd if requeue done or required, NULL otherwise
530 * Notes: This is called for block device requests in order to
531 * mark some number of sectors as complete.
533 * We are guaranteeing that the request queue will be goosed
534 * at some point during this call.
536 static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
537 int bytes, int requeue)
539 request_queue_t *q = cmd->device->request_queue;
540 struct request *req = cmd->request;
544 * If there are blocks left over at the end, set up the command
545 * to queue the remainder of them.
547 if (end_that_request_chunk(req, uptodate, bytes)) {
548 int leftover = (req->hard_nr_sectors << 9);
550 if (blk_pc_request(req))
551 leftover = req->data_len;
553 /* kill remainder if no retrys */
554 if (!uptodate && blk_noretry_request(req))
555 end_that_request_chunk(req, 0, leftover);
559 * Bleah. Leftovers again. Stick the
560 * leftovers in the front of the
561 * queue, and goose the queue again.
563 scsi_requeue_command(q, cmd);
569 add_disk_randomness(req->rq_disk);
571 spin_lock_irqsave(q->queue_lock, flags);
572 if (blk_rq_tagged(req))
573 blk_queue_end_tag(q, req);
574 end_that_request_last(req);
575 spin_unlock_irqrestore(q->queue_lock, flags);
578 * This will goose the queue request function at the end, so we don't
579 * need to worry about launching another command.
581 scsi_next_command(cmd);
585 static struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, int gfp_mask)
587 struct scsi_host_sg_pool *sgp;
588 struct scatterlist *sgl;
590 BUG_ON(!cmd->use_sg);
592 switch (cmd->use_sg) {
602 #if (SCSI_MAX_PHYS_SEGMENTS > 32)
606 #if (SCSI_MAX_PHYS_SEGMENTS > 64)
610 #if (SCSI_MAX_PHYS_SEGMENTS > 128)
621 sgp = scsi_sg_pools + cmd->sglist_len;
622 sgl = mempool_alloc(sgp->pool, gfp_mask);
624 memset(sgl, 0, sgp->size);
628 static void scsi_free_sgtable(struct scatterlist *sgl, int index)
630 struct scsi_host_sg_pool *sgp;
632 BUG_ON(index > SG_MEMPOOL_NR);
634 sgp = scsi_sg_pools + index;
635 mempool_free(sgl, sgp->pool);
639 * Function: scsi_release_buffers()
641 * Purpose: Completion processing for block device I/O requests.
643 * Arguments: cmd - command that we are bailing.
645 * Lock status: Assumed that no lock is held upon entry.
649 * Notes: In the event that an upper level driver rejects a
650 * command, we must release resources allocated during
651 * the __init_io() function. Primarily this would involve
652 * the scatter-gather table, and potentially any bounce
655 static void scsi_release_buffers(struct scsi_cmnd *cmd)
657 struct request *req = cmd->request;
660 * Free up any indirection buffers we allocated for DMA purposes.
663 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
664 else if (cmd->request_buffer != req->buffer)
665 kfree(cmd->request_buffer);
668 * Zero these out. They now point to freed memory, and it is
669 * dangerous to hang onto the pointers.
673 cmd->request_buffer = NULL;
674 cmd->request_bufflen = 0;
678 * Function: scsi_io_completion()
680 * Purpose: Completion processing for block device I/O requests.
682 * Arguments: cmd - command that is finished.
684 * Lock status: Assumed that no lock is held upon entry.
688 * Notes: This function is matched in terms of capabilities to
689 * the function that created the scatter-gather list.
690 * In other words, if there are no bounce buffers
691 * (the normal case for most drivers), we don't need
692 * the logic to deal with cleaning up afterwards.
694 * We must do one of several things here:
696 * a) Call scsi_end_request. This will finish off the
697 * specified number of sectors. If we are done, the
698 * command block will be released, and the queue
699 * function will be goosed. If we are not done, then
700 * scsi_end_request will directly goose the queue.
702 * b) We can just use scsi_requeue_command() here. This would
703 * be used if we just wanted to retry, for example.
705 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes,
706 unsigned int block_bytes)
708 int result = cmd->result;
709 int this_count = cmd->bufflen;
710 request_queue_t *q = cmd->device->request_queue;
711 struct request *req = cmd->request;
712 int clear_errors = 1;
713 struct scsi_sense_hdr sshdr;
715 int sense_deferred = 0;
717 if (blk_complete_barrier_rq(q, req, good_bytes >> 9))
721 * Free up any indirection buffers we allocated for DMA purposes.
722 * For the case of a READ, we need to copy the data out of the
723 * bounce buffer and into the real buffer.
726 scsi_free_sgtable(cmd->buffer, cmd->sglist_len);
727 else if (cmd->buffer != req->buffer) {
728 if (rq_data_dir(req) == READ) {
730 char *to = bio_kmap_irq(req->bio, &flags);
731 memcpy(to, cmd->buffer, cmd->bufflen);
732 bio_kunmap_irq(to, &flags);
738 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
740 sense_deferred = scsi_sense_is_deferred(&sshdr);
742 if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
743 req->errors = result;
746 if (sense_valid && req->sense) {
748 * SG_IO wants current and deferred errors
750 int len = 8 + cmd->sense_buffer[7];
752 if (len > SCSI_SENSE_BUFFERSIZE)
753 len = SCSI_SENSE_BUFFERSIZE;
754 memcpy(req->sense, cmd->sense_buffer, len);
755 req->sense_len = len;
758 req->data_len = cmd->resid;
762 * Zero these out. They now point to freed memory, and it is
763 * dangerous to hang onto the pointers.
767 cmd->request_buffer = NULL;
768 cmd->request_bufflen = 0;
771 * Next deal with any sectors which we were able to correctly
774 if (good_bytes >= 0) {
775 SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, %d bytes done.\n",
776 req->nr_sectors, good_bytes));
777 SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n", cmd->use_sg));
782 * If multiple sectors are requested in one buffer, then
783 * they will have been finished off by the first command.
784 * If not, then we have a multi-buffer command.
786 * If block_bytes != 0, it means we had a medium error
787 * of some sort, and that we want to mark some number of
788 * sectors as not uptodate. Thus we want to inhibit
789 * requeueing right here - we will requeue down below
790 * when we handle the bad sectors.
792 cmd = scsi_end_request(cmd, 1, good_bytes, result == 0);
795 * If the command completed without error, then either finish off the
796 * rest of the command, or start a new one.
798 if (result == 0 || cmd == NULL ) {
803 * Now, if we were good little boys and girls, Santa left us a request
804 * sense buffer. We can extract information from this, so we
805 * can choose a block to remap, etc.
807 if (sense_valid && !sense_deferred) {
808 switch (sshdr.sense_key) {
810 if (cmd->device->removable) {
811 /* detected disc change. set a bit
812 * and quietly refuse further access.
814 cmd->device->changed = 1;
815 cmd = scsi_end_request(cmd, 0,
820 * Must have been a power glitch, or a
821 * bus reset. Could not have been a
822 * media change, so we just retry the
823 * request and see what happens.
825 scsi_requeue_command(q, cmd);
829 case ILLEGAL_REQUEST:
831 * If we had an ILLEGAL REQUEST returned, then we may
832 * have performed an unsupported command. The only
833 * thing this should be would be a ten byte read where
834 * only a six byte read was supported. Also, on a
835 * system where READ CAPACITY failed, we may have read
836 * past the end of the disk.
838 if (cmd->device->use_10_for_rw &&
839 (cmd->cmnd[0] == READ_10 ||
840 cmd->cmnd[0] == WRITE_10)) {
841 cmd->device->use_10_for_rw = 0;
843 * This will cause a retry with a 6-byte
846 scsi_requeue_command(q, cmd);
849 cmd = scsi_end_request(cmd, 0, this_count, 1);
855 * If the device is in the process of becoming ready,
858 if (sshdr.asc == 0x04 && sshdr.ascq == 0x01) {
859 scsi_requeue_command(q, cmd);
862 printk(KERN_INFO "Device %s not ready.\n",
863 req->rq_disk ? req->rq_disk->disk_name : "");
864 cmd = scsi_end_request(cmd, 0, this_count, 1);
866 case VOLUME_OVERFLOW:
867 printk(KERN_INFO "Volume overflow <%d %d %d %d> CDB: ",
868 cmd->device->host->host_no,
869 (int)cmd->device->channel,
870 (int)cmd->device->id, (int)cmd->device->lun);
871 __scsi_print_command(cmd->data_cmnd);
872 scsi_print_sense("", cmd);
873 cmd = scsi_end_request(cmd, 0, block_bytes, 1);
878 } /* driver byte != 0 */
879 if (host_byte(result) == DID_RESET) {
881 * Third party bus reset or reset for error
882 * recovery reasons. Just retry the request
883 * and see what happens.
885 scsi_requeue_command(q, cmd);
889 printk(KERN_INFO "SCSI error : <%d %d %d %d> return code "
890 "= 0x%x\n", cmd->device->host->host_no,
891 cmd->device->channel,
893 cmd->device->lun, result);
895 if (driver_byte(result) & DRIVER_SENSE)
896 scsi_print_sense("", cmd);
898 * Mark a single buffer as not uptodate. Queue the remainder.
899 * We sometimes get this cruft in the event that a medium error
900 * isn't properly reported.
902 block_bytes = req->hard_cur_sectors << 9;
904 block_bytes = req->data_len;
905 cmd = scsi_end_request(cmd, 0, block_bytes, 1);
908 EXPORT_SYMBOL(scsi_io_completion);
911 * Function: scsi_init_io()
913 * Purpose: SCSI I/O initialize function.
915 * Arguments: cmd - Command descriptor we wish to initialize
917 * Returns: 0 on success
918 * BLKPREP_DEFER if the failure is retryable
919 * BLKPREP_KILL if the failure is fatal
921 static int scsi_init_io(struct scsi_cmnd *cmd)
923 struct request *req = cmd->request;
924 struct scatterlist *sgpnt;
928 * if this is a rq->data based REQ_BLOCK_PC, setup for a non-sg xfer
930 if ((req->flags & REQ_BLOCK_PC) && !req->bio) {
931 cmd->request_bufflen = req->data_len;
932 cmd->request_buffer = req->data;
933 req->buffer = req->data;
939 * we used to not use scatter-gather for single segment request,
940 * but now we do (it makes highmem I/O easier to support without
943 cmd->use_sg = req->nr_phys_segments;
946 * if sg table allocation fails, requeue request later.
948 sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
949 if (unlikely(!sgpnt))
950 return BLKPREP_DEFER;
952 cmd->request_buffer = (char *) sgpnt;
953 cmd->request_bufflen = req->nr_sectors << 9;
954 if (blk_pc_request(req))
955 cmd->request_bufflen = req->data_len;
959 * Next, walk the list, and fill in the addresses and sizes of
962 count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
965 * mapped well, send it off
967 if (likely(count <= cmd->use_sg)) {
972 printk(KERN_ERR "Incorrect number of segments after building list\n");
973 printk(KERN_ERR "counted %d, received %d\n", count, cmd->use_sg);
974 printk(KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n", req->nr_sectors,
975 req->current_nr_sectors);
977 /* release the command and kill it */
978 scsi_release_buffers(cmd);
979 scsi_put_command(cmd);
983 static int scsi_prepare_flush_fn(request_queue_t *q, struct request *rq)
985 struct scsi_device *sdev = q->queuedata;
986 struct scsi_driver *drv;
988 if (sdev->sdev_state == SDEV_RUNNING) {
989 drv = *(struct scsi_driver **) rq->rq_disk->private_data;
991 if (drv->prepare_flush)
992 return drv->prepare_flush(q, rq);
998 static void scsi_end_flush_fn(request_queue_t *q, struct request *rq)
1000 struct scsi_device *sdev = q->queuedata;
1001 struct request *flush_rq = rq->end_io_data;
1002 struct scsi_driver *drv;
1004 if (flush_rq->errors) {
1005 printk("scsi: barrier error, disabling flush support\n");
1006 blk_queue_ordered(q, QUEUE_ORDERED_NONE);
1009 if (sdev->sdev_state == SDEV_RUNNING) {
1010 drv = *(struct scsi_driver **) rq->rq_disk->private_data;
1011 drv->end_flush(q, rq);
1015 static int scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
1016 sector_t *error_sector)
1018 struct scsi_device *sdev = q->queuedata;
1019 struct scsi_driver *drv;
1021 if (sdev->sdev_state != SDEV_RUNNING)
1024 drv = *(struct scsi_driver **) disk->private_data;
1025 if (drv->issue_flush)
1026 return drv->issue_flush(&sdev->sdev_gendev, error_sector);
1031 static int scsi_prep_fn(struct request_queue *q, struct request *req)
1033 struct scsi_device *sdev = q->queuedata;
1034 struct scsi_cmnd *cmd;
1035 int specials_only = 0;
1038 * Just check to see if the device is online. If it isn't, we
1039 * refuse to process any commands. The device must be brought
1040 * online before trying any recovery commands
1042 if (unlikely(!scsi_device_online(sdev))) {
1043 printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to offline device\n",
1044 sdev->host->host_no, sdev->id, sdev->lun);
1045 return BLKPREP_KILL;
1047 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1048 /* OK, we're not in a running state don't prep
1050 if (sdev->sdev_state == SDEV_DEL) {
1051 /* Device is fully deleted, no commands
1052 * at all allowed down */
1053 printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to dead device\n",
1054 sdev->host->host_no, sdev->id, sdev->lun);
1055 return BLKPREP_KILL;
1057 /* OK, we only allow special commands (i.e. not
1058 * user initiated ones */
1059 specials_only = sdev->sdev_state;
1063 * Find the actual device driver associated with this command.
1064 * The SPECIAL requests are things like character device or
1065 * ioctls, which did not originate from ll_rw_blk. Note that
1066 * the special field is also used to indicate the cmd for
1067 * the remainder of a partially fulfilled request that can
1068 * come up when there is a medium error. We have to treat
1069 * these two cases differently. We differentiate by looking
1070 * at request->cmd, as this tells us the real story.
1072 if (req->flags & REQ_SPECIAL) {
1073 struct scsi_request *sreq = req->special;
1075 if (sreq->sr_magic == SCSI_REQ_MAGIC) {
1076 cmd = scsi_get_command(sreq->sr_device, GFP_ATOMIC);
1079 scsi_init_cmd_from_req(cmd, sreq);
1082 } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
1084 if(unlikely(specials_only)) {
1085 if(specials_only == SDEV_QUIESCE ||
1086 specials_only == SDEV_BLOCK)
1087 return BLKPREP_DEFER;
1089 printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to device being removed\n",
1090 sdev->host->host_no, sdev->id, sdev->lun);
1091 return BLKPREP_KILL;
1096 * Now try and find a command block that we can use.
1098 if (!req->special) {
1099 cmd = scsi_get_command(sdev, GFP_ATOMIC);
1105 /* pull a tag out of the request if we have one */
1106 cmd->tag = req->tag;
1108 blk_dump_rq_flags(req, "SCSI bad req");
1109 return BLKPREP_KILL;
1112 /* note the overloading of req->special. When the tag
1113 * is active it always means cmd. If the tag goes
1114 * back for re-queueing, it may be reset */
1119 * FIXME: drop the lock here because the functions below
1120 * expect to be called without the queue lock held. Also,
1121 * previously, we dequeued the request before dropping the
1122 * lock. We hope REQ_STARTED prevents anything untoward from
1125 if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
1126 struct scsi_driver *drv;
1130 * This will do a couple of things:
1131 * 1) Fill in the actual SCSI command.
1132 * 2) Fill in any other upper-level specific fields
1135 * If this returns 0, it means that the request failed
1136 * (reading past end of disk, reading offline device,
1137 * etc). This won't actually talk to the device, but
1138 * some kinds of consistency checking may cause the
1139 * request to be rejected immediately.
1143 * This sets up the scatter-gather table (allocating if
1146 ret = scsi_init_io(cmd);
1147 if (ret) /* BLKPREP_KILL return also releases the command */
1151 * Initialize the actual SCSI command for this request.
1153 drv = *(struct scsi_driver **)req->rq_disk->private_data;
1154 if (unlikely(!drv->init_command(cmd))) {
1155 scsi_release_buffers(cmd);
1156 scsi_put_command(cmd);
1157 return BLKPREP_KILL;
1162 * The request is now prepped, no need to come back here
1164 req->flags |= REQ_DONTPREP;
1168 /* If we defer, the elv_next_request() returns NULL, but the
1169 * queue must be restarted, so we plug here if no returning
1170 * command will automatically do that. */
1171 if (sdev->device_busy == 0)
1173 return BLKPREP_DEFER;
1177 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1180 * Called with the queue_lock held.
1182 static inline int scsi_dev_queue_ready(struct request_queue *q,
1183 struct scsi_device *sdev)
1185 if (sdev->device_busy >= sdev->queue_depth)
1187 if (sdev->device_busy == 0 && sdev->device_blocked) {
1189 * unblock after device_blocked iterates to zero
1191 if (--sdev->device_blocked == 0) {
1193 printk("scsi%d (%d:%d) unblocking device at"
1194 " zero depth\n", sdev->host->host_no,
1195 sdev->id, sdev->lun));
1201 if (sdev->device_blocked)
1208 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1209 * return 0. We must end up running the queue again whenever 0 is
1210 * returned, else IO can hang.
1212 * Called with host_lock held.
1214 static inline int scsi_host_queue_ready(struct request_queue *q,
1215 struct Scsi_Host *shost,
1216 struct scsi_device *sdev)
1218 if (test_bit(SHOST_RECOVERY, &shost->shost_state))
1220 if (shost->host_busy == 0 && shost->host_blocked) {
1222 * unblock after host_blocked iterates to zero
1224 if (--shost->host_blocked == 0) {
1226 printk("scsi%d unblocking host at zero depth\n",
1233 if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
1234 shost->host_blocked || shost->host_self_blocked) {
1235 if (list_empty(&sdev->starved_entry))
1236 list_add_tail(&sdev->starved_entry, &shost->starved_list);
1240 /* We're OK to process the command, so we can't be starved */
1241 if (!list_empty(&sdev->starved_entry))
1242 list_del_init(&sdev->starved_entry);
1248 * Kill requests for a dead device
1250 static void scsi_kill_requests(request_queue_t *q)
1252 struct request *req;
1254 while ((req = elv_next_request(q)) != NULL) {
1255 blkdev_dequeue_request(req);
1256 req->flags |= REQ_QUIET;
1257 while (end_that_request_first(req, 0, req->nr_sectors))
1259 end_that_request_last(req);
1264 * Function: scsi_request_fn()
1266 * Purpose: Main strategy routine for SCSI.
1268 * Arguments: q - Pointer to actual queue.
1272 * Lock status: IO request lock assumed to be held when called.
1274 static void scsi_request_fn(struct request_queue *q)
1276 struct scsi_device *sdev = q->queuedata;
1277 struct Scsi_Host *shost;
1278 struct scsi_cmnd *cmd;
1279 struct request *req;
1282 printk("scsi: killing requests for dead queue\n");
1283 scsi_kill_requests(q);
1287 if(!get_device(&sdev->sdev_gendev))
1288 /* We must be tearing the block queue down already */
1292 * To start with, we keep looping until the queue is empty, or until
1293 * the host is no longer able to accept any more requests.
1296 while (!blk_queue_plugged(q)) {
1299 * get next queueable request. We do this early to make sure
1300 * that the request is fully prepared even if we cannot
1303 req = elv_next_request(q);
1304 if (!req || !scsi_dev_queue_ready(q, sdev))
1307 if (unlikely(!scsi_device_online(sdev))) {
1308 printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to offline device\n",
1309 sdev->host->host_no, sdev->id, sdev->lun);
1310 blkdev_dequeue_request(req);
1311 req->flags |= REQ_QUIET;
1312 while (end_that_request_first(req, 0, req->nr_sectors))
1314 end_that_request_last(req);
1320 * Remove the request from the request list.
1322 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
1323 blkdev_dequeue_request(req);
1324 sdev->device_busy++;
1326 spin_unlock(q->queue_lock);
1327 spin_lock(shost->host_lock);
1329 if (!scsi_host_queue_ready(q, shost, sdev))
1331 if (sdev->single_lun) {
1332 if (scsi_target(sdev)->starget_sdev_user &&
1333 scsi_target(sdev)->starget_sdev_user != sdev)
1335 scsi_target(sdev)->starget_sdev_user = sdev;
1340 * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
1341 * take the lock again.
1343 spin_unlock_irq(shost->host_lock);
1346 if (unlikely(cmd == NULL)) {
1347 printk(KERN_CRIT "impossible request in %s.\n"
1348 "please mail a stack trace to "
1349 "linux-scsi@vger.kernel.org",
1355 * Finally, initialize any error handling parameters, and set up
1356 * the timers for timeouts.
1358 scsi_init_cmd_errh(cmd);
1361 * Dispatch the command to the low-level driver.
1363 rtn = scsi_dispatch_cmd(cmd);
1364 spin_lock_irq(q->queue_lock);
1366 /* we're refusing the command; because of
1367 * the way locks get dropped, we need to
1368 * check here if plugging is required */
1369 if(sdev->device_busy == 0)
1379 spin_unlock_irq(shost->host_lock);
1382 * lock q, handle tag, requeue req, and decrement device_busy. We
1383 * must return with queue_lock held.
1385 * Decrementing device_busy without checking it is OK, as all such
1386 * cases (host limits or settings) should run the queue at some
1389 spin_lock_irq(q->queue_lock);
1390 blk_requeue_request(q, req);
1391 sdev->device_busy--;
1392 if(sdev->device_busy == 0)
1395 /* must be careful here...if we trigger the ->remove() function
1396 * we cannot be holding the q lock */
1397 spin_unlock_irq(q->queue_lock);
1398 put_device(&sdev->sdev_gendev);
1399 spin_lock_irq(q->queue_lock);
1402 u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
1404 struct device *host_dev;
1405 u64 bounce_limit = 0xffffffff;
1407 if (shost->unchecked_isa_dma)
1408 return BLK_BOUNCE_ISA;
1410 * Platforms with virtual-DMA translation
1411 * hardware have no practical limit.
1413 if (!PCI_DMA_BUS_IS_PHYS)
1414 return BLK_BOUNCE_ANY;
1416 host_dev = scsi_get_device(shost);
1417 if (host_dev && host_dev->dma_mask)
1418 bounce_limit = *host_dev->dma_mask;
1420 return bounce_limit;
1422 EXPORT_SYMBOL(scsi_calculate_bounce_limit);
1424 struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
1426 struct Scsi_Host *shost = sdev->host;
1427 struct request_queue *q;
1429 q = blk_init_queue(scsi_request_fn, NULL);
1433 blk_queue_prep_rq(q, scsi_prep_fn);
1435 blk_queue_max_hw_segments(q, shost->sg_tablesize);
1436 blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
1437 blk_queue_max_sectors(q, shost->max_sectors);
1438 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
1439 blk_queue_segment_boundary(q, shost->dma_boundary);
1440 blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
1443 * ordered tags are superior to flush ordering
1445 if (shost->ordered_tag)
1446 blk_queue_ordered(q, QUEUE_ORDERED_TAG);
1447 else if (shost->ordered_flush) {
1448 blk_queue_ordered(q, QUEUE_ORDERED_FLUSH);
1449 q->prepare_flush_fn = scsi_prepare_flush_fn;
1450 q->end_flush_fn = scsi_end_flush_fn;
1453 if (!shost->use_clustering)
1454 clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
1458 void scsi_free_queue(struct request_queue *q)
1460 blk_cleanup_queue(q);
1464 * Function: scsi_block_requests()
1466 * Purpose: Utility function used by low-level drivers to prevent further
1467 * commands from being queued to the device.
1469 * Arguments: shost - Host in question
1473 * Lock status: No locks are assumed held.
1475 * Notes: There is no timer nor any other means by which the requests
1476 * get unblocked other than the low-level driver calling
1477 * scsi_unblock_requests().
1479 void scsi_block_requests(struct Scsi_Host *shost)
1481 shost->host_self_blocked = 1;
1483 EXPORT_SYMBOL(scsi_block_requests);
1486 * Function: scsi_unblock_requests()
1488 * Purpose: Utility function used by low-level drivers to allow further
1489 * commands from being queued to the device.
1491 * Arguments: shost - Host in question
1495 * Lock status: No locks are assumed held.
1497 * Notes: There is no timer nor any other means by which the requests
1498 * get unblocked other than the low-level driver calling
1499 * scsi_unblock_requests().
1501 * This is done as an API function so that changes to the
1502 * internals of the scsi mid-layer won't require wholesale
1503 * changes to drivers that use this feature.
1505 void scsi_unblock_requests(struct Scsi_Host *shost)
1507 shost->host_self_blocked = 0;
1508 scsi_run_host_queues(shost);
1510 EXPORT_SYMBOL(scsi_unblock_requests);
1512 int __init scsi_init_queue(void)
1516 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1517 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1518 int size = sgp->size * sizeof(struct scatterlist);
1520 sgp->slab = kmem_cache_create(sgp->name, size, 0,
1521 SLAB_HWCACHE_ALIGN, NULL, NULL);
1523 printk(KERN_ERR "SCSI: can't init sg slab %s\n",
1527 sgp->pool = mempool_create(SG_MEMPOOL_SIZE,
1528 mempool_alloc_slab, mempool_free_slab,
1531 printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
1539 void scsi_exit_queue(void)
1543 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1544 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1545 mempool_destroy(sgp->pool);
1546 kmem_cache_destroy(sgp->slab);
1550 * __scsi_mode_sense - issue a mode sense, falling back from 10 to
1551 * six bytes if necessary.
1552 * @sreq: SCSI request to fill in with the MODE_SENSE
1553 * @dbd: set if mode sense will allow block descriptors to be returned
1554 * @modepage: mode page being requested
1555 * @buffer: request buffer (may not be smaller than eight bytes)
1556 * @len: length of request buffer.
1557 * @timeout: command timeout
1558 * @retries: number of retries before failing
1559 * @data: returns a structure abstracting the mode header data
1561 * Returns zero if unsuccessful, or the header offset (either 4
1562 * or 8 depending on whether a six or ten byte command was
1563 * issued) if successful.
1566 __scsi_mode_sense(struct scsi_request *sreq, int dbd, int modepage,
1567 unsigned char *buffer, int len, int timeout, int retries,
1568 struct scsi_mode_data *data) {
1569 unsigned char cmd[12];
1573 memset(data, 0, sizeof(*data));
1574 memset(&cmd[0], 0, 12);
1575 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
1579 use_10_for_ms = sreq->sr_device->use_10_for_ms;
1581 if (use_10_for_ms) {
1585 cmd[0] = MODE_SENSE_10;
1592 cmd[0] = MODE_SENSE;
1597 sreq->sr_cmd_len = 0;
1598 memset(sreq->sr_sense_buffer, 0, sizeof(sreq->sr_sense_buffer));
1599 sreq->sr_data_direction = DMA_FROM_DEVICE;
1601 memset(buffer, 0, len);
1603 scsi_wait_req(sreq, cmd, buffer, len, timeout, retries);
1605 /* This code looks awful: what it's doing is making sure an
1606 * ILLEGAL REQUEST sense return identifies the actual command
1607 * byte as the problem. MODE_SENSE commands can return
1608 * ILLEGAL REQUEST if the code page isn't supported */
1610 if (use_10_for_ms && !scsi_status_is_good(sreq->sr_result) &&
1611 (driver_byte(sreq->sr_result) & DRIVER_SENSE)) {
1612 struct scsi_sense_hdr sshdr;
1614 if (scsi_request_normalize_sense(sreq, &sshdr)) {
1615 if ((sshdr.sense_key == ILLEGAL_REQUEST) &&
1616 (sshdr.asc == 0x20) && (sshdr.ascq == 0)) {
1618 * Invalid command operation code
1620 sreq->sr_device->use_10_for_ms = 0;
1626 if(scsi_status_is_good(sreq->sr_result)) {
1627 data->header_length = header_length;
1629 data->length = buffer[0]*256 + buffer[1] + 2;
1630 data->medium_type = buffer[2];
1631 data->device_specific = buffer[3];
1632 data->longlba = buffer[4] & 0x01;
1633 data->block_descriptor_length = buffer[6]*256
1636 data->length = buffer[0] + 1;
1637 data->medium_type = buffer[1];
1638 data->device_specific = buffer[2];
1639 data->block_descriptor_length = buffer[3];
1643 return sreq->sr_result;
1645 EXPORT_SYMBOL(__scsi_mode_sense);
1648 * scsi_mode_sense - issue a mode sense, falling back from 10 to
1649 * six bytes if necessary.
1650 * @sdev: scsi device to send command to.
1651 * @dbd: set if mode sense will disable block descriptors in the return
1652 * @modepage: mode page being requested
1653 * @buffer: request buffer (may not be smaller than eight bytes)
1654 * @len: length of request buffer.
1655 * @timeout: command timeout
1656 * @retries: number of retries before failing
1658 * Returns zero if unsuccessful, or the header offset (either 4
1659 * or 8 depending on whether a six or ten byte command was
1660 * issued) if successful.
1663 scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
1664 unsigned char *buffer, int len, int timeout, int retries,
1665 struct scsi_mode_data *data)
1667 struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
1673 ret = __scsi_mode_sense(sreq, dbd, modepage, buffer, len,
1674 timeout, retries, data);
1676 scsi_release_request(sreq);
1680 EXPORT_SYMBOL(scsi_mode_sense);
1683 scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
1685 struct scsi_request *sreq;
1687 TEST_UNIT_READY, 0, 0, 0, 0, 0,
1691 sreq = scsi_allocate_request(sdev, GFP_KERNEL);
1695 sreq->sr_data_direction = DMA_NONE;
1696 scsi_wait_req(sreq, cmd, NULL, 0, timeout, retries);
1698 if ((driver_byte(sreq->sr_result) & DRIVER_SENSE) && sdev->removable) {
1699 struct scsi_sense_hdr sshdr;
1701 if ((scsi_request_normalize_sense(sreq, &sshdr)) &&
1702 ((sshdr.sense_key == UNIT_ATTENTION) ||
1703 (sshdr.sense_key == NOT_READY))) {
1705 sreq->sr_result = 0;
1708 result = sreq->sr_result;
1709 scsi_release_request(sreq);
1712 EXPORT_SYMBOL(scsi_test_unit_ready);
1715 * scsi_device_set_state - Take the given device through the device
1717 * @sdev: scsi device to change the state of.
1718 * @state: state to change to.
1720 * Returns zero if unsuccessful or an error if the requested
1721 * transition is illegal.
1724 scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
1726 enum scsi_device_state oldstate = sdev->sdev_state;
1728 if (state == oldstate)
1733 /* There are no legal states that come back to
1734 * created. This is the manually initialised start
1804 sdev->sdev_state = state;
1808 SCSI_LOG_ERROR_RECOVERY(1,
1809 dev_printk(KERN_ERR, &sdev->sdev_gendev,
1810 "Illegal state transition %s->%s\n",
1811 scsi_device_state_name(oldstate),
1812 scsi_device_state_name(state))
1816 EXPORT_SYMBOL(scsi_device_set_state);
1819 * scsi_device_quiesce - Block user issued commands.
1820 * @sdev: scsi device to quiesce.
1822 * This works by trying to transition to the SDEV_QUIESCE state
1823 * (which must be a legal transition). When the device is in this
1824 * state, only special requests will be accepted, all others will
1825 * be deferred. Since special requests may also be requeued requests,
1826 * a successful return doesn't guarantee the device will be
1827 * totally quiescent.
1829 * Must be called with user context, may sleep.
1831 * Returns zero if unsuccessful or an error if not.
1834 scsi_device_quiesce(struct scsi_device *sdev)
1836 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
1840 scsi_run_queue(sdev->request_queue);
1841 while (sdev->device_busy) {
1842 msleep_interruptible(200);
1843 scsi_run_queue(sdev->request_queue);
1847 EXPORT_SYMBOL(scsi_device_quiesce);
1850 * scsi_device_resume - Restart user issued commands to a quiesced device.
1851 * @sdev: scsi device to resume.
1853 * Moves the device from quiesced back to running and restarts the
1856 * Must be called with user context, may sleep.
1859 scsi_device_resume(struct scsi_device *sdev)
1861 if(scsi_device_set_state(sdev, SDEV_RUNNING))
1863 scsi_run_queue(sdev->request_queue);
1865 EXPORT_SYMBOL(scsi_device_resume);
1868 device_quiesce_fn(struct scsi_device *sdev, void *data)
1870 scsi_device_quiesce(sdev);
1874 scsi_target_quiesce(struct scsi_target *starget)
1876 starget_for_each_device(starget, NULL, device_quiesce_fn);
1878 EXPORT_SYMBOL(scsi_target_quiesce);
1881 device_resume_fn(struct scsi_device *sdev, void *data)
1883 scsi_device_resume(sdev);
1887 scsi_target_resume(struct scsi_target *starget)
1889 starget_for_each_device(starget, NULL, device_resume_fn);
1891 EXPORT_SYMBOL(scsi_target_resume);
1894 * scsi_internal_device_block - internal function to put a device
1895 * temporarily into the SDEV_BLOCK state
1896 * @sdev: device to block
1898 * Block request made by scsi lld's to temporarily stop all
1899 * scsi commands on the specified device. Called from interrupt
1900 * or normal process context.
1902 * Returns zero if successful or error if not
1905 * This routine transitions the device to the SDEV_BLOCK state
1906 * (which must be a legal transition). When the device is in this
1907 * state, all commands are deferred until the scsi lld reenables
1908 * the device with scsi_device_unblock or device_block_tmo fires.
1909 * This routine assumes the host_lock is held on entry.
1912 scsi_internal_device_block(struct scsi_device *sdev)
1914 request_queue_t *q = sdev->request_queue;
1915 unsigned long flags;
1918 err = scsi_device_set_state(sdev, SDEV_BLOCK);
1923 * The device has transitioned to SDEV_BLOCK. Stop the
1924 * block layer from calling the midlayer with this device's
1927 spin_lock_irqsave(q->queue_lock, flags);
1929 spin_unlock_irqrestore(q->queue_lock, flags);
1933 EXPORT_SYMBOL_GPL(scsi_internal_device_block);
1936 * scsi_internal_device_unblock - resume a device after a block request
1937 * @sdev: device to resume
1939 * Called by scsi lld's or the midlayer to restart the device queue
1940 * for the previously suspended scsi device. Called from interrupt or
1941 * normal process context.
1943 * Returns zero if successful or error if not.
1946 * This routine transitions the device to the SDEV_RUNNING state
1947 * (which must be a legal transition) allowing the midlayer to
1948 * goose the queue for this device. This routine assumes the
1949 * host_lock is held upon entry.
1952 scsi_internal_device_unblock(struct scsi_device *sdev)
1954 request_queue_t *q = sdev->request_queue;
1956 unsigned long flags;
1959 * Try to transition the scsi device to SDEV_RUNNING
1960 * and goose the device queue if successful.
1962 err = scsi_device_set_state(sdev, SDEV_RUNNING);
1966 spin_lock_irqsave(q->queue_lock, flags);
1968 spin_unlock_irqrestore(q->queue_lock, flags);
1972 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
1975 device_block(struct scsi_device *sdev, void *data)
1977 scsi_internal_device_block(sdev);
1981 target_block(struct device *dev, void *data)
1983 if (scsi_is_target_device(dev))
1984 starget_for_each_device(to_scsi_target(dev), NULL,
1990 scsi_target_block(struct device *dev)
1992 if (scsi_is_target_device(dev))
1993 starget_for_each_device(to_scsi_target(dev), NULL,
1996 device_for_each_child(dev, NULL, target_block);
1998 EXPORT_SYMBOL_GPL(scsi_target_block);
2001 device_unblock(struct scsi_device *sdev, void *data)
2003 scsi_internal_device_unblock(sdev);
2007 target_unblock(struct device *dev, void *data)
2009 if (scsi_is_target_device(dev))
2010 starget_for_each_device(to_scsi_target(dev), NULL,
2016 scsi_target_unblock(struct device *dev)
2018 if (scsi_is_target_device(dev))
2019 starget_for_each_device(to_scsi_target(dev), NULL,
2022 device_for_each_child(dev, NULL, target_unblock);
2024 EXPORT_SYMBOL_GPL(scsi_target_unblock);