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