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