Merge HEAD from ../scsi-misc-2.6-tmp
[linux-2.6/verdex.git] / drivers / scsi / scsi_lib.c
blob278e0c99b2aecf287430df8f0c1ca7da13f32c1f
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/blkdev.h>
12 #include <linux/completion.h>
13 #include <linux/kernel.h>
14 #include <linux/mempool.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/delay.h>
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_dbg.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/scsi_driver.h>
24 #include <scsi/scsi_eh.h>
25 #include <scsi/scsi_host.h>
26 #include <scsi/scsi_request.h>
28 #include "scsi_priv.h"
29 #include "scsi_logging.h"
32 #define SG_MEMPOOL_NR (sizeof(scsi_sg_pools)/sizeof(struct scsi_host_sg_pool))
33 #define SG_MEMPOOL_SIZE 32
35 struct scsi_host_sg_pool {
36 size_t size;
37 char *name;
38 kmem_cache_t *slab;
39 mempool_t *pool;
42 #if (SCSI_MAX_PHYS_SEGMENTS < 32)
43 #error SCSI_MAX_PHYS_SEGMENTS is too small
44 #endif
46 #define SP(x) { x, "sgpool-" #x }
47 static struct scsi_host_sg_pool scsi_sg_pools[] = {
48 SP(8),
49 SP(16),
50 SP(32),
51 #if (SCSI_MAX_PHYS_SEGMENTS > 32)
52 SP(64),
53 #if (SCSI_MAX_PHYS_SEGMENTS > 64)
54 SP(128),
55 #if (SCSI_MAX_PHYS_SEGMENTS > 128)
56 SP(256),
57 #if (SCSI_MAX_PHYS_SEGMENTS > 256)
58 #error SCSI_MAX_PHYS_SEGMENTS is too large
59 #endif
60 #endif
61 #endif
62 #endif
63 };
64 #undef SP
68 * Function: scsi_insert_special_req()
70 * Purpose: Insert pre-formed request into request queue.
72 * Arguments: sreq - request that is ready to be queued.
73 * at_head - boolean. True if we should insert at head
74 * of queue, false if we should insert at tail.
76 * Lock status: Assumed that lock is not held upon entry.
78 * Returns: Nothing
80 * Notes: This function is called from character device and from
81 * ioctl types of functions where the caller knows exactly
82 * what SCSI command needs to be issued. The idea is that
83 * we merely inject the command into the queue (at the head
84 * for now), and then call the queue request function to actually
85 * process it.
87 int scsi_insert_special_req(struct scsi_request *sreq, int at_head)
90 * Because users of this function are apt to reuse requests with no
91 * modification, we have to sanitise the request flags here
93 sreq->sr_request->flags &= ~REQ_DONTPREP;
94 blk_insert_request(sreq->sr_device->request_queue, sreq->sr_request,
95 at_head, sreq);
96 return 0;
99 static void scsi_run_queue(struct request_queue *q);
102 * Function: scsi_queue_insert()
104 * Purpose: Insert a command in the midlevel queue.
106 * Arguments: cmd - command that we are adding to queue.
107 * reason - why we are inserting command to queue.
109 * Lock status: Assumed that lock is not held upon entry.
111 * Returns: Nothing.
113 * Notes: We do this for one of two cases. Either the host is busy
114 * and it cannot accept any more commands for the time being,
115 * or the device returned QUEUE_FULL and can accept no more
116 * commands.
117 * Notes: This could be called either from an interrupt context or a
118 * normal process context.
120 int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
122 struct Scsi_Host *host = cmd->device->host;
123 struct scsi_device *device = cmd->device;
124 struct request_queue *q = device->request_queue;
125 unsigned long flags;
127 SCSI_LOG_MLQUEUE(1,
128 printk("Inserting command %p into mlqueue\n", cmd));
131 * Set the appropriate busy bit for the device/host.
133 * If the host/device isn't busy, assume that something actually
134 * completed, and that we should be able to queue a command now.
136 * Note that the prior mid-layer assumption that any host could
137 * always queue at least one command is now broken. The mid-layer
138 * will implement a user specifiable stall (see
139 * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
140 * if a command is requeued with no other commands outstanding
141 * either for the device or for the host.
143 if (reason == SCSI_MLQUEUE_HOST_BUSY)
144 host->host_blocked = host->max_host_blocked;
145 else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
146 device->device_blocked = device->max_device_blocked;
149 * Decrement the counters, since these commands are no longer
150 * active on the host/device.
152 scsi_device_unbusy(device);
155 * Requeue this command. It will go before all other commands
156 * that are already in the queue.
158 * NOTE: there is magic here about the way the queue is plugged if
159 * we have no outstanding commands.
161 * Although we *don't* plug the queue, we call the request
162 * function. The SCSI request function detects the blocked condition
163 * and plugs the queue appropriately.
165 spin_lock_irqsave(q->queue_lock, flags);
166 blk_requeue_request(q, cmd->request);
167 spin_unlock_irqrestore(q->queue_lock, flags);
169 scsi_run_queue(q);
171 return 0;
175 * Function: scsi_do_req
177 * Purpose: Queue a SCSI request
179 * Arguments: sreq - command descriptor.
180 * cmnd - actual SCSI command to be performed.
181 * buffer - data buffer.
182 * bufflen - size of data buffer.
183 * done - completion function to be run.
184 * timeout - how long to let it run before timeout.
185 * retries - number of retries we allow.
187 * Lock status: No locks held upon entry.
189 * Returns: Nothing.
191 * Notes: This function is only used for queueing requests for things
192 * like ioctls and character device requests - this is because
193 * we essentially just inject a request into the queue for the
194 * device.
196 * In order to support the scsi_device_quiesce function, we
197 * now inject requests on the *head* of the device queue
198 * rather than the tail.
200 void scsi_do_req(struct scsi_request *sreq, const void *cmnd,
201 void *buffer, unsigned bufflen,
202 void (*done)(struct scsi_cmnd *),
203 int timeout, int retries)
206 * If the upper level driver is reusing these things, then
207 * we should release the low-level block now. Another one will
208 * be allocated later when this request is getting queued.
210 __scsi_release_request(sreq);
213 * Our own function scsi_done (which marks the host as not busy,
214 * disables the timeout counter, etc) will be called by us or by the
215 * scsi_hosts[host].queuecommand() function needs to also call
216 * the completion function for the high level driver.
218 memcpy(sreq->sr_cmnd, cmnd, sizeof(sreq->sr_cmnd));
219 sreq->sr_bufflen = bufflen;
220 sreq->sr_buffer = buffer;
221 sreq->sr_allowed = retries;
222 sreq->sr_done = done;
223 sreq->sr_timeout_per_command = timeout;
225 if (sreq->sr_cmd_len == 0)
226 sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
229 * head injection *required* here otherwise quiesce won't work
231 scsi_insert_special_req(sreq, 1);
233 EXPORT_SYMBOL(scsi_do_req);
235 /* This is the end routine we get to if a command was never attached
236 * to the request. Simply complete the request without changing
237 * rq_status; this will cause a DRIVER_ERROR. */
238 static void scsi_wait_req_end_io(struct request *req)
240 BUG_ON(!req->waiting);
242 complete(req->waiting);
245 void scsi_wait_req(struct scsi_request *sreq, const void *cmnd, void *buffer,
246 unsigned bufflen, int timeout, int retries)
248 DECLARE_COMPLETION(wait);
249 int write = (sreq->sr_data_direction == DMA_TO_DEVICE);
250 struct request *req;
252 req = blk_get_request(sreq->sr_device->request_queue, write,
253 __GFP_WAIT);
254 if (bufflen && blk_rq_map_kern(sreq->sr_device->request_queue, req,
255 buffer, bufflen, __GFP_WAIT)) {
256 sreq->sr_result = DRIVER_ERROR << 24;
257 blk_put_request(req);
258 return;
261 req->flags |= REQ_NOMERGE;
262 req->waiting = &wait;
263 req->end_io = scsi_wait_req_end_io;
264 req->cmd_len = COMMAND_SIZE(((u8 *)cmnd)[0]);
265 req->sense = sreq->sr_sense_buffer;
266 req->sense_len = 0;
267 memcpy(req->cmd, cmnd, req->cmd_len);
268 req->timeout = timeout;
269 req->flags |= REQ_BLOCK_PC;
270 req->rq_disk = NULL;
271 blk_insert_request(sreq->sr_device->request_queue, req,
272 sreq->sr_data_direction == DMA_TO_DEVICE, NULL);
273 wait_for_completion(&wait);
274 sreq->sr_request->waiting = NULL;
275 sreq->sr_result = req->errors;
276 if (req->errors)
277 sreq->sr_result |= (DRIVER_ERROR << 24);
279 blk_put_request(req);
282 EXPORT_SYMBOL(scsi_wait_req);
285 * scsi_execute_req - insert request and wait for the result
286 * @sdev: scsi device
287 * @cmd: scsi command
288 * @data_direction: data direction
289 * @buffer: data buffer
290 * @bufflen: len of buffer
291 * @sense: optional sense buffer
292 * @timeout: request timeout in seconds
293 * @retries: number of times to retry request
295 * scsi_execute_req returns the req->errors value which is the
296 * the scsi_cmnd result field.
298 int scsi_execute_req(struct scsi_device *sdev, unsigned char *cmd,
299 int data_direction, void *buffer, unsigned bufflen,
300 unsigned char *sense, int timeout, int retries)
302 struct request *req;
303 int write = (data_direction == DMA_TO_DEVICE);
304 int ret = DRIVER_ERROR << 24;
306 req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
308 if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
309 buffer, bufflen, __GFP_WAIT))
310 goto out;
312 req->cmd_len = COMMAND_SIZE(cmd[0]);
313 memcpy(req->cmd, cmd, req->cmd_len);
314 req->sense = sense;
315 req->sense_len = 0;
316 req->timeout = timeout;
317 req->flags |= REQ_BLOCK_PC | REQ_SPECIAL;
320 * head injection *required* here otherwise quiesce won't work
322 blk_execute_rq(req->q, NULL, req, 1);
324 ret = req->errors;
325 out:
326 blk_put_request(req);
328 return ret;
331 EXPORT_SYMBOL(scsi_execute_req);
334 * Function: scsi_init_cmd_errh()
336 * Purpose: Initialize cmd fields related to error handling.
338 * Arguments: cmd - command that is ready to be queued.
340 * Returns: Nothing
342 * Notes: This function has the job of initializing a number of
343 * fields related to error handling. Typically this will
344 * be called once for each command, as required.
346 static int scsi_init_cmd_errh(struct scsi_cmnd *cmd)
348 cmd->serial_number = 0;
350 memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
352 if (cmd->cmd_len == 0)
353 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
356 * We need saved copies of a number of fields - this is because
357 * error handling may need to overwrite these with different values
358 * to run different commands, and once error handling is complete,
359 * we will need to restore these values prior to running the actual
360 * command.
362 cmd->old_use_sg = cmd->use_sg;
363 cmd->old_cmd_len = cmd->cmd_len;
364 cmd->sc_old_data_direction = cmd->sc_data_direction;
365 cmd->old_underflow = cmd->underflow;
366 memcpy(cmd->data_cmnd, cmd->cmnd, sizeof(cmd->cmnd));
367 cmd->buffer = cmd->request_buffer;
368 cmd->bufflen = cmd->request_bufflen;
370 return 1;
374 * Function: scsi_setup_cmd_retry()
376 * Purpose: Restore the command state for a retry
378 * Arguments: cmd - command to be restored
380 * Returns: Nothing
382 * Notes: Immediately prior to retrying a command, we need
383 * to restore certain fields that we saved above.
385 void scsi_setup_cmd_retry(struct scsi_cmnd *cmd)
387 memcpy(cmd->cmnd, cmd->data_cmnd, sizeof(cmd->data_cmnd));
388 cmd->request_buffer = cmd->buffer;
389 cmd->request_bufflen = cmd->bufflen;
390 cmd->use_sg = cmd->old_use_sg;
391 cmd->cmd_len = cmd->old_cmd_len;
392 cmd->sc_data_direction = cmd->sc_old_data_direction;
393 cmd->underflow = cmd->old_underflow;
396 void scsi_device_unbusy(struct scsi_device *sdev)
398 struct Scsi_Host *shost = sdev->host;
399 unsigned long flags;
401 spin_lock_irqsave(shost->host_lock, flags);
402 shost->host_busy--;
403 if (unlikely((shost->shost_state == SHOST_RECOVERY) &&
404 shost->host_failed))
405 scsi_eh_wakeup(shost);
406 spin_unlock(shost->host_lock);
407 spin_lock(sdev->request_queue->queue_lock);
408 sdev->device_busy--;
409 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
413 * Called for single_lun devices on IO completion. Clear starget_sdev_user,
414 * and call blk_run_queue for all the scsi_devices on the target -
415 * including current_sdev first.
417 * Called with *no* scsi locks held.
419 static void scsi_single_lun_run(struct scsi_device *current_sdev)
421 struct Scsi_Host *shost = current_sdev->host;
422 struct scsi_device *sdev, *tmp;
423 struct scsi_target *starget = scsi_target(current_sdev);
424 unsigned long flags;
426 spin_lock_irqsave(shost->host_lock, flags);
427 starget->starget_sdev_user = NULL;
428 spin_unlock_irqrestore(shost->host_lock, flags);
431 * Call blk_run_queue for all LUNs on the target, starting with
432 * current_sdev. We race with others (to set starget_sdev_user),
433 * but in most cases, we will be first. Ideally, each LU on the
434 * target would get some limited time or requests on the target.
436 blk_run_queue(current_sdev->request_queue);
438 spin_lock_irqsave(shost->host_lock, flags);
439 if (starget->starget_sdev_user)
440 goto out;
441 list_for_each_entry_safe(sdev, tmp, &starget->devices,
442 same_target_siblings) {
443 if (sdev == current_sdev)
444 continue;
445 if (scsi_device_get(sdev))
446 continue;
448 spin_unlock_irqrestore(shost->host_lock, flags);
449 blk_run_queue(sdev->request_queue);
450 spin_lock_irqsave(shost->host_lock, flags);
452 scsi_device_put(sdev);
454 out:
455 spin_unlock_irqrestore(shost->host_lock, flags);
459 * Function: scsi_run_queue()
461 * Purpose: Select a proper request queue to serve next
463 * Arguments: q - last request's queue
465 * Returns: Nothing
467 * Notes: The previous command was completely finished, start
468 * a new one if possible.
470 static void scsi_run_queue(struct request_queue *q)
472 struct scsi_device *sdev = q->queuedata;
473 struct Scsi_Host *shost = sdev->host;
474 unsigned long flags;
476 if (sdev->single_lun)
477 scsi_single_lun_run(sdev);
479 spin_lock_irqsave(shost->host_lock, flags);
480 while (!list_empty(&shost->starved_list) &&
481 !shost->host_blocked && !shost->host_self_blocked &&
482 !((shost->can_queue > 0) &&
483 (shost->host_busy >= shost->can_queue))) {
485 * As long as shost is accepting commands and we have
486 * starved queues, call blk_run_queue. scsi_request_fn
487 * drops the queue_lock and can add us back to the
488 * starved_list.
490 * host_lock protects the starved_list and starved_entry.
491 * scsi_request_fn must get the host_lock before checking
492 * or modifying starved_list or starved_entry.
494 sdev = list_entry(shost->starved_list.next,
495 struct scsi_device, starved_entry);
496 list_del_init(&sdev->starved_entry);
497 spin_unlock_irqrestore(shost->host_lock, flags);
499 blk_run_queue(sdev->request_queue);
501 spin_lock_irqsave(shost->host_lock, flags);
502 if (unlikely(!list_empty(&sdev->starved_entry)))
504 * sdev lost a race, and was put back on the
505 * starved list. This is unlikely but without this
506 * in theory we could loop forever.
508 break;
510 spin_unlock_irqrestore(shost->host_lock, flags);
512 blk_run_queue(q);
516 * Function: scsi_requeue_command()
518 * Purpose: Handle post-processing of completed commands.
520 * Arguments: q - queue to operate on
521 * cmd - command that may need to be requeued.
523 * Returns: Nothing
525 * Notes: After command completion, there may be blocks left
526 * over which weren't finished by the previous command
527 * this can be for a number of reasons - the main one is
528 * I/O errors in the middle of the request, in which case
529 * we need to request the blocks that come after the bad
530 * sector.
532 static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
534 unsigned long flags;
536 cmd->request->flags &= ~REQ_DONTPREP;
538 spin_lock_irqsave(q->queue_lock, flags);
539 blk_requeue_request(q, cmd->request);
540 spin_unlock_irqrestore(q->queue_lock, flags);
542 scsi_run_queue(q);
545 void scsi_next_command(struct scsi_cmnd *cmd)
547 struct request_queue *q = cmd->device->request_queue;
549 scsi_put_command(cmd);
550 scsi_run_queue(q);
553 void scsi_run_host_queues(struct Scsi_Host *shost)
555 struct scsi_device *sdev;
557 shost_for_each_device(sdev, shost)
558 scsi_run_queue(sdev->request_queue);
562 * Function: scsi_end_request()
564 * Purpose: Post-processing of completed commands (usually invoked at end
565 * of upper level post-processing and scsi_io_completion).
567 * Arguments: cmd - command that is complete.
568 * uptodate - 1 if I/O indicates success, <= 0 for I/O error.
569 * bytes - number of bytes of completed I/O
570 * requeue - indicates whether we should requeue leftovers.
572 * Lock status: Assumed that lock is not held upon entry.
574 * Returns: cmd if requeue done or required, NULL otherwise
576 * Notes: This is called for block device requests in order to
577 * mark some number of sectors as complete.
579 * We are guaranteeing that the request queue will be goosed
580 * at some point during this call.
582 static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
583 int bytes, int requeue)
585 request_queue_t *q = cmd->device->request_queue;
586 struct request *req = cmd->request;
587 unsigned long flags;
590 * If there are blocks left over at the end, set up the command
591 * to queue the remainder of them.
593 if (end_that_request_chunk(req, uptodate, bytes)) {
594 int leftover = (req->hard_nr_sectors << 9);
596 if (blk_pc_request(req))
597 leftover = req->data_len;
599 /* kill remainder if no retrys */
600 if (!uptodate && blk_noretry_request(req))
601 end_that_request_chunk(req, 0, leftover);
602 else {
603 if (requeue)
605 * Bleah. Leftovers again. Stick the
606 * leftovers in the front of the
607 * queue, and goose the queue again.
609 scsi_requeue_command(q, cmd);
611 return cmd;
615 add_disk_randomness(req->rq_disk);
617 spin_lock_irqsave(q->queue_lock, flags);
618 if (blk_rq_tagged(req))
619 blk_queue_end_tag(q, req);
620 end_that_request_last(req);
621 spin_unlock_irqrestore(q->queue_lock, flags);
624 * This will goose the queue request function at the end, so we don't
625 * need to worry about launching another command.
627 scsi_next_command(cmd);
628 return NULL;
631 static struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, int gfp_mask)
633 struct scsi_host_sg_pool *sgp;
634 struct scatterlist *sgl;
636 BUG_ON(!cmd->use_sg);
638 switch (cmd->use_sg) {
639 case 1 ... 8:
640 cmd->sglist_len = 0;
641 break;
642 case 9 ... 16:
643 cmd->sglist_len = 1;
644 break;
645 case 17 ... 32:
646 cmd->sglist_len = 2;
647 break;
648 #if (SCSI_MAX_PHYS_SEGMENTS > 32)
649 case 33 ... 64:
650 cmd->sglist_len = 3;
651 break;
652 #if (SCSI_MAX_PHYS_SEGMENTS > 64)
653 case 65 ... 128:
654 cmd->sglist_len = 4;
655 break;
656 #if (SCSI_MAX_PHYS_SEGMENTS > 128)
657 case 129 ... 256:
658 cmd->sglist_len = 5;
659 break;
660 #endif
661 #endif
662 #endif
663 default:
664 return NULL;
667 sgp = scsi_sg_pools + cmd->sglist_len;
668 sgl = mempool_alloc(sgp->pool, gfp_mask);
669 return sgl;
672 static void scsi_free_sgtable(struct scatterlist *sgl, int index)
674 struct scsi_host_sg_pool *sgp;
676 BUG_ON(index >= SG_MEMPOOL_NR);
678 sgp = scsi_sg_pools + index;
679 mempool_free(sgl, sgp->pool);
683 * Function: scsi_release_buffers()
685 * Purpose: Completion processing for block device I/O requests.
687 * Arguments: cmd - command that we are bailing.
689 * Lock status: Assumed that no lock is held upon entry.
691 * Returns: Nothing
693 * Notes: In the event that an upper level driver rejects a
694 * command, we must release resources allocated during
695 * the __init_io() function. Primarily this would involve
696 * the scatter-gather table, and potentially any bounce
697 * buffers.
699 static void scsi_release_buffers(struct scsi_cmnd *cmd)
701 struct request *req = cmd->request;
704 * Free up any indirection buffers we allocated for DMA purposes.
706 if (cmd->use_sg)
707 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
708 else if (cmd->request_buffer != req->buffer)
709 kfree(cmd->request_buffer);
712 * Zero these out. They now point to freed memory, and it is
713 * dangerous to hang onto the pointers.
715 cmd->buffer = NULL;
716 cmd->bufflen = 0;
717 cmd->request_buffer = NULL;
718 cmd->request_bufflen = 0;
722 * Function: scsi_io_completion()
724 * Purpose: Completion processing for block device I/O requests.
726 * Arguments: cmd - command that is finished.
728 * Lock status: Assumed that no lock is held upon entry.
730 * Returns: Nothing
732 * Notes: This function is matched in terms of capabilities to
733 * the function that created the scatter-gather list.
734 * In other words, if there are no bounce buffers
735 * (the normal case for most drivers), we don't need
736 * the logic to deal with cleaning up afterwards.
738 * We must do one of several things here:
740 * a) Call scsi_end_request. This will finish off the
741 * specified number of sectors. If we are done, the
742 * command block will be released, and the queue
743 * function will be goosed. If we are not done, then
744 * scsi_end_request will directly goose the queue.
746 * b) We can just use scsi_requeue_command() here. This would
747 * be used if we just wanted to retry, for example.
749 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes,
750 unsigned int block_bytes)
752 int result = cmd->result;
753 int this_count = cmd->bufflen;
754 request_queue_t *q = cmd->device->request_queue;
755 struct request *req = cmd->request;
756 int clear_errors = 1;
757 struct scsi_sense_hdr sshdr;
758 int sense_valid = 0;
759 int sense_deferred = 0;
761 if (blk_complete_barrier_rq(q, req, good_bytes >> 9))
762 return;
765 * Free up any indirection buffers we allocated for DMA purposes.
766 * For the case of a READ, we need to copy the data out of the
767 * bounce buffer and into the real buffer.
769 if (cmd->use_sg)
770 scsi_free_sgtable(cmd->buffer, cmd->sglist_len);
771 else if (cmd->buffer != req->buffer) {
772 if (rq_data_dir(req) == READ) {
773 unsigned long flags;
774 char *to = bio_kmap_irq(req->bio, &flags);
775 memcpy(to, cmd->buffer, cmd->bufflen);
776 bio_kunmap_irq(to, &flags);
778 kfree(cmd->buffer);
781 if (result) {
782 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
783 if (sense_valid)
784 sense_deferred = scsi_sense_is_deferred(&sshdr);
786 if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
787 req->errors = result;
788 if (result) {
789 clear_errors = 0;
790 if (sense_valid && req->sense) {
792 * SG_IO wants current and deferred errors
794 int len = 8 + cmd->sense_buffer[7];
796 if (len > SCSI_SENSE_BUFFERSIZE)
797 len = SCSI_SENSE_BUFFERSIZE;
798 memcpy(req->sense, cmd->sense_buffer, len);
799 req->sense_len = len;
801 } else
802 req->data_len = cmd->resid;
806 * Zero these out. They now point to freed memory, and it is
807 * dangerous to hang onto the pointers.
809 cmd->buffer = NULL;
810 cmd->bufflen = 0;
811 cmd->request_buffer = NULL;
812 cmd->request_bufflen = 0;
815 * Next deal with any sectors which we were able to correctly
816 * handle.
818 if (good_bytes >= 0) {
819 SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, %d bytes done.\n",
820 req->nr_sectors, good_bytes));
821 SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n", cmd->use_sg));
823 if (clear_errors)
824 req->errors = 0;
826 * If multiple sectors are requested in one buffer, then
827 * they will have been finished off by the first command.
828 * If not, then we have a multi-buffer command.
830 * If block_bytes != 0, it means we had a medium error
831 * of some sort, and that we want to mark some number of
832 * sectors as not uptodate. Thus we want to inhibit
833 * requeueing right here - we will requeue down below
834 * when we handle the bad sectors.
836 cmd = scsi_end_request(cmd, 1, good_bytes, result == 0);
839 * If the command completed without error, then either finish off the
840 * rest of the command, or start a new one.
842 if (result == 0 || cmd == NULL ) {
843 return;
847 * Now, if we were good little boys and girls, Santa left us a request
848 * sense buffer. We can extract information from this, so we
849 * can choose a block to remap, etc.
851 if (sense_valid && !sense_deferred) {
852 switch (sshdr.sense_key) {
853 case UNIT_ATTENTION:
854 if (cmd->device->removable) {
855 /* detected disc change. set a bit
856 * and quietly refuse further access.
858 cmd->device->changed = 1;
859 cmd = scsi_end_request(cmd, 0,
860 this_count, 1);
861 return;
862 } else {
864 * Must have been a power glitch, or a
865 * bus reset. Could not have been a
866 * media change, so we just retry the
867 * request and see what happens.
869 scsi_requeue_command(q, cmd);
870 return;
872 break;
873 case ILLEGAL_REQUEST:
875 * If we had an ILLEGAL REQUEST returned, then we may
876 * have performed an unsupported command. The only
877 * thing this should be would be a ten byte read where
878 * only a six byte read was supported. Also, on a
879 * system where READ CAPACITY failed, we may have read
880 * past the end of the disk.
882 if (cmd->device->use_10_for_rw &&
883 (cmd->cmnd[0] == READ_10 ||
884 cmd->cmnd[0] == WRITE_10)) {
885 cmd->device->use_10_for_rw = 0;
887 * This will cause a retry with a 6-byte
888 * command.
890 scsi_requeue_command(q, cmd);
891 result = 0;
892 } else {
893 cmd = scsi_end_request(cmd, 0, this_count, 1);
894 return;
896 break;
897 case NOT_READY:
899 * If the device is in the process of becoming ready,
900 * retry.
902 if (sshdr.asc == 0x04 && sshdr.ascq == 0x01) {
903 scsi_requeue_command(q, cmd);
904 return;
906 printk(KERN_INFO "Device %s not ready.\n",
907 req->rq_disk ? req->rq_disk->disk_name : "");
908 cmd = scsi_end_request(cmd, 0, this_count, 1);
909 return;
910 case VOLUME_OVERFLOW:
911 printk(KERN_INFO "Volume overflow <%d %d %d %d> CDB: ",
912 cmd->device->host->host_no,
913 (int)cmd->device->channel,
914 (int)cmd->device->id, (int)cmd->device->lun);
915 __scsi_print_command(cmd->data_cmnd);
916 scsi_print_sense("", cmd);
917 cmd = scsi_end_request(cmd, 0, block_bytes, 1);
918 return;
919 default:
920 break;
922 } /* driver byte != 0 */
923 if (host_byte(result) == DID_RESET) {
925 * Third party bus reset or reset for error
926 * recovery reasons. Just retry the request
927 * and see what happens.
929 scsi_requeue_command(q, cmd);
930 return;
932 if (result) {
933 if (!(req->flags & REQ_SPECIAL))
934 printk(KERN_INFO "SCSI error : <%d %d %d %d> return code "
935 "= 0x%x\n", cmd->device->host->host_no,
936 cmd->device->channel,
937 cmd->device->id,
938 cmd->device->lun, result);
940 if (driver_byte(result) & DRIVER_SENSE)
941 scsi_print_sense("", cmd);
943 * Mark a single buffer as not uptodate. Queue the remainder.
944 * We sometimes get this cruft in the event that a medium error
945 * isn't properly reported.
947 block_bytes = req->hard_cur_sectors << 9;
948 if (!block_bytes)
949 block_bytes = req->data_len;
950 cmd = scsi_end_request(cmd, 0, block_bytes, 1);
953 EXPORT_SYMBOL(scsi_io_completion);
956 * Function: scsi_init_io()
958 * Purpose: SCSI I/O initialize function.
960 * Arguments: cmd - Command descriptor we wish to initialize
962 * Returns: 0 on success
963 * BLKPREP_DEFER if the failure is retryable
964 * BLKPREP_KILL if the failure is fatal
966 static int scsi_init_io(struct scsi_cmnd *cmd)
968 struct request *req = cmd->request;
969 struct scatterlist *sgpnt;
970 int count;
973 * if this is a rq->data based REQ_BLOCK_PC, setup for a non-sg xfer
975 if ((req->flags & REQ_BLOCK_PC) && !req->bio) {
976 cmd->request_bufflen = req->data_len;
977 cmd->request_buffer = req->data;
978 req->buffer = req->data;
979 cmd->use_sg = 0;
980 return 0;
984 * we used to not use scatter-gather for single segment request,
985 * but now we do (it makes highmem I/O easier to support without
986 * kmapping pages)
988 cmd->use_sg = req->nr_phys_segments;
991 * if sg table allocation fails, requeue request later.
993 sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
994 if (unlikely(!sgpnt))
995 return BLKPREP_DEFER;
997 cmd->request_buffer = (char *) sgpnt;
998 cmd->request_bufflen = req->nr_sectors << 9;
999 if (blk_pc_request(req))
1000 cmd->request_bufflen = req->data_len;
1001 req->buffer = NULL;
1004 * Next, walk the list, and fill in the addresses and sizes of
1005 * each segment.
1007 count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
1010 * mapped well, send it off
1012 if (likely(count <= cmd->use_sg)) {
1013 cmd->use_sg = count;
1014 return 0;
1017 printk(KERN_ERR "Incorrect number of segments after building list\n");
1018 printk(KERN_ERR "counted %d, received %d\n", count, cmd->use_sg);
1019 printk(KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n", req->nr_sectors,
1020 req->current_nr_sectors);
1022 /* release the command and kill it */
1023 scsi_release_buffers(cmd);
1024 scsi_put_command(cmd);
1025 return BLKPREP_KILL;
1028 static int scsi_prepare_flush_fn(request_queue_t *q, struct request *rq)
1030 struct scsi_device *sdev = q->queuedata;
1031 struct scsi_driver *drv;
1033 if (sdev->sdev_state == SDEV_RUNNING) {
1034 drv = *(struct scsi_driver **) rq->rq_disk->private_data;
1036 if (drv->prepare_flush)
1037 return drv->prepare_flush(q, rq);
1040 return 0;
1043 static void scsi_end_flush_fn(request_queue_t *q, struct request *rq)
1045 struct scsi_device *sdev = q->queuedata;
1046 struct request *flush_rq = rq->end_io_data;
1047 struct scsi_driver *drv;
1049 if (flush_rq->errors) {
1050 printk("scsi: barrier error, disabling flush support\n");
1051 blk_queue_ordered(q, QUEUE_ORDERED_NONE);
1054 if (sdev->sdev_state == SDEV_RUNNING) {
1055 drv = *(struct scsi_driver **) rq->rq_disk->private_data;
1056 drv->end_flush(q, rq);
1060 static int scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
1061 sector_t *error_sector)
1063 struct scsi_device *sdev = q->queuedata;
1064 struct scsi_driver *drv;
1066 if (sdev->sdev_state != SDEV_RUNNING)
1067 return -ENXIO;
1069 drv = *(struct scsi_driver **) disk->private_data;
1070 if (drv->issue_flush)
1071 return drv->issue_flush(&sdev->sdev_gendev, error_sector);
1073 return -EOPNOTSUPP;
1076 static void scsi_generic_done(struct scsi_cmnd *cmd)
1078 BUG_ON(!blk_pc_request(cmd->request));
1079 scsi_io_completion(cmd, cmd->result == 0 ? cmd->bufflen : 0, 0);
1082 static int scsi_prep_fn(struct request_queue *q, struct request *req)
1084 struct scsi_device *sdev = q->queuedata;
1085 struct scsi_cmnd *cmd;
1086 int specials_only = 0;
1089 * Just check to see if the device is online. If it isn't, we
1090 * refuse to process any commands. The device must be brought
1091 * online before trying any recovery commands
1093 if (unlikely(!scsi_device_online(sdev))) {
1094 printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to offline device\n",
1095 sdev->host->host_no, sdev->id, sdev->lun);
1096 return BLKPREP_KILL;
1098 if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1099 /* OK, we're not in a running state don't prep
1100 * user commands */
1101 if (sdev->sdev_state == SDEV_DEL) {
1102 /* Device is fully deleted, no commands
1103 * at all allowed down */
1104 printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to dead device\n",
1105 sdev->host->host_no, sdev->id, sdev->lun);
1106 return BLKPREP_KILL;
1108 /* OK, we only allow special commands (i.e. not
1109 * user initiated ones */
1110 specials_only = sdev->sdev_state;
1114 * Find the actual device driver associated with this command.
1115 * The SPECIAL requests are things like character device or
1116 * ioctls, which did not originate from ll_rw_blk. Note that
1117 * the special field is also used to indicate the cmd for
1118 * the remainder of a partially fulfilled request that can
1119 * come up when there is a medium error. We have to treat
1120 * these two cases differently. We differentiate by looking
1121 * at request->cmd, as this tells us the real story.
1123 if (req->flags & REQ_SPECIAL && req->special) {
1124 struct scsi_request *sreq = req->special;
1126 if (sreq->sr_magic == SCSI_REQ_MAGIC) {
1127 cmd = scsi_get_command(sreq->sr_device, GFP_ATOMIC);
1128 if (unlikely(!cmd))
1129 goto defer;
1130 scsi_init_cmd_from_req(cmd, sreq);
1131 } else
1132 cmd = req->special;
1133 } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
1135 if(unlikely(specials_only) && !(req->flags & REQ_SPECIAL)) {
1136 if(specials_only == SDEV_QUIESCE ||
1137 specials_only == SDEV_BLOCK)
1138 return BLKPREP_DEFER;
1140 printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to device being removed\n",
1141 sdev->host->host_no, sdev->id, sdev->lun);
1142 return BLKPREP_KILL;
1147 * Now try and find a command block that we can use.
1149 if (!req->special) {
1150 cmd = scsi_get_command(sdev, GFP_ATOMIC);
1151 if (unlikely(!cmd))
1152 goto defer;
1153 } else
1154 cmd = req->special;
1156 /* pull a tag out of the request if we have one */
1157 cmd->tag = req->tag;
1158 } else {
1159 blk_dump_rq_flags(req, "SCSI bad req");
1160 return BLKPREP_KILL;
1163 /* note the overloading of req->special. When the tag
1164 * is active it always means cmd. If the tag goes
1165 * back for re-queueing, it may be reset */
1166 req->special = cmd;
1167 cmd->request = req;
1170 * FIXME: drop the lock here because the functions below
1171 * expect to be called without the queue lock held. Also,
1172 * previously, we dequeued the request before dropping the
1173 * lock. We hope REQ_STARTED prevents anything untoward from
1174 * happening now.
1176 if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
1177 struct scsi_driver *drv;
1178 int ret;
1181 * This will do a couple of things:
1182 * 1) Fill in the actual SCSI command.
1183 * 2) Fill in any other upper-level specific fields
1184 * (timeout).
1186 * If this returns 0, it means that the request failed
1187 * (reading past end of disk, reading offline device,
1188 * etc). This won't actually talk to the device, but
1189 * some kinds of consistency checking may cause the
1190 * request to be rejected immediately.
1194 * This sets up the scatter-gather table (allocating if
1195 * required).
1197 ret = scsi_init_io(cmd);
1198 if (ret) /* BLKPREP_KILL return also releases the command */
1199 return ret;
1202 * Initialize the actual SCSI command for this request.
1204 if (req->rq_disk) {
1205 drv = *(struct scsi_driver **)req->rq_disk->private_data;
1206 if (unlikely(!drv->init_command(cmd))) {
1207 scsi_release_buffers(cmd);
1208 scsi_put_command(cmd);
1209 return BLKPREP_KILL;
1211 } else {
1212 memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
1213 if (rq_data_dir(req) == WRITE)
1214 cmd->sc_data_direction = DMA_TO_DEVICE;
1215 else if (req->data_len)
1216 cmd->sc_data_direction = DMA_FROM_DEVICE;
1217 else
1218 cmd->sc_data_direction = DMA_NONE;
1220 cmd->transfersize = req->data_len;
1221 cmd->allowed = 3;
1222 cmd->timeout_per_command = req->timeout;
1223 cmd->done = scsi_generic_done;
1228 * The request is now prepped, no need to come back here
1230 req->flags |= REQ_DONTPREP;
1231 return BLKPREP_OK;
1233 defer:
1234 /* If we defer, the elv_next_request() returns NULL, but the
1235 * queue must be restarted, so we plug here if no returning
1236 * command will automatically do that. */
1237 if (sdev->device_busy == 0)
1238 blk_plug_device(q);
1239 return BLKPREP_DEFER;
1243 * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1244 * return 0.
1246 * Called with the queue_lock held.
1248 static inline int scsi_dev_queue_ready(struct request_queue *q,
1249 struct scsi_device *sdev)
1251 if (sdev->device_busy >= sdev->queue_depth)
1252 return 0;
1253 if (sdev->device_busy == 0 && sdev->device_blocked) {
1255 * unblock after device_blocked iterates to zero
1257 if (--sdev->device_blocked == 0) {
1258 SCSI_LOG_MLQUEUE(3,
1259 printk("scsi%d (%d:%d) unblocking device at"
1260 " zero depth\n", sdev->host->host_no,
1261 sdev->id, sdev->lun));
1262 } else {
1263 blk_plug_device(q);
1264 return 0;
1267 if (sdev->device_blocked)
1268 return 0;
1270 return 1;
1274 * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1275 * return 0. We must end up running the queue again whenever 0 is
1276 * returned, else IO can hang.
1278 * Called with host_lock held.
1280 static inline int scsi_host_queue_ready(struct request_queue *q,
1281 struct Scsi_Host *shost,
1282 struct scsi_device *sdev)
1284 if (shost->shost_state == SHOST_RECOVERY)
1285 return 0;
1286 if (shost->host_busy == 0 && shost->host_blocked) {
1288 * unblock after host_blocked iterates to zero
1290 if (--shost->host_blocked == 0) {
1291 SCSI_LOG_MLQUEUE(3,
1292 printk("scsi%d unblocking host at zero depth\n",
1293 shost->host_no));
1294 } else {
1295 blk_plug_device(q);
1296 return 0;
1299 if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
1300 shost->host_blocked || shost->host_self_blocked) {
1301 if (list_empty(&sdev->starved_entry))
1302 list_add_tail(&sdev->starved_entry, &shost->starved_list);
1303 return 0;
1306 /* We're OK to process the command, so we can't be starved */
1307 if (!list_empty(&sdev->starved_entry))
1308 list_del_init(&sdev->starved_entry);
1310 return 1;
1314 * Kill requests for a dead device
1316 static void scsi_kill_requests(request_queue_t *q)
1318 struct request *req;
1320 while ((req = elv_next_request(q)) != NULL) {
1321 blkdev_dequeue_request(req);
1322 req->flags |= REQ_QUIET;
1323 while (end_that_request_first(req, 0, req->nr_sectors))
1325 end_that_request_last(req);
1330 * Function: scsi_request_fn()
1332 * Purpose: Main strategy routine for SCSI.
1334 * Arguments: q - Pointer to actual queue.
1336 * Returns: Nothing
1338 * Lock status: IO request lock assumed to be held when called.
1340 static void scsi_request_fn(struct request_queue *q)
1342 struct scsi_device *sdev = q->queuedata;
1343 struct Scsi_Host *shost;
1344 struct scsi_cmnd *cmd;
1345 struct request *req;
1347 if (!sdev) {
1348 printk("scsi: killing requests for dead queue\n");
1349 scsi_kill_requests(q);
1350 return;
1353 if(!get_device(&sdev->sdev_gendev))
1354 /* We must be tearing the block queue down already */
1355 return;
1358 * To start with, we keep looping until the queue is empty, or until
1359 * the host is no longer able to accept any more requests.
1361 shost = sdev->host;
1362 while (!blk_queue_plugged(q)) {
1363 int rtn;
1365 * get next queueable request. We do this early to make sure
1366 * that the request is fully prepared even if we cannot
1367 * accept it.
1369 req = elv_next_request(q);
1370 if (!req || !scsi_dev_queue_ready(q, sdev))
1371 break;
1373 if (unlikely(!scsi_device_online(sdev))) {
1374 printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to offline device\n",
1375 sdev->host->host_no, sdev->id, sdev->lun);
1376 blkdev_dequeue_request(req);
1377 req->flags |= REQ_QUIET;
1378 while (end_that_request_first(req, 0, req->nr_sectors))
1380 end_that_request_last(req);
1381 continue;
1386 * Remove the request from the request list.
1388 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
1389 blkdev_dequeue_request(req);
1390 sdev->device_busy++;
1392 spin_unlock(q->queue_lock);
1393 spin_lock(shost->host_lock);
1395 if (!scsi_host_queue_ready(q, shost, sdev))
1396 goto not_ready;
1397 if (sdev->single_lun) {
1398 if (scsi_target(sdev)->starget_sdev_user &&
1399 scsi_target(sdev)->starget_sdev_user != sdev)
1400 goto not_ready;
1401 scsi_target(sdev)->starget_sdev_user = sdev;
1403 shost->host_busy++;
1406 * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
1407 * take the lock again.
1409 spin_unlock_irq(shost->host_lock);
1411 cmd = req->special;
1412 if (unlikely(cmd == NULL)) {
1413 printk(KERN_CRIT "impossible request in %s.\n"
1414 "please mail a stack trace to "
1415 "linux-scsi@vger.kernel.org",
1416 __FUNCTION__);
1417 BUG();
1421 * Finally, initialize any error handling parameters, and set up
1422 * the timers for timeouts.
1424 scsi_init_cmd_errh(cmd);
1427 * Dispatch the command to the low-level driver.
1429 rtn = scsi_dispatch_cmd(cmd);
1430 spin_lock_irq(q->queue_lock);
1431 if(rtn) {
1432 /* we're refusing the command; because of
1433 * the way locks get dropped, we need to
1434 * check here if plugging is required */
1435 if(sdev->device_busy == 0)
1436 blk_plug_device(q);
1438 break;
1442 goto out;
1444 not_ready:
1445 spin_unlock_irq(shost->host_lock);
1448 * lock q, handle tag, requeue req, and decrement device_busy. We
1449 * must return with queue_lock held.
1451 * Decrementing device_busy without checking it is OK, as all such
1452 * cases (host limits or settings) should run the queue at some
1453 * later time.
1455 spin_lock_irq(q->queue_lock);
1456 blk_requeue_request(q, req);
1457 sdev->device_busy--;
1458 if(sdev->device_busy == 0)
1459 blk_plug_device(q);
1460 out:
1461 /* must be careful here...if we trigger the ->remove() function
1462 * we cannot be holding the q lock */
1463 spin_unlock_irq(q->queue_lock);
1464 put_device(&sdev->sdev_gendev);
1465 spin_lock_irq(q->queue_lock);
1468 u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
1470 struct device *host_dev;
1471 u64 bounce_limit = 0xffffffff;
1473 if (shost->unchecked_isa_dma)
1474 return BLK_BOUNCE_ISA;
1476 * Platforms with virtual-DMA translation
1477 * hardware have no practical limit.
1479 if (!PCI_DMA_BUS_IS_PHYS)
1480 return BLK_BOUNCE_ANY;
1482 host_dev = scsi_get_device(shost);
1483 if (host_dev && host_dev->dma_mask)
1484 bounce_limit = *host_dev->dma_mask;
1486 return bounce_limit;
1488 EXPORT_SYMBOL(scsi_calculate_bounce_limit);
1490 struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
1492 struct Scsi_Host *shost = sdev->host;
1493 struct request_queue *q;
1495 q = blk_init_queue(scsi_request_fn, NULL);
1496 if (!q)
1497 return NULL;
1499 blk_queue_prep_rq(q, scsi_prep_fn);
1501 blk_queue_max_hw_segments(q, shost->sg_tablesize);
1502 blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
1503 blk_queue_max_sectors(q, shost->max_sectors);
1504 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
1505 blk_queue_segment_boundary(q, shost->dma_boundary);
1506 blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
1509 * ordered tags are superior to flush ordering
1511 if (shost->ordered_tag)
1512 blk_queue_ordered(q, QUEUE_ORDERED_TAG);
1513 else if (shost->ordered_flush) {
1514 blk_queue_ordered(q, QUEUE_ORDERED_FLUSH);
1515 q->prepare_flush_fn = scsi_prepare_flush_fn;
1516 q->end_flush_fn = scsi_end_flush_fn;
1519 if (!shost->use_clustering)
1520 clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
1521 return q;
1524 void scsi_free_queue(struct request_queue *q)
1526 blk_cleanup_queue(q);
1530 * Function: scsi_block_requests()
1532 * Purpose: Utility function used by low-level drivers to prevent further
1533 * commands from being queued to the device.
1535 * Arguments: shost - Host in question
1537 * Returns: Nothing
1539 * Lock status: No locks are assumed held.
1541 * Notes: There is no timer nor any other means by which the requests
1542 * get unblocked other than the low-level driver calling
1543 * scsi_unblock_requests().
1545 void scsi_block_requests(struct Scsi_Host *shost)
1547 shost->host_self_blocked = 1;
1549 EXPORT_SYMBOL(scsi_block_requests);
1552 * Function: scsi_unblock_requests()
1554 * Purpose: Utility function used by low-level drivers to allow further
1555 * commands from being queued to the device.
1557 * Arguments: shost - Host in question
1559 * Returns: Nothing
1561 * Lock status: No locks are assumed held.
1563 * Notes: There is no timer nor any other means by which the requests
1564 * get unblocked other than the low-level driver calling
1565 * scsi_unblock_requests().
1567 * This is done as an API function so that changes to the
1568 * internals of the scsi mid-layer won't require wholesale
1569 * changes to drivers that use this feature.
1571 void scsi_unblock_requests(struct Scsi_Host *shost)
1573 shost->host_self_blocked = 0;
1574 scsi_run_host_queues(shost);
1576 EXPORT_SYMBOL(scsi_unblock_requests);
1578 int __init scsi_init_queue(void)
1580 int i;
1582 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1583 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1584 int size = sgp->size * sizeof(struct scatterlist);
1586 sgp->slab = kmem_cache_create(sgp->name, size, 0,
1587 SLAB_HWCACHE_ALIGN, NULL, NULL);
1588 if (!sgp->slab) {
1589 printk(KERN_ERR "SCSI: can't init sg slab %s\n",
1590 sgp->name);
1593 sgp->pool = mempool_create(SG_MEMPOOL_SIZE,
1594 mempool_alloc_slab, mempool_free_slab,
1595 sgp->slab);
1596 if (!sgp->pool) {
1597 printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
1598 sgp->name);
1602 return 0;
1605 void scsi_exit_queue(void)
1607 int i;
1609 for (i = 0; i < SG_MEMPOOL_NR; i++) {
1610 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1611 mempool_destroy(sgp->pool);
1612 kmem_cache_destroy(sgp->slab);
1616 * __scsi_mode_sense - issue a mode sense, falling back from 10 to
1617 * six bytes if necessary.
1618 * @sreq: SCSI request to fill in with the MODE_SENSE
1619 * @dbd: set if mode sense will allow block descriptors to be returned
1620 * @modepage: mode page being requested
1621 * @buffer: request buffer (may not be smaller than eight bytes)
1622 * @len: length of request buffer.
1623 * @timeout: command timeout
1624 * @retries: number of retries before failing
1625 * @data: returns a structure abstracting the mode header data
1627 * Returns zero if unsuccessful, or the header offset (either 4
1628 * or 8 depending on whether a six or ten byte command was
1629 * issued) if successful.
1632 __scsi_mode_sense(struct scsi_request *sreq, int dbd, int modepage,
1633 unsigned char *buffer, int len, int timeout, int retries,
1634 struct scsi_mode_data *data) {
1635 unsigned char cmd[12];
1636 int use_10_for_ms;
1637 int header_length;
1639 memset(data, 0, sizeof(*data));
1640 memset(&cmd[0], 0, 12);
1641 cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
1642 cmd[2] = modepage;
1644 retry:
1645 use_10_for_ms = sreq->sr_device->use_10_for_ms;
1647 if (use_10_for_ms) {
1648 if (len < 8)
1649 len = 8;
1651 cmd[0] = MODE_SENSE_10;
1652 cmd[8] = len;
1653 header_length = 8;
1654 } else {
1655 if (len < 4)
1656 len = 4;
1658 cmd[0] = MODE_SENSE;
1659 cmd[4] = len;
1660 header_length = 4;
1663 sreq->sr_cmd_len = 0;
1664 memset(sreq->sr_sense_buffer, 0, sizeof(sreq->sr_sense_buffer));
1665 sreq->sr_data_direction = DMA_FROM_DEVICE;
1667 memset(buffer, 0, len);
1669 scsi_wait_req(sreq, cmd, buffer, len, timeout, retries);
1671 /* This code looks awful: what it's doing is making sure an
1672 * ILLEGAL REQUEST sense return identifies the actual command
1673 * byte as the problem. MODE_SENSE commands can return
1674 * ILLEGAL REQUEST if the code page isn't supported */
1676 if (use_10_for_ms && !scsi_status_is_good(sreq->sr_result) &&
1677 (driver_byte(sreq->sr_result) & DRIVER_SENSE)) {
1678 struct scsi_sense_hdr sshdr;
1680 if (scsi_request_normalize_sense(sreq, &sshdr)) {
1681 if ((sshdr.sense_key == ILLEGAL_REQUEST) &&
1682 (sshdr.asc == 0x20) && (sshdr.ascq == 0)) {
1684 * Invalid command operation code
1686 sreq->sr_device->use_10_for_ms = 0;
1687 goto retry;
1692 if(scsi_status_is_good(sreq->sr_result)) {
1693 data->header_length = header_length;
1694 if(use_10_for_ms) {
1695 data->length = buffer[0]*256 + buffer[1] + 2;
1696 data->medium_type = buffer[2];
1697 data->device_specific = buffer[3];
1698 data->longlba = buffer[4] & 0x01;
1699 data->block_descriptor_length = buffer[6]*256
1700 + buffer[7];
1701 } else {
1702 data->length = buffer[0] + 1;
1703 data->medium_type = buffer[1];
1704 data->device_specific = buffer[2];
1705 data->block_descriptor_length = buffer[3];
1709 return sreq->sr_result;
1711 EXPORT_SYMBOL(__scsi_mode_sense);
1714 * scsi_mode_sense - issue a mode sense, falling back from 10 to
1715 * six bytes if necessary.
1716 * @sdev: scsi device to send command to.
1717 * @dbd: set if mode sense will disable block descriptors in the return
1718 * @modepage: mode page being requested
1719 * @buffer: request buffer (may not be smaller than eight bytes)
1720 * @len: length of request buffer.
1721 * @timeout: command timeout
1722 * @retries: number of retries before failing
1724 * Returns zero if unsuccessful, or the header offset (either 4
1725 * or 8 depending on whether a six or ten byte command was
1726 * issued) if successful.
1729 scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
1730 unsigned char *buffer, int len, int timeout, int retries,
1731 struct scsi_mode_data *data)
1733 struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
1734 int ret;
1736 if (!sreq)
1737 return -1;
1739 ret = __scsi_mode_sense(sreq, dbd, modepage, buffer, len,
1740 timeout, retries, data);
1742 scsi_release_request(sreq);
1744 return ret;
1746 EXPORT_SYMBOL(scsi_mode_sense);
1749 scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
1751 struct scsi_request *sreq;
1752 char cmd[] = {
1753 TEST_UNIT_READY, 0, 0, 0, 0, 0,
1755 int result;
1757 sreq = scsi_allocate_request(sdev, GFP_KERNEL);
1758 if (!sreq)
1759 return -ENOMEM;
1761 sreq->sr_data_direction = DMA_NONE;
1762 scsi_wait_req(sreq, cmd, NULL, 0, timeout, retries);
1764 if ((driver_byte(sreq->sr_result) & DRIVER_SENSE) && sdev->removable) {
1765 struct scsi_sense_hdr sshdr;
1767 if ((scsi_request_normalize_sense(sreq, &sshdr)) &&
1768 ((sshdr.sense_key == UNIT_ATTENTION) ||
1769 (sshdr.sense_key == NOT_READY))) {
1770 sdev->changed = 1;
1771 sreq->sr_result = 0;
1774 result = sreq->sr_result;
1775 scsi_release_request(sreq);
1776 return result;
1778 EXPORT_SYMBOL(scsi_test_unit_ready);
1781 * scsi_device_set_state - Take the given device through the device
1782 * state model.
1783 * @sdev: scsi device to change the state of.
1784 * @state: state to change to.
1786 * Returns zero if unsuccessful or an error if the requested
1787 * transition is illegal.
1790 scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
1792 enum scsi_device_state oldstate = sdev->sdev_state;
1794 if (state == oldstate)
1795 return 0;
1797 switch (state) {
1798 case SDEV_CREATED:
1799 /* There are no legal states that come back to
1800 * created. This is the manually initialised start
1801 * state */
1802 goto illegal;
1804 case SDEV_RUNNING:
1805 switch (oldstate) {
1806 case SDEV_CREATED:
1807 case SDEV_OFFLINE:
1808 case SDEV_QUIESCE:
1809 case SDEV_BLOCK:
1810 break;
1811 default:
1812 goto illegal;
1814 break;
1816 case SDEV_QUIESCE:
1817 switch (oldstate) {
1818 case SDEV_RUNNING:
1819 case SDEV_OFFLINE:
1820 break;
1821 default:
1822 goto illegal;
1824 break;
1826 case SDEV_OFFLINE:
1827 switch (oldstate) {
1828 case SDEV_CREATED:
1829 case SDEV_RUNNING:
1830 case SDEV_QUIESCE:
1831 case SDEV_BLOCK:
1832 break;
1833 default:
1834 goto illegal;
1836 break;
1838 case SDEV_BLOCK:
1839 switch (oldstate) {
1840 case SDEV_CREATED:
1841 case SDEV_RUNNING:
1842 break;
1843 default:
1844 goto illegal;
1846 break;
1848 case SDEV_CANCEL:
1849 switch (oldstate) {
1850 case SDEV_CREATED:
1851 case SDEV_RUNNING:
1852 case SDEV_OFFLINE:
1853 case SDEV_BLOCK:
1854 break;
1855 default:
1856 goto illegal;
1858 break;
1860 case SDEV_DEL:
1861 switch (oldstate) {
1862 case SDEV_CANCEL:
1863 break;
1864 default:
1865 goto illegal;
1867 break;
1870 sdev->sdev_state = state;
1871 return 0;
1873 illegal:
1874 SCSI_LOG_ERROR_RECOVERY(1,
1875 dev_printk(KERN_ERR, &sdev->sdev_gendev,
1876 "Illegal state transition %s->%s\n",
1877 scsi_device_state_name(oldstate),
1878 scsi_device_state_name(state))
1880 return -EINVAL;
1882 EXPORT_SYMBOL(scsi_device_set_state);
1885 * scsi_device_quiesce - Block user issued commands.
1886 * @sdev: scsi device to quiesce.
1888 * This works by trying to transition to the SDEV_QUIESCE state
1889 * (which must be a legal transition). When the device is in this
1890 * state, only special requests will be accepted, all others will
1891 * be deferred. Since special requests may also be requeued requests,
1892 * a successful return doesn't guarantee the device will be
1893 * totally quiescent.
1895 * Must be called with user context, may sleep.
1897 * Returns zero if unsuccessful or an error if not.
1900 scsi_device_quiesce(struct scsi_device *sdev)
1902 int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
1903 if (err)
1904 return err;
1906 scsi_run_queue(sdev->request_queue);
1907 while (sdev->device_busy) {
1908 msleep_interruptible(200);
1909 scsi_run_queue(sdev->request_queue);
1911 return 0;
1913 EXPORT_SYMBOL(scsi_device_quiesce);
1916 * scsi_device_resume - Restart user issued commands to a quiesced device.
1917 * @sdev: scsi device to resume.
1919 * Moves the device from quiesced back to running and restarts the
1920 * queues.
1922 * Must be called with user context, may sleep.
1924 void
1925 scsi_device_resume(struct scsi_device *sdev)
1927 if(scsi_device_set_state(sdev, SDEV_RUNNING))
1928 return;
1929 scsi_run_queue(sdev->request_queue);
1931 EXPORT_SYMBOL(scsi_device_resume);
1933 static void
1934 device_quiesce_fn(struct scsi_device *sdev, void *data)
1936 scsi_device_quiesce(sdev);
1939 void
1940 scsi_target_quiesce(struct scsi_target *starget)
1942 starget_for_each_device(starget, NULL, device_quiesce_fn);
1944 EXPORT_SYMBOL(scsi_target_quiesce);
1946 static void
1947 device_resume_fn(struct scsi_device *sdev, void *data)
1949 scsi_device_resume(sdev);
1952 void
1953 scsi_target_resume(struct scsi_target *starget)
1955 starget_for_each_device(starget, NULL, device_resume_fn);
1957 EXPORT_SYMBOL(scsi_target_resume);
1960 * scsi_internal_device_block - internal function to put a device
1961 * temporarily into the SDEV_BLOCK state
1962 * @sdev: device to block
1964 * Block request made by scsi lld's to temporarily stop all
1965 * scsi commands on the specified device. Called from interrupt
1966 * or normal process context.
1968 * Returns zero if successful or error if not
1970 * Notes:
1971 * This routine transitions the device to the SDEV_BLOCK state
1972 * (which must be a legal transition). When the device is in this
1973 * state, all commands are deferred until the scsi lld reenables
1974 * the device with scsi_device_unblock or device_block_tmo fires.
1975 * This routine assumes the host_lock is held on entry.
1978 scsi_internal_device_block(struct scsi_device *sdev)
1980 request_queue_t *q = sdev->request_queue;
1981 unsigned long flags;
1982 int err = 0;
1984 err = scsi_device_set_state(sdev, SDEV_BLOCK);
1985 if (err)
1986 return err;
1989 * The device has transitioned to SDEV_BLOCK. Stop the
1990 * block layer from calling the midlayer with this device's
1991 * request queue.
1993 spin_lock_irqsave(q->queue_lock, flags);
1994 blk_stop_queue(q);
1995 spin_unlock_irqrestore(q->queue_lock, flags);
1997 return 0;
1999 EXPORT_SYMBOL_GPL(scsi_internal_device_block);
2002 * scsi_internal_device_unblock - resume a device after a block request
2003 * @sdev: device to resume
2005 * Called by scsi lld's or the midlayer to restart the device queue
2006 * for the previously suspended scsi device. Called from interrupt or
2007 * normal process context.
2009 * Returns zero if successful or error if not.
2011 * Notes:
2012 * This routine transitions the device to the SDEV_RUNNING state
2013 * (which must be a legal transition) allowing the midlayer to
2014 * goose the queue for this device. This routine assumes the
2015 * host_lock is held upon entry.
2018 scsi_internal_device_unblock(struct scsi_device *sdev)
2020 request_queue_t *q = sdev->request_queue;
2021 int err;
2022 unsigned long flags;
2025 * Try to transition the scsi device to SDEV_RUNNING
2026 * and goose the device queue if successful.
2028 err = scsi_device_set_state(sdev, SDEV_RUNNING);
2029 if (err)
2030 return err;
2032 spin_lock_irqsave(q->queue_lock, flags);
2033 blk_start_queue(q);
2034 spin_unlock_irqrestore(q->queue_lock, flags);
2036 return 0;
2038 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
2040 static void
2041 device_block(struct scsi_device *sdev, void *data)
2043 scsi_internal_device_block(sdev);
2046 static int
2047 target_block(struct device *dev, void *data)
2049 if (scsi_is_target_device(dev))
2050 starget_for_each_device(to_scsi_target(dev), NULL,
2051 device_block);
2052 return 0;
2055 void
2056 scsi_target_block(struct device *dev)
2058 if (scsi_is_target_device(dev))
2059 starget_for_each_device(to_scsi_target(dev), NULL,
2060 device_block);
2061 else
2062 device_for_each_child(dev, NULL, target_block);
2064 EXPORT_SYMBOL_GPL(scsi_target_block);
2066 static void
2067 device_unblock(struct scsi_device *sdev, void *data)
2069 scsi_internal_device_unblock(sdev);
2072 static int
2073 target_unblock(struct device *dev, void *data)
2075 if (scsi_is_target_device(dev))
2076 starget_for_each_device(to_scsi_target(dev), NULL,
2077 device_unblock);
2078 return 0;
2081 void
2082 scsi_target_unblock(struct device *dev)
2084 if (scsi_is_target_device(dev))
2085 starget_for_each_device(to_scsi_target(dev), NULL,
2086 device_unblock);
2087 else
2088 device_for_each_child(dev, NULL, target_unblock);
2090 EXPORT_SYMBOL_GPL(scsi_target_unblock);