]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/scsi_lib.c
1f2782767ca9685b02db14845bff4b4a78c74624
[linux-2.6-omap-h63xx.git] / drivers / scsi / scsi_lib.c
1 /*
2  *  scsi_lib.c Copyright (C) 1999 Eric Youngdale
3  *
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  */
9
10 #include <linux/bio.h>
11 #include <linux/blkdev.h>
12 #include <linux/completion.h>
13 #include <linux/kernel.h>
14 #include <linux/mempool.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/delay.h>
19
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>
27
28 #include "scsi_priv.h"
29 #include "scsi_logging.h"
30
31
32 #define SG_MEMPOOL_NR           (sizeof(scsi_sg_pools)/sizeof(struct scsi_host_sg_pool))
33 #define SG_MEMPOOL_SIZE         32
34
35 struct scsi_host_sg_pool {
36         size_t          size;
37         char            *name; 
38         kmem_cache_t    *slab;
39         mempool_t       *pool;
40 };
41
42 #if (SCSI_MAX_PHYS_SEGMENTS < 32)
43 #error SCSI_MAX_PHYS_SEGMENTS is too small
44 #endif
45
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
65
66
67 /*
68  * Function:    scsi_insert_special_req()
69  *
70  * Purpose:     Insert pre-formed request into request queue.
71  *
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.
75  *
76  * Lock status: Assumed that lock is not held upon entry.
77  *
78  * Returns:     Nothing
79  *
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.
86  */
87 int scsi_insert_special_req(struct scsi_request *sreq, int at_head)
88 {
89         /*
90          * Because users of this function are apt to reuse requests with no
91          * modification, we have to sanitise the request flags here
92          */
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;
97 }
98
99 static void scsi_run_queue(struct request_queue *q);
100
101 /*
102  * Function:    scsi_unprep_request()
103  *
104  * Purpose:     Remove all preparation done for a request, including its
105  *              associated scsi_cmnd, so that it can be requeued.
106  *
107  * Arguments:   req     - request to unprepare
108  *
109  * Lock status: Assumed that no locks are held upon entry.
110  *
111  * Returns:     Nothing.
112  */
113 static void scsi_unprep_request(struct request *req)
114 {
115         struct scsi_cmnd *cmd = req->special;
116
117         req->flags &= ~REQ_DONTPREP;
118         req->special = (req->flags & REQ_SPECIAL) ? cmd->sc_request : NULL;
119
120         scsi_put_command(cmd);
121 }
122
123 /*
124  * Function:    scsi_queue_insert()
125  *
126  * Purpose:     Insert a command in the midlevel queue.
127  *
128  * Arguments:   cmd    - command that we are adding to queue.
129  *              reason - why we are inserting command to queue.
130  *
131  * Lock status: Assumed that lock is not held upon entry.
132  *
133  * Returns:     Nothing.
134  *
135  * Notes:       We do this for one of two cases.  Either the host is busy
136  *              and it cannot accept any more commands for the time being,
137  *              or the device returned QUEUE_FULL and can accept no more
138  *              commands.
139  * Notes:       This could be called either from an interrupt context or a
140  *              normal process context.
141  */
142 int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
143 {
144         struct Scsi_Host *host = cmd->device->host;
145         struct scsi_device *device = cmd->device;
146         struct request_queue *q = device->request_queue;
147         unsigned long flags;
148
149         SCSI_LOG_MLQUEUE(1,
150                  printk("Inserting command %p into mlqueue\n", cmd));
151
152         /*
153          * Set the appropriate busy bit for the device/host.
154          *
155          * If the host/device isn't busy, assume that something actually
156          * completed, and that we should be able to queue a command now.
157          *
158          * Note that the prior mid-layer assumption that any host could
159          * always queue at least one command is now broken.  The mid-layer
160          * will implement a user specifiable stall (see
161          * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
162          * if a command is requeued with no other commands outstanding
163          * either for the device or for the host.
164          */
165         if (reason == SCSI_MLQUEUE_HOST_BUSY)
166                 host->host_blocked = host->max_host_blocked;
167         else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
168                 device->device_blocked = device->max_device_blocked;
169
170         /*
171          * Decrement the counters, since these commands are no longer
172          * active on the host/device.
173          */
174         scsi_device_unbusy(device);
175
176         /*
177          * Requeue this command.  It will go before all other commands
178          * that are already in the queue.
179          *
180          * NOTE: there is magic here about the way the queue is plugged if
181          * we have no outstanding commands.
182          * 
183          * Although we *don't* plug the queue, we call the request
184          * function.  The SCSI request function detects the blocked condition
185          * and plugs the queue appropriately.
186          */
187         spin_lock_irqsave(q->queue_lock, flags);
188         blk_requeue_request(q, cmd->request);
189         spin_unlock_irqrestore(q->queue_lock, flags);
190
191         scsi_run_queue(q);
192
193         return 0;
194 }
195
196 /*
197  * Function:    scsi_do_req
198  *
199  * Purpose:     Queue a SCSI request
200  *
201  * Arguments:   sreq      - command descriptor.
202  *              cmnd      - actual SCSI command to be performed.
203  *              buffer    - data buffer.
204  *              bufflen   - size of data buffer.
205  *              done      - completion function to be run.
206  *              timeout   - how long to let it run before timeout.
207  *              retries   - number of retries we allow.
208  *
209  * Lock status: No locks held upon entry.
210  *
211  * Returns:     Nothing.
212  *
213  * Notes:       This function is only used for queueing requests for things
214  *              like ioctls and character device requests - this is because
215  *              we essentially just inject a request into the queue for the
216  *              device.
217  *
218  *              In order to support the scsi_device_quiesce function, we
219  *              now inject requests on the *head* of the device queue
220  *              rather than the tail.
221  */
222 void scsi_do_req(struct scsi_request *sreq, const void *cmnd,
223                  void *buffer, unsigned bufflen,
224                  void (*done)(struct scsi_cmnd *),
225                  int timeout, int retries)
226 {
227         /*
228          * If the upper level driver is reusing these things, then
229          * we should release the low-level block now.  Another one will
230          * be allocated later when this request is getting queued.
231          */
232         __scsi_release_request(sreq);
233
234         /*
235          * Our own function scsi_done (which marks the host as not busy,
236          * disables the timeout counter, etc) will be called by us or by the
237          * scsi_hosts[host].queuecommand() function needs to also call
238          * the completion function for the high level driver.
239          */
240         memcpy(sreq->sr_cmnd, cmnd, sizeof(sreq->sr_cmnd));
241         sreq->sr_bufflen = bufflen;
242         sreq->sr_buffer = buffer;
243         sreq->sr_allowed = retries;
244         sreq->sr_done = done;
245         sreq->sr_timeout_per_command = timeout;
246
247         if (sreq->sr_cmd_len == 0)
248                 sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
249
250         /*
251          * head injection *required* here otherwise quiesce won't work
252          */
253         scsi_insert_special_req(sreq, 1);
254 }
255 EXPORT_SYMBOL(scsi_do_req);
256
257 /**
258  * scsi_execute - insert request and wait for the result
259  * @sdev:       scsi device
260  * @cmd:        scsi command
261  * @data_direction: data direction
262  * @buffer:     data buffer
263  * @bufflen:    len of buffer
264  * @sense:      optional sense buffer
265  * @timeout:    request timeout in seconds
266  * @retries:    number of times to retry request
267  * @flags:      or into request flags;
268  *
269  * returns the req->errors value which is the the scsi_cmnd result
270  * field.
271  **/
272 int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
273                  int data_direction, void *buffer, unsigned bufflen,
274                  unsigned char *sense, int timeout, int retries, int flags)
275 {
276         struct request *req;
277         int write = (data_direction == DMA_TO_DEVICE);
278         int ret = DRIVER_ERROR << 24;
279
280         req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
281
282         if (bufflen &&  blk_rq_map_kern(sdev->request_queue, req,
283                                         buffer, bufflen, __GFP_WAIT))
284                 goto out;
285
286         req->cmd_len = COMMAND_SIZE(cmd[0]);
287         memcpy(req->cmd, cmd, req->cmd_len);
288         req->sense = sense;
289         req->sense_len = 0;
290         req->timeout = timeout;
291         req->flags |= flags | REQ_BLOCK_PC | REQ_SPECIAL | REQ_QUIET;
292
293         /*
294          * head injection *required* here otherwise quiesce won't work
295          */
296         blk_execute_rq(req->q, NULL, req, 1);
297
298         ret = req->errors;
299  out:
300         blk_put_request(req);
301
302         return ret;
303 }
304 EXPORT_SYMBOL(scsi_execute);
305
306
307 int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
308                      int data_direction, void *buffer, unsigned bufflen,
309                      struct scsi_sense_hdr *sshdr, int timeout, int retries)
310 {
311         char *sense = NULL;
312         int result;
313         
314         if (sshdr) {
315                 sense = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
316                 if (!sense)
317                         return DRIVER_ERROR << 24;
318                 memset(sense, 0, SCSI_SENSE_BUFFERSIZE);
319         }
320         result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
321                                   sense, timeout, retries, 0);
322         if (sshdr)
323                 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
324
325         kfree(sense);
326         return result;
327 }
328 EXPORT_SYMBOL(scsi_execute_req);
329
330 /*
331  * Function:    scsi_init_cmd_errh()
332  *
333  * Purpose:     Initialize cmd fields related to error handling.
334  *
335  * Arguments:   cmd     - command that is ready to be queued.
336  *
337  * Returns:     Nothing
338  *
339  * Notes:       This function has the job of initializing a number of
340  *              fields related to error handling.   Typically this will
341  *              be called once for each command, as required.
342  */
343 static int scsi_init_cmd_errh(struct scsi_cmnd *cmd)
344 {
345         cmd->serial_number = 0;
346
347         memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
348
349         if (cmd->cmd_len == 0)
350                 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
351
352         /*
353          * We need saved copies of a number of fields - this is because
354          * error handling may need to overwrite these with different values
355          * to run different commands, and once error handling is complete,
356          * we will need to restore these values prior to running the actual
357          * command.
358          */
359         cmd->old_use_sg = cmd->use_sg;
360         cmd->old_cmd_len = cmd->cmd_len;
361         cmd->sc_old_data_direction = cmd->sc_data_direction;
362         cmd->old_underflow = cmd->underflow;
363         memcpy(cmd->data_cmnd, cmd->cmnd, sizeof(cmd->cmnd));
364         cmd->buffer = cmd->request_buffer;
365         cmd->bufflen = cmd->request_bufflen;
366
367         return 1;
368 }
369
370 /*
371  * Function:   scsi_setup_cmd_retry()
372  *
373  * Purpose:    Restore the command state for a retry
374  *
375  * Arguments:  cmd      - command to be restored
376  *
377  * Returns:    Nothing
378  *
379  * Notes:      Immediately prior to retrying a command, we need
380  *             to restore certain fields that we saved above.
381  */
382 void scsi_setup_cmd_retry(struct scsi_cmnd *cmd)
383 {
384         memcpy(cmd->cmnd, cmd->data_cmnd, sizeof(cmd->data_cmnd));
385         cmd->request_buffer = cmd->buffer;
386         cmd->request_bufflen = cmd->bufflen;
387         cmd->use_sg = cmd->old_use_sg;
388         cmd->cmd_len = cmd->old_cmd_len;
389         cmd->sc_data_direction = cmd->sc_old_data_direction;
390         cmd->underflow = cmd->old_underflow;
391 }
392
393 void scsi_device_unbusy(struct scsi_device *sdev)
394 {
395         struct Scsi_Host *shost = sdev->host;
396         unsigned long flags;
397
398         spin_lock_irqsave(shost->host_lock, flags);
399         shost->host_busy--;
400         if (unlikely(scsi_host_in_recovery(shost) &&
401                      shost->host_failed))
402                 scsi_eh_wakeup(shost);
403         spin_unlock(shost->host_lock);
404         spin_lock(sdev->request_queue->queue_lock);
405         sdev->device_busy--;
406         spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
407 }
408
409 /*
410  * Called for single_lun devices on IO completion. Clear starget_sdev_user,
411  * and call blk_run_queue for all the scsi_devices on the target -
412  * including current_sdev first.
413  *
414  * Called with *no* scsi locks held.
415  */
416 static void scsi_single_lun_run(struct scsi_device *current_sdev)
417 {
418         struct Scsi_Host *shost = current_sdev->host;
419         struct scsi_device *sdev, *tmp;
420         struct scsi_target *starget = scsi_target(current_sdev);
421         unsigned long flags;
422
423         spin_lock_irqsave(shost->host_lock, flags);
424         starget->starget_sdev_user = NULL;
425         spin_unlock_irqrestore(shost->host_lock, flags);
426
427         /*
428          * Call blk_run_queue for all LUNs on the target, starting with
429          * current_sdev. We race with others (to set starget_sdev_user),
430          * but in most cases, we will be first. Ideally, each LU on the
431          * target would get some limited time or requests on the target.
432          */
433         blk_run_queue(current_sdev->request_queue);
434
435         spin_lock_irqsave(shost->host_lock, flags);
436         if (starget->starget_sdev_user)
437                 goto out;
438         list_for_each_entry_safe(sdev, tmp, &starget->devices,
439                         same_target_siblings) {
440                 if (sdev == current_sdev)
441                         continue;
442                 if (scsi_device_get(sdev))
443                         continue;
444
445                 spin_unlock_irqrestore(shost->host_lock, flags);
446                 blk_run_queue(sdev->request_queue);
447                 spin_lock_irqsave(shost->host_lock, flags);
448         
449                 scsi_device_put(sdev);
450         }
451  out:
452         spin_unlock_irqrestore(shost->host_lock, flags);
453 }
454
455 /*
456  * Function:    scsi_run_queue()
457  *
458  * Purpose:     Select a proper request queue to serve next
459  *
460  * Arguments:   q       - last request's queue
461  *
462  * Returns:     Nothing
463  *
464  * Notes:       The previous command was completely finished, start
465  *              a new one if possible.
466  */
467 static void scsi_run_queue(struct request_queue *q)
468 {
469         struct scsi_device *sdev = q->queuedata;
470         struct Scsi_Host *shost = sdev->host;
471         unsigned long flags;
472
473         if (sdev->single_lun)
474                 scsi_single_lun_run(sdev);
475
476         spin_lock_irqsave(shost->host_lock, flags);
477         while (!list_empty(&shost->starved_list) &&
478                !shost->host_blocked && !shost->host_self_blocked &&
479                 !((shost->can_queue > 0) &&
480                   (shost->host_busy >= shost->can_queue))) {
481                 /*
482                  * As long as shost is accepting commands and we have
483                  * starved queues, call blk_run_queue. scsi_request_fn
484                  * drops the queue_lock and can add us back to the
485                  * starved_list.
486                  *
487                  * host_lock protects the starved_list and starved_entry.
488                  * scsi_request_fn must get the host_lock before checking
489                  * or modifying starved_list or starved_entry.
490                  */
491                 sdev = list_entry(shost->starved_list.next,
492                                           struct scsi_device, starved_entry);
493                 list_del_init(&sdev->starved_entry);
494                 spin_unlock_irqrestore(shost->host_lock, flags);
495
496                 blk_run_queue(sdev->request_queue);
497
498                 spin_lock_irqsave(shost->host_lock, flags);
499                 if (unlikely(!list_empty(&sdev->starved_entry)))
500                         /*
501                          * sdev lost a race, and was put back on the
502                          * starved list. This is unlikely but without this
503                          * in theory we could loop forever.
504                          */
505                         break;
506         }
507         spin_unlock_irqrestore(shost->host_lock, flags);
508
509         blk_run_queue(q);
510 }
511
512 /*
513  * Function:    scsi_requeue_command()
514  *
515  * Purpose:     Handle post-processing of completed commands.
516  *
517  * Arguments:   q       - queue to operate on
518  *              cmd     - command that may need to be requeued.
519  *
520  * Returns:     Nothing
521  *
522  * Notes:       After command completion, there may be blocks left
523  *              over which weren't finished by the previous command
524  *              this can be for a number of reasons - the main one is
525  *              I/O errors in the middle of the request, in which case
526  *              we need to request the blocks that come after the bad
527  *              sector.
528  * Notes:       Upon return, cmd is a stale pointer.
529  */
530 static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
531 {
532         struct request *req = cmd->request;
533         unsigned long flags;
534
535         scsi_unprep_request(req);
536         spin_lock_irqsave(q->queue_lock, flags);
537         blk_requeue_request(q, req);
538         spin_unlock_irqrestore(q->queue_lock, flags);
539
540         scsi_run_queue(q);
541 }
542
543 void scsi_next_command(struct scsi_cmnd *cmd)
544 {
545         struct scsi_device *sdev = cmd->device;
546         struct request_queue *q = sdev->request_queue;
547
548         /* need to hold a reference on the device before we let go of the cmd */
549         get_device(&sdev->sdev_gendev);
550
551         scsi_put_command(cmd);
552         scsi_run_queue(q);
553
554         /* ok to remove device now */
555         put_device(&sdev->sdev_gendev);
556 }
557
558 void scsi_run_host_queues(struct Scsi_Host *shost)
559 {
560         struct scsi_device *sdev;
561
562         shost_for_each_device(sdev, shost)
563                 scsi_run_queue(sdev->request_queue);
564 }
565
566 /*
567  * Function:    scsi_end_request()
568  *
569  * Purpose:     Post-processing of completed commands (usually invoked at end
570  *              of upper level post-processing and scsi_io_completion).
571  *
572  * Arguments:   cmd      - command that is complete.
573  *              uptodate - 1 if I/O indicates success, <= 0 for I/O error.
574  *              bytes    - number of bytes of completed I/O
575  *              requeue  - indicates whether we should requeue leftovers.
576  *
577  * Lock status: Assumed that lock is not held upon entry.
578  *
579  * Returns:     cmd if requeue required, NULL otherwise.
580  *
581  * Notes:       This is called for block device requests in order to
582  *              mark some number of sectors as complete.
583  * 
584  *              We are guaranteeing that the request queue will be goosed
585  *              at some point during this call.
586  * Notes:       If cmd was requeued, upon return it will be a stale pointer.
587  */
588 static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
589                                           int bytes, int requeue)
590 {
591         request_queue_t *q = cmd->device->request_queue;
592         struct request *req = cmd->request;
593         unsigned long flags;
594
595         /*
596          * If there are blocks left over at the end, set up the command
597          * to queue the remainder of them.
598          */
599         if (end_that_request_chunk(req, uptodate, bytes)) {
600                 int leftover = (req->hard_nr_sectors << 9);
601
602                 if (blk_pc_request(req))
603                         leftover = req->data_len;
604
605                 /* kill remainder if no retrys */
606                 if (!uptodate && blk_noretry_request(req))
607                         end_that_request_chunk(req, 0, leftover);
608                 else {
609                         if (requeue) {
610                                 /*
611                                  * Bleah.  Leftovers again.  Stick the
612                                  * leftovers in the front of the
613                                  * queue, and goose the queue again.
614                                  */
615                                 scsi_requeue_command(q, cmd);
616                                 cmd = NULL;
617                         }
618                         return cmd;
619                 }
620         }
621
622         add_disk_randomness(req->rq_disk);
623
624         spin_lock_irqsave(q->queue_lock, flags);
625         if (blk_rq_tagged(req))
626                 blk_queue_end_tag(q, req);
627         end_that_request_last(req);
628         spin_unlock_irqrestore(q->queue_lock, flags);
629
630         /*
631          * This will goose the queue request function at the end, so we don't
632          * need to worry about launching another command.
633          */
634         scsi_next_command(cmd);
635         return NULL;
636 }
637
638 static struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask)
639 {
640         struct scsi_host_sg_pool *sgp;
641         struct scatterlist *sgl;
642
643         BUG_ON(!cmd->use_sg);
644
645         switch (cmd->use_sg) {
646         case 1 ... 8:
647                 cmd->sglist_len = 0;
648                 break;
649         case 9 ... 16:
650                 cmd->sglist_len = 1;
651                 break;
652         case 17 ... 32:
653                 cmd->sglist_len = 2;
654                 break;
655 #if (SCSI_MAX_PHYS_SEGMENTS > 32)
656         case 33 ... 64:
657                 cmd->sglist_len = 3;
658                 break;
659 #if (SCSI_MAX_PHYS_SEGMENTS > 64)
660         case 65 ... 128:
661                 cmd->sglist_len = 4;
662                 break;
663 #if (SCSI_MAX_PHYS_SEGMENTS  > 128)
664         case 129 ... 256:
665                 cmd->sglist_len = 5;
666                 break;
667 #endif
668 #endif
669 #endif
670         default:
671                 return NULL;
672         }
673
674         sgp = scsi_sg_pools + cmd->sglist_len;
675         sgl = mempool_alloc(sgp->pool, gfp_mask);
676         return sgl;
677 }
678
679 static void scsi_free_sgtable(struct scatterlist *sgl, int index)
680 {
681         struct scsi_host_sg_pool *sgp;
682
683         BUG_ON(index >= SG_MEMPOOL_NR);
684
685         sgp = scsi_sg_pools + index;
686         mempool_free(sgl, sgp->pool);
687 }
688
689 /*
690  * Function:    scsi_release_buffers()
691  *
692  * Purpose:     Completion processing for block device I/O requests.
693  *
694  * Arguments:   cmd     - command that we are bailing.
695  *
696  * Lock status: Assumed that no lock is held upon entry.
697  *
698  * Returns:     Nothing
699  *
700  * Notes:       In the event that an upper level driver rejects a
701  *              command, we must release resources allocated during
702  *              the __init_io() function.  Primarily this would involve
703  *              the scatter-gather table, and potentially any bounce
704  *              buffers.
705  */
706 static void scsi_release_buffers(struct scsi_cmnd *cmd)
707 {
708         struct request *req = cmd->request;
709
710         /*
711          * Free up any indirection buffers we allocated for DMA purposes. 
712          */
713         if (cmd->use_sg)
714                 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
715         else if (cmd->request_buffer != req->buffer)
716                 kfree(cmd->request_buffer);
717
718         /*
719          * Zero these out.  They now point to freed memory, and it is
720          * dangerous to hang onto the pointers.
721          */
722         cmd->buffer  = NULL;
723         cmd->bufflen = 0;
724         cmd->request_buffer = NULL;
725         cmd->request_bufflen = 0;
726 }
727
728 /*
729  * Function:    scsi_io_completion()
730  *
731  * Purpose:     Completion processing for block device I/O requests.
732  *
733  * Arguments:   cmd   - command that is finished.
734  *
735  * Lock status: Assumed that no lock is held upon entry.
736  *
737  * Returns:     Nothing
738  *
739  * Notes:       This function is matched in terms of capabilities to
740  *              the function that created the scatter-gather list.
741  *              In other words, if there are no bounce buffers
742  *              (the normal case for most drivers), we don't need
743  *              the logic to deal with cleaning up afterwards.
744  *
745  *              We must do one of several things here:
746  *
747  *              a) Call scsi_end_request.  This will finish off the
748  *                 specified number of sectors.  If we are done, the
749  *                 command block will be released, and the queue
750  *                 function will be goosed.  If we are not done, then
751  *                 scsi_end_request will directly goose the queue.
752  *
753  *              b) We can just use scsi_requeue_command() here.  This would
754  *                 be used if we just wanted to retry, for example.
755  */
756 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes,
757                         unsigned int block_bytes)
758 {
759         int result = cmd->result;
760         int this_count = cmd->bufflen;
761         request_queue_t *q = cmd->device->request_queue;
762         struct request *req = cmd->request;
763         int clear_errors = 1;
764         struct scsi_sense_hdr sshdr;
765         int sense_valid = 0;
766         int sense_deferred = 0;
767
768         if (blk_complete_barrier_rq(q, req, good_bytes >> 9))
769                 return;
770
771         /*
772          * Free up any indirection buffers we allocated for DMA purposes. 
773          * For the case of a READ, we need to copy the data out of the
774          * bounce buffer and into the real buffer.
775          */
776         if (cmd->use_sg)
777                 scsi_free_sgtable(cmd->buffer, cmd->sglist_len);
778         else if (cmd->buffer != req->buffer) {
779                 if (rq_data_dir(req) == READ) {
780                         unsigned long flags;
781                         char *to = bio_kmap_irq(req->bio, &flags);
782                         memcpy(to, cmd->buffer, cmd->bufflen);
783                         bio_kunmap_irq(to, &flags);
784                 }
785                 kfree(cmd->buffer);
786         }
787
788         if (result) {
789                 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
790                 if (sense_valid)
791                         sense_deferred = scsi_sense_is_deferred(&sshdr);
792         }
793         if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
794                 req->errors = result;
795                 if (result) {
796                         clear_errors = 0;
797                         if (sense_valid && req->sense) {
798                                 /*
799                                  * SG_IO wants current and deferred errors
800                                  */
801                                 int len = 8 + cmd->sense_buffer[7];
802
803                                 if (len > SCSI_SENSE_BUFFERSIZE)
804                                         len = SCSI_SENSE_BUFFERSIZE;
805                                 memcpy(req->sense, cmd->sense_buffer,  len);
806                                 req->sense_len = len;
807                         }
808                 } else
809                         req->data_len = cmd->resid;
810         }
811
812         /*
813          * Zero these out.  They now point to freed memory, and it is
814          * dangerous to hang onto the pointers.
815          */
816         cmd->buffer  = NULL;
817         cmd->bufflen = 0;
818         cmd->request_buffer = NULL;
819         cmd->request_bufflen = 0;
820
821         /*
822          * Next deal with any sectors which we were able to correctly
823          * handle.
824          */
825         if (good_bytes >= 0) {
826                 SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, %d bytes done.\n",
827                                               req->nr_sectors, good_bytes));
828                 SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n", cmd->use_sg));
829
830                 if (clear_errors)
831                         req->errors = 0;
832                 /*
833                  * If multiple sectors are requested in one buffer, then
834                  * they will have been finished off by the first command.
835                  * If not, then we have a multi-buffer command.
836                  *
837                  * If block_bytes != 0, it means we had a medium error
838                  * of some sort, and that we want to mark some number of
839                  * sectors as not uptodate.  Thus we want to inhibit
840                  * requeueing right here - we will requeue down below
841                  * when we handle the bad sectors.
842                  */
843
844                 /*
845                  * If the command completed without error, then either
846                  * finish off the rest of the command, or start a new one.
847                  */
848                 if (scsi_end_request(cmd, 1, good_bytes, result == 0) == NULL)
849                         return;
850         }
851         /*
852          * Now, if we were good little boys and girls, Santa left us a request
853          * sense buffer.  We can extract information from this, so we
854          * can choose a block to remap, etc.
855          */
856         if (sense_valid && !sense_deferred) {
857                 switch (sshdr.sense_key) {
858                 case UNIT_ATTENTION:
859                         if (cmd->device->removable) {
860                                 /* detected disc change.  set a bit 
861                                  * and quietly refuse further access.
862                                  */
863                                 cmd->device->changed = 1;
864                                 scsi_end_request(cmd, 0,
865                                                 this_count, 1);
866                                 return;
867                         } else {
868                                 /*
869                                 * Must have been a power glitch, or a
870                                 * bus reset.  Could not have been a
871                                 * media change, so we just retry the
872                                 * request and see what happens.  
873                                 */
874                                 scsi_requeue_command(q, cmd);
875                                 return;
876                         }
877                         break;
878                 case ILLEGAL_REQUEST:
879                         /*
880                         * If we had an ILLEGAL REQUEST returned, then we may
881                         * have performed an unsupported command.  The only
882                         * thing this should be would be a ten byte read where
883                         * only a six byte read was supported.  Also, on a
884                         * system where READ CAPACITY failed, we may have read
885                         * past the end of the disk.
886                         */
887                         if ((cmd->device->use_10_for_rw &&
888                             sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
889                             (cmd->cmnd[0] == READ_10 ||
890                              cmd->cmnd[0] == WRITE_10)) {
891                                 cmd->device->use_10_for_rw = 0;
892                                 /*
893                                  * This will cause a retry with a 6-byte
894                                  * command.
895                                  */
896                                 scsi_requeue_command(q, cmd);
897                                 result = 0;
898                         } else {
899                                 scsi_end_request(cmd, 0, this_count, 1);
900                                 return;
901                         }
902                         break;
903                 case NOT_READY:
904                         /*
905                          * If the device is in the process of becoming ready,
906                          * retry.
907                          */
908                         if (sshdr.asc == 0x04 && sshdr.ascq == 0x01) {
909                                 scsi_requeue_command(q, cmd);
910                                 return;
911                         }
912                         if (!(req->flags & REQ_QUIET))
913                                 scmd_printk(KERN_INFO, cmd,
914                                            "Device not ready.\n");
915                         scsi_end_request(cmd, 0, this_count, 1);
916                         return;
917                 case VOLUME_OVERFLOW:
918                         if (!(req->flags & REQ_QUIET)) {
919                                 scmd_printk(KERN_INFO, cmd,
920                                            "Volume overflow, CDB: ");
921                                 __scsi_print_command(cmd->data_cmnd);
922                                 scsi_print_sense("", cmd);
923                         }
924                         scsi_end_request(cmd, 0, block_bytes, 1);
925                         return;
926                 default:
927                         break;
928                 }
929         }                       /* driver byte != 0 */
930         if (host_byte(result) == DID_RESET) {
931                 /*
932                  * Third party bus reset or reset for error
933                  * recovery reasons.  Just retry the request
934                  * and see what happens.  
935                  */
936                 scsi_requeue_command(q, cmd);
937                 return;
938         }
939         if (result) {
940                 if (!(req->flags & REQ_QUIET)) {
941                         scmd_printk(KERN_INFO, cmd,
942                                    "SCSI error: return code = 0x%x\n", result);
943
944                         if (driver_byte(result) & DRIVER_SENSE)
945                                 scsi_print_sense("", cmd);
946                 }
947                 /*
948                  * Mark a single buffer as not uptodate.  Queue the remainder.
949                  * We sometimes get this cruft in the event that a medium error
950                  * isn't properly reported.
951                  */
952                 block_bytes = req->hard_cur_sectors << 9;
953                 if (!block_bytes)
954                         block_bytes = req->data_len;
955                 scsi_end_request(cmd, 0, block_bytes, 1);
956         }
957 }
958 EXPORT_SYMBOL(scsi_io_completion);
959
960 /*
961  * Function:    scsi_init_io()
962  *
963  * Purpose:     SCSI I/O initialize function.
964  *
965  * Arguments:   cmd   - Command descriptor we wish to initialize
966  *
967  * Returns:     0 on success
968  *              BLKPREP_DEFER if the failure is retryable
969  *              BLKPREP_KILL if the failure is fatal
970  */
971 static int scsi_init_io(struct scsi_cmnd *cmd)
972 {
973         struct request     *req = cmd->request;
974         struct scatterlist *sgpnt;
975         int                count;
976
977         /*
978          * if this is a rq->data based REQ_BLOCK_PC, setup for a non-sg xfer
979          */
980         if ((req->flags & REQ_BLOCK_PC) && !req->bio) {
981                 cmd->request_bufflen = req->data_len;
982                 cmd->request_buffer = req->data;
983                 req->buffer = req->data;
984                 cmd->use_sg = 0;
985                 return 0;
986         }
987
988         /*
989          * we used to not use scatter-gather for single segment request,
990          * but now we do (it makes highmem I/O easier to support without
991          * kmapping pages)
992          */
993         cmd->use_sg = req->nr_phys_segments;
994
995         /*
996          * if sg table allocation fails, requeue request later.
997          */
998         sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
999         if (unlikely(!sgpnt)) {
1000                 scsi_unprep_request(req);
1001                 return BLKPREP_DEFER;
1002         }
1003
1004         cmd->request_buffer = (char *) sgpnt;
1005         cmd->request_bufflen = req->nr_sectors << 9;
1006         if (blk_pc_request(req))
1007                 cmd->request_bufflen = req->data_len;
1008         req->buffer = NULL;
1009
1010         /* 
1011          * Next, walk the list, and fill in the addresses and sizes of
1012          * each segment.
1013          */
1014         count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
1015
1016         /*
1017          * mapped well, send it off
1018          */
1019         if (likely(count <= cmd->use_sg)) {
1020                 cmd->use_sg = count;
1021                 return 0;
1022         }
1023
1024         printk(KERN_ERR "Incorrect number of segments after building list\n");
1025         printk(KERN_ERR "counted %d, received %d\n", count, cmd->use_sg);
1026         printk(KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n", req->nr_sectors,
1027                         req->current_nr_sectors);
1028
1029         /* release the command and kill it */
1030         scsi_release_buffers(cmd);
1031         scsi_put_command(cmd);
1032         return BLKPREP_KILL;
1033 }
1034
1035 static int scsi_prepare_flush_fn(request_queue_t *q, struct request *rq)
1036 {
1037         struct scsi_device *sdev = q->queuedata;
1038         struct scsi_driver *drv;
1039
1040         if (sdev->sdev_state == SDEV_RUNNING) {
1041                 drv = *(struct scsi_driver **) rq->rq_disk->private_data;
1042
1043                 if (drv->prepare_flush)
1044                         return drv->prepare_flush(q, rq);
1045         }
1046
1047         return 0;
1048 }
1049
1050 static void scsi_end_flush_fn(request_queue_t *q, struct request *rq)
1051 {
1052         struct scsi_device *sdev = q->queuedata;
1053         struct request *flush_rq = rq->end_io_data;
1054         struct scsi_driver *drv;
1055
1056         if (flush_rq->errors) {
1057                 printk("scsi: barrier error, disabling flush support\n");
1058                 blk_queue_ordered(q, QUEUE_ORDERED_NONE);
1059         }
1060
1061         if (sdev->sdev_state == SDEV_RUNNING) {
1062                 drv = *(struct scsi_driver **) rq->rq_disk->private_data;
1063                 drv->end_flush(q, rq);
1064         }
1065 }
1066
1067 static int scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
1068                                sector_t *error_sector)
1069 {
1070         struct scsi_device *sdev = q->queuedata;
1071         struct scsi_driver *drv;
1072
1073         if (sdev->sdev_state != SDEV_RUNNING)
1074                 return -ENXIO;
1075
1076         drv = *(struct scsi_driver **) disk->private_data;
1077         if (drv->issue_flush)
1078                 return drv->issue_flush(&sdev->sdev_gendev, error_sector);
1079
1080         return -EOPNOTSUPP;
1081 }
1082
1083 static void scsi_generic_done(struct scsi_cmnd *cmd)
1084 {
1085         BUG_ON(!blk_pc_request(cmd->request));
1086         scsi_io_completion(cmd, cmd->result == 0 ? cmd->bufflen : 0, 0);
1087 }
1088
1089 static int scsi_prep_fn(struct request_queue *q, struct request *req)
1090 {
1091         struct scsi_device *sdev = q->queuedata;
1092         struct scsi_cmnd *cmd;
1093         int specials_only = 0;
1094
1095         /*
1096          * Just check to see if the device is online.  If it isn't, we
1097          * refuse to process any commands.  The device must be brought
1098          * online before trying any recovery commands
1099          */
1100         if (unlikely(!scsi_device_online(sdev))) {
1101                 sdev_printk(KERN_ERR, sdev,
1102                             "rejecting I/O to offline device\n");
1103                 goto kill;
1104         }
1105         if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
1106                 /* OK, we're not in a running state don't prep
1107                  * user commands */
1108                 if (sdev->sdev_state == SDEV_DEL) {
1109                         /* Device is fully deleted, no commands
1110                          * at all allowed down */
1111                         sdev_printk(KERN_ERR, sdev,
1112                                     "rejecting I/O to dead device\n");
1113                         goto kill;
1114                 }
1115                 /* OK, we only allow special commands (i.e. not
1116                  * user initiated ones */
1117                 specials_only = sdev->sdev_state;
1118         }
1119
1120         /*
1121          * Find the actual device driver associated with this command.
1122          * The SPECIAL requests are things like character device or
1123          * ioctls, which did not originate from ll_rw_blk.  Note that
1124          * the special field is also used to indicate the cmd for
1125          * the remainder of a partially fulfilled request that can 
1126          * come up when there is a medium error.  We have to treat
1127          * these two cases differently.  We differentiate by looking
1128          * at request->cmd, as this tells us the real story.
1129          */
1130         if (req->flags & REQ_SPECIAL && req->special) {
1131                 struct scsi_request *sreq = req->special;
1132
1133                 if (sreq->sr_magic == SCSI_REQ_MAGIC) {
1134                         cmd = scsi_get_command(sreq->sr_device, GFP_ATOMIC);
1135                         if (unlikely(!cmd))
1136                                 goto defer;
1137                         scsi_init_cmd_from_req(cmd, sreq);
1138                 } else
1139                         cmd = req->special;
1140         } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
1141
1142                 if(unlikely(specials_only) && !(req->flags & REQ_SPECIAL)) {
1143                         if(specials_only == SDEV_QUIESCE ||
1144                                         specials_only == SDEV_BLOCK)
1145                                 goto defer;
1146                         
1147                         sdev_printk(KERN_ERR, sdev,
1148                                     "rejecting I/O to device being removed\n");
1149                         goto kill;
1150                 }
1151                         
1152                         
1153                 /*
1154                  * Now try and find a command block that we can use.
1155                  */
1156                 if (!req->special) {
1157                         cmd = scsi_get_command(sdev, GFP_ATOMIC);
1158                         if (unlikely(!cmd))
1159                                 goto defer;
1160                 } else
1161                         cmd = req->special;
1162                 
1163                 /* pull a tag out of the request if we have one */
1164                 cmd->tag = req->tag;
1165         } else {
1166                 blk_dump_rq_flags(req, "SCSI bad req");
1167                 goto kill;
1168         }
1169         
1170         /* note the overloading of req->special.  When the tag
1171          * is active it always means cmd.  If the tag goes
1172          * back for re-queueing, it may be reset */
1173         req->special = cmd;
1174         cmd->request = req;
1175         
1176         /*
1177          * FIXME: drop the lock here because the functions below
1178          * expect to be called without the queue lock held.  Also,
1179          * previously, we dequeued the request before dropping the
1180          * lock.  We hope REQ_STARTED prevents anything untoward from
1181          * happening now.
1182          */
1183         if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
1184                 struct scsi_driver *drv;
1185                 int ret;
1186
1187                 /*
1188                  * This will do a couple of things:
1189                  *  1) Fill in the actual SCSI command.
1190                  *  2) Fill in any other upper-level specific fields
1191                  * (timeout).
1192                  *
1193                  * If this returns 0, it means that the request failed
1194                  * (reading past end of disk, reading offline device,
1195                  * etc).   This won't actually talk to the device, but
1196                  * some kinds of consistency checking may cause the     
1197                  * request to be rejected immediately.
1198                  */
1199
1200                 /* 
1201                  * This sets up the scatter-gather table (allocating if
1202                  * required).
1203                  */
1204                 ret = scsi_init_io(cmd);
1205                 switch(ret) {
1206                         /* For BLKPREP_KILL/DEFER the cmd was released */
1207                 case BLKPREP_KILL:
1208                         goto kill;
1209                 case BLKPREP_DEFER:
1210                         goto defer;
1211                 }
1212                 
1213                 /*
1214                  * Initialize the actual SCSI command for this request.
1215                  */
1216                 if (req->rq_disk) {
1217                         drv = *(struct scsi_driver **)req->rq_disk->private_data;
1218                         if (unlikely(!drv->init_command(cmd))) {
1219                                 scsi_release_buffers(cmd);
1220                                 scsi_put_command(cmd);
1221                                 goto kill;
1222                         }
1223                 } else {
1224                         memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
1225                         cmd->cmd_len = req->cmd_len;
1226                         if (rq_data_dir(req) == WRITE)
1227                                 cmd->sc_data_direction = DMA_TO_DEVICE;
1228                         else if (req->data_len)
1229                                 cmd->sc_data_direction = DMA_FROM_DEVICE;
1230                         else
1231                                 cmd->sc_data_direction = DMA_NONE;
1232                         
1233                         cmd->transfersize = req->data_len;
1234                         cmd->allowed = 3;
1235                         cmd->timeout_per_command = req->timeout;
1236                         cmd->done = scsi_generic_done;
1237                 }
1238         }
1239
1240         /*
1241          * The request is now prepped, no need to come back here
1242          */
1243         req->flags |= REQ_DONTPREP;
1244         return BLKPREP_OK;
1245
1246  defer:
1247         /* If we defer, the elv_next_request() returns NULL, but the
1248          * queue must be restarted, so we plug here if no returning
1249          * command will automatically do that. */
1250         if (sdev->device_busy == 0)
1251                 blk_plug_device(q);
1252         return BLKPREP_DEFER;
1253  kill:
1254         req->errors = DID_NO_CONNECT << 16;
1255         return BLKPREP_KILL;
1256 }
1257
1258 /*
1259  * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
1260  * return 0.
1261  *
1262  * Called with the queue_lock held.
1263  */
1264 static inline int scsi_dev_queue_ready(struct request_queue *q,
1265                                   struct scsi_device *sdev)
1266 {
1267         if (sdev->device_busy >= sdev->queue_depth)
1268                 return 0;
1269         if (sdev->device_busy == 0 && sdev->device_blocked) {
1270                 /*
1271                  * unblock after device_blocked iterates to zero
1272                  */
1273                 if (--sdev->device_blocked == 0) {
1274                         SCSI_LOG_MLQUEUE(3,
1275                                    sdev_printk(KERN_INFO, sdev,
1276                                    "unblocking device at zero depth\n"));
1277                 } else {
1278                         blk_plug_device(q);
1279                         return 0;
1280                 }
1281         }
1282         if (sdev->device_blocked)
1283                 return 0;
1284
1285         return 1;
1286 }
1287
1288 /*
1289  * scsi_host_queue_ready: if we can send requests to shost, return 1 else
1290  * return 0. We must end up running the queue again whenever 0 is
1291  * returned, else IO can hang.
1292  *
1293  * Called with host_lock held.
1294  */
1295 static inline int scsi_host_queue_ready(struct request_queue *q,
1296                                    struct Scsi_Host *shost,
1297                                    struct scsi_device *sdev)
1298 {
1299         if (scsi_host_in_recovery(shost))
1300                 return 0;
1301         if (shost->host_busy == 0 && shost->host_blocked) {
1302                 /*
1303                  * unblock after host_blocked iterates to zero
1304                  */
1305                 if (--shost->host_blocked == 0) {
1306                         SCSI_LOG_MLQUEUE(3,
1307                                 printk("scsi%d unblocking host at zero depth\n",
1308                                         shost->host_no));
1309                 } else {
1310                         blk_plug_device(q);
1311                         return 0;
1312                 }
1313         }
1314         if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
1315             shost->host_blocked || shost->host_self_blocked) {
1316                 if (list_empty(&sdev->starved_entry))
1317                         list_add_tail(&sdev->starved_entry, &shost->starved_list);
1318                 return 0;
1319         }
1320
1321         /* We're OK to process the command, so we can't be starved */
1322         if (!list_empty(&sdev->starved_entry))
1323                 list_del_init(&sdev->starved_entry);
1324
1325         return 1;
1326 }
1327
1328 /*
1329  * Kill a request for a dead device
1330  */
1331 static void scsi_kill_request(struct request *req, request_queue_t *q)
1332 {
1333         struct scsi_cmnd *cmd = req->special;
1334
1335         blkdev_dequeue_request(req);
1336
1337         if (unlikely(cmd == NULL)) {
1338                 printk(KERN_CRIT "impossible request in %s.\n",
1339                                  __FUNCTION__);
1340                 BUG();
1341         }
1342
1343         scsi_init_cmd_errh(cmd);
1344         cmd->result = DID_NO_CONNECT << 16;
1345         atomic_inc(&cmd->device->iorequest_cnt);
1346         __scsi_done(cmd);
1347 }
1348
1349 /*
1350  * Function:    scsi_request_fn()
1351  *
1352  * Purpose:     Main strategy routine for SCSI.
1353  *
1354  * Arguments:   q       - Pointer to actual queue.
1355  *
1356  * Returns:     Nothing
1357  *
1358  * Lock status: IO request lock assumed to be held when called.
1359  */
1360 static void scsi_request_fn(struct request_queue *q)
1361 {
1362         struct scsi_device *sdev = q->queuedata;
1363         struct Scsi_Host *shost;
1364         struct scsi_cmnd *cmd;
1365         struct request *req;
1366
1367         if (!sdev) {
1368                 printk("scsi: killing requests for dead queue\n");
1369                 while ((req = elv_next_request(q)) != NULL)
1370                         scsi_kill_request(req, q);
1371                 return;
1372         }
1373
1374         if(!get_device(&sdev->sdev_gendev))
1375                 /* We must be tearing the block queue down already */
1376                 return;
1377
1378         /*
1379          * To start with, we keep looping until the queue is empty, or until
1380          * the host is no longer able to accept any more requests.
1381          */
1382         shost = sdev->host;
1383         while (!blk_queue_plugged(q)) {
1384                 int rtn;
1385                 /*
1386                  * get next queueable request.  We do this early to make sure
1387                  * that the request is fully prepared even if we cannot 
1388                  * accept it.
1389                  */
1390                 req = elv_next_request(q);
1391                 if (!req || !scsi_dev_queue_ready(q, sdev))
1392                         break;
1393
1394                 if (unlikely(!scsi_device_online(sdev))) {
1395                         sdev_printk(KERN_ERR, sdev,
1396                                     "rejecting I/O to offline device\n");
1397                         scsi_kill_request(req, q);
1398                         continue;
1399                 }
1400
1401
1402                 /*
1403                  * Remove the request from the request list.
1404                  */
1405                 if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
1406                         blkdev_dequeue_request(req);
1407                 sdev->device_busy++;
1408
1409                 spin_unlock(q->queue_lock);
1410                 cmd = req->special;
1411                 if (unlikely(cmd == NULL)) {
1412                         printk(KERN_CRIT "impossible request in %s.\n"
1413                                          "please mail a stack trace to "
1414                                          "linux-scsi@vger.kernel.org",
1415                                          __FUNCTION__);
1416                         BUG();
1417                 }
1418                 spin_lock(shost->host_lock);
1419
1420                 if (!scsi_host_queue_ready(q, shost, sdev))
1421                         goto not_ready;
1422                 if (sdev->single_lun) {
1423                         if (scsi_target(sdev)->starget_sdev_user &&
1424                             scsi_target(sdev)->starget_sdev_user != sdev)
1425                                 goto not_ready;
1426                         scsi_target(sdev)->starget_sdev_user = sdev;
1427                 }
1428                 shost->host_busy++;
1429
1430                 /*
1431                  * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
1432                  *              take the lock again.
1433                  */
1434                 spin_unlock_irq(shost->host_lock);
1435
1436                 /*
1437                  * Finally, initialize any error handling parameters, and set up
1438                  * the timers for timeouts.
1439                  */
1440                 scsi_init_cmd_errh(cmd);
1441
1442                 /*
1443                  * Dispatch the command to the low-level driver.
1444                  */
1445                 rtn = scsi_dispatch_cmd(cmd);
1446                 spin_lock_irq(q->queue_lock);
1447                 if(rtn) {
1448                         /* we're refusing the command; because of
1449                          * the way locks get dropped, we need to 
1450                          * check here if plugging is required */
1451                         if(sdev->device_busy == 0)
1452                                 blk_plug_device(q);
1453
1454                         break;
1455                 }
1456         }
1457
1458         goto out;
1459
1460  not_ready:
1461         spin_unlock_irq(shost->host_lock);
1462
1463         /*
1464          * lock q, handle tag, requeue req, and decrement device_busy. We
1465          * must return with queue_lock held.
1466          *
1467          * Decrementing device_busy without checking it is OK, as all such
1468          * cases (host limits or settings) should run the queue at some
1469          * later time.
1470          */
1471         spin_lock_irq(q->queue_lock);
1472         blk_requeue_request(q, req);
1473         sdev->device_busy--;
1474         if(sdev->device_busy == 0)
1475                 blk_plug_device(q);
1476  out:
1477         /* must be careful here...if we trigger the ->remove() function
1478          * we cannot be holding the q lock */
1479         spin_unlock_irq(q->queue_lock);
1480         put_device(&sdev->sdev_gendev);
1481         spin_lock_irq(q->queue_lock);
1482 }
1483
1484 u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
1485 {
1486         struct device *host_dev;
1487         u64 bounce_limit = 0xffffffff;
1488
1489         if (shost->unchecked_isa_dma)
1490                 return BLK_BOUNCE_ISA;
1491         /*
1492          * Platforms with virtual-DMA translation
1493          * hardware have no practical limit.
1494          */
1495         if (!PCI_DMA_BUS_IS_PHYS)
1496                 return BLK_BOUNCE_ANY;
1497
1498         host_dev = scsi_get_device(shost);
1499         if (host_dev && host_dev->dma_mask)
1500                 bounce_limit = *host_dev->dma_mask;
1501
1502         return bounce_limit;
1503 }
1504 EXPORT_SYMBOL(scsi_calculate_bounce_limit);
1505
1506 struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
1507 {
1508         struct Scsi_Host *shost = sdev->host;
1509         struct request_queue *q;
1510
1511         q = blk_init_queue(scsi_request_fn, NULL);
1512         if (!q)
1513                 return NULL;
1514
1515         blk_queue_prep_rq(q, scsi_prep_fn);
1516
1517         blk_queue_max_hw_segments(q, shost->sg_tablesize);
1518         blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
1519         blk_queue_max_sectors(q, shost->max_sectors);
1520         blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
1521         blk_queue_segment_boundary(q, shost->dma_boundary);
1522         blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
1523
1524         /*
1525          * ordered tags are superior to flush ordering
1526          */
1527         if (shost->ordered_tag)
1528                 blk_queue_ordered(q, QUEUE_ORDERED_TAG);
1529         else if (shost->ordered_flush) {
1530                 blk_queue_ordered(q, QUEUE_ORDERED_FLUSH);
1531                 q->prepare_flush_fn = scsi_prepare_flush_fn;
1532                 q->end_flush_fn = scsi_end_flush_fn;
1533         }
1534
1535         if (!shost->use_clustering)
1536                 clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
1537         return q;
1538 }
1539
1540 void scsi_free_queue(struct request_queue *q)
1541 {
1542         blk_cleanup_queue(q);
1543 }
1544
1545 /*
1546  * Function:    scsi_block_requests()
1547  *
1548  * Purpose:     Utility function used by low-level drivers to prevent further
1549  *              commands from being queued to the device.
1550  *
1551  * Arguments:   shost       - Host in question
1552  *
1553  * Returns:     Nothing
1554  *
1555  * Lock status: No locks are assumed held.
1556  *
1557  * Notes:       There is no timer nor any other means by which the requests
1558  *              get unblocked other than the low-level driver calling
1559  *              scsi_unblock_requests().
1560  */
1561 void scsi_block_requests(struct Scsi_Host *shost)
1562 {
1563         shost->host_self_blocked = 1;
1564 }
1565 EXPORT_SYMBOL(scsi_block_requests);
1566
1567 /*
1568  * Function:    scsi_unblock_requests()
1569  *
1570  * Purpose:     Utility function used by low-level drivers to allow further
1571  *              commands from being queued to the device.
1572  *
1573  * Arguments:   shost       - Host in question
1574  *
1575  * Returns:     Nothing
1576  *
1577  * Lock status: No locks are assumed held.
1578  *
1579  * Notes:       There is no timer nor any other means by which the requests
1580  *              get unblocked other than the low-level driver calling
1581  *              scsi_unblock_requests().
1582  *
1583  *              This is done as an API function so that changes to the
1584  *              internals of the scsi mid-layer won't require wholesale
1585  *              changes to drivers that use this feature.
1586  */
1587 void scsi_unblock_requests(struct Scsi_Host *shost)
1588 {
1589         shost->host_self_blocked = 0;
1590         scsi_run_host_queues(shost);
1591 }
1592 EXPORT_SYMBOL(scsi_unblock_requests);
1593
1594 int __init scsi_init_queue(void)
1595 {
1596         int i;
1597
1598         for (i = 0; i < SG_MEMPOOL_NR; i++) {
1599                 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1600                 int size = sgp->size * sizeof(struct scatterlist);
1601
1602                 sgp->slab = kmem_cache_create(sgp->name, size, 0,
1603                                 SLAB_HWCACHE_ALIGN, NULL, NULL);
1604                 if (!sgp->slab) {
1605                         printk(KERN_ERR "SCSI: can't init sg slab %s\n",
1606                                         sgp->name);
1607                 }
1608
1609                 sgp->pool = mempool_create(SG_MEMPOOL_SIZE,
1610                                 mempool_alloc_slab, mempool_free_slab,
1611                                 sgp->slab);
1612                 if (!sgp->pool) {
1613                         printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
1614                                         sgp->name);
1615                 }
1616         }
1617
1618         return 0;
1619 }
1620
1621 void scsi_exit_queue(void)
1622 {
1623         int i;
1624
1625         for (i = 0; i < SG_MEMPOOL_NR; i++) {
1626                 struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
1627                 mempool_destroy(sgp->pool);
1628                 kmem_cache_destroy(sgp->slab);
1629         }
1630 }
1631 /**
1632  *      scsi_mode_sense - issue a mode sense, falling back from 10 to 
1633  *              six bytes if necessary.
1634  *      @sdev:  SCSI device to be queried
1635  *      @dbd:   set if mode sense will allow block descriptors to be returned
1636  *      @modepage: mode page being requested
1637  *      @buffer: request buffer (may not be smaller than eight bytes)
1638  *      @len:   length of request buffer.
1639  *      @timeout: command timeout
1640  *      @retries: number of retries before failing
1641  *      @data: returns a structure abstracting the mode header data
1642  *      @sense: place to put sense data (or NULL if no sense to be collected).
1643  *              must be SCSI_SENSE_BUFFERSIZE big.
1644  *
1645  *      Returns zero if unsuccessful, or the header offset (either 4
1646  *      or 8 depending on whether a six or ten byte command was
1647  *      issued) if successful.
1648  **/
1649 int
1650 scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
1651                   unsigned char *buffer, int len, int timeout, int retries,
1652                   struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) {
1653         unsigned char cmd[12];
1654         int use_10_for_ms;
1655         int header_length;
1656         int result;
1657         struct scsi_sense_hdr my_sshdr;
1658
1659         memset(data, 0, sizeof(*data));
1660         memset(&cmd[0], 0, 12);
1661         cmd[1] = dbd & 0x18;    /* allows DBD and LLBA bits */
1662         cmd[2] = modepage;
1663
1664         /* caller might not be interested in sense, but we need it */
1665         if (!sshdr)
1666                 sshdr = &my_sshdr;
1667
1668  retry:
1669         use_10_for_ms = sdev->use_10_for_ms;
1670
1671         if (use_10_for_ms) {
1672                 if (len < 8)
1673                         len = 8;
1674
1675                 cmd[0] = MODE_SENSE_10;
1676                 cmd[8] = len;
1677                 header_length = 8;
1678         } else {
1679                 if (len < 4)
1680                         len = 4;
1681
1682                 cmd[0] = MODE_SENSE;
1683                 cmd[4] = len;
1684                 header_length = 4;
1685         }
1686
1687         memset(buffer, 0, len);
1688
1689         result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
1690                                   sshdr, timeout, retries);
1691
1692         /* This code looks awful: what it's doing is making sure an
1693          * ILLEGAL REQUEST sense return identifies the actual command
1694          * byte as the problem.  MODE_SENSE commands can return
1695          * ILLEGAL REQUEST if the code page isn't supported */
1696
1697         if (use_10_for_ms && !scsi_status_is_good(result) &&
1698             (driver_byte(result) & DRIVER_SENSE)) {
1699                 if (scsi_sense_valid(sshdr)) {
1700                         if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
1701                             (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
1702                                 /* 
1703                                  * Invalid command operation code
1704                                  */
1705                                 sdev->use_10_for_ms = 0;
1706                                 goto retry;
1707                         }
1708                 }
1709         }
1710
1711         if(scsi_status_is_good(result)) {
1712                 data->header_length = header_length;
1713                 if(use_10_for_ms) {
1714                         data->length = buffer[0]*256 + buffer[1] + 2;
1715                         data->medium_type = buffer[2];
1716                         data->device_specific = buffer[3];
1717                         data->longlba = buffer[4] & 0x01;
1718                         data->block_descriptor_length = buffer[6]*256
1719                                 + buffer[7];
1720                 } else {
1721                         data->length = buffer[0] + 1;
1722                         data->medium_type = buffer[1];
1723                         data->device_specific = buffer[2];
1724                         data->block_descriptor_length = buffer[3];
1725                 }
1726         }
1727
1728         return result;
1729 }
1730 EXPORT_SYMBOL(scsi_mode_sense);
1731
1732 int
1733 scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
1734 {
1735         char cmd[] = {
1736                 TEST_UNIT_READY, 0, 0, 0, 0, 0,
1737         };
1738         struct scsi_sense_hdr sshdr;
1739         int result;
1740         
1741         result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, &sshdr,
1742                                   timeout, retries);
1743
1744         if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) {
1745
1746                 if ((scsi_sense_valid(&sshdr)) &&
1747                     ((sshdr.sense_key == UNIT_ATTENTION) ||
1748                      (sshdr.sense_key == NOT_READY))) {
1749                         sdev->changed = 1;
1750                         result = 0;
1751                 }
1752         }
1753         return result;
1754 }
1755 EXPORT_SYMBOL(scsi_test_unit_ready);
1756
1757 /**
1758  *      scsi_device_set_state - Take the given device through the device
1759  *              state model.
1760  *      @sdev:  scsi device to change the state of.
1761  *      @state: state to change to.
1762  *
1763  *      Returns zero if unsuccessful or an error if the requested 
1764  *      transition is illegal.
1765  **/
1766 int
1767 scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
1768 {
1769         enum scsi_device_state oldstate = sdev->sdev_state;
1770
1771         if (state == oldstate)
1772                 return 0;
1773
1774         switch (state) {
1775         case SDEV_CREATED:
1776                 /* There are no legal states that come back to
1777                  * created.  This is the manually initialised start
1778                  * state */
1779                 goto illegal;
1780                         
1781         case SDEV_RUNNING:
1782                 switch (oldstate) {
1783                 case SDEV_CREATED:
1784                 case SDEV_OFFLINE:
1785                 case SDEV_QUIESCE:
1786                 case SDEV_BLOCK:
1787                         break;
1788                 default:
1789                         goto illegal;
1790                 }
1791                 break;
1792
1793         case SDEV_QUIESCE:
1794                 switch (oldstate) {
1795                 case SDEV_RUNNING:
1796                 case SDEV_OFFLINE:
1797                         break;
1798                 default:
1799                         goto illegal;
1800                 }
1801                 break;
1802
1803         case SDEV_OFFLINE:
1804                 switch (oldstate) {
1805                 case SDEV_CREATED:
1806                 case SDEV_RUNNING:
1807                 case SDEV_QUIESCE:
1808                 case SDEV_BLOCK:
1809                         break;
1810                 default:
1811                         goto illegal;
1812                 }
1813                 break;
1814
1815         case SDEV_BLOCK:
1816                 switch (oldstate) {
1817                 case SDEV_CREATED:
1818                 case SDEV_RUNNING:
1819                         break;
1820                 default:
1821                         goto illegal;
1822                 }
1823                 break;
1824
1825         case SDEV_CANCEL:
1826                 switch (oldstate) {
1827                 case SDEV_CREATED:
1828                 case SDEV_RUNNING:
1829                 case SDEV_OFFLINE:
1830                 case SDEV_BLOCK:
1831                         break;
1832                 default:
1833                         goto illegal;
1834                 }
1835                 break;
1836
1837         case SDEV_DEL:
1838                 switch (oldstate) {
1839                 case SDEV_CANCEL:
1840                         break;
1841                 default:
1842                         goto illegal;
1843                 }
1844                 break;
1845
1846         }
1847         sdev->sdev_state = state;
1848         return 0;
1849
1850  illegal:
1851         SCSI_LOG_ERROR_RECOVERY(1, 
1852                                 sdev_printk(KERN_ERR, sdev,
1853                                             "Illegal state transition %s->%s\n",
1854                                             scsi_device_state_name(oldstate),
1855                                             scsi_device_state_name(state))
1856                                 );
1857         return -EINVAL;
1858 }
1859 EXPORT_SYMBOL(scsi_device_set_state);
1860
1861 /**
1862  *      scsi_device_quiesce - Block user issued commands.
1863  *      @sdev:  scsi device to quiesce.
1864  *
1865  *      This works by trying to transition to the SDEV_QUIESCE state
1866  *      (which must be a legal transition).  When the device is in this
1867  *      state, only special requests will be accepted, all others will
1868  *      be deferred.  Since special requests may also be requeued requests,
1869  *      a successful return doesn't guarantee the device will be 
1870  *      totally quiescent.
1871  *
1872  *      Must be called with user context, may sleep.
1873  *
1874  *      Returns zero if unsuccessful or an error if not.
1875  **/
1876 int
1877 scsi_device_quiesce(struct scsi_device *sdev)
1878 {
1879         int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
1880         if (err)
1881                 return err;
1882
1883         scsi_run_queue(sdev->request_queue);
1884         while (sdev->device_busy) {
1885                 msleep_interruptible(200);
1886                 scsi_run_queue(sdev->request_queue);
1887         }
1888         return 0;
1889 }
1890 EXPORT_SYMBOL(scsi_device_quiesce);
1891
1892 /**
1893  *      scsi_device_resume - Restart user issued commands to a quiesced device.
1894  *      @sdev:  scsi device to resume.
1895  *
1896  *      Moves the device from quiesced back to running and restarts the
1897  *      queues.
1898  *
1899  *      Must be called with user context, may sleep.
1900  **/
1901 void
1902 scsi_device_resume(struct scsi_device *sdev)
1903 {
1904         if(scsi_device_set_state(sdev, SDEV_RUNNING))
1905                 return;
1906         scsi_run_queue(sdev->request_queue);
1907 }
1908 EXPORT_SYMBOL(scsi_device_resume);
1909
1910 static void
1911 device_quiesce_fn(struct scsi_device *sdev, void *data)
1912 {
1913         scsi_device_quiesce(sdev);
1914 }
1915
1916 void
1917 scsi_target_quiesce(struct scsi_target *starget)
1918 {
1919         starget_for_each_device(starget, NULL, device_quiesce_fn);
1920 }
1921 EXPORT_SYMBOL(scsi_target_quiesce);
1922
1923 static void
1924 device_resume_fn(struct scsi_device *sdev, void *data)
1925 {
1926         scsi_device_resume(sdev);
1927 }
1928
1929 void
1930 scsi_target_resume(struct scsi_target *starget)
1931 {
1932         starget_for_each_device(starget, NULL, device_resume_fn);
1933 }
1934 EXPORT_SYMBOL(scsi_target_resume);
1935
1936 /**
1937  * scsi_internal_device_block - internal function to put a device
1938  *                              temporarily into the SDEV_BLOCK state
1939  * @sdev:       device to block
1940  *
1941  * Block request made by scsi lld's to temporarily stop all
1942  * scsi commands on the specified device.  Called from interrupt
1943  * or normal process context.
1944  *
1945  * Returns zero if successful or error if not
1946  *
1947  * Notes:       
1948  *      This routine transitions the device to the SDEV_BLOCK state
1949  *      (which must be a legal transition).  When the device is in this
1950  *      state, all commands are deferred until the scsi lld reenables
1951  *      the device with scsi_device_unblock or device_block_tmo fires.
1952  *      This routine assumes the host_lock is held on entry.
1953  **/
1954 int
1955 scsi_internal_device_block(struct scsi_device *sdev)
1956 {
1957         request_queue_t *q = sdev->request_queue;
1958         unsigned long flags;
1959         int err = 0;
1960
1961         err = scsi_device_set_state(sdev, SDEV_BLOCK);
1962         if (err)
1963                 return err;
1964
1965         /* 
1966          * The device has transitioned to SDEV_BLOCK.  Stop the
1967          * block layer from calling the midlayer with this device's
1968          * request queue. 
1969          */
1970         spin_lock_irqsave(q->queue_lock, flags);
1971         blk_stop_queue(q);
1972         spin_unlock_irqrestore(q->queue_lock, flags);
1973
1974         return 0;
1975 }
1976 EXPORT_SYMBOL_GPL(scsi_internal_device_block);
1977  
1978 /**
1979  * scsi_internal_device_unblock - resume a device after a block request
1980  * @sdev:       device to resume
1981  *
1982  * Called by scsi lld's or the midlayer to restart the device queue
1983  * for the previously suspended scsi device.  Called from interrupt or
1984  * normal process context.
1985  *
1986  * Returns zero if successful or error if not.
1987  *
1988  * Notes:       
1989  *      This routine transitions the device to the SDEV_RUNNING state
1990  *      (which must be a legal transition) allowing the midlayer to
1991  *      goose the queue for this device.  This routine assumes the 
1992  *      host_lock is held upon entry.
1993  **/
1994 int
1995 scsi_internal_device_unblock(struct scsi_device *sdev)
1996 {
1997         request_queue_t *q = sdev->request_queue; 
1998         int err;
1999         unsigned long flags;
2000         
2001         /* 
2002          * Try to transition the scsi device to SDEV_RUNNING
2003          * and goose the device queue if successful.  
2004          */
2005         err = scsi_device_set_state(sdev, SDEV_RUNNING);
2006         if (err)
2007                 return err;
2008
2009         spin_lock_irqsave(q->queue_lock, flags);
2010         blk_start_queue(q);
2011         spin_unlock_irqrestore(q->queue_lock, flags);
2012
2013         return 0;
2014 }
2015 EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
2016
2017 static void
2018 device_block(struct scsi_device *sdev, void *data)
2019 {
2020         scsi_internal_device_block(sdev);
2021 }
2022
2023 static int
2024 target_block(struct device *dev, void *data)
2025 {
2026         if (scsi_is_target_device(dev))
2027                 starget_for_each_device(to_scsi_target(dev), NULL,
2028                                         device_block);
2029         return 0;
2030 }
2031
2032 void
2033 scsi_target_block(struct device *dev)
2034 {
2035         if (scsi_is_target_device(dev))
2036                 starget_for_each_device(to_scsi_target(dev), NULL,
2037                                         device_block);
2038         else
2039                 device_for_each_child(dev, NULL, target_block);
2040 }
2041 EXPORT_SYMBOL_GPL(scsi_target_block);
2042
2043 static void
2044 device_unblock(struct scsi_device *sdev, void *data)
2045 {
2046         scsi_internal_device_unblock(sdev);
2047 }
2048
2049 static int
2050 target_unblock(struct device *dev, void *data)
2051 {
2052         if (scsi_is_target_device(dev))
2053                 starget_for_each_device(to_scsi_target(dev), NULL,
2054                                         device_unblock);
2055         return 0;
2056 }
2057
2058 void
2059 scsi_target_unblock(struct device *dev)
2060 {
2061         if (scsi_is_target_device(dev))
2062                 starget_for_each_device(to_scsi_target(dev), NULL,
2063                                         device_unblock);
2064         else
2065                 device_for_each_child(dev, NULL, target_unblock);
2066 }
2067 EXPORT_SYMBOL_GPL(scsi_target_unblock);