Linux 2.6.33.13
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / scsi_lib.c
blobb288d7213268d048816bb442a5daa074cd2b3f0e
1 /*
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.
8 */
10 #include <linux/bio.h>
11 #include <linux/bitops.h>
12 #include <linux/blkdev.h>
13 #include <linux/completion.h>
14 #include <linux/kernel.h>
15 #include <linux/mempool.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/delay.h>
20 #include <linux/hardirq.h>
21 #include <linux/scatterlist.h>
23 #include <scsi/scsi.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_dbg.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_driver.h>
28 #include <scsi/scsi_eh.h>
29 #include <scsi/scsi_host.h>
31 #include "scsi_priv.h"
32 #include "scsi_logging.h"
35 #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)
36 #define SG_MEMPOOL_SIZE 2
38 struct scsi_host_sg_pool {
39 size_t size;
40 char *name;
41 struct kmem_cache *slab;
42 mempool_t *pool;
45 #define SP(x) { x, "sgpool-" __stringify(x) }
46 #if (SCSI_MAX_SG_SEGMENTS < 32)
47 #error SCSI_MAX_SG_SEGMENTS is too small (must be 32 or greater)
48 #endif
49 static struct scsi_host_sg_pool scsi_sg_pools[] = {
50 SP(8),
51 SP(16),
52 #if (SCSI_MAX_SG_SEGMENTS > 32)
53 SP(32),
54 #if (SCSI_MAX_SG_SEGMENTS > 64)
55 SP(64),
56 #if (SCSI_MAX_SG_SEGMENTS > 128)
57 SP(128),
58 #if (SCSI_MAX_SG_SEGMENTS > 256)
59 #error SCSI_MAX_SG_SEGMENTS is too large (256 MAX)
60 #endif
61 #endif
62 #endif
63 #endif
64 SP(SCSI_MAX_SG_SEGMENTS)
66 #undef SP
68 struct kmem_cache *scsi_sdb_cache;
70 static void scsi_run_queue(struct request_queue *q);
73 * Function: scsi_unprep_request()
75 * Purpose: Remove all preparation done for a request, including its
76 * associated scsi_cmnd, so that it can be requeued.
78 * Arguments: req - request to unprepare
80 * Lock status: Assumed that no locks are held upon entry.
82 * Returns: Nothing.
84 static void scsi_unprep_request(struct request *req)
86 struct scsi_cmnd *cmd = req->special;
88 req->cmd_flags &= ~REQ_DONTPREP;
89 req->special = NULL;
91 scsi_put_command(cmd);
94 /**
95 * __scsi_queue_insert - private queue insertion
96 * @cmd: The SCSI command being requeued
97 * @reason: The reason for the requeue
98 * @unbusy: Whether the queue should be unbusied
100 * This is a private queue insertion. The public interface
101 * scsi_queue_insert() always assumes the queue should be unbusied
102 * because it's always called before the completion. This function is
103 * for a requeue after completion, which should only occur in this
104 * file.
106 static int __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
108 struct Scsi_Host *host = cmd->device->host;
109 struct scsi_device *device = cmd->device;
110 struct scsi_target *starget = scsi_target(device);
111 struct request_queue *q = device->request_queue;
112 unsigned long flags;
114 SCSI_LOG_MLQUEUE(1,
115 printk("Inserting command %p into mlqueue\n", cmd));
118 * Set the appropriate busy bit for the device/host.
120 * If the host/device isn't busy, assume that something actually
121 * completed, and that we should be able to queue a command now.
123 * Note that the prior mid-layer assumption that any host could
124 * always queue at least one command is now broken. The mid-layer
125 * will implement a user specifiable stall (see
126 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
127 * if a command is requeued with no other commands outstanding
128 * either for the device or for the host.
130 switch (reason) {
131 case SCSI_MLQUEUE_HOST_BUSY:
132 host->host_blocked = host->max_host_blocked;
133 break;
134 case SCSI_MLQUEUE_DEVICE_BUSY:
135 device->device_blocked = device->max_device_blocked;
136 break;
137 case SCSI_MLQUEUE_TARGET_BUSY:
138 starget->target_blocked = starget->max_target_blocked;
139 break;
143 * Decrement the counters, since these commands are no longer
144 * active on the host/device.
146 if (unbusy)
147 scsi_device_unbusy(device);
150 * Requeue this command. It will go before all other commands
151 * that are already in the queue.
153 * NOTE: there is magic here about the way the queue is plugged if
154 * we have no outstanding commands.
156 * Although we *don't* plug the queue, we call the request
157 * function. The SCSI request function detects the blocked condition
158 * and plugs the queue appropriately.
160 spin_lock_irqsave(q->queue_lock, flags);
161 blk_requeue_request(q, cmd->request);
162 spin_unlock_irqrestore(q->queue_lock, flags);
164 scsi_run_queue(q);
166 return 0;
170 * Function: scsi_queue_insert()
172 * Purpose: Insert a command in the midlevel queue.
174 * Arguments: cmd - command that we are adding to queue.
175 * reason - why we are inserting command to queue.
177 * Lock status: Assumed that lock is not held upon entry.
179 * Returns: Nothing.
181 * Notes: We do this for one of two cases. Either the host is busy
182 * and it cannot accept any more commands for the time being,
183 * or the device returned QUEUE_FULL and can accept no more
184 * commands.
185 * Notes: This could be called either from an interrupt context or a
186 * normal process context.
188 int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
190 return __scsi_queue_insert(cmd, reason, 1);
193 * scsi_execute - insert request and wait for the result
194 * @sdev: scsi device
195 * @cmd: scsi command
196 * @data_direction: data direction
197 * @buffer: data buffer
198 * @bufflen: len of buffer
199 * @sense: optional sense buffer
200 * @timeout: request timeout in seconds
201 * @retries: number of times to retry request
202 * @flags: or into request flags;
203 * @resid: optional residual length
205 * returns the req->errors value which is the scsi_cmnd result
206 * field.
208 int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
209 int data_direction, void *buffer, unsigned bufflen,
210 unsigned char *sense, int timeout, int retries, int flags,
211 int *resid)
213 struct request *req;
214 int write = (data_direction == DMA_TO_DEVICE);
215 int ret = DRIVER_ERROR << 24;
217 req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
219 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
220 buffer, bufflen, __GFP_WAIT))
221 goto out;
223 req->cmd_len = COMMAND_SIZE(cmd[0]);
224 memcpy(req->cmd, cmd, req->cmd_len);
225 req->sense = sense;
226 req->sense_len = 0;
227 req->retries = retries;
228 req->timeout = timeout;
229 req->cmd_type = REQ_TYPE_BLOCK_PC;
230 req->cmd_flags |= flags | REQ_QUIET | REQ_PREEMPT;
233 * head injection *required* here otherwise quiesce won't work
235 blk_execute_rq(req->q, NULL, req, 1);
238 * Some devices (USB mass-storage in particular) may transfer
239 * garbage data together with a residue indicating that the data
240 * is invalid. Prevent the garbage from being misinterpreted
241 * and prevent security leaks by zeroing out the excess data.
243 if (unlikely(req->resid_len > 0 && req->resid_len <= bufflen))
244 memset(buffer + (bufflen - req->resid_len), 0, req->resid_len);
246 if (resid)
247 *resid = req->resid_len;
248 ret = req->errors;
249 out:
250 blk_put_request(req);
252 return ret;
254 EXPORT_SYMBOL(scsi_execute);
257 int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
258 int data_direction, void *buffer, unsigned bufflen,
259 struct scsi_sense_hdr *sshdr, int timeout, int retries,
260 int *resid)
262 char *sense = NULL;
263 int result;
265 if (sshdr) {
266 sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
267 if (!sense)
268 return DRIVER_ERROR << 24;
270 result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
271 sense, timeout, retries, 0, resid);
272 if (sshdr)
273 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
275 kfree(sense);
276 return result;
278 EXPORT_SYMBOL(scsi_execute_req);
281 * Function: scsi_init_cmd_errh()
283 * Purpose: Initialize cmd fields related to error handling.
285 * Arguments: cmd - command that is ready to be queued.
287 * Notes: This function has the job of initializing a number of
288 * fields related to error handling. Typically this will
289 * be called once for each command, as required.
291 static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
293 cmd->serial_number = 0;
294 scsi_set_resid(cmd, 0);
295 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
296 if (cmd->cmd_len == 0)
297 cmd->cmd_len = scsi_command_size(cmd->cmnd);
300 void scsi_device_unbusy(struct scsi_device *sdev)
302 struct Scsi_Host *shost = sdev->host;
303 struct scsi_target *starget = scsi_target(sdev);
304 unsigned long flags;
306 spin_lock_irqsave(shost->host_lock, flags);
307 shost->host_busy--;
308 starget->target_busy--;
309 if (unlikely(scsi_host_in_recovery(shost) &&
310 (shost->host_failed || shost->host_eh_scheduled)))
311 scsi_eh_wakeup(shost);
312 spin_unlock(shost->host_lock);
313 spin_lock(sdev->request_queue->queue_lock);
314 sdev->device_busy--;
315 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
319 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
320 * and call blk_run_queue for all the scsi_devices on the target -
321 * including current_sdev first.
323 * Called with *no* scsi locks held.
325 static void scsi_single_lun_run(struct scsi_device *current_sdev)
327 struct Scsi_Host *shost = current_sdev->host;
328 struct scsi_device *sdev, *tmp;
329 struct scsi_target *starget = scsi_target(current_sdev);
330 unsigned long flags;
332 spin_lock_irqsave(shost->host_lock, flags);
333 starget->starget_sdev_user = NULL;
334 spin_unlock_irqrestore(shost->host_lock, flags);
337 * Call blk_run_queue for all LUNs on the target, starting with
338 * current_sdev. We race with others (to set starget_sdev_user),
339 * but in most cases, we will be first. Ideally, each LU on the
340 * target would get some limited time or requests on the target.
342 blk_run_queue(current_sdev->request_queue);
344 spin_lock_irqsave(shost->host_lock, flags);
345 if (starget->starget_sdev_user)
346 goto out;
347 list_for_each_entry_safe(sdev, tmp, &starget->devices,
348 same_target_siblings) {
349 if (sdev == current_sdev)
350 continue;
351 if (scsi_device_get(sdev))
352 continue;
354 spin_unlock_irqrestore(shost->host_lock, flags);
355 blk_run_queue(sdev->request_queue);
356 spin_lock_irqsave(shost->host_lock, flags);
358 scsi_device_put(sdev);
360 out:
361 spin_unlock_irqrestore(shost->host_lock, flags);
364 static inline int scsi_device_is_busy(struct scsi_device *sdev)
366 if (sdev->device_busy >= sdev->queue_depth || sdev->device_blocked)
367 return 1;
369 return 0;
372 static inline int scsi_target_is_busy(struct scsi_target *starget)
374 return ((starget->can_queue > 0 &&
375 starget->target_busy >= starget->can_queue) ||
376 starget->target_blocked);
379 static inline int scsi_host_is_busy(struct Scsi_Host *shost)
381 if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
382 shost->host_blocked || shost->host_self_blocked)
383 return 1;
385 return 0;
389 * Function: scsi_run_queue()
391 * Purpose: Select a proper request queue to serve next
393 * Arguments: q - last request's queue
395 * Returns: Nothing
397 * Notes: The previous command was completely finished, start
398 * a new one if possible.
400 static void scsi_run_queue(struct request_queue *q)
402 struct scsi_device *sdev = q->queuedata;
403 struct Scsi_Host *shost;
404 LIST_HEAD(starved_list);
405 unsigned long flags;
407 /* if the device is dead, sdev will be NULL, so no queue to run */
408 if (!sdev)
409 return;
411 shost = sdev->host;
412 if (scsi_target(sdev)->single_lun)
413 scsi_single_lun_run(sdev);
415 spin_lock_irqsave(shost->host_lock, flags);
416 list_splice_init(&shost->starved_list, &starved_list);
418 while (!list_empty(&starved_list)) {
419 int flagset;
422 * As long as shost is accepting commands and we have
423 * starved queues, call blk_run_queue. scsi_request_fn
424 * drops the queue_lock and can add us back to the
425 * starved_list.
427 * host_lock protects the starved_list and starved_entry.
428 * scsi_request_fn must get the host_lock before checking
429 * or modifying starved_list or starved_entry.
431 if (scsi_host_is_busy(shost))
432 break;
434 sdev = list_entry(starved_list.next,
435 struct scsi_device, starved_entry);
436 list_del_init(&sdev->starved_entry);
437 if (scsi_target_is_busy(scsi_target(sdev))) {
438 list_move_tail(&sdev->starved_entry,
439 &shost->starved_list);
440 continue;
443 spin_unlock(shost->host_lock);
445 spin_lock(sdev->request_queue->queue_lock);
446 flagset = test_bit(QUEUE_FLAG_REENTER, &q->queue_flags) &&
447 !test_bit(QUEUE_FLAG_REENTER,
448 &sdev->request_queue->queue_flags);
449 if (flagset)
450 queue_flag_set(QUEUE_FLAG_REENTER, sdev->request_queue);
451 __blk_run_queue(sdev->request_queue);
452 if (flagset)
453 queue_flag_clear(QUEUE_FLAG_REENTER, sdev->request_queue);
454 spin_unlock(sdev->request_queue->queue_lock);
456 spin_lock(shost->host_lock);
458 /* put any unprocessed entries back */
459 list_splice(&starved_list, &shost->starved_list);
460 spin_unlock_irqrestore(shost->host_lock, flags);
462 blk_run_queue(q);
466 * Function: scsi_requeue_command()
468 * Purpose: Handle post-processing of completed commands.
470 * Arguments: q - queue to operate on
471 * cmd - command that may need to be requeued.
473 * Returns: Nothing
475 * Notes: After command completion, there may be blocks left
476 * over which weren't finished by the previous command
477 * this can be for a number of reasons - the main one is
478 * I/O errors in the middle of the request, in which case
479 * we need to request the blocks that come after the bad
480 * sector.
481 * Notes: Upon return, cmd is a stale pointer.
483 static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
485 struct request *req = cmd->request;
486 unsigned long flags;
488 spin_lock_irqsave(q->queue_lock, flags);
489 scsi_unprep_request(req);
490 blk_requeue_request(q, req);
491 spin_unlock_irqrestore(q->queue_lock, flags);
493 scsi_run_queue(q);
496 void scsi_next_command(struct scsi_cmnd *cmd)
498 struct scsi_device *sdev = cmd->device;
499 struct request_queue *q = sdev->request_queue;
501 /* need to hold a reference on the device before we let go of the cmd */
502 get_device(&sdev->sdev_gendev);
504 scsi_put_command(cmd);
505 scsi_run_queue(q);
507 /* ok to remove device now */
508 put_device(&sdev->sdev_gendev);
511 void scsi_run_host_queues(struct Scsi_Host *shost)
513 struct scsi_device *sdev;
515 shost_for_each_device(sdev, shost)
516 scsi_run_queue(sdev->request_queue);
519 static void __scsi_release_buffers(struct scsi_cmnd *, int);
522 * Function: scsi_end_request()
524 * Purpose: Post-processing of completed commands (usually invoked at end
525 * of upper level post-processing and scsi_io_completion).
527 * Arguments: cmd - command that is complete.
528 * error - 0 if I/O indicates success, < 0 for I/O error.
529 * bytes - number of bytes of completed I/O
530 * requeue - indicates whether we should requeue leftovers.
532 * Lock status: Assumed that lock is not held upon entry.
534 * Returns: cmd if requeue required, NULL otherwise.
536 * Notes: This is called for block device requests in order to
537 * mark some number of sectors as complete.
539 * We are guaranteeing that the request queue will be goosed
540 * at some point during this call.
541 * Notes: If cmd was requeued, upon return it will be a stale pointer.
543 static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int error,
544 int bytes, int requeue)
546 struct request_queue *q = cmd->device->request_queue;
547 struct request *req = cmd->request;
550 * If there are blocks left over at the end, set up the command
551 * to queue the remainder of them.
553 if (blk_end_request(req, error, bytes)) {
554 /* kill remainder if no retrys */
555 if (error && scsi_noretry_cmd(cmd))
556 blk_end_request_all(req, error);
557 else {
558 if (requeue) {
560 * Bleah. Leftovers again. Stick the
561 * leftovers in the front of the
562 * queue, and goose the queue again.
564 scsi_release_buffers(cmd);
565 scsi_requeue_command(q, cmd);
566 cmd = NULL;
568 return cmd;
573 * This will goose the queue request function at the end, so we don't
574 * need to worry about launching another command.
576 __scsi_release_buffers(cmd, 0);
577 scsi_next_command(cmd);
578 return NULL;
581 static inline unsigned int scsi_sgtable_index(unsigned short nents)
583 unsigned int index;
585 BUG_ON(nents > SCSI_MAX_SG_SEGMENTS);
587 if (nents <= 8)
588 index = 0;
589 else
590 index = get_count_order(nents) - 3;
592 return index;
595 static void scsi_sg_free(struct scatterlist *sgl, unsigned int nents)
597 struct scsi_host_sg_pool *sgp;
599 sgp = scsi_sg_pools + scsi_sgtable_index(nents);
600 mempool_free(sgl, sgp->pool);
603 static struct scatterlist *scsi_sg_alloc(unsigned int nents, gfp_t gfp_mask)
605 struct scsi_host_sg_pool *sgp;
607 sgp = scsi_sg_pools + scsi_sgtable_index(nents);
608 return mempool_alloc(sgp->pool, gfp_mask);
611 static int scsi_alloc_sgtable(struct scsi_data_buffer *sdb, int nents,
612 gfp_t gfp_mask)
614 int ret;
616 BUG_ON(!nents);
618 ret = __sg_alloc_table(&sdb->table, nents, SCSI_MAX_SG_SEGMENTS,
619 gfp_mask, scsi_sg_alloc);
620 if (unlikely(ret))
621 __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS,
622 scsi_sg_free);
624 return ret;
627 static void scsi_free_sgtable(struct scsi_data_buffer *sdb)
629 __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS, scsi_sg_free);
632 static void __scsi_release_buffers(struct scsi_cmnd *cmd, int do_bidi_check)
635 if (cmd->sdb.table.nents)
636 scsi_free_sgtable(&cmd->sdb);
638 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
640 if (do_bidi_check && scsi_bidi_cmnd(cmd)) {
641 struct scsi_data_buffer *bidi_sdb =
642 cmd->request->next_rq->special;
643 scsi_free_sgtable(bidi_sdb);
644 kmem_cache_free(scsi_sdb_cache, bidi_sdb);
645 cmd->request->next_rq->special = NULL;
648 if (scsi_prot_sg_count(cmd))
649 scsi_free_sgtable(cmd->prot_sdb);
653 * Function: scsi_release_buffers()
655 * Purpose: Completion processing for block device I/O requests.
657 * Arguments: cmd - command that we are bailing.
659 * Lock status: Assumed that no lock is held upon entry.
661 * Returns: Nothing
663 * Notes: In the event that an upper level driver rejects a
664 * command, we must release resources allocated during
665 * the __init_io() function. Primarily this would involve
666 * the scatter-gather table, and potentially any bounce
667 * buffers.
669 void scsi_release_buffers(struct scsi_cmnd *cmd)
671 __scsi_release_buffers(cmd, 1);
673 EXPORT_SYMBOL(scsi_release_buffers);
676 * Function: scsi_io_completion()
678 * Purpose: Completion processing for block device I/O requests.
680 * Arguments: cmd - command that is finished.
682 * Lock status: Assumed that no lock is held upon entry.
684 * Returns: Nothing
686 * Notes: This function is matched in terms of capabilities to
687 * the function that created the scatter-gather list.
688 * In other words, if there are no bounce buffers
689 * (the normal case for most drivers), we don't need
690 * the logic to deal with cleaning up afterwards.
692 * We must call scsi_end_request(). This will finish off
693 * the specified number of sectors. If we are done, the
694 * command block will be released and the queue function
695 * will be goosed. If we are not done then we have to
696 * figure out what to do next:
698 * a) We can call scsi_requeue_command(). The request
699 * will be unprepared and put back on the queue. Then
700 * a new command will be created for it. This should
701 * be used if we made forward progress, or if we want
702 * to switch from READ(10) to READ(6) for example.
704 * b) We can call scsi_queue_insert(). The request will
705 * be put back on the queue and retried using the same
706 * command as before, possibly after a delay.
708 * c) We can call blk_end_request() with -EIO to fail
709 * the remainder of the request.
711 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
713 int result = cmd->result;
714 struct request_queue *q = cmd->device->request_queue;
715 struct request *req = cmd->request;
716 int error = 0;
717 struct scsi_sense_hdr sshdr;
718 int sense_valid = 0;
719 int sense_deferred = 0;
720 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
721 ACTION_DELAYED_RETRY} action;
722 char *description = NULL;
724 if (result) {
725 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
726 if (sense_valid)
727 sense_deferred = scsi_sense_is_deferred(&sshdr);
730 if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
731 req->errors = result;
732 if (result) {
733 if (sense_valid && req->sense) {
735 * SG_IO wants current and deferred errors
737 int len = 8 + cmd->sense_buffer[7];
739 if (len > SCSI_SENSE_BUFFERSIZE)
740 len = SCSI_SENSE_BUFFERSIZE;
741 memcpy(req->sense, cmd->sense_buffer, len);
742 req->sense_len = len;
744 if (!sense_deferred)
745 error = -EIO;
748 req->resid_len = scsi_get_resid(cmd);
750 if (scsi_bidi_cmnd(cmd)) {
752 * Bidi commands Must be complete as a whole,
753 * both sides at once.
755 req->next_rq->resid_len = scsi_in(cmd)->resid;
757 scsi_release_buffers(cmd);
758 blk_end_request_all(req, 0);
760 scsi_next_command(cmd);
761 return;
765 BUG_ON(blk_bidi_rq(req)); /* bidi not support for !blk_pc_request yet */
768 * Next deal with any sectors which we were able to correctly
769 * handle.
771 SCSI_LOG_HLCOMPLETE(1, printk("%u sectors total, "
772 "%d bytes done.\n",
773 blk_rq_sectors(req), good_bytes));
776 * Recovered errors need reporting, but they're always treated
777 * as success, so fiddle the result code here. For BLOCK_PC
778 * we already took a copy of the original into rq->errors which
779 * is what gets returned to the user
781 if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
782 /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
783 * print since caller wants ATA registers. Only occurs on
784 * SCSI ATA PASS_THROUGH commands when CK_COND=1
786 if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
788 else if (!(req->cmd_flags & REQ_QUIET))
789 scsi_print_sense("", cmd);
790 result = 0;
791 /* BLOCK_PC may have set error */
792 error = 0;
796 * A number of bytes were successfully read. If there
797 * are leftovers and there is some kind of error
798 * (result != 0), retry the rest.
800 if (scsi_end_request(cmd, error, good_bytes, result == 0) == NULL)
801 return;
803 error = -EIO;
805 if (host_byte(result) == DID_RESET) {
806 /* Third party bus reset or reset for error recovery
807 * reasons. Just retry the command and see what
808 * happens.
810 action = ACTION_RETRY;
811 } else if (sense_valid && !sense_deferred) {
812 switch (sshdr.sense_key) {
813 case UNIT_ATTENTION:
814 if (cmd->device->removable) {
815 /* Detected disc change. Set a bit
816 * and quietly refuse further access.
818 cmd->device->changed = 1;
819 description = "Media Changed";
820 action = ACTION_FAIL;
821 } else {
822 /* Must have been a power glitch, or a
823 * bus reset. Could not have been a
824 * media change, so we just retry the
825 * command and see what happens.
827 action = ACTION_RETRY;
829 break;
830 case ILLEGAL_REQUEST:
831 /* If we had an ILLEGAL REQUEST returned, then
832 * we may have performed an unsupported
833 * command. The only thing this should be
834 * would be a ten byte read where only a six
835 * byte read was supported. Also, on a system
836 * where READ CAPACITY failed, we may have
837 * read past the end of the disk.
839 if ((cmd->device->use_10_for_rw &&
840 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
841 (cmd->cmnd[0] == READ_10 ||
842 cmd->cmnd[0] == WRITE_10)) {
843 /* This will issue a new 6-byte command. */
844 cmd->device->use_10_for_rw = 0;
845 action = ACTION_REPREP;
846 } else if (sshdr.asc == 0x10) /* DIX */ {
847 description = "Host Data Integrity Failure";
848 action = ACTION_FAIL;
849 error = -EILSEQ;
850 } else
851 action = ACTION_FAIL;
852 break;
853 case ABORTED_COMMAND:
854 action = ACTION_FAIL;
855 if (sshdr.asc == 0x10) { /* DIF */
856 description = "Target Data Integrity Failure";
857 error = -EILSEQ;
859 break;
860 case NOT_READY:
861 /* If the device is in the process of becoming
862 * ready, or has a temporary blockage, retry.
864 if (sshdr.asc == 0x04) {
865 switch (sshdr.ascq) {
866 case 0x01: /* becoming ready */
867 case 0x04: /* format in progress */
868 case 0x05: /* rebuild in progress */
869 case 0x06: /* recalculation in progress */
870 case 0x07: /* operation in progress */
871 case 0x08: /* Long write in progress */
872 case 0x09: /* self test in progress */
873 case 0x14: /* space allocation in progress */
874 action = ACTION_DELAYED_RETRY;
875 break;
876 default:
877 description = "Device not ready";
878 action = ACTION_FAIL;
879 break;
881 } else {
882 description = "Device not ready";
883 action = ACTION_FAIL;
885 break;
886 case VOLUME_OVERFLOW:
887 /* See SSC3rXX or current. */
888 action = ACTION_FAIL;
889 break;
890 default:
891 description = "Unhandled sense code";
892 action = ACTION_FAIL;
893 break;
895 } else {
896 description = "Unhandled error code";
897 action = ACTION_FAIL;
900 switch (action) {
901 case ACTION_FAIL:
902 /* Give up and fail the remainder of the request */
903 scsi_release_buffers(cmd);
904 if (!(req->cmd_flags & REQ_QUIET)) {
905 if (description)
906 scmd_printk(KERN_INFO, cmd, "%s\n",
907 description);
908 scsi_print_result(cmd);
909 if (driver_byte(result) & DRIVER_SENSE)
910 scsi_print_sense("", cmd);
911 scsi_print_command(cmd);
913 if (blk_end_request_err(req, error))
914 scsi_requeue_command(q, cmd);
915 else
916 scsi_next_command(cmd);
917 break;
918 case ACTION_REPREP:
919 /* Unprep the request and put it back at the head of the queue.
920 * A new command will be prepared and issued.
922 scsi_release_buffers(cmd);
923 scsi_requeue_command(q, cmd);
924 break;
925 case ACTION_RETRY:
926 /* Retry the same command immediately */
927 __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
928 break;
929 case ACTION_DELAYED_RETRY:
930 /* Retry the same command after a delay */
931 __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
932 break;
936 static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb,
937 gfp_t gfp_mask)
939 int count;
942 * If sg table allocation fails, requeue request later.
944 if (unlikely(scsi_alloc_sgtable(sdb, req->nr_phys_segments,
945 gfp_mask))) {
946 return BLKPREP_DEFER;
949 req->buffer = NULL;
952 * Next, walk the list, and fill in the addresses and sizes of
953 * each segment.
955 count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
956 BUG_ON(count > sdb->table.nents);
957 sdb->table.nents = count;
958 sdb->length = blk_rq_bytes(req);
959 return BLKPREP_OK;
963 * Function: scsi_init_io()
965 * Purpose: SCSI I/O initialize function.
967 * Arguments: cmd - Command descriptor we wish to initialize
969 * Returns: 0 on success
970 * BLKPREP_DEFER if the failure is retryable
971 * BLKPREP_KILL if the failure is fatal
973 int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
975 int error = scsi_init_sgtable(cmd->request, &cmd->sdb, gfp_mask);
976 if (error)
977 goto err_exit;
979 if (blk_bidi_rq(cmd->request)) {
980 struct scsi_data_buffer *bidi_sdb = kmem_cache_zalloc(
981 scsi_sdb_cache, GFP_ATOMIC);
982 if (!bidi_sdb) {
983 error = BLKPREP_DEFER;
984 goto err_exit;
987 cmd->request->next_rq->special = bidi_sdb;
988 error = scsi_init_sgtable(cmd->request->next_rq, bidi_sdb,
989 GFP_ATOMIC);
990 if (error)
991 goto err_exit;
994 if (blk_integrity_rq(cmd->request)) {
995 struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
996 int ivecs, count;
998 BUG_ON(prot_sdb == NULL);
999 ivecs = blk_rq_count_integrity_sg(cmd->request);
1001 if (scsi_alloc_sgtable(prot_sdb, ivecs, gfp_mask)) {
1002 error = BLKPREP_DEFER;
1003 goto err_exit;
1006 count = blk_rq_map_integrity_sg(cmd->request,
1007 prot_sdb->table.sgl);
1008 BUG_ON(unlikely(count > ivecs));
1010 cmd->prot_sdb = prot_sdb;
1011 cmd->prot_sdb->table.nents = count;
1014 return BLKPREP_OK ;
1016 err_exit:
1017 scsi_release_buffers(cmd);
1018 if (error == BLKPREP_KILL)
1019 scsi_put_command(cmd);
1020 else /* BLKPREP_DEFER */
1021 scsi_unprep_request(cmd->request);
1023 return error;
1025 EXPORT_SYMBOL(scsi_init_io);
1027 static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
1028 struct request *req)
1030 struct scsi_cmnd *cmd;
1032 if (!req->special) {
1033 cmd = scsi_get_command(sdev, GFP_ATOMIC);
1034 if (unlikely(!cmd))
1035 return NULL;
1036 req->special = cmd;
1037 } else {
1038 cmd = req->special;
1041 /* pull a tag out of the request if we have one */
1042 cmd->tag = req->tag;
1043 cmd->request = req;
1045 cmd->cmnd = req->cmd;
1047 return cmd;
1050 int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
1052 struct scsi_cmnd *cmd;
1053 int ret = scsi_prep_state_check(sdev, req);
1055 if (ret != BLKPREP_OK)
1056 return ret;
1058 cmd = scsi_get_cmd_from_req(sdev, req);
1059 if (unlikely(!cmd))
1060 return BLKPREP_DEFER;
1063 * BLOCK_PC requests may transfer data, in which case they must
1064 * a bio attached to them. Or they might contain a SCSI command
1065 * that does not transfer data, in which case they may optionally
1066 * submit a request without an attached bio.
1068 if (req->bio) {
1069 int ret;
1071 BUG_ON(!req->nr_phys_segments);
1073 ret = scsi_init_io(cmd, GFP_ATOMIC);
1074 if (unlikely(ret))
1075 return ret;
1076 } else {
1077 BUG_ON(blk_rq_bytes(req));
1079 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1080 req->buffer = NULL;
1083 cmd->cmd_len = req->cmd_len;
1084 if (!blk_rq_bytes(req))
1085 cmd->sc_data_direction = DMA_NONE;
1086 else if (rq_data_dir(req) == WRITE)
1087 cmd->sc_data_direction = DMA_TO_DEVICE;
1088 else
1089 cmd->sc_data_direction = DMA_FROM_DEVICE;
1091 cmd->transfersize = blk_rq_bytes(req);
1092 cmd->allowed = req->retries;
1093 return BLKPREP_OK;
1095 EXPORT_SYMBOL(scsi_setup_blk_pc_cmnd);
1098 * Setup a REQ_TYPE_FS command. These are simple read/write request
1099 * from filesystems that still need to be translated to SCSI CDBs from
1100 * the ULD.
1102 int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
1104 struct scsi_cmnd *cmd;
1105 int ret = scsi_prep_state_check(sdev, req);
1107 if (ret != BLKPREP_OK)
1108 return ret;
1110 if (unlikely(sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh
1111 && sdev->scsi_dh_data->scsi_dh->prep_fn)) {
1112 ret = sdev->scsi_dh_data->scsi_dh->prep_fn(sdev, req);
1113 if (ret != BLKPREP_OK)
1114 return ret;
1118 * Filesystem requests must transfer data.
1120 BUG_ON(!req->nr_phys_segments);
1122 cmd = scsi_get_cmd_from_req(sdev, req);
1123 if (unlikely(!cmd))
1124 return BLKPREP_DEFER;
1126 memset(cmd->cmnd, 0, BLK_MAX_CDB);
1127 return scsi_init_io(cmd, GFP_ATOMIC);
1129 EXPORT_SYMBOL(scsi_setup_fs_cmnd);
1131 int scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
1133 int ret = BLKPREP_OK;
1136 * If the device is not in running state we will reject some
1137 * or all commands.
1139 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1140 switch (sdev->sdev_state) {
1141 case SDEV_OFFLINE:
1143 * If the device is offline we refuse to process any
1144 * commands. The device must be brought online
1145 * before trying any recovery commands.
1147 sdev_printk(KERN_ERR, sdev,
1148 "rejecting I/O to offline device\n");
1149 ret = BLKPREP_KILL;
1150 break;
1151 case SDEV_DEL:
1153 * If the device is fully deleted, we refuse to
1154 * process any commands as well.
1156 sdev_printk(KERN_ERR, sdev,
1157 "rejecting I/O to dead device\n");
1158 ret = BLKPREP_KILL;
1159 break;
1160 case SDEV_QUIESCE:
1161 case SDEV_BLOCK:
1162 case SDEV_CREATED_BLOCK:
1164 * If the devices is blocked we defer normal commands.
1166 if (!(req->cmd_flags & REQ_PREEMPT))
1167 ret = BLKPREP_DEFER;
1168 break;
1169 default:
1171 * For any other not fully online state we only allow
1172 * special commands. In particular any user initiated
1173 * command is not allowed.
1175 if (!(req->cmd_flags & REQ_PREEMPT))
1176 ret = BLKPREP_KILL;
1177 break;
1180 return ret;
1182 EXPORT_SYMBOL(scsi_prep_state_check);
1184 int scsi_prep_return(struct request_queue *q, struct request *req, int ret)
1186 struct scsi_device *sdev = q->queuedata;
1188 switch (ret) {
1189 case BLKPREP_KILL:
1190 req->errors = DID_NO_CONNECT << 16;
1191 /* release the command and kill it */
1192 if (req->special) {
1193 struct scsi_cmnd *cmd = req->special;
1194 scsi_release_buffers(cmd);
1195 scsi_put_command(cmd);
1196 req->special = NULL;
1198 break;
1199 case BLKPREP_DEFER:
1201 * If we defer, the blk_peek_request() returns NULL, but the
1202 * queue must be restarted, so we plug here if no returning
1203 * command will automatically do that.
1205 if (sdev->device_busy == 0)
1206 blk_plug_device(q);
1207 break;
1208 default:
1209 req->cmd_flags |= REQ_DONTPREP;
1212 return ret;
1214 EXPORT_SYMBOL(scsi_prep_return);
1216 int scsi_prep_fn(struct request_queue *q, struct request *req)
1218 struct scsi_device *sdev = q->queuedata;
1219 int ret = BLKPREP_KILL;
1221 if (req->cmd_type == REQ_TYPE_BLOCK_PC)
1222 ret = scsi_setup_blk_pc_cmnd(sdev, req);
1223 return scsi_prep_return(q, req, ret);
1225 EXPORT_SYMBOL(scsi_prep_fn);
1228 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1229 * return 0.
1231 * Called with the queue_lock held.
1233 static inline int scsi_dev_queue_ready(struct request_queue *q,
1234 struct scsi_device *sdev)
1236 if (sdev->device_busy == 0 && sdev->device_blocked) {
1238 * unblock after device_blocked iterates to zero
1240 if (--sdev->device_blocked == 0) {
1241 SCSI_LOG_MLQUEUE(3,
1242 sdev_printk(KERN_INFO, sdev,
1243 "unblocking device at zero depth\n"));
1244 } else {
1245 blk_plug_device(q);
1246 return 0;
1249 if (scsi_device_is_busy(sdev))
1250 return 0;
1252 return 1;
1257 * scsi_target_queue_ready: checks if there we can send commands to target
1258 * @sdev: scsi device on starget to check.
1260 * Called with the host lock held.
1262 static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
1263 struct scsi_device *sdev)
1265 struct scsi_target *starget = scsi_target(sdev);
1267 if (starget->single_lun) {
1268 if (starget->starget_sdev_user &&
1269 starget->starget_sdev_user != sdev)
1270 return 0;
1271 starget->starget_sdev_user = sdev;
1274 if (starget->target_busy == 0 && starget->target_blocked) {
1276 * unblock after target_blocked iterates to zero
1278 if (--starget->target_blocked == 0) {
1279 SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
1280 "unblocking target at zero depth\n"));
1281 } else
1282 return 0;
1285 if (scsi_target_is_busy(starget)) {
1286 if (list_empty(&sdev->starved_entry)) {
1287 list_add_tail(&sdev->starved_entry,
1288 &shost->starved_list);
1289 return 0;
1293 /* We're OK to process the command, so we can't be starved */
1294 if (!list_empty(&sdev->starved_entry))
1295 list_del_init(&sdev->starved_entry);
1296 return 1;
1300 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1301 * return 0. We must end up running the queue again whenever 0 is
1302 * returned, else IO can hang.
1304 * Called with host_lock held.
1306 static inline int scsi_host_queue_ready(struct request_queue *q,
1307 struct Scsi_Host *shost,
1308 struct scsi_device *sdev)
1310 if (scsi_host_in_recovery(shost))
1311 return 0;
1312 if (shost->host_busy == 0 && shost->host_blocked) {
1314 * unblock after host_blocked iterates to zero
1316 if (--shost->host_blocked == 0) {
1317 SCSI_LOG_MLQUEUE(3,
1318 printk("scsi%d unblocking host at zero depth\n",
1319 shost->host_no));
1320 } else {
1321 return 0;
1324 if (scsi_host_is_busy(shost)) {
1325 if (list_empty(&sdev->starved_entry))
1326 list_add_tail(&sdev->starved_entry, &shost->starved_list);
1327 return 0;
1330 /* We're OK to process the command, so we can't be starved */
1331 if (!list_empty(&sdev->starved_entry))
1332 list_del_init(&sdev->starved_entry);
1334 return 1;
1338 * Busy state exporting function for request stacking drivers.
1340 * For efficiency, no lock is taken to check the busy state of
1341 * shost/starget/sdev, since the returned value is not guaranteed and
1342 * may be changed after request stacking drivers call the function,
1343 * regardless of taking lock or not.
1345 * When scsi can't dispatch I/Os anymore and needs to kill I/Os
1346 * (e.g. !sdev), scsi needs to return 'not busy'.
1347 * Otherwise, request stacking drivers may hold requests forever.
1349 static int scsi_lld_busy(struct request_queue *q)
1351 struct scsi_device *sdev = q->queuedata;
1352 struct Scsi_Host *shost;
1353 struct scsi_target *starget;
1355 if (!sdev)
1356 return 0;
1358 shost = sdev->host;
1359 starget = scsi_target(sdev);
1361 if (scsi_host_in_recovery(shost) || scsi_host_is_busy(shost) ||
1362 scsi_target_is_busy(starget) || scsi_device_is_busy(sdev))
1363 return 1;
1365 return 0;
1369 * Kill a request for a dead device
1371 static void scsi_kill_request(struct request *req, struct request_queue *q)
1373 struct scsi_cmnd *cmd = req->special;
1374 struct scsi_device *sdev;
1375 struct scsi_target *starget;
1376 struct Scsi_Host *shost;
1378 blk_start_request(req);
1380 if (unlikely(cmd == NULL)) {
1381 printk(KERN_CRIT "impossible request in %s.\n",
1382 __func__);
1383 BUG();
1386 sdev = cmd->device;
1387 starget = scsi_target(sdev);
1388 shost = sdev->host;
1389 scsi_init_cmd_errh(cmd);
1390 cmd->result = DID_NO_CONNECT << 16;
1391 atomic_inc(&cmd->device->iorequest_cnt);
1394 * SCSI request completion path will do scsi_device_unbusy(),
1395 * bump busy counts. To bump the counters, we need to dance
1396 * with the locks as normal issue path does.
1398 sdev->device_busy++;
1399 spin_unlock(sdev->request_queue->queue_lock);
1400 spin_lock(shost->host_lock);
1401 shost->host_busy++;
1402 starget->target_busy++;
1403 spin_unlock(shost->host_lock);
1404 spin_lock(sdev->request_queue->queue_lock);
1406 blk_complete_request(req);
1409 static void scsi_softirq_done(struct request *rq)
1411 struct scsi_cmnd *cmd = rq->special;
1412 unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
1413 int disposition;
1415 INIT_LIST_HEAD(&cmd->eh_entry);
1418 * Set the serial numbers back to zero
1420 cmd->serial_number = 0;
1422 atomic_inc(&cmd->device->iodone_cnt);
1423 if (cmd->result)
1424 atomic_inc(&cmd->device->ioerr_cnt);
1426 disposition = scsi_decide_disposition(cmd);
1427 if (disposition != SUCCESS &&
1428 time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
1429 sdev_printk(KERN_ERR, cmd->device,
1430 "timing out command, waited %lus\n",
1431 wait_for/HZ);
1432 disposition = SUCCESS;
1435 scsi_log_completion(cmd, disposition);
1437 switch (disposition) {
1438 case SUCCESS:
1439 scsi_finish_command(cmd);
1440 break;
1441 case NEEDS_RETRY:
1442 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
1443 break;
1444 case ADD_TO_MLQUEUE:
1445 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1446 break;
1447 default:
1448 if (!scsi_eh_scmd_add(cmd, 0))
1449 scsi_finish_command(cmd);
1454 * Function: scsi_request_fn()
1456 * Purpose: Main strategy routine for SCSI.
1458 * Arguments: q - Pointer to actual queue.
1460 * Returns: Nothing
1462 * Lock status: IO request lock assumed to be held when called.
1464 static void scsi_request_fn(struct request_queue *q)
1466 struct scsi_device *sdev = q->queuedata;
1467 struct Scsi_Host *shost;
1468 struct scsi_cmnd *cmd;
1469 struct request *req;
1471 if (!sdev) {
1472 printk("scsi: killing requests for dead queue\n");
1473 while ((req = blk_peek_request(q)) != NULL)
1474 scsi_kill_request(req, q);
1475 return;
1478 if(!get_device(&sdev->sdev_gendev))
1479 /* We must be tearing the block queue down already */
1480 return;
1483 * To start with, we keep looping until the queue is empty, or until
1484 * the host is no longer able to accept any more requests.
1486 shost = sdev->host;
1487 while (!blk_queue_plugged(q)) {
1488 int rtn;
1490 * get next queueable request. We do this early to make sure
1491 * that the request is fully prepared even if we cannot
1492 * accept it.
1494 req = blk_peek_request(q);
1495 if (!req || !scsi_dev_queue_ready(q, sdev))
1496 break;
1498 if (unlikely(!scsi_device_online(sdev))) {
1499 sdev_printk(KERN_ERR, sdev,
1500 "rejecting I/O to offline device\n");
1501 scsi_kill_request(req, q);
1502 continue;
1507 * Remove the request from the request list.
1509 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
1510 blk_start_request(req);
1511 sdev->device_busy++;
1513 spin_unlock(q->queue_lock);
1514 cmd = req->special;
1515 if (unlikely(cmd == NULL)) {
1516 printk(KERN_CRIT "impossible request in %s.\n"
1517 "please mail a stack trace to "
1518 "linux-scsi@vger.kernel.org\n",
1519 __func__);
1520 blk_dump_rq_flags(req, "foo");
1521 BUG();
1523 spin_lock(shost->host_lock);
1526 * We hit this when the driver is using a host wide
1527 * tag map. For device level tag maps the queue_depth check
1528 * in the device ready fn would prevent us from trying
1529 * to allocate a tag. Since the map is a shared host resource
1530 * we add the dev to the starved list so it eventually gets
1531 * a run when a tag is freed.
1533 if (blk_queue_tagged(q) && !blk_rq_tagged(req)) {
1534 if (list_empty(&sdev->starved_entry))
1535 list_add_tail(&sdev->starved_entry,
1536 &shost->starved_list);
1537 goto not_ready;
1540 if (!scsi_target_queue_ready(shost, sdev))
1541 goto not_ready;
1543 if (!scsi_host_queue_ready(q, shost, sdev))
1544 goto not_ready;
1546 scsi_target(sdev)->target_busy++;
1547 shost->host_busy++;
1550 * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
1551 * take the lock again.
1553 spin_unlock_irq(shost->host_lock);
1556 * Finally, initialize any error handling parameters, and set up
1557 * the timers for timeouts.
1559 scsi_init_cmd_errh(cmd);
1562 * Dispatch the command to the low-level driver.
1564 rtn = scsi_dispatch_cmd(cmd);
1565 spin_lock_irq(q->queue_lock);
1566 if(rtn) {
1567 /* we're refusing the command; because of
1568 * the way locks get dropped, we need to
1569 * check here if plugging is required */
1570 if(sdev->device_busy == 0)
1571 blk_plug_device(q);
1573 break;
1577 goto out;
1579 not_ready:
1580 spin_unlock_irq(shost->host_lock);
1583 * lock q, handle tag, requeue req, and decrement device_busy. We
1584 * must return with queue_lock held.
1586 * Decrementing device_busy without checking it is OK, as all such
1587 * cases (host limits or settings) should run the queue at some
1588 * later time.
1590 spin_lock_irq(q->queue_lock);
1591 blk_requeue_request(q, req);
1592 sdev->device_busy--;
1593 if(sdev->device_busy == 0)
1594 blk_plug_device(q);
1595 out:
1596 /* must be careful here...if we trigger the ->remove() function
1597 * we cannot be holding the q lock */
1598 spin_unlock_irq(q->queue_lock);
1599 put_device(&sdev->sdev_gendev);
1600 spin_lock_irq(q->queue_lock);
1603 u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
1605 struct device *host_dev;
1606 u64 bounce_limit = 0xffffffff;
1608 if (shost->unchecked_isa_dma)
1609 return BLK_BOUNCE_ISA;
1611 * Platforms with virtual-DMA translation
1612 * hardware have no practical limit.
1614 if (!PCI_DMA_BUS_IS_PHYS)
1615 return BLK_BOUNCE_ANY;
1617 host_dev = scsi_get_device(shost);
1618 if (host_dev && host_dev->dma_mask)
1619 bounce_limit = *host_dev->dma_mask;
1621 return bounce_limit;
1623 EXPORT_SYMBOL(scsi_calculate_bounce_limit);
1625 struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
1626 request_fn_proc *request_fn)
1628 struct request_queue *q;
1629 struct device *dev = shost->shost_gendev.parent;
1631 q = blk_init_queue(request_fn, NULL);
1632 if (!q)
1633 return NULL;
1636 * this limit is imposed by hardware restrictions
1638 blk_queue_max_hw_segments(q, shost->sg_tablesize);
1639 blk_queue_max_phys_segments(q, SCSI_MAX_SG_CHAIN_SEGMENTS);
1641 blk_queue_max_sectors(q, shost->max_sectors);
1642 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
1643 blk_queue_segment_boundary(q, shost->dma_boundary);
1644 dma_set_seg_boundary(dev, shost->dma_boundary);
1646 blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
1648 /* New queue, no concurrency on queue_flags */
1649 if (!shost->use_clustering)
1650 queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
1653 * set a reasonable default alignment on word boundaries: the
1654 * host and device may alter it using
1655 * blk_queue_update_dma_alignment() later.
1657 blk_queue_dma_alignment(q, 0x03);
1659 return q;
1661 EXPORT_SYMBOL(__scsi_alloc_queue);
1663 struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
1665 struct request_queue *q;
1667 q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
1668 if (!q)
1669 return NULL;
1671 blk_queue_prep_rq(q, scsi_prep_fn);
1672 blk_queue_softirq_done(q, scsi_softirq_done);
1673 blk_queue_rq_timed_out(q, scsi_times_out);
1674 blk_queue_lld_busy(q, scsi_lld_busy);
1675 return q;
1678 void scsi_free_queue(struct request_queue *q)
1680 blk_cleanup_queue(q);
1684 * Function: scsi_block_requests()
1686 * Purpose: Utility function used by low-level drivers to prevent further
1687 * commands from being queued to the device.
1689 * Arguments: shost - Host in question
1691 * Returns: Nothing
1693 * Lock status: No locks are assumed held.
1695 * Notes: There is no timer nor any other means by which the requests
1696 * get unblocked other than the low-level driver calling
1697 * scsi_unblock_requests().
1699 void scsi_block_requests(struct Scsi_Host *shost)
1701 shost->host_self_blocked = 1;
1703 EXPORT_SYMBOL(scsi_block_requests);
1706 * Function: scsi_unblock_requests()
1708 * Purpose: Utility function used by low-level drivers to allow further
1709 * commands from being queued to the device.
1711 * Arguments: shost - Host in question
1713 * Returns: Nothing
1715 * Lock status: No locks are assumed held.
1717 * Notes: There is no timer nor any other means by which the requests
1718 * get unblocked other than the low-level driver calling
1719 * scsi_unblock_requests().
1721 * This is done as an API function so that changes to the
1722 * internals of the scsi mid-layer won't require wholesale
1723 * changes to drivers that use this feature.
1725 void scsi_unblock_requests(struct Scsi_Host *shost)
1727 shost->host_self_blocked = 0;
1728 scsi_run_host_queues(shost);
1730 EXPORT_SYMBOL(scsi_unblock_requests);
1732 int __init scsi_init_queue(void)
1734 int i;
1736 scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
1737 sizeof(struct scsi_data_buffer),
1738 0, 0, NULL);
1739 if (!scsi_sdb_cache) {
1740 printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
1741 return -ENOMEM;
1744 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1745 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1746 int size = sgp->size * sizeof(struct scatterlist);
1748 sgp->slab = kmem_cache_create(sgp->name, size, 0,
1749 SLAB_HWCACHE_ALIGN, NULL);
1750 if (!sgp->slab) {
1751 printk(KERN_ERR "SCSI: can't init sg slab %s\n",
1752 sgp->name);
1753 goto cleanup_sdb;
1756 sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
1757 sgp->slab);
1758 if (!sgp->pool) {
1759 printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
1760 sgp->name);
1761 goto cleanup_sdb;
1765 return 0;
1767 cleanup_sdb:
1768 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1769 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1770 if (sgp->pool)
1771 mempool_destroy(sgp->pool);
1772 if (sgp->slab)
1773 kmem_cache_destroy(sgp->slab);
1775 kmem_cache_destroy(scsi_sdb_cache);
1777 return -ENOMEM;
1780 void scsi_exit_queue(void)
1782 int i;
1784 kmem_cache_destroy(scsi_sdb_cache);
1786 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1787 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1788 mempool_destroy(sgp->pool);
1789 kmem_cache_destroy(sgp->slab);
1794 * scsi_mode_select - issue a mode select
1795 * @sdev: SCSI device to be queried
1796 * @pf: Page format bit (1 == standard, 0 == vendor specific)
1797 * @sp: Save page bit (0 == don't save, 1 == save)
1798 * @modepage: mode page being requested
1799 * @buffer: request buffer (may not be smaller than eight bytes)
1800 * @len: length of request buffer.
1801 * @timeout: command timeout
1802 * @retries: number of retries before failing
1803 * @data: returns a structure abstracting the mode header data
1804 * @sshdr: place to put sense data (or NULL if no sense to be collected).
1805 * must be SCSI_SENSE_BUFFERSIZE big.
1807 * Returns zero if successful; negative error number or scsi
1808 * status on error
1812 scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
1813 unsigned char *buffer, int len, int timeout, int retries,
1814 struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
1816 unsigned char cmd[10];
1817 unsigned char *real_buffer;
1818 int ret;
1820 memset(cmd, 0, sizeof(cmd));
1821 cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
1823 if (sdev->use_10_for_ms) {
1824 if (len > 65535)
1825 return -EINVAL;
1826 real_buffer = kmalloc(8 + len, GFP_KERNEL);
1827 if (!real_buffer)
1828 return -ENOMEM;
1829 memcpy(real_buffer + 8, buffer, len);
1830 len += 8;
1831 real_buffer[0] = 0;
1832 real_buffer[1] = 0;
1833 real_buffer[2] = data->medium_type;
1834 real_buffer[3] = data->device_specific;
1835 real_buffer[4] = data->longlba ? 0x01 : 0;
1836 real_buffer[5] = 0;
1837 real_buffer[6] = data->block_descriptor_length >> 8;
1838 real_buffer[7] = data->block_descriptor_length;
1840 cmd[0] = MODE_SELECT_10;
1841 cmd[7] = len >> 8;
1842 cmd[8] = len;
1843 } else {
1844 if (len > 255 || data->block_descriptor_length > 255 ||
1845 data->longlba)
1846 return -EINVAL;
1848 real_buffer = kmalloc(4 + len, GFP_KERNEL);
1849 if (!real_buffer)
1850 return -ENOMEM;
1851 memcpy(real_buffer + 4, buffer, len);
1852 len += 4;
1853 real_buffer[0] = 0;
1854 real_buffer[1] = data->medium_type;
1855 real_buffer[2] = data->device_specific;
1856 real_buffer[3] = data->block_descriptor_length;
1859 cmd[0] = MODE_SELECT;
1860 cmd[4] = len;
1863 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
1864 sshdr, timeout, retries, NULL);
1865 kfree(real_buffer);
1866 return ret;
1868 EXPORT_SYMBOL_GPL(scsi_mode_select);
1871 * scsi_mode_sense - issue a mode sense, falling back from 10 to 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 * @sshdr: 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];
1893 int use_10_for_ms;
1894 int header_length;
1895 int result;
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 */
1901 cmd[2] = modepage;
1903 /* caller might not be interested in sense, but we need it */
1904 if (!sshdr)
1905 sshdr = &my_sshdr;
1907 retry:
1908 use_10_for_ms = sdev->use_10_for_ms;
1910 if (use_10_for_ms) {
1911 if (len < 8)
1912 len = 8;
1914 cmd[0] = MODE_SENSE_10;
1915 cmd[8] = len;
1916 header_length = 8;
1917 } else {
1918 if (len < 4)
1919 len = 4;
1921 cmd[0] = MODE_SENSE;
1922 cmd[4] = len;
1923 header_length = 4;
1926 memset(buffer, 0, len);
1928 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
1929 sshdr, timeout, retries, NULL);
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;
1945 goto retry;
1950 if(scsi_status_is_good(result)) {
1951 if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
1952 (modepage == 6 || modepage == 8))) {
1953 /* Initio breakage? */
1954 header_length = 0;
1955 data->length = 13;
1956 data->medium_type = 0;
1957 data->device_specific = 0;
1958 data->longlba = 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
1966 + buffer[7];
1967 } else {
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;
1976 return result;
1978 EXPORT_SYMBOL(scsi_mode_sense);
1981 * scsi_test_unit_ready - test if unit is ready
1982 * @sdev: scsi device to change the state of.
1983 * @timeout: command timeout
1984 * @retries: number of retries before failing
1985 * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
1986 * returning sense. Make sure that this is cleared before passing
1987 * in.
1989 * Returns zero if unsuccessful or an error if TUR failed. For
1990 * removable media, a return of NOT_READY or UNIT_ATTENTION is
1991 * translated to success, with the ->changed flag updated.
1994 scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
1995 struct scsi_sense_hdr *sshdr_external)
1997 char cmd[] = {
1998 TEST_UNIT_READY, 0, 0, 0, 0, 0,
2000 struct scsi_sense_hdr *sshdr;
2001 int result;
2003 if (!sshdr_external)
2004 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
2005 else
2006 sshdr = sshdr_external;
2008 /* try to eat the UNIT_ATTENTION if there are enough retries */
2009 do {
2010 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
2011 timeout, retries, NULL);
2012 if (sdev->removable && scsi_sense_valid(sshdr) &&
2013 sshdr->sense_key == UNIT_ATTENTION)
2014 sdev->changed = 1;
2015 } while (scsi_sense_valid(sshdr) &&
2016 sshdr->sense_key == UNIT_ATTENTION && --retries);
2018 if (!sshdr)
2019 /* could not allocate sense buffer, so can't process it */
2020 return result;
2022 if (sdev->removable && scsi_sense_valid(sshdr) &&
2023 (sshdr->sense_key == UNIT_ATTENTION ||
2024 sshdr->sense_key == NOT_READY)) {
2025 sdev->changed = 1;
2026 result = 0;
2028 if (!sshdr_external)
2029 kfree(sshdr);
2030 return result;
2032 EXPORT_SYMBOL(scsi_test_unit_ready);
2035 * scsi_device_set_state - Take the given device through the device state model.
2036 * @sdev: scsi device to change the state of.
2037 * @state: state to change to.
2039 * Returns zero if unsuccessful or an error if the requested
2040 * transition is illegal.
2043 scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2045 enum scsi_device_state oldstate = sdev->sdev_state;
2047 if (state == oldstate)
2048 return 0;
2050 switch (state) {
2051 case SDEV_CREATED:
2052 switch (oldstate) {
2053 case SDEV_CREATED_BLOCK:
2054 break;
2055 default:
2056 goto illegal;
2058 break;
2060 case SDEV_RUNNING:
2061 switch (oldstate) {
2062 case SDEV_CREATED:
2063 case SDEV_OFFLINE:
2064 case SDEV_QUIESCE:
2065 case SDEV_BLOCK:
2066 break;
2067 default:
2068 goto illegal;
2070 break;
2072 case SDEV_QUIESCE:
2073 switch (oldstate) {
2074 case SDEV_RUNNING:
2075 case SDEV_OFFLINE:
2076 break;
2077 default:
2078 goto illegal;
2080 break;
2082 case SDEV_OFFLINE:
2083 switch (oldstate) {
2084 case SDEV_CREATED:
2085 case SDEV_RUNNING:
2086 case SDEV_QUIESCE:
2087 case SDEV_BLOCK:
2088 break;
2089 default:
2090 goto illegal;
2092 break;
2094 case SDEV_BLOCK:
2095 switch (oldstate) {
2096 case SDEV_RUNNING:
2097 case SDEV_CREATED_BLOCK:
2098 break;
2099 default:
2100 goto illegal;
2102 break;
2104 case SDEV_CREATED_BLOCK:
2105 switch (oldstate) {
2106 case SDEV_CREATED:
2107 break;
2108 default:
2109 goto illegal;
2111 break;
2113 case SDEV_CANCEL:
2114 switch (oldstate) {
2115 case SDEV_CREATED:
2116 case SDEV_RUNNING:
2117 case SDEV_QUIESCE:
2118 case SDEV_OFFLINE:
2119 case SDEV_BLOCK:
2120 break;
2121 default:
2122 goto illegal;
2124 break;
2126 case SDEV_DEL:
2127 switch (oldstate) {
2128 case SDEV_CREATED:
2129 case SDEV_RUNNING:
2130 case SDEV_OFFLINE:
2131 case SDEV_CANCEL:
2132 break;
2133 default:
2134 goto illegal;
2136 break;
2139 sdev->sdev_state = state;
2140 return 0;
2142 illegal:
2143 SCSI_LOG_ERROR_RECOVERY(1,
2144 sdev_printk(KERN_ERR, sdev,
2145 "Illegal state transition %s->%s\n",
2146 scsi_device_state_name(oldstate),
2147 scsi_device_state_name(state))
2149 return -EINVAL;
2151 EXPORT_SYMBOL(scsi_device_set_state);
2154 * sdev_evt_emit - emit a single SCSI device uevent
2155 * @sdev: associated SCSI device
2156 * @evt: event to emit
2158 * Send a single uevent (scsi_event) to the associated scsi_device.
2160 static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2162 int idx = 0;
2163 char *envp[3];
2165 switch (evt->evt_type) {
2166 case SDEV_EVT_MEDIA_CHANGE:
2167 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2168 break;
2170 default:
2171 /* do nothing */
2172 break;
2175 envp[idx++] = NULL;
2177 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2181 * sdev_evt_thread - send a uevent for each scsi event
2182 * @work: work struct for scsi_device
2184 * Dispatch queued events to their associated scsi_device kobjects
2185 * as uevents.
2187 void scsi_evt_thread(struct work_struct *work)
2189 struct scsi_device *sdev;
2190 LIST_HEAD(event_list);
2192 sdev = container_of(work, struct scsi_device, event_work);
2194 while (1) {
2195 struct scsi_event *evt;
2196 struct list_head *this, *tmp;
2197 unsigned long flags;
2199 spin_lock_irqsave(&sdev->list_lock, flags);
2200 list_splice_init(&sdev->event_list, &event_list);
2201 spin_unlock_irqrestore(&sdev->list_lock, flags);
2203 if (list_empty(&event_list))
2204 break;
2206 list_for_each_safe(this, tmp, &event_list) {
2207 evt = list_entry(this, struct scsi_event, node);
2208 list_del(&evt->node);
2209 scsi_evt_emit(sdev, evt);
2210 kfree(evt);
2216 * sdev_evt_send - send asserted event to uevent thread
2217 * @sdev: scsi_device event occurred on
2218 * @evt: event to send
2220 * Assert scsi device event asynchronously.
2222 void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2224 unsigned long flags;
2226 #if 0
2227 /* FIXME: currently this check eliminates all media change events
2228 * for polled devices. Need to update to discriminate between AN
2229 * and polled events */
2230 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2231 kfree(evt);
2232 return;
2234 #endif
2236 spin_lock_irqsave(&sdev->list_lock, flags);
2237 list_add_tail(&evt->node, &sdev->event_list);
2238 schedule_work(&sdev->event_work);
2239 spin_unlock_irqrestore(&sdev->list_lock, flags);
2241 EXPORT_SYMBOL_GPL(sdev_evt_send);
2244 * sdev_evt_alloc - allocate a new scsi event
2245 * @evt_type: type of event to allocate
2246 * @gfpflags: GFP flags for allocation
2248 * Allocates and returns a new scsi_event.
2250 struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2251 gfp_t gfpflags)
2253 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2254 if (!evt)
2255 return NULL;
2257 evt->evt_type = evt_type;
2258 INIT_LIST_HEAD(&evt->node);
2260 /* evt_type-specific initialization, if any */
2261 switch (evt_type) {
2262 case SDEV_EVT_MEDIA_CHANGE:
2263 default:
2264 /* do nothing */
2265 break;
2268 return evt;
2270 EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2273 * sdev_evt_send_simple - send asserted event to uevent thread
2274 * @sdev: scsi_device event occurred on
2275 * @evt_type: type of event to send
2276 * @gfpflags: GFP flags for allocation
2278 * Assert scsi device event asynchronously, given an event type.
2280 void sdev_evt_send_simple(struct scsi_device *sdev,
2281 enum scsi_device_event evt_type, gfp_t gfpflags)
2283 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2284 if (!evt) {
2285 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2286 evt_type);
2287 return;
2290 sdev_evt_send(sdev, evt);
2292 EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2295 * scsi_device_quiesce - Block user issued commands.
2296 * @sdev: scsi device to quiesce.
2298 * This works by trying to transition to the SDEV_QUIESCE state
2299 * (which must be a legal transition). When the device is in this
2300 * state, only special requests will be accepted, all others will
2301 * be deferred. Since special requests may also be requeued requests,
2302 * a successful return doesn't guarantee the device will be
2303 * totally quiescent.
2305 * Must be called with user context, may sleep.
2307 * Returns zero if unsuccessful or an error if not.
2310 scsi_device_quiesce(struct scsi_device *sdev)
2312 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
2313 if (err)
2314 return err;
2316 scsi_run_queue(sdev->request_queue);
2317 while (sdev->device_busy) {
2318 msleep_interruptible(200);
2319 scsi_run_queue(sdev->request_queue);
2321 return 0;
2323 EXPORT_SYMBOL(scsi_device_quiesce);
2326 * scsi_device_resume - Restart user issued commands to a quiesced device.
2327 * @sdev: scsi device to resume.
2329 * Moves the device from quiesced back to running and restarts the
2330 * queues.
2332 * Must be called with user context, may sleep.
2334 void
2335 scsi_device_resume(struct scsi_device *sdev)
2337 if(scsi_device_set_state(sdev, SDEV_RUNNING))
2338 return;
2339 scsi_run_queue(sdev->request_queue);
2341 EXPORT_SYMBOL(scsi_device_resume);
2343 static void
2344 device_quiesce_fn(struct scsi_device *sdev, void *data)
2346 scsi_device_quiesce(sdev);
2349 void
2350 scsi_target_quiesce(struct scsi_target *starget)
2352 starget_for_each_device(starget, NULL, device_quiesce_fn);
2354 EXPORT_SYMBOL(scsi_target_quiesce);
2356 static void
2357 device_resume_fn(struct scsi_device *sdev, void *data)
2359 scsi_device_resume(sdev);
2362 void
2363 scsi_target_resume(struct scsi_target *starget)
2365 starget_for_each_device(starget, NULL, device_resume_fn);
2367 EXPORT_SYMBOL(scsi_target_resume);
2370 * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
2371 * @sdev: device to block
2373 * Block request made by scsi lld's to temporarily stop all
2374 * scsi commands on the specified device. Called from interrupt
2375 * or normal process context.
2377 * Returns zero if successful or error if not
2379 * Notes:
2380 * This routine transitions the device to the SDEV_BLOCK state
2381 * (which must be a legal transition). When the device is in this
2382 * state, all commands are deferred until the scsi lld reenables
2383 * the device with scsi_device_unblock or device_block_tmo fires.
2384 * This routine assumes the host_lock is held on entry.
2387 scsi_internal_device_block(struct scsi_device *sdev)
2389 struct request_queue *q = sdev->request_queue;
2390 unsigned long flags;
2391 int err = 0;
2393 err = scsi_device_set_state(sdev, SDEV_BLOCK);
2394 if (err) {
2395 err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
2397 if (err)
2398 return err;
2402 * The device has transitioned to SDEV_BLOCK. Stop the
2403 * block layer from calling the midlayer with this device's
2404 * request queue.
2406 spin_lock_irqsave(q->queue_lock, flags);
2407 blk_stop_queue(q);
2408 spin_unlock_irqrestore(q->queue_lock, flags);
2410 return 0;
2412 EXPORT_SYMBOL_GPL(scsi_internal_device_block);
2415 * scsi_internal_device_unblock - resume a device after a block request
2416 * @sdev: device to resume
2418 * Called by scsi lld's or the midlayer to restart the device queue
2419 * for the previously suspended scsi device. Called from interrupt or
2420 * normal process context.
2422 * Returns zero if successful or error if not.
2424 * Notes:
2425 * This routine transitions the device to the SDEV_RUNNING state
2426 * (which must be a legal transition) allowing the midlayer to
2427 * goose the queue for this device. This routine assumes the
2428 * host_lock is held upon entry.
2431 scsi_internal_device_unblock(struct scsi_device *sdev)
2433 struct request_queue *q = sdev->request_queue;
2434 unsigned long flags;
2437 * Try to transition the scsi device to SDEV_RUNNING
2438 * and goose the device queue if successful.
2440 if (sdev->sdev_state == SDEV_BLOCK)
2441 sdev->sdev_state = SDEV_RUNNING;
2442 else if (sdev->sdev_state == SDEV_CREATED_BLOCK)
2443 sdev->sdev_state = SDEV_CREATED;
2444 else if (sdev->sdev_state != SDEV_CANCEL &&
2445 sdev->sdev_state != SDEV_OFFLINE)
2446 return -EINVAL;
2448 spin_lock_irqsave(q->queue_lock, flags);
2449 blk_start_queue(q);
2450 spin_unlock_irqrestore(q->queue_lock, flags);
2452 return 0;
2454 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
2456 static void
2457 device_block(struct scsi_device *sdev, void *data)
2459 scsi_internal_device_block(sdev);
2462 static int
2463 target_block(struct device *dev, void *data)
2465 if (scsi_is_target_device(dev))
2466 starget_for_each_device(to_scsi_target(dev), NULL,
2467 device_block);
2468 return 0;
2471 void
2472 scsi_target_block(struct device *dev)
2474 if (scsi_is_target_device(dev))
2475 starget_for_each_device(to_scsi_target(dev), NULL,
2476 device_block);
2477 else
2478 device_for_each_child(dev, NULL, target_block);
2480 EXPORT_SYMBOL_GPL(scsi_target_block);
2482 static void
2483 device_unblock(struct scsi_device *sdev, void *data)
2485 scsi_internal_device_unblock(sdev);
2488 static int
2489 target_unblock(struct device *dev, void *data)
2491 if (scsi_is_target_device(dev))
2492 starget_for_each_device(to_scsi_target(dev), NULL,
2493 device_unblock);
2494 return 0;
2497 void
2498 scsi_target_unblock(struct device *dev)
2500 if (scsi_is_target_device(dev))
2501 starget_for_each_device(to_scsi_target(dev), NULL,
2502 device_unblock);
2503 else
2504 device_for_each_child(dev, NULL, target_unblock);
2506 EXPORT_SYMBOL_GPL(scsi_target_unblock);
2509 * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
2510 * @sgl: scatter-gather list
2511 * @sg_count: number of segments in sg
2512 * @offset: offset in bytes into sg, on return offset into the mapped area
2513 * @len: bytes to map, on return number of bytes mapped
2515 * Returns virtual address of the start of the mapped page
2517 void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
2518 size_t *offset, size_t *len)
2520 int i;
2521 size_t sg_len = 0, len_complete = 0;
2522 struct scatterlist *sg;
2523 struct page *page;
2525 WARN_ON(!irqs_disabled());
2527 for_each_sg(sgl, sg, sg_count, i) {
2528 len_complete = sg_len; /* Complete sg-entries */
2529 sg_len += sg->length;
2530 if (sg_len > *offset)
2531 break;
2534 if (unlikely(i == sg_count)) {
2535 printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
2536 "elements %d\n",
2537 __func__, sg_len, *offset, sg_count);
2538 WARN_ON(1);
2539 return NULL;
2542 /* Offset starting from the beginning of first page in this sg-entry */
2543 *offset = *offset - len_complete + sg->offset;
2545 /* Assumption: contiguous pages can be accessed as "page + i" */
2546 page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
2547 *offset &= ~PAGE_MASK;
2549 /* Bytes in this sg-entry from *offset to the end of the page */
2550 sg_len = PAGE_SIZE - *offset;
2551 if (*len > sg_len)
2552 *len = sg_len;
2554 return kmap_atomic(page, KM_BIO_SRC_IRQ);
2556 EXPORT_SYMBOL(scsi_kmap_atomic_sg);
2559 * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
2560 * @virt: virtual address to be unmapped
2562 void scsi_kunmap_atomic_sg(void *virt)
2564 kunmap_atomic(virt, KM_BIO_SRC_IRQ);
2566 EXPORT_SYMBOL(scsi_kunmap_atomic_sg);