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>
19 #include <linux/hardirq.h>
20 #include <linux/scatterlist.h>
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_cmnd.h>
24 #include <scsi/scsi_dbg.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_driver.h>
27 #include <scsi/scsi_eh.h>
28 #include <scsi/scsi_host.h>
30 #include "scsi_priv.h"
31 #include "scsi_logging.h"
34 #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)
35 #define SG_MEMPOOL_SIZE 2
37 struct scsi_host_sg_pool
{
40 struct kmem_cache
*slab
;
44 #if (SCSI_MAX_PHYS_SEGMENTS < 32)
45 #error SCSI_MAX_PHYS_SEGMENTS is too small
48 #define SP(x) { x, "sgpool-" #x }
49 static struct scsi_host_sg_pool scsi_sg_pools
[] = {
53 #if (SCSI_MAX_PHYS_SEGMENTS > 32)
55 #if (SCSI_MAX_PHYS_SEGMENTS > 64)
57 #if (SCSI_MAX_PHYS_SEGMENTS > 128)
59 #if (SCSI_MAX_PHYS_SEGMENTS > 256)
60 #error SCSI_MAX_PHYS_SEGMENTS is too large
68 static void scsi_run_queue(struct request_queue
*q
);
71 * Function: scsi_unprep_request()
73 * Purpose: Remove all preparation done for a request, including its
74 * associated scsi_cmnd, so that it can be requeued.
76 * Arguments: req - request to unprepare
78 * Lock status: Assumed that no locks are held upon entry.
82 static void scsi_unprep_request(struct request
*req
)
84 struct scsi_cmnd
*cmd
= req
->special
;
86 req
->cmd_flags
&= ~REQ_DONTPREP
;
89 scsi_put_command(cmd
);
93 * __scsi_queue_insert - private queue insertion
94 * @cmd: The SCSI command being requeued
95 * @reason: The reason for the requeue
96 * @unbusy: Whether the queue should be unbusied
98 * This is a private queue insertion. The public interface
99 * scsi_queue_insert() always assumes the queue should be unbusied
100 * because it's always called before the completion. This function is
101 * for a requeue after completion, which should only occur in this
104 static int __scsi_queue_insert(struct scsi_cmnd
*cmd
, int reason
, int unbusy
)
106 struct Scsi_Host
*host
= cmd
->device
->host
;
107 struct scsi_device
*device
= cmd
->device
;
108 struct request_queue
*q
= device
->request_queue
;
112 printk("Inserting command %p into mlqueue\n", cmd
));
115 * Set the appropriate busy bit for the device/host.
117 * If the host/device isn't busy, assume that something actually
118 * completed, and that we should be able to queue a command now.
120 * Note that the prior mid-layer assumption that any host could
121 * always queue at least one command is now broken. The mid-layer
122 * will implement a user specifiable stall (see
123 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
124 * if a command is requeued with no other commands outstanding
125 * either for the device or for the host.
127 if (reason
== SCSI_MLQUEUE_HOST_BUSY
)
128 host
->host_blocked
= host
->max_host_blocked
;
129 else if (reason
== SCSI_MLQUEUE_DEVICE_BUSY
)
130 device
->device_blocked
= device
->max_device_blocked
;
133 * Decrement the counters, since these commands are no longer
134 * active on the host/device.
137 scsi_device_unbusy(device
);
140 * Requeue this command. It will go before all other commands
141 * that are already in the queue.
143 * NOTE: there is magic here about the way the queue is plugged if
144 * we have no outstanding commands.
146 * Although we *don't* plug the queue, we call the request
147 * function. The SCSI request function detects the blocked condition
148 * and plugs the queue appropriately.
150 spin_lock_irqsave(q
->queue_lock
, flags
);
151 blk_requeue_request(q
, cmd
->request
);
152 spin_unlock_irqrestore(q
->queue_lock
, flags
);
160 * Function: scsi_queue_insert()
162 * Purpose: Insert a command in the midlevel queue.
164 * Arguments: cmd - command that we are adding to queue.
165 * reason - why we are inserting command to queue.
167 * Lock status: Assumed that lock is not held upon entry.
171 * Notes: We do this for one of two cases. Either the host is busy
172 * and it cannot accept any more commands for the time being,
173 * or the device returned QUEUE_FULL and can accept no more
175 * Notes: This could be called either from an interrupt context or a
176 * normal process context.
178 int scsi_queue_insert(struct scsi_cmnd
*cmd
, int reason
)
180 return __scsi_queue_insert(cmd
, reason
, 1);
183 * scsi_execute - insert request and wait for the result
186 * @data_direction: data direction
187 * @buffer: data buffer
188 * @bufflen: len of buffer
189 * @sense: optional sense buffer
190 * @timeout: request timeout in seconds
191 * @retries: number of times to retry request
192 * @flags: or into request flags;
194 * returns the req->errors value which is the scsi_cmnd result
197 int scsi_execute(struct scsi_device
*sdev
, const unsigned char *cmd
,
198 int data_direction
, void *buffer
, unsigned bufflen
,
199 unsigned char *sense
, int timeout
, int retries
, int flags
)
202 int write
= (data_direction
== DMA_TO_DEVICE
);
203 int ret
= DRIVER_ERROR
<< 24;
205 req
= blk_get_request(sdev
->request_queue
, write
, __GFP_WAIT
);
207 if (bufflen
&& blk_rq_map_kern(sdev
->request_queue
, req
,
208 buffer
, bufflen
, __GFP_WAIT
))
211 req
->cmd_len
= COMMAND_SIZE(cmd
[0]);
212 memcpy(req
->cmd
, cmd
, req
->cmd_len
);
215 req
->retries
= retries
;
216 req
->timeout
= timeout
;
217 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
218 req
->cmd_flags
|= flags
| REQ_QUIET
| REQ_PREEMPT
;
221 * head injection *required* here otherwise quiesce won't work
223 blk_execute_rq(req
->q
, NULL
, req
, 1);
226 * Some devices (USB mass-storage in particular) may transfer
227 * garbage data together with a residue indicating that the data
228 * is invalid. Prevent the garbage from being misinterpreted
229 * and prevent security leaks by zeroing out the excess data.
231 if (unlikely(req
->data_len
> 0 && req
->data_len
<= bufflen
))
232 memset(buffer
+ (bufflen
- req
->data_len
), 0, req
->data_len
);
236 blk_put_request(req
);
240 EXPORT_SYMBOL(scsi_execute
);
243 int scsi_execute_req(struct scsi_device
*sdev
, const unsigned char *cmd
,
244 int data_direction
, void *buffer
, unsigned bufflen
,
245 struct scsi_sense_hdr
*sshdr
, int timeout
, int retries
)
251 sense
= kzalloc(SCSI_SENSE_BUFFERSIZE
, GFP_NOIO
);
253 return DRIVER_ERROR
<< 24;
255 result
= scsi_execute(sdev
, cmd
, data_direction
, buffer
, bufflen
,
256 sense
, timeout
, retries
, 0);
258 scsi_normalize_sense(sense
, SCSI_SENSE_BUFFERSIZE
, sshdr
);
263 EXPORT_SYMBOL(scsi_execute_req
);
265 struct scsi_io_context
{
267 void (*done
)(void *data
, char *sense
, int result
, int resid
);
268 char sense
[SCSI_SENSE_BUFFERSIZE
];
271 static struct kmem_cache
*scsi_io_context_cache
;
273 static void scsi_end_async(struct request
*req
, int uptodate
)
275 struct scsi_io_context
*sioc
= req
->end_io_data
;
278 sioc
->done(sioc
->data
, sioc
->sense
, req
->errors
, req
->data_len
);
280 kmem_cache_free(scsi_io_context_cache
, sioc
);
281 __blk_put_request(req
->q
, req
);
284 static int scsi_merge_bio(struct request
*rq
, struct bio
*bio
)
286 struct request_queue
*q
= rq
->q
;
288 bio
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
289 if (rq_data_dir(rq
) == WRITE
)
290 bio
->bi_rw
|= (1 << BIO_RW
);
291 blk_queue_bounce(q
, &bio
);
294 blk_rq_bio_prep(q
, rq
, bio
);
295 else if (!ll_back_merge_fn(q
, rq
, bio
))
298 rq
->biotail
->bi_next
= bio
;
305 static int scsi_bi_endio(struct bio
*bio
, unsigned int bytes_done
, int error
)
315 * scsi_req_map_sg - map a scatterlist into a request
316 * @rq: request to fill
318 * @nsegs: number of elements
319 * @bufflen: len of buffer
320 * @gfp: memory allocation flags
322 * scsi_req_map_sg maps a scatterlist into a request so that the
323 * request can be sent to the block layer. We do not trust the scatterlist
324 * sent to use, as some ULDs use that struct to only organize the pages.
326 static int scsi_req_map_sg(struct request
*rq
, struct scatterlist
*sgl
,
327 int nsegs
, unsigned bufflen
, gfp_t gfp
)
329 struct request_queue
*q
= rq
->q
;
330 int nr_pages
= (bufflen
+ sgl
[0].offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
331 unsigned int data_len
= 0, len
, bytes
, off
;
332 struct scatterlist
*sg
;
334 struct bio
*bio
= NULL
;
335 int i
, err
, nr_vecs
= 0;
337 for_each_sg(sgl
, sg
, nsegs
, i
) {
344 bytes
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
347 nr_vecs
= min_t(int, BIO_MAX_PAGES
, nr_pages
);
350 bio
= bio_alloc(gfp
, nr_vecs
);
355 bio
->bi_end_io
= scsi_bi_endio
;
358 if (bio_add_pc_page(q
, bio
, page
, bytes
, off
) !=
365 if (bio
->bi_vcnt
>= nr_vecs
) {
366 err
= scsi_merge_bio(rq
, bio
);
368 bio_endio(bio
, bio
->bi_size
, 0);
380 rq
->buffer
= rq
->data
= NULL
;
381 rq
->data_len
= data_len
;
385 while ((bio
= rq
->bio
) != NULL
) {
386 rq
->bio
= bio
->bi_next
;
388 * call endio instead of bio_put incase it was bounced
390 bio_endio(bio
, bio
->bi_size
, 0);
397 * scsi_execute_async - insert request
400 * @cmd_len: length of scsi cdb
401 * @data_direction: data direction
402 * @buffer: data buffer (this can be a kernel buffer or scatterlist)
403 * @bufflen: len of buffer
404 * @use_sg: if buffer is a scatterlist this is the number of elements
405 * @timeout: request timeout in seconds
406 * @retries: number of times to retry request
407 * @flags: or into request flags
409 int scsi_execute_async(struct scsi_device
*sdev
, const unsigned char *cmd
,
410 int cmd_len
, int data_direction
, void *buffer
, unsigned bufflen
,
411 int use_sg
, int timeout
, int retries
, void *privdata
,
412 void (*done
)(void *, char *, int, int), gfp_t gfp
)
415 struct scsi_io_context
*sioc
;
417 int write
= (data_direction
== DMA_TO_DEVICE
);
419 sioc
= kmem_cache_zalloc(scsi_io_context_cache
, gfp
);
421 return DRIVER_ERROR
<< 24;
423 req
= blk_get_request(sdev
->request_queue
, write
, gfp
);
426 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
427 req
->cmd_flags
|= REQ_QUIET
;
430 err
= scsi_req_map_sg(req
, buffer
, use_sg
, bufflen
, gfp
);
432 err
= blk_rq_map_kern(req
->q
, req
, buffer
, bufflen
, gfp
);
437 req
->cmd_len
= cmd_len
;
438 memset(req
->cmd
, 0, BLK_MAX_CDB
); /* ATAPI hates garbage after CDB */
439 memcpy(req
->cmd
, cmd
, req
->cmd_len
);
440 req
->sense
= sioc
->sense
;
442 req
->timeout
= timeout
;
443 req
->retries
= retries
;
444 req
->end_io_data
= sioc
;
446 sioc
->data
= privdata
;
449 blk_execute_rq_nowait(req
->q
, NULL
, req
, 1, scsi_end_async
);
453 blk_put_request(req
);
455 kmem_cache_free(scsi_io_context_cache
, sioc
);
456 return DRIVER_ERROR
<< 24;
458 EXPORT_SYMBOL_GPL(scsi_execute_async
);
461 * Function: scsi_init_cmd_errh()
463 * Purpose: Initialize cmd fields related to error handling.
465 * Arguments: cmd - command that is ready to be queued.
467 * Notes: This function has the job of initializing a number of
468 * fields related to error handling. Typically this will
469 * be called once for each command, as required.
471 static void scsi_init_cmd_errh(struct scsi_cmnd
*cmd
)
473 cmd
->serial_number
= 0;
475 memset(cmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
476 if (cmd
->cmd_len
== 0)
477 cmd
->cmd_len
= COMMAND_SIZE(cmd
->cmnd
[0]);
480 void scsi_device_unbusy(struct scsi_device
*sdev
)
482 struct Scsi_Host
*shost
= sdev
->host
;
485 spin_lock_irqsave(shost
->host_lock
, flags
);
487 if (unlikely(scsi_host_in_recovery(shost
) &&
488 (shost
->host_failed
|| shost
->host_eh_scheduled
)))
489 scsi_eh_wakeup(shost
);
490 spin_unlock(shost
->host_lock
);
491 spin_lock(sdev
->request_queue
->queue_lock
);
493 spin_unlock_irqrestore(sdev
->request_queue
->queue_lock
, flags
);
497 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
498 * and call blk_run_queue for all the scsi_devices on the target -
499 * including current_sdev first.
501 * Called with *no* scsi locks held.
503 static void scsi_single_lun_run(struct scsi_device
*current_sdev
)
505 struct Scsi_Host
*shost
= current_sdev
->host
;
506 struct scsi_device
*sdev
, *tmp
;
507 struct scsi_target
*starget
= scsi_target(current_sdev
);
510 spin_lock_irqsave(shost
->host_lock
, flags
);
511 starget
->starget_sdev_user
= NULL
;
512 spin_unlock_irqrestore(shost
->host_lock
, flags
);
515 * Call blk_run_queue for all LUNs on the target, starting with
516 * current_sdev. We race with others (to set starget_sdev_user),
517 * but in most cases, we will be first. Ideally, each LU on the
518 * target would get some limited time or requests on the target.
520 blk_run_queue(current_sdev
->request_queue
);
522 spin_lock_irqsave(shost
->host_lock
, flags
);
523 if (starget
->starget_sdev_user
)
525 list_for_each_entry_safe(sdev
, tmp
, &starget
->devices
,
526 same_target_siblings
) {
527 if (sdev
== current_sdev
)
529 if (scsi_device_get(sdev
))
532 spin_unlock_irqrestore(shost
->host_lock
, flags
);
533 blk_run_queue(sdev
->request_queue
);
534 spin_lock_irqsave(shost
->host_lock
, flags
);
536 scsi_device_put(sdev
);
539 spin_unlock_irqrestore(shost
->host_lock
, flags
);
543 * Function: scsi_run_queue()
545 * Purpose: Select a proper request queue to serve next
547 * Arguments: q - last request's queue
551 * Notes: The previous command was completely finished, start
552 * a new one if possible.
554 static void scsi_run_queue(struct request_queue
*q
)
556 struct scsi_device
*sdev
= q
->queuedata
;
557 struct Scsi_Host
*shost
= sdev
->host
;
560 if (sdev
->single_lun
)
561 scsi_single_lun_run(sdev
);
563 spin_lock_irqsave(shost
->host_lock
, flags
);
564 while (!list_empty(&shost
->starved_list
) &&
565 !shost
->host_blocked
&& !shost
->host_self_blocked
&&
566 !((shost
->can_queue
> 0) &&
567 (shost
->host_busy
>= shost
->can_queue
))) {
569 * As long as shost is accepting commands and we have
570 * starved queues, call blk_run_queue. scsi_request_fn
571 * drops the queue_lock and can add us back to the
574 * host_lock protects the starved_list and starved_entry.
575 * scsi_request_fn must get the host_lock before checking
576 * or modifying starved_list or starved_entry.
578 sdev
= list_entry(shost
->starved_list
.next
,
579 struct scsi_device
, starved_entry
);
580 list_del_init(&sdev
->starved_entry
);
581 spin_unlock_irqrestore(shost
->host_lock
, flags
);
584 if (test_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
) &&
585 !test_and_set_bit(QUEUE_FLAG_REENTER
,
586 &sdev
->request_queue
->queue_flags
)) {
587 blk_run_queue(sdev
->request_queue
);
588 clear_bit(QUEUE_FLAG_REENTER
,
589 &sdev
->request_queue
->queue_flags
);
591 blk_run_queue(sdev
->request_queue
);
593 spin_lock_irqsave(shost
->host_lock
, flags
);
594 if (unlikely(!list_empty(&sdev
->starved_entry
)))
596 * sdev lost a race, and was put back on the
597 * starved list. This is unlikely but without this
598 * in theory we could loop forever.
602 spin_unlock_irqrestore(shost
->host_lock
, flags
);
608 * Function: scsi_requeue_command()
610 * Purpose: Handle post-processing of completed commands.
612 * Arguments: q - queue to operate on
613 * cmd - command that may need to be requeued.
617 * Notes: After command completion, there may be blocks left
618 * over which weren't finished by the previous command
619 * this can be for a number of reasons - the main one is
620 * I/O errors in the middle of the request, in which case
621 * we need to request the blocks that come after the bad
623 * Notes: Upon return, cmd is a stale pointer.
625 static void scsi_requeue_command(struct request_queue
*q
, struct scsi_cmnd
*cmd
)
627 struct request
*req
= cmd
->request
;
630 spin_lock_irqsave(q
->queue_lock
, flags
);
631 scsi_unprep_request(req
);
632 blk_requeue_request(q
, req
);
633 spin_unlock_irqrestore(q
->queue_lock
, flags
);
638 void scsi_next_command(struct scsi_cmnd
*cmd
)
640 struct scsi_device
*sdev
= cmd
->device
;
641 struct request_queue
*q
= sdev
->request_queue
;
643 /* need to hold a reference on the device before we let go of the cmd */
644 get_device(&sdev
->sdev_gendev
);
646 scsi_put_command(cmd
);
649 /* ok to remove device now */
650 put_device(&sdev
->sdev_gendev
);
653 void scsi_run_host_queues(struct Scsi_Host
*shost
)
655 struct scsi_device
*sdev
;
657 shost_for_each_device(sdev
, shost
)
658 scsi_run_queue(sdev
->request_queue
);
661 static void scsi_release_buffers(struct scsi_cmnd
*);
664 * Function: scsi_end_request()
666 * Purpose: Post-processing of completed commands (usually invoked at end
667 * of upper level post-processing and scsi_io_completion).
669 * Arguments: cmd - command that is complete.
670 * error - 0 if I/O indicates success, < 0 for I/O error.
671 * bytes - number of bytes of completed I/O
672 * requeue - indicates whether we should requeue leftovers.
674 * Lock status: Assumed that lock is not held upon entry.
676 * Returns: cmd if requeue required, NULL otherwise.
678 * Notes: This is called for block device requests in order to
679 * mark some number of sectors as complete.
681 * We are guaranteeing that the request queue will be goosed
682 * at some point during this call.
683 * Notes: If cmd was requeued, upon return it will be a stale pointer.
685 static struct scsi_cmnd
*scsi_end_request(struct scsi_cmnd
*cmd
, int error
,
686 int bytes
, int requeue
)
688 request_queue_t
*q
= cmd
->device
->request_queue
;
689 struct request
*req
= cmd
->request
;
692 * If there are blocks left over at the end, set up the command
693 * to queue the remainder of them.
695 if (blk_end_request(req
, error
, bytes
)) {
696 int leftover
= (req
->hard_nr_sectors
<< 9);
698 if (blk_pc_request(req
))
699 leftover
= req
->data_len
;
701 /* kill remainder if no retrys */
702 if (error
&& blk_noretry_request(req
))
703 blk_end_request(req
, error
, leftover
);
707 * Bleah. Leftovers again. Stick the
708 * leftovers in the front of the
709 * queue, and goose the queue again.
711 scsi_release_buffers(cmd
);
712 scsi_requeue_command(q
, cmd
);
720 * This will goose the queue request function at the end, so we don't
721 * need to worry about launching another command.
723 scsi_release_buffers(cmd
);
724 scsi_next_command(cmd
);
728 struct scatterlist
*scsi_alloc_sgtable(struct scsi_cmnd
*cmd
, gfp_t gfp_mask
)
730 struct scsi_host_sg_pool
*sgp
;
731 struct scatterlist
*sgl
;
733 BUG_ON(!cmd
->use_sg
);
735 switch (cmd
->use_sg
) {
745 #if (SCSI_MAX_PHYS_SEGMENTS > 32)
749 #if (SCSI_MAX_PHYS_SEGMENTS > 64)
753 #if (SCSI_MAX_PHYS_SEGMENTS > 128)
764 sgp
= scsi_sg_pools
+ cmd
->sglist_len
;
765 sgl
= mempool_alloc(sgp
->pool
, gfp_mask
);
769 EXPORT_SYMBOL(scsi_alloc_sgtable
);
771 void scsi_free_sgtable(struct scsi_cmnd
*cmd
)
773 struct scatterlist
*sgl
= cmd
->request_buffer
;
774 struct scsi_host_sg_pool
*sgp
;
776 BUG_ON(cmd
->sglist_len
>= SG_MEMPOOL_NR
);
778 sgp
= scsi_sg_pools
+ cmd
->sglist_len
;
779 mempool_free(sgl
, sgp
->pool
);
782 EXPORT_SYMBOL(scsi_free_sgtable
);
785 * Function: scsi_release_buffers()
787 * Purpose: Completion processing for block device I/O requests.
789 * Arguments: cmd - command that we are bailing.
791 * Lock status: Assumed that no lock is held upon entry.
795 * Notes: In the event that an upper level driver rejects a
796 * command, we must release resources allocated during
797 * the __init_io() function. Primarily this would involve
798 * the scatter-gather table, and potentially any bounce
801 static void scsi_release_buffers(struct scsi_cmnd
*cmd
)
804 scsi_free_sgtable(cmd
);
807 * Zero these out. They now point to freed memory, and it is
808 * dangerous to hang onto the pointers.
810 cmd
->request_buffer
= NULL
;
811 cmd
->request_bufflen
= 0;
815 * Function: scsi_io_completion()
817 * Purpose: Completion processing for block device I/O requests.
819 * Arguments: cmd - command that is finished.
821 * Lock status: Assumed that no lock is held upon entry.
825 * Notes: This function is matched in terms of capabilities to
826 * the function that created the scatter-gather list.
827 * In other words, if there are no bounce buffers
828 * (the normal case for most drivers), we don't need
829 * the logic to deal with cleaning up afterwards.
831 * We must call scsi_end_request(). This will finish off
832 * the specified number of sectors. If we are done, the
833 * command block will be released and the queue function
834 * will be goosed. If we are not done then we have to
835 * figure out what to do next:
837 * a) We can call scsi_requeue_command(). The request
838 * will be unprepared and put back on the queue. Then
839 * a new command will be created for it. This should
840 * be used if we made forward progress, or if we want
841 * to switch from READ(10) to READ(6) for example.
843 * b) We can call scsi_queue_insert(). The request will
844 * be put back on the queue and retried using the same
845 * command as before, possibly after a delay.
847 * c) We can call blk_end_request() with -EIO to fail
848 * the remainder of the request.
850 void scsi_io_completion(struct scsi_cmnd
*cmd
, unsigned int good_bytes
)
852 int result
= cmd
->result
;
853 request_queue_t
*q
= cmd
->device
->request_queue
;
854 struct request
*req
= cmd
->request
;
856 struct scsi_sense_hdr sshdr
;
858 int sense_deferred
= 0;
859 enum {ACTION_FAIL
, ACTION_REPREP
, ACTION_RETRY
,
860 ACTION_DELAYED_RETRY
} action
;
861 char *description
= NULL
;
864 sense_valid
= scsi_command_normalize_sense(cmd
, &sshdr
);
866 sense_deferred
= scsi_sense_is_deferred(&sshdr
);
869 if (blk_pc_request(req
)) { /* SG_IO ioctl from block level */
870 req
->errors
= result
;
872 if (sense_valid
&& req
->sense
) {
874 * SG_IO wants current and deferred errors
876 int len
= 8 + cmd
->sense_buffer
[7];
878 if (len
> SCSI_SENSE_BUFFERSIZE
)
879 len
= SCSI_SENSE_BUFFERSIZE
;
880 memcpy(req
->sense
, cmd
->sense_buffer
, len
);
881 req
->sense_len
= len
;
886 req
->data_len
= cmd
->resid
;
890 * Next deal with any sectors which we were able to correctly
893 SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, "
895 req
->nr_sectors
, good_bytes
));
898 * Recovered errors need reporting, but they're always treated
899 * as success, so fiddle the result code here. For BLOCK_PC
900 * we already took a copy of the original into rq->errors which
901 * is what gets returned to the user
903 if (sense_valid
&& (sshdr
.sense_key
== RECOVERED_ERROR
)) {
904 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
905 * print since caller wants ATA registers. Only occurs on
906 * SCSI ATA PASS_THROUGH commands when CK_COND=1
908 if ((sshdr
.asc
== 0x0) && (sshdr
.ascq
== 0x1d))
910 else if (!(req
->cmd_flags
& REQ_QUIET
))
911 scsi_print_sense("", cmd
);
913 /* BLOCK_PC may have set error */
918 * A number of bytes were successfully read. If there
919 * are leftovers and there is some kind of error
920 * (result != 0), retry the rest.
922 if (scsi_end_request(cmd
, error
, good_bytes
, result
== 0) == NULL
)
927 if (host_byte(result
) == DID_RESET
) {
928 /* Third party bus reset or reset for error recovery
929 * reasons. Just retry the command and see what
932 action
= ACTION_RETRY
;
933 } else if (sense_valid
&& !sense_deferred
) {
934 switch (sshdr
.sense_key
) {
936 if (cmd
->device
->removable
) {
937 /* Detected disc change. Set a bit
938 * and quietly refuse further access.
940 cmd
->device
->changed
= 1;
941 description
= "Media Changed";
942 action
= ACTION_FAIL
;
944 /* Must have been a power glitch, or a
945 * bus reset. Could not have been a
946 * media change, so we just retry the
947 * command and see what happens.
949 action
= ACTION_RETRY
;
952 case ILLEGAL_REQUEST
:
953 /* If we had an ILLEGAL REQUEST returned, then
954 * we may have performed an unsupported
955 * command. The only thing this should be
956 * would be a ten byte read where only a six
957 * byte read was supported. Also, on a system
958 * where READ CAPACITY failed, we may have
959 * read past the end of the disk.
961 if ((cmd
->device
->use_10_for_rw
&&
962 sshdr
.asc
== 0x20 && sshdr
.ascq
== 0x00) &&
963 (cmd
->cmnd
[0] == READ_10
||
964 cmd
->cmnd
[0] == WRITE_10
)) {
965 /* This will issue a new 6-byte command. */
966 cmd
->device
->use_10_for_rw
= 0;
967 action
= ACTION_REPREP
;
968 } else if (sshdr
.asc
== 0x10) /* DIX */ {
969 description
= "Host Data Integrity Failure";
970 action
= ACTION_FAIL
;
973 action
= ACTION_FAIL
;
975 case ABORTED_COMMAND
:
976 action
= ACTION_FAIL
;
977 if (sshdr
.asc
== 0x10) { /* DIF */
978 description
= "Target Data Integrity Failure";
983 /* If the device is in the process of becoming
984 * ready, or has a temporary blockage, retry.
986 if (sshdr
.asc
== 0x04) {
987 switch (sshdr
.ascq
) {
988 case 0x01: /* becoming ready */
989 case 0x04: /* format in progress */
990 case 0x05: /* rebuild in progress */
991 case 0x06: /* recalculation in progress */
992 case 0x07: /* operation in progress */
993 case 0x08: /* Long write in progress */
994 case 0x09: /* self test in progress */
995 case 0x14: /* space allocation in progress */
996 action
= ACTION_DELAYED_RETRY
;
999 description
= "Device not ready";
1000 action
= ACTION_FAIL
;
1004 description
= "Device not ready";
1005 action
= ACTION_FAIL
;
1008 case VOLUME_OVERFLOW
:
1009 /* See SSC3rXX or current. */
1010 action
= ACTION_FAIL
;
1013 description
= "Unhandled sense code";
1014 action
= ACTION_FAIL
;
1018 description
= "Unhandled error code";
1019 action
= ACTION_FAIL
;
1024 /* Give up and fail the remainder of the request */
1025 scsi_release_buffers(cmd
);
1026 if (!(req
->cmd_flags
& REQ_QUIET
)) {
1028 scmd_printk(KERN_INFO
, cmd
, "%s\n",
1030 scsi_print_result(cmd
);
1031 if (driver_byte(result
) & DRIVER_SENSE
)
1032 scsi_print_sense("", cmd
);
1033 scsi_print_command(cmd
);
1035 blk_end_request(req
, error
, blk_rq_bytes(req
));
1036 scsi_next_command(cmd
);
1039 /* Unprep the request and put it back at the head of the queue.
1040 * A new command will be prepared and issued.
1042 scsi_release_buffers(cmd
);
1043 scsi_requeue_command(q
, cmd
);
1046 /* Retry the same command immediately */
1047 __scsi_queue_insert(cmd
, SCSI_MLQUEUE_EH_RETRY
, 0);
1049 case ACTION_DELAYED_RETRY
:
1050 /* Retry the same command after a delay */
1051 __scsi_queue_insert(cmd
, SCSI_MLQUEUE_DEVICE_BUSY
, 0);
1055 EXPORT_SYMBOL(scsi_io_completion
);
1058 * Function: scsi_init_io()
1060 * Purpose: SCSI I/O initialize function.
1062 * Arguments: cmd - Command descriptor we wish to initialize
1064 * Returns: 0 on success
1065 * BLKPREP_DEFER if the failure is retryable
1066 * BLKPREP_KILL if the failure is fatal
1068 static int scsi_init_io(struct scsi_cmnd
*cmd
)
1070 struct request
*req
= cmd
->request
;
1071 struct scatterlist
*sgpnt
;
1075 * We used to not use scatter-gather for single segment request,
1076 * but now we do (it makes highmem I/O easier to support without
1079 cmd
->use_sg
= req
->nr_phys_segments
;
1082 * If sg table allocation fails, requeue request later.
1084 sgpnt
= scsi_alloc_sgtable(cmd
, GFP_ATOMIC
);
1085 if (unlikely(!sgpnt
)) {
1086 scsi_unprep_request(req
);
1087 return BLKPREP_DEFER
;
1091 cmd
->request_buffer
= (char *) sgpnt
;
1092 if (blk_pc_request(req
))
1093 cmd
->request_bufflen
= req
->data_len
;
1095 cmd
->request_bufflen
= req
->nr_sectors
<< 9;
1098 * Next, walk the list, and fill in the addresses and sizes of
1101 count
= blk_rq_map_sg(req
->q
, req
, cmd
->request_buffer
);
1102 if (likely(count
<= cmd
->use_sg
)) {
1103 cmd
->use_sg
= count
;
1107 printk(KERN_ERR
"Incorrect number of segments after building list\n");
1108 printk(KERN_ERR
"counted %d, received %d\n", count
, cmd
->use_sg
);
1109 printk(KERN_ERR
"req nr_sec %lu, cur_nr_sec %u\n", req
->nr_sectors
,
1110 req
->current_nr_sectors
);
1112 /* release the command and kill it */
1113 scsi_release_buffers(cmd
);
1114 scsi_put_command(cmd
);
1115 return BLKPREP_KILL
;
1118 static int scsi_issue_flush_fn(request_queue_t
*q
, struct gendisk
*disk
,
1119 sector_t
*error_sector
)
1121 struct scsi_device
*sdev
= q
->queuedata
;
1122 struct scsi_driver
*drv
;
1124 if (sdev
->sdev_state
!= SDEV_RUNNING
)
1127 drv
= *(struct scsi_driver
**) disk
->private_data
;
1128 if (drv
->issue_flush
)
1129 return drv
->issue_flush(&sdev
->sdev_gendev
, error_sector
);
1134 static struct scsi_cmnd
*scsi_get_cmd_from_req(struct scsi_device
*sdev
,
1135 struct request
*req
)
1137 struct scsi_cmnd
*cmd
;
1139 if (!req
->special
) {
1140 cmd
= scsi_get_command(sdev
, GFP_ATOMIC
);
1148 /* pull a tag out of the request if we have one */
1149 cmd
->tag
= req
->tag
;
1155 static void scsi_blk_pc_done(struct scsi_cmnd
*cmd
)
1157 BUG_ON(!blk_pc_request(cmd
->request
));
1159 * This will complete the whole command with uptodate=1 so
1160 * as far as the block layer is concerned the command completed
1161 * successfully. Since this is a REQ_BLOCK_PC command the
1162 * caller should check the request's errors value
1164 scsi_io_completion(cmd
, cmd
->request_bufflen
);
1167 static int scsi_setup_blk_pc_cmnd(struct scsi_device
*sdev
, struct request
*req
)
1169 struct scsi_cmnd
*cmd
;
1171 cmd
= scsi_get_cmd_from_req(sdev
, req
);
1173 return BLKPREP_DEFER
;
1176 * BLOCK_PC requests may transfer data, in which case they must
1177 * a bio attached to them. Or they might contain a SCSI command
1178 * that does not transfer data, in which case they may optionally
1179 * submit a request without an attached bio.
1184 BUG_ON(!req
->nr_phys_segments
);
1186 ret
= scsi_init_io(cmd
);
1190 BUG_ON(req
->data_len
);
1193 cmd
->request_bufflen
= 0;
1194 cmd
->request_buffer
= NULL
;
1199 BUILD_BUG_ON(sizeof(req
->cmd
) > sizeof(cmd
->cmnd
));
1200 memcpy(cmd
->cmnd
, req
->cmd
, sizeof(cmd
->cmnd
));
1201 cmd
->cmd_len
= req
->cmd_len
;
1203 cmd
->sc_data_direction
= DMA_NONE
;
1204 else if (rq_data_dir(req
) == WRITE
)
1205 cmd
->sc_data_direction
= DMA_TO_DEVICE
;
1207 cmd
->sc_data_direction
= DMA_FROM_DEVICE
;
1209 cmd
->transfersize
= req
->data_len
;
1210 cmd
->allowed
= req
->retries
;
1211 cmd
->timeout_per_command
= req
->timeout
;
1212 cmd
->done
= scsi_blk_pc_done
;
1217 * Setup a REQ_TYPE_FS command. These are simple read/write request
1218 * from filesystems that still need to be translated to SCSI CDBs from
1221 static int scsi_setup_fs_cmnd(struct scsi_device
*sdev
, struct request
*req
)
1223 struct scsi_cmnd
*cmd
;
1224 struct scsi_driver
*drv
;
1228 * Filesystem requests must transfer data.
1230 BUG_ON(!req
->nr_phys_segments
);
1232 cmd
= scsi_get_cmd_from_req(sdev
, req
);
1234 return BLKPREP_DEFER
;
1236 ret
= scsi_init_io(cmd
);
1241 * Initialize the actual SCSI command for this request.
1243 drv
= *(struct scsi_driver
**)req
->rq_disk
->private_data
;
1244 if (unlikely(!drv
->init_command(cmd
))) {
1245 scsi_release_buffers(cmd
);
1246 scsi_put_command(cmd
);
1247 return BLKPREP_KILL
;
1253 static int scsi_prep_fn(struct request_queue
*q
, struct request
*req
)
1255 struct scsi_device
*sdev
= q
->queuedata
;
1256 int ret
= BLKPREP_OK
;
1259 * If the device is not in running state we will reject some
1262 if (unlikely(sdev
->sdev_state
!= SDEV_RUNNING
)) {
1263 switch (sdev
->sdev_state
) {
1266 * If the device is offline we refuse to process any
1267 * commands. The device must be brought online
1268 * before trying any recovery commands.
1270 sdev_printk(KERN_ERR
, sdev
,
1271 "rejecting I/O to offline device\n");
1276 * If the device is fully deleted, we refuse to
1277 * process any commands as well.
1279 sdev_printk(KERN_ERR
, sdev
,
1280 "rejecting I/O to dead device\n");
1286 * If the devices is blocked we defer normal commands.
1288 if (!(req
->cmd_flags
& REQ_PREEMPT
))
1289 ret
= BLKPREP_DEFER
;
1293 * For any other not fully online state we only allow
1294 * special commands. In particular any user initiated
1295 * command is not allowed.
1297 if (!(req
->cmd_flags
& REQ_PREEMPT
))
1302 if (ret
!= BLKPREP_OK
)
1306 switch (req
->cmd_type
) {
1307 case REQ_TYPE_BLOCK_PC
:
1308 ret
= scsi_setup_blk_pc_cmnd(sdev
, req
);
1311 ret
= scsi_setup_fs_cmnd(sdev
, req
);
1315 * All other command types are not supported.
1317 * Note that these days the SCSI subsystem does not use
1318 * REQ_TYPE_SPECIAL requests anymore. These are only used
1319 * (directly or via blk_insert_request) by non-SCSI drivers.
1321 blk_dump_rq_flags(req
, "SCSI bad req");
1329 req
->errors
= DID_NO_CONNECT
<< 16;
1333 * If we defer, the elv_next_request() returns NULL, but the
1334 * queue must be restarted, so we plug here if no returning
1335 * command will automatically do that.
1337 if (sdev
->device_busy
== 0)
1341 req
->cmd_flags
|= REQ_DONTPREP
;
1348 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1351 * Called with the queue_lock held.
1353 static inline int scsi_dev_queue_ready(struct request_queue
*q
,
1354 struct scsi_device
*sdev
)
1356 if (sdev
->device_busy
>= sdev
->queue_depth
)
1358 if (sdev
->device_busy
== 0 && sdev
->device_blocked
) {
1360 * unblock after device_blocked iterates to zero
1362 if (--sdev
->device_blocked
== 0) {
1364 sdev_printk(KERN_INFO
, sdev
,
1365 "unblocking device at zero depth\n"));
1371 if (sdev
->device_blocked
)
1378 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1379 * return 0. We must end up running the queue again whenever 0 is
1380 * returned, else IO can hang.
1382 * Called with host_lock held.
1384 static inline int scsi_host_queue_ready(struct request_queue
*q
,
1385 struct Scsi_Host
*shost
,
1386 struct scsi_device
*sdev
)
1388 if (scsi_host_in_recovery(shost
))
1390 if (shost
->host_busy
== 0 && shost
->host_blocked
) {
1392 * unblock after host_blocked iterates to zero
1394 if (--shost
->host_blocked
== 0) {
1396 printk("scsi%d unblocking host at zero depth\n",
1403 if ((shost
->can_queue
> 0 && shost
->host_busy
>= shost
->can_queue
) ||
1404 shost
->host_blocked
|| shost
->host_self_blocked
) {
1405 if (list_empty(&sdev
->starved_entry
))
1406 list_add_tail(&sdev
->starved_entry
, &shost
->starved_list
);
1410 /* We're OK to process the command, so we can't be starved */
1411 if (!list_empty(&sdev
->starved_entry
))
1412 list_del_init(&sdev
->starved_entry
);
1418 * Kill a request for a dead device
1420 static void scsi_kill_request(struct request
*req
, request_queue_t
*q
)
1422 struct scsi_cmnd
*cmd
= req
->special
;
1423 struct scsi_device
*sdev
;
1424 struct Scsi_Host
*shost
;
1426 blkdev_dequeue_request(req
);
1428 if (unlikely(cmd
== NULL
)) {
1429 printk(KERN_CRIT
"impossible request in %s.\n",
1436 scsi_init_cmd_errh(cmd
);
1437 cmd
->result
= DID_NO_CONNECT
<< 16;
1438 atomic_inc(&cmd
->device
->iorequest_cnt
);
1441 * SCSI request completion path will do scsi_device_unbusy(),
1442 * bump busy counts. To bump the counters, we need to dance
1443 * with the locks as normal issue path does.
1445 sdev
->device_busy
++;
1446 spin_unlock(sdev
->request_queue
->queue_lock
);
1447 spin_lock(shost
->host_lock
);
1449 spin_unlock(shost
->host_lock
);
1450 spin_lock(sdev
->request_queue
->queue_lock
);
1455 static void scsi_softirq_done(struct request
*rq
)
1457 struct scsi_cmnd
*cmd
= rq
->completion_data
;
1458 unsigned long wait_for
= (cmd
->allowed
+ 1) * cmd
->timeout_per_command
;
1461 INIT_LIST_HEAD(&cmd
->eh_entry
);
1463 disposition
= scsi_decide_disposition(cmd
);
1464 if (disposition
!= SUCCESS
&&
1465 time_before(cmd
->jiffies_at_alloc
+ wait_for
, jiffies
)) {
1466 sdev_printk(KERN_ERR
, cmd
->device
,
1467 "timing out command, waited %lus\n",
1469 disposition
= SUCCESS
;
1472 scsi_log_completion(cmd
, disposition
);
1474 switch (disposition
) {
1476 scsi_finish_command(cmd
);
1479 scsi_queue_insert(cmd
, SCSI_MLQUEUE_EH_RETRY
);
1481 case ADD_TO_MLQUEUE
:
1482 scsi_queue_insert(cmd
, SCSI_MLQUEUE_DEVICE_BUSY
);
1485 if (!scsi_eh_scmd_add(cmd
, 0))
1486 scsi_finish_command(cmd
);
1491 * Function: scsi_request_fn()
1493 * Purpose: Main strategy routine for SCSI.
1495 * Arguments: q - Pointer to actual queue.
1499 * Lock status: IO request lock assumed to be held when called.
1501 static void scsi_request_fn(struct request_queue
*q
)
1503 struct scsi_device
*sdev
= q
->queuedata
;
1504 struct Scsi_Host
*shost
;
1505 struct scsi_cmnd
*cmd
;
1506 struct request
*req
;
1509 printk("scsi: killing requests for dead queue\n");
1510 while ((req
= elv_next_request(q
)) != NULL
)
1511 scsi_kill_request(req
, q
);
1515 if(!get_device(&sdev
->sdev_gendev
))
1516 /* We must be tearing the block queue down already */
1520 * To start with, we keep looping until the queue is empty, or until
1521 * the host is no longer able to accept any more requests.
1524 while (!blk_queue_plugged(q
)) {
1527 * get next queueable request. We do this early to make sure
1528 * that the request is fully prepared even if we cannot
1531 req
= elv_next_request(q
);
1532 if (!req
|| !scsi_dev_queue_ready(q
, sdev
))
1535 if (unlikely(!scsi_device_online(sdev
))) {
1536 sdev_printk(KERN_ERR
, sdev
,
1537 "rejecting I/O to offline device\n");
1538 scsi_kill_request(req
, q
);
1544 * Remove the request from the request list.
1546 if (!(blk_queue_tagged(q
) && !blk_queue_start_tag(q
, req
)))
1547 blkdev_dequeue_request(req
);
1548 sdev
->device_busy
++;
1550 spin_unlock(q
->queue_lock
);
1552 if (unlikely(cmd
== NULL
)) {
1553 printk(KERN_CRIT
"impossible request in %s.\n"
1554 "please mail a stack trace to "
1555 "linux-scsi@vger.kernel.org\n",
1557 blk_dump_rq_flags(req
, "foo");
1560 spin_lock(shost
->host_lock
);
1562 if (!scsi_host_queue_ready(q
, shost
, sdev
))
1564 if (sdev
->single_lun
) {
1565 if (scsi_target(sdev
)->starget_sdev_user
&&
1566 scsi_target(sdev
)->starget_sdev_user
!= sdev
)
1568 scsi_target(sdev
)->starget_sdev_user
= sdev
;
1573 * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
1574 * take the lock again.
1576 spin_unlock_irq(shost
->host_lock
);
1579 * Finally, initialize any error handling parameters, and set up
1580 * the timers for timeouts.
1582 scsi_init_cmd_errh(cmd
);
1585 * Dispatch the command to the low-level driver.
1587 rtn
= scsi_dispatch_cmd(cmd
);
1588 spin_lock_irq(q
->queue_lock
);
1590 /* we're refusing the command; because of
1591 * the way locks get dropped, we need to
1592 * check here if plugging is required */
1593 if(sdev
->device_busy
== 0)
1603 spin_unlock_irq(shost
->host_lock
);
1606 * lock q, handle tag, requeue req, and decrement device_busy. We
1607 * must return with queue_lock held.
1609 * Decrementing device_busy without checking it is OK, as all such
1610 * cases (host limits or settings) should run the queue at some
1613 spin_lock_irq(q
->queue_lock
);
1614 blk_requeue_request(q
, req
);
1615 sdev
->device_busy
--;
1616 if(sdev
->device_busy
== 0)
1619 /* must be careful here...if we trigger the ->remove() function
1620 * we cannot be holding the q lock */
1621 spin_unlock_irq(q
->queue_lock
);
1622 put_device(&sdev
->sdev_gendev
);
1623 spin_lock_irq(q
->queue_lock
);
1626 u64
scsi_calculate_bounce_limit(struct Scsi_Host
*shost
)
1628 struct device
*host_dev
;
1629 u64 bounce_limit
= 0xffffffff;
1631 if (shost
->unchecked_isa_dma
)
1632 return BLK_BOUNCE_ISA
;
1634 * Platforms with virtual-DMA translation
1635 * hardware have no practical limit.
1637 if (!PCI_DMA_BUS_IS_PHYS
)
1638 return BLK_BOUNCE_ANY
;
1640 host_dev
= scsi_get_device(shost
);
1641 if (host_dev
&& host_dev
->dma_mask
)
1642 bounce_limit
= *host_dev
->dma_mask
;
1644 return bounce_limit
;
1646 EXPORT_SYMBOL(scsi_calculate_bounce_limit
);
1648 struct request_queue
*__scsi_alloc_queue(struct Scsi_Host
*shost
,
1649 request_fn_proc
*request_fn
)
1651 struct request_queue
*q
;
1653 q
= blk_init_queue(request_fn
, NULL
);
1657 blk_queue_max_hw_segments(q
, shost
->sg_tablesize
);
1658 blk_queue_max_phys_segments(q
, SCSI_MAX_PHYS_SEGMENTS
);
1659 blk_queue_max_sectors(q
, shost
->max_sectors
);
1660 blk_queue_bounce_limit(q
, scsi_calculate_bounce_limit(shost
));
1661 blk_queue_segment_boundary(q
, shost
->dma_boundary
);
1663 if (!shost
->use_clustering
)
1664 clear_bit(QUEUE_FLAG_CLUSTER
, &q
->queue_flags
);
1667 * set a reasonable default alignment on word boundaries: the
1668 * host and device may alter it using
1669 * blk_queue_update_dma_alignment() later.
1671 blk_queue_dma_alignment(q
, 0x03);
1675 EXPORT_SYMBOL(__scsi_alloc_queue
);
1677 struct request_queue
*scsi_alloc_queue(struct scsi_device
*sdev
)
1679 struct request_queue
*q
;
1681 q
= __scsi_alloc_queue(sdev
->host
, scsi_request_fn
);
1685 blk_queue_prep_rq(q
, scsi_prep_fn
);
1686 blk_queue_issue_flush_fn(q
, scsi_issue_flush_fn
);
1687 blk_queue_softirq_done(q
, scsi_softirq_done
);
1691 void scsi_free_queue(struct request_queue
*q
)
1693 blk_cleanup_queue(q
);
1697 * Function: scsi_block_requests()
1699 * Purpose: Utility function used by low-level drivers to prevent further
1700 * commands from being queued to the device.
1702 * Arguments: shost - Host in question
1706 * Lock status: No locks are assumed held.
1708 * Notes: There is no timer nor any other means by which the requests
1709 * get unblocked other than the low-level driver calling
1710 * scsi_unblock_requests().
1712 void scsi_block_requests(struct Scsi_Host
*shost
)
1714 shost
->host_self_blocked
= 1;
1716 EXPORT_SYMBOL(scsi_block_requests
);
1719 * Function: scsi_unblock_requests()
1721 * Purpose: Utility function used by low-level drivers to allow further
1722 * commands from being queued to the device.
1724 * Arguments: shost - Host in question
1728 * Lock status: No locks are assumed held.
1730 * Notes: There is no timer nor any other means by which the requests
1731 * get unblocked other than the low-level driver calling
1732 * scsi_unblock_requests().
1734 * This is done as an API function so that changes to the
1735 * internals of the scsi mid-layer won't require wholesale
1736 * changes to drivers that use this feature.
1738 void scsi_unblock_requests(struct Scsi_Host
*shost
)
1740 shost
->host_self_blocked
= 0;
1741 scsi_run_host_queues(shost
);
1743 EXPORT_SYMBOL(scsi_unblock_requests
);
1745 int __init
scsi_init_queue(void)
1749 scsi_io_context_cache
= kmem_cache_create("scsi_io_context",
1750 sizeof(struct scsi_io_context
),
1752 if (!scsi_io_context_cache
) {
1753 printk(KERN_ERR
"SCSI: can't init scsi io context cache\n");
1757 for (i
= 0; i
< SG_MEMPOOL_NR
; i
++) {
1758 struct scsi_host_sg_pool
*sgp
= scsi_sg_pools
+ i
;
1759 int size
= sgp
->size
* sizeof(struct scatterlist
);
1761 sgp
->slab
= kmem_cache_create(sgp
->name
, size
, 0,
1762 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
1764 printk(KERN_ERR
"SCSI: can't init sg slab %s\n",
1768 sgp
->pool
= mempool_create_slab_pool(SG_MEMPOOL_SIZE
,
1771 printk(KERN_ERR
"SCSI: can't init sg mempool %s\n",
1779 void scsi_exit_queue(void)
1783 kmem_cache_destroy(scsi_io_context_cache
);
1785 for (i
= 0; i
< SG_MEMPOOL_NR
; i
++) {
1786 struct scsi_host_sg_pool
*sgp
= scsi_sg_pools
+ i
;
1787 mempool_destroy(sgp
->pool
);
1788 kmem_cache_destroy(sgp
->slab
);
1793 * scsi_mode_select - issue a mode select
1794 * @sdev: SCSI device to be queried
1795 * @pf: Page format bit (1 == standard, 0 == vendor specific)
1796 * @sp: Save page bit (0 == don't save, 1 == save)
1797 * @modepage: mode page being requested
1798 * @buffer: request buffer (may not be smaller than eight bytes)
1799 * @len: length of request buffer.
1800 * @timeout: command timeout
1801 * @retries: number of retries before failing
1802 * @data: returns a structure abstracting the mode header data
1803 * @sense: place to put sense data (or NULL if no sense to be collected).
1804 * must be SCSI_SENSE_BUFFERSIZE big.
1806 * Returns zero if successful; negative error number or scsi
1811 scsi_mode_select(struct scsi_device
*sdev
, int pf
, int sp
, int modepage
,
1812 unsigned char *buffer
, int len
, int timeout
, int retries
,
1813 struct scsi_mode_data
*data
, struct scsi_sense_hdr
*sshdr
)
1815 unsigned char cmd
[10];
1816 unsigned char *real_buffer
;
1819 memset(cmd
, 0, sizeof(cmd
));
1820 cmd
[1] = (pf
? 0x10 : 0) | (sp
? 0x01 : 0);
1822 if (sdev
->use_10_for_ms
) {
1825 real_buffer
= kmalloc(8 + len
, GFP_KERNEL
);
1828 memcpy(real_buffer
+ 8, buffer
, len
);
1832 real_buffer
[2] = data
->medium_type
;
1833 real_buffer
[3] = data
->device_specific
;
1834 real_buffer
[4] = data
->longlba
? 0x01 : 0;
1836 real_buffer
[6] = data
->block_descriptor_length
>> 8;
1837 real_buffer
[7] = data
->block_descriptor_length
;
1839 cmd
[0] = MODE_SELECT_10
;
1843 if (len
> 255 || data
->block_descriptor_length
> 255 ||
1847 real_buffer
= kmalloc(4 + len
, GFP_KERNEL
);
1850 memcpy(real_buffer
+ 4, buffer
, len
);
1853 real_buffer
[1] = data
->medium_type
;
1854 real_buffer
[2] = data
->device_specific
;
1855 real_buffer
[3] = data
->block_descriptor_length
;
1858 cmd
[0] = MODE_SELECT
;
1862 ret
= scsi_execute_req(sdev
, cmd
, DMA_TO_DEVICE
, real_buffer
, len
,
1863 sshdr
, timeout
, retries
);
1867 EXPORT_SYMBOL_GPL(scsi_mode_select
);
1870 * scsi_mode_sense - issue a mode sense, falling back from 10 to
1871 * six bytes if necessary.
1872 * @sdev: SCSI device to be queried
1873 * @dbd: set if mode sense will allow block descriptors to be returned
1874 * @modepage: mode page being requested
1875 * @buffer: request buffer (may not be smaller than eight bytes)
1876 * @len: length of request buffer.
1877 * @timeout: command timeout
1878 * @retries: number of retries before failing
1879 * @data: returns a structure abstracting the mode header data
1880 * @sense: place to put sense data (or NULL if no sense to be collected).
1881 * must be SCSI_SENSE_BUFFERSIZE big.
1883 * Returns zero if unsuccessful, or the header offset (either 4
1884 * or 8 depending on whether a six or ten byte command was
1885 * issued) if successful.
1888 scsi_mode_sense(struct scsi_device
*sdev
, int dbd
, int modepage
,
1889 unsigned char *buffer
, int len
, int timeout
, int retries
,
1890 struct scsi_mode_data
*data
, struct scsi_sense_hdr
*sshdr
)
1892 unsigned char cmd
[12];
1896 struct scsi_sense_hdr my_sshdr
;
1898 memset(data
, 0, sizeof(*data
));
1899 memset(&cmd
[0], 0, 12);
1900 cmd
[1] = dbd
& 0x18; /* allows DBD and LLBA bits */
1903 /* caller might not be interested in sense, but we need it */
1908 use_10_for_ms
= sdev
->use_10_for_ms
;
1910 if (use_10_for_ms
) {
1914 cmd
[0] = MODE_SENSE_10
;
1921 cmd
[0] = MODE_SENSE
;
1926 memset(buffer
, 0, len
);
1928 result
= scsi_execute_req(sdev
, cmd
, DMA_FROM_DEVICE
, buffer
, len
,
1929 sshdr
, timeout
, retries
);
1931 /* This code looks awful: what it's doing is making sure an
1932 * ILLEGAL REQUEST sense return identifies the actual command
1933 * byte as the problem. MODE_SENSE commands can return
1934 * ILLEGAL REQUEST if the code page isn't supported */
1936 if (use_10_for_ms
&& !scsi_status_is_good(result
) &&
1937 (driver_byte(result
) & DRIVER_SENSE
)) {
1938 if (scsi_sense_valid(sshdr
)) {
1939 if ((sshdr
->sense_key
== ILLEGAL_REQUEST
) &&
1940 (sshdr
->asc
== 0x20) && (sshdr
->ascq
== 0)) {
1942 * Invalid command operation code
1944 sdev
->use_10_for_ms
= 0;
1950 if(scsi_status_is_good(result
)) {
1951 if (unlikely(buffer
[0] == 0x86 && buffer
[1] == 0x0b &&
1952 (modepage
== 6 || modepage
== 8))) {
1953 /* Initio breakage? */
1956 data
->medium_type
= 0;
1957 data
->device_specific
= 0;
1959 data
->block_descriptor_length
= 0;
1960 } else if(use_10_for_ms
) {
1961 data
->length
= buffer
[0]*256 + buffer
[1] + 2;
1962 data
->medium_type
= buffer
[2];
1963 data
->device_specific
= buffer
[3];
1964 data
->longlba
= buffer
[4] & 0x01;
1965 data
->block_descriptor_length
= buffer
[6]*256
1968 data
->length
= buffer
[0] + 1;
1969 data
->medium_type
= buffer
[1];
1970 data
->device_specific
= buffer
[2];
1971 data
->block_descriptor_length
= buffer
[3];
1973 data
->header_length
= header_length
;
1978 EXPORT_SYMBOL(scsi_mode_sense
);
1981 scsi_test_unit_ready(struct scsi_device
*sdev
, int timeout
, int retries
)
1984 TEST_UNIT_READY
, 0, 0, 0, 0, 0,
1986 struct scsi_sense_hdr sshdr
;
1989 result
= scsi_execute_req(sdev
, cmd
, DMA_NONE
, NULL
, 0, &sshdr
,
1992 if ((driver_byte(result
) & DRIVER_SENSE
) && sdev
->removable
) {
1994 if ((scsi_sense_valid(&sshdr
)) &&
1995 ((sshdr
.sense_key
== UNIT_ATTENTION
) ||
1996 (sshdr
.sense_key
== NOT_READY
))) {
2003 EXPORT_SYMBOL(scsi_test_unit_ready
);
2006 * scsi_device_set_state - Take the given device through the device
2008 * @sdev: scsi device to change the state of.
2009 * @state: state to change to.
2011 * Returns zero if unsuccessful or an error if the requested
2012 * transition is illegal.
2015 scsi_device_set_state(struct scsi_device
*sdev
, enum scsi_device_state state
)
2017 enum scsi_device_state oldstate
= sdev
->sdev_state
;
2019 if (state
== oldstate
)
2024 /* There are no legal states that come back to
2025 * created. This is the manually initialised start
2099 sdev
->sdev_state
= state
;
2103 SCSI_LOG_ERROR_RECOVERY(1,
2104 sdev_printk(KERN_ERR
, sdev
,
2105 "Illegal state transition %s->%s\n",
2106 scsi_device_state_name(oldstate
),
2107 scsi_device_state_name(state
))
2111 EXPORT_SYMBOL(scsi_device_set_state
);
2114 * scsi_device_quiesce - Block user issued commands.
2115 * @sdev: scsi device to quiesce.
2117 * This works by trying to transition to the SDEV_QUIESCE state
2118 * (which must be a legal transition). When the device is in this
2119 * state, only special requests will be accepted, all others will
2120 * be deferred. Since special requests may also be requeued requests,
2121 * a successful return doesn't guarantee the device will be
2122 * totally quiescent.
2124 * Must be called with user context, may sleep.
2126 * Returns zero if unsuccessful or an error if not.
2129 scsi_device_quiesce(struct scsi_device
*sdev
)
2131 int err
= scsi_device_set_state(sdev
, SDEV_QUIESCE
);
2135 scsi_run_queue(sdev
->request_queue
);
2136 while (sdev
->device_busy
) {
2137 msleep_interruptible(200);
2138 scsi_run_queue(sdev
->request_queue
);
2142 EXPORT_SYMBOL(scsi_device_quiesce
);
2145 * scsi_device_resume - Restart user issued commands to a quiesced device.
2146 * @sdev: scsi device to resume.
2148 * Moves the device from quiesced back to running and restarts the
2151 * Must be called with user context, may sleep.
2154 scsi_device_resume(struct scsi_device
*sdev
)
2156 if(scsi_device_set_state(sdev
, SDEV_RUNNING
))
2158 scsi_run_queue(sdev
->request_queue
);
2160 EXPORT_SYMBOL(scsi_device_resume
);
2163 device_quiesce_fn(struct scsi_device
*sdev
, void *data
)
2165 scsi_device_quiesce(sdev
);
2169 scsi_target_quiesce(struct scsi_target
*starget
)
2171 starget_for_each_device(starget
, NULL
, device_quiesce_fn
);
2173 EXPORT_SYMBOL(scsi_target_quiesce
);
2176 device_resume_fn(struct scsi_device
*sdev
, void *data
)
2178 scsi_device_resume(sdev
);
2182 scsi_target_resume(struct scsi_target
*starget
)
2184 starget_for_each_device(starget
, NULL
, device_resume_fn
);
2186 EXPORT_SYMBOL(scsi_target_resume
);
2189 * scsi_internal_device_block - internal function to put a device
2190 * temporarily into the SDEV_BLOCK state
2191 * @sdev: device to block
2193 * Block request made by scsi lld's to temporarily stop all
2194 * scsi commands on the specified device. Called from interrupt
2195 * or normal process context.
2197 * Returns zero if successful or error if not
2200 * This routine transitions the device to the SDEV_BLOCK state
2201 * (which must be a legal transition). When the device is in this
2202 * state, all commands are deferred until the scsi lld reenables
2203 * the device with scsi_device_unblock or device_block_tmo fires.
2204 * This routine assumes the host_lock is held on entry.
2207 scsi_internal_device_block(struct scsi_device
*sdev
)
2209 request_queue_t
*q
= sdev
->request_queue
;
2210 unsigned long flags
;
2213 err
= scsi_device_set_state(sdev
, SDEV_BLOCK
);
2218 * The device has transitioned to SDEV_BLOCK. Stop the
2219 * block layer from calling the midlayer with this device's
2222 spin_lock_irqsave(q
->queue_lock
, flags
);
2224 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2228 EXPORT_SYMBOL_GPL(scsi_internal_device_block
);
2231 * scsi_internal_device_unblock - resume a device after a block request
2232 * @sdev: device to resume
2234 * Called by scsi lld's or the midlayer to restart the device queue
2235 * for the previously suspended scsi device. Called from interrupt or
2236 * normal process context.
2238 * Returns zero if successful or error if not.
2241 * This routine transitions the device to the SDEV_RUNNING state
2242 * (which must be a legal transition) allowing the midlayer to
2243 * goose the queue for this device. This routine assumes the
2244 * host_lock is held upon entry.
2247 scsi_internal_device_unblock(struct scsi_device
*sdev
)
2249 request_queue_t
*q
= sdev
->request_queue
;
2251 unsigned long flags
;
2254 * Try to transition the scsi device to SDEV_RUNNING
2255 * and goose the device queue if successful.
2257 err
= scsi_device_set_state(sdev
, SDEV_RUNNING
);
2261 spin_lock_irqsave(q
->queue_lock
, flags
);
2263 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2267 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock
);
2270 device_block(struct scsi_device
*sdev
, void *data
)
2272 scsi_internal_device_block(sdev
);
2276 target_block(struct device
*dev
, void *data
)
2278 if (scsi_is_target_device(dev
))
2279 starget_for_each_device(to_scsi_target(dev
), NULL
,
2285 scsi_target_block(struct device
*dev
)
2287 if (scsi_is_target_device(dev
))
2288 starget_for_each_device(to_scsi_target(dev
), NULL
,
2291 device_for_each_child(dev
, NULL
, target_block
);
2293 EXPORT_SYMBOL_GPL(scsi_target_block
);
2296 device_unblock(struct scsi_device
*sdev
, void *data
)
2298 scsi_internal_device_unblock(sdev
);
2302 target_unblock(struct device
*dev
, void *data
)
2304 if (scsi_is_target_device(dev
))
2305 starget_for_each_device(to_scsi_target(dev
), NULL
,
2311 scsi_target_unblock(struct device
*dev
)
2313 if (scsi_is_target_device(dev
))
2314 starget_for_each_device(to_scsi_target(dev
), NULL
,
2317 device_for_each_child(dev
, NULL
, target_unblock
);
2319 EXPORT_SYMBOL_GPL(scsi_target_unblock
);
2322 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
2323 * @sg: scatter-gather list
2324 * @sg_count: number of segments in sg
2325 * @offset: offset in bytes into sg, on return offset into the mapped area
2326 * @len: bytes to map, on return number of bytes mapped
2328 * Returns virtual address of the start of the mapped page
2330 void *scsi_kmap_atomic_sg(struct scatterlist
*sgl
, int sg_count
,
2331 size_t *offset
, size_t *len
)
2334 size_t sg_len
= 0, len_complete
= 0;
2335 struct scatterlist
*sg
;
2338 WARN_ON(!irqs_disabled());
2340 for_each_sg(sgl
, sg
, sg_count
, i
) {
2341 len_complete
= sg_len
; /* Complete sg-entries */
2342 sg_len
+= sg
->length
;
2343 if (sg_len
> *offset
)
2347 if (unlikely(i
== sg_count
)) {
2348 printk(KERN_ERR
"%s: Bytes in sg: %zu, requested offset %zu, "
2350 __FUNCTION__
, sg_len
, *offset
, sg_count
);
2355 /* Offset starting from the beginning of first page in this sg-entry */
2356 *offset
= *offset
- len_complete
+ sg
->offset
;
2358 /* Assumption: contiguous pages can be accessed as "page + i" */
2359 page
= nth_page(sg
->page
, (*offset
>> PAGE_SHIFT
));
2360 *offset
&= ~PAGE_MASK
;
2362 /* Bytes in this sg-entry from *offset to the end of the page */
2363 sg_len
= PAGE_SIZE
- *offset
;
2367 return kmap_atomic(page
, KM_BIO_SRC_IRQ
);
2369 EXPORT_SYMBOL(scsi_kmap_atomic_sg
);
2372 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously
2373 * mapped with scsi_kmap_atomic_sg
2374 * @virt: virtual address to be unmapped
2376 void scsi_kunmap_atomic_sg(void *virt
)
2378 kunmap_atomic(virt
, KM_BIO_SRC_IRQ
);
2380 EXPORT_SYMBOL(scsi_kunmap_atomic_sg
);
2383 * scsi_dma_map - perform DMA mapping against command's sg lists
2384 * @cmd: scsi command
2386 * Returns the number of sg lists actually used, zero if the sg lists
2387 * is NULL, or -ENOMEM if the mapping failed.
2389 int scsi_dma_map(struct scsi_cmnd
*cmd
)
2393 if (scsi_sg_count(cmd
)) {
2394 struct device
*dev
= cmd
->device
->host
->shost_gendev
.parent
;
2396 nseg
= dma_map_sg(dev
, scsi_sglist(cmd
), scsi_sg_count(cmd
),
2397 cmd
->sc_data_direction
);
2398 if (unlikely(!nseg
))
2403 EXPORT_SYMBOL(scsi_dma_map
);
2406 * scsi_dma_unmap - unmap command's sg lists mapped by scsi_dma_map
2407 * @cmd: scsi command
2409 void scsi_dma_unmap(struct scsi_cmnd
*cmd
)
2411 if (scsi_sg_count(cmd
)) {
2412 struct device
*dev
= cmd
->device
->host
->shost_gendev
.parent
;
2414 dma_unmap_sg(dev
, scsi_sglist(cmd
), scsi_sg_count(cmd
),
2415 cmd
->sc_data_direction
);
2418 EXPORT_SYMBOL(scsi_dma_unmap
);