scsi_lib.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:34k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  * The fundamental purpose of this file is to contain a library of utility
  11.  * routines that can be used by low-level drivers.   Ultimately the idea
  12.  * is that there should be a sufficiently rich number of functions that it
  13.  * would be possible for a driver author to fashion a queueing function for
  14.  * a low-level driver if they wished.   Note however that this file also
  15.  * contains the "default" versions of these functions, as we don't want to
  16.  * go through and retrofit queueing functions into all 30 some-odd drivers.
  17.  */
  18. #define __NO_VERSION__
  19. #include <linux/module.h>
  20. #include <linux/sched.h>
  21. #include <linux/timer.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/ioport.h>
  25. #include <linux/kernel.h>
  26. #include <linux/stat.h>
  27. #include <linux/blk.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/completion.h>
  32. #define __KERNEL_SYSCALLS__
  33. #include <linux/unistd.h>
  34. #include <asm/system.h>
  35. #include <asm/irq.h>
  36. #include <asm/dma.h>
  37. #include "scsi.h"
  38. #include "hosts.h"
  39. #include "constants.h"
  40. #include <scsi/scsi_ioctl.h>
  41. /*
  42.  * This entire source file deals with the new queueing code.
  43.  */
  44. /*
  45.  * Function: __scsi_insert_special()
  46.  *
  47.  * Purpose: worker for scsi_insert_special_*()
  48.  *
  49.  * Arguments: q - request queue where request should be inserted
  50.  * rq - request to be inserted
  51.  *  data - private data
  52.  * at_head - insert request at head or tail of queue
  53.  *
  54.  * Lock status: Assumed that io_request_lock is not held upon entry.
  55.  *
  56.  * Returns: Nothing
  57.  */
  58. static void __scsi_insert_special(request_queue_t *q, struct request *rq,
  59.   void *data, int at_head)
  60. {
  61. unsigned long flags;
  62. ASSERT_LOCK(&io_request_lock, 0);
  63. rq->cmd = SPECIAL;
  64. rq->special = data;
  65. rq->q = NULL;
  66. rq->nr_segments = 0;
  67. rq->elevator_sequence = 0;
  68. /*
  69.  * We have the option of inserting the head or the tail of the queue.
  70.  * Typically we use the tail for new ioctls and so forth.  We use the
  71.  * head of the queue for things like a QUEUE_FULL message from a
  72.  * device, or a host that is unable to accept a particular command.
  73.  */
  74. spin_lock_irqsave(&io_request_lock, flags);
  75. if (at_head)
  76. list_add(&rq->queue, &q->queue_head);
  77. else
  78. list_add_tail(&rq->queue, &q->queue_head);
  79. q->request_fn(q);
  80. spin_unlock_irqrestore(&io_request_lock, flags);
  81. }
  82. /*
  83.  * Function:    scsi_insert_special_cmd()
  84.  *
  85.  * Purpose:     Insert pre-formed command into request queue.
  86.  *
  87.  * Arguments:   SCpnt   - command that is ready to be queued.
  88.  *              at_head - boolean.  True if we should insert at head
  89.  *                        of queue, false if we should insert at tail.
  90.  *
  91.  * Lock status: Assumed that lock is not held upon entry.
  92.  *
  93.  * Returns:     Nothing
  94.  *
  95.  * Notes:       This function is called from character device and from
  96.  *              ioctl types of functions where the caller knows exactly
  97.  *              what SCSI command needs to be issued.   The idea is that
  98.  *              we merely inject the command into the queue (at the head
  99.  *              for now), and then call the queue request function to actually
  100.  *              process it.
  101.  */
  102. int scsi_insert_special_cmd(Scsi_Cmnd * SCpnt, int at_head)
  103. {
  104. request_queue_t *q = &SCpnt->device->request_queue;
  105. __scsi_insert_special(q, &SCpnt->request, SCpnt, at_head);
  106. return 0;
  107. }
  108. /*
  109.  * Function:    scsi_insert_special_req()
  110.  *
  111.  * Purpose:     Insert pre-formed request into request queue.
  112.  *
  113.  * Arguments:   SRpnt   - request that is ready to be queued.
  114.  *              at_head - boolean.  True if we should insert at head
  115.  *                        of queue, false if we should insert at tail.
  116.  *
  117.  * Lock status: Assumed that lock is not held upon entry.
  118.  *
  119.  * Returns:     Nothing
  120.  *
  121.  * Notes:       This function is called from character device and from
  122.  *              ioctl types of functions where the caller knows exactly
  123.  *              what SCSI command needs to be issued.   The idea is that
  124.  *              we merely inject the command into the queue (at the head
  125.  *              for now), and then call the queue request function to actually
  126.  *              process it.
  127.  */
  128. int scsi_insert_special_req(Scsi_Request * SRpnt, int at_head)
  129. {
  130. request_queue_t *q = &SRpnt->sr_device->request_queue;
  131. __scsi_insert_special(q, &SRpnt->sr_request, SRpnt, at_head);
  132. return 0;
  133. }
  134. /*
  135.  * Function:    scsi_init_cmd_errh()
  136.  *
  137.  * Purpose:     Initialize SCpnt fields related to error handling.
  138.  *
  139.  * Arguments:   SCpnt   - command that is ready to be queued.
  140.  *
  141.  * Returns:     Nothing
  142.  *
  143.  * Notes:       This function has the job of initializing a number of
  144.  *              fields related to error handling.   Typically this will
  145.  *              be called once for each command, as required.
  146.  */
  147. int scsi_init_cmd_errh(Scsi_Cmnd * SCpnt)
  148. {
  149. ASSERT_LOCK(&io_request_lock, 0);
  150. SCpnt->owner = SCSI_OWNER_MIDLEVEL;
  151. SCpnt->reset_chain = NULL;
  152. SCpnt->serial_number = 0;
  153. SCpnt->serial_number_at_timeout = 0;
  154. SCpnt->flags = 0;
  155. SCpnt->retries = 0;
  156. SCpnt->abort_reason = 0;
  157. memset((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
  158. if (SCpnt->cmd_len == 0)
  159. SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
  160. /*
  161.  * We need saved copies of a number of fields - this is because
  162.  * error handling may need to overwrite these with different values
  163.  * to run different commands, and once error handling is complete,
  164.  * we will need to restore these values prior to running the actual
  165.  * command.
  166.  */
  167. SCpnt->old_use_sg = SCpnt->use_sg;
  168. SCpnt->old_cmd_len = SCpnt->cmd_len;
  169. SCpnt->sc_old_data_direction = SCpnt->sc_data_direction;
  170. SCpnt->old_underflow = SCpnt->underflow;
  171. memcpy((void *) SCpnt->data_cmnd,
  172.        (const void *) SCpnt->cmnd, sizeof(SCpnt->cmnd));
  173. SCpnt->buffer = SCpnt->request_buffer;
  174. SCpnt->bufflen = SCpnt->request_bufflen;
  175. SCpnt->reset_chain = NULL;
  176. SCpnt->internal_timeout = NORMAL_TIMEOUT;
  177. SCpnt->abort_reason = 0;
  178. return 1;
  179. }
  180. /*
  181.  * Function:    scsi_queue_next_request()
  182.  *
  183.  * Purpose:     Handle post-processing of completed commands.
  184.  *
  185.  * Arguments:   SCpnt   - command that may need to be requeued.
  186.  *
  187.  * Returns:     Nothing
  188.  *
  189.  * Notes:       After command completion, there may be blocks left
  190.  *              over which weren't finished by the previous command
  191.  *              this can be for a number of reasons - the main one is
  192.  *              that a medium error occurred, and the sectors after
  193.  *              the bad block need to be re-read.
  194.  *
  195.  *              If SCpnt is NULL, it means that the previous command
  196.  *              was completely finished, and we should simply start
  197.  *              a new command, if possible.
  198.  *
  199.  * This is where a lot of special case code has begun to
  200.  * accumulate.  It doesn't really affect readability or
  201.  * anything, but it might be considered architecturally
  202.  * inelegant.  If more of these special cases start to
  203.  * accumulate, I am thinking along the lines of implementing
  204.  * an atexit() like technology that gets run when commands
  205.  * complete.  I am not convinced that it is worth the
  206.  * added overhead, however.  Right now as things stand,
  207.  * there are simple conditional checks, and most hosts
  208.  * would skip past.
  209.  *
  210.  * Another possible solution would be to tailor different
  211.  * handler functions, sort of like what we did in scsi_merge.c.
  212.  * This is probably a better solution, but the number of different
  213.  * permutations grows as 2**N, and if too many more special cases
  214.  * get added, we start to get screwed.
  215.  */
  216. void scsi_queue_next_request(request_queue_t * q, Scsi_Cmnd * SCpnt)
  217. {
  218. int all_clear;
  219. unsigned long flags;
  220. Scsi_Device *SDpnt;
  221. struct Scsi_Host *SHpnt;
  222. ASSERT_LOCK(&io_request_lock, 0);
  223. spin_lock_irqsave(&io_request_lock, flags);
  224. if (SCpnt != NULL) {
  225. /*
  226.  * For some reason, we are not done with this request.
  227.  * This happens for I/O errors in the middle of the request,
  228.  * in which case we need to request the blocks that come after
  229.  * the bad sector.
  230.  */
  231. SCpnt->request.special = (void *) SCpnt;
  232. list_add(&SCpnt->request.queue, &q->queue_head);
  233. }
  234. /*
  235.  * Just hit the requeue function for the queue.
  236.  */
  237. q->request_fn(q);
  238. SDpnt = (Scsi_Device *) q->queuedata;
  239. SHpnt = SDpnt->host;
  240. /*
  241.  * If this is a single-lun device, and we are currently finished
  242.  * with this device, then see if we need to get another device
  243.  * started.  FIXME(eric) - if this function gets too cluttered
  244.  * with special case code, then spin off separate versions and
  245.  * use function pointers to pick the right one.
  246.  */
  247. if (SDpnt->single_lun
  248.     && list_empty(&q->queue_head)
  249.     && SDpnt->device_busy == 0) {
  250. request_queue_t *q;
  251. for (SDpnt = SHpnt->host_queue;
  252.      SDpnt;
  253.      SDpnt = SDpnt->next) {
  254. if (((SHpnt->can_queue > 0)
  255.      && (SHpnt->host_busy >= SHpnt->can_queue))
  256.     || (SHpnt->host_blocked)
  257.     || (SHpnt->host_self_blocked)
  258.     || (SDpnt->device_blocked)) {
  259. break;
  260. }
  261. q = &SDpnt->request_queue;
  262. q->request_fn(q);
  263. }
  264. }
  265. /*
  266.  * Now see whether there are other devices on the bus which
  267.  * might be starved.  If so, hit the request function.  If we
  268.  * don't find any, then it is safe to reset the flag.  If we
  269.  * find any device that it is starved, it isn't safe to reset the
  270.  * flag as the queue function releases the lock and thus some
  271.  * other device might have become starved along the way.
  272.  */
  273. all_clear = 1;
  274. if (SHpnt->some_device_starved) {
  275. for (SDpnt = SHpnt->host_queue; SDpnt; SDpnt = SDpnt->next) {
  276. request_queue_t *q;
  277. if ((SHpnt->can_queue > 0 && (SHpnt->host_busy >= SHpnt->can_queue))
  278.     || (SHpnt->host_blocked) 
  279.     || (SHpnt->host_self_blocked)) {
  280. break;
  281. }
  282. if (SDpnt->device_blocked || !SDpnt->starved) {
  283. continue;
  284. }
  285. q = &SDpnt->request_queue;
  286. q->request_fn(q);
  287. all_clear = 0;
  288. }
  289. if (SDpnt == NULL && all_clear) {
  290. SHpnt->some_device_starved = 0;
  291. }
  292. }
  293. spin_unlock_irqrestore(&io_request_lock, flags);
  294. }
  295. /*
  296.  * Function:    scsi_end_request()
  297.  *
  298.  * Purpose:     Post-processing of completed commands called from interrupt
  299.  *              handler or a bottom-half handler.
  300.  *
  301.  * Arguments:   SCpnt    - command that is complete.
  302.  *              uptodate - 1 if I/O indicates success, 0 for I/O error.
  303.  *              sectors  - number of sectors we want to mark.
  304.  * requeue  - indicates whether we should requeue leftovers.
  305.  * frequeue - indicates that if we release the command block
  306.  *    that the queue request function should be called.
  307.  *
  308.  * Lock status: Assumed that lock is not held upon entry.
  309.  *
  310.  * Returns:     Nothing
  311.  *
  312.  * Notes:       This is called for block device requests in order to
  313.  *              mark some number of sectors as complete.
  314.  * 
  315.  * We are guaranteeing that the request queue will be goosed
  316.  * at some point during this call.
  317.  */
  318. static Scsi_Cmnd *__scsi_end_request(Scsi_Cmnd * SCpnt, 
  319.      int uptodate, 
  320.      int sectors,
  321.      int requeue,
  322.      int frequeue)
  323. {
  324. request_queue_t *q = &SCpnt->device->request_queue;
  325. struct request *req;
  326. struct buffer_head *bh;
  327. unsigned long flags;
  328. int nsect;
  329. ASSERT_LOCK(&io_request_lock, 0);
  330. req = &SCpnt->request;
  331. req->errors = 0;
  332. if (!uptodate) {
  333. printk(" I/O error: dev %s, sector %lun",
  334.        kdevname(req->rq_dev), req->sector);
  335. }
  336. do {
  337. if ((bh = req->bh) != NULL) {
  338. nsect = bh->b_size >> 9;
  339. blk_finished_io(nsect);
  340. req->bh = bh->b_reqnext;
  341. bh->b_reqnext = NULL;
  342. sectors -= nsect;
  343. bh->b_end_io(bh, uptodate);
  344. if ((bh = req->bh) != NULL) {
  345. req->hard_sector += nsect;
  346. req->hard_nr_sectors -= nsect;
  347. req->sector += nsect;
  348. req->nr_sectors -= nsect;
  349. req->current_nr_sectors = bh->b_size >> 9;
  350. req->hard_cur_sectors = req->current_nr_sectors;
  351. if (req->nr_sectors < req->current_nr_sectors) {
  352. req->nr_sectors = req->current_nr_sectors;
  353. printk("scsi_end_request: buffer-list destroyedn");
  354. }
  355. }
  356. }
  357. } while (sectors && bh);
  358. /*
  359.  * If there are blocks left over at the end, set up the command
  360.  * to queue the remainder of them.
  361.  */
  362. if (req->bh) {
  363. /*
  364.  * Bleah.  Leftovers again.  Stick the leftovers in
  365.  * the front of the queue, and goose the queue again.
  366.  */
  367. if (requeue)
  368. scsi_queue_next_request(q, SCpnt);
  369. return SCpnt;
  370. }
  371. /*
  372.  * This request is done.  If there is someone blocked waiting for this
  373.  * request, wake them up.  Typically used to wake up processes trying
  374.  * to swap a page into memory.
  375.  */
  376. if (req->waiting)
  377. complete(req->waiting);
  378. spin_lock_irqsave(&io_request_lock, flags);
  379. req_finished_io(req);
  380. spin_unlock_irqrestore(&io_request_lock, flags);
  381. add_blkdev_randomness(MAJOR(req->rq_dev));
  382. /*
  383.  * This will goose the queue request function at the end, so we don't
  384.  * need to worry about launching another command.
  385.  */
  386. __scsi_release_command(SCpnt);
  387. if (frequeue)
  388. scsi_queue_next_request(q, NULL);
  389. return NULL;
  390. }
  391. /*
  392.  * Function:    scsi_end_request()
  393.  *
  394.  * Purpose:     Post-processing of completed commands called from interrupt
  395.  *              handler or a bottom-half handler.
  396.  *
  397.  * Arguments:   SCpnt    - command that is complete.
  398.  *              uptodate - 1 if I/O indicates success, 0 for I/O error.
  399.  *              sectors  - number of sectors we want to mark.
  400.  *
  401.  * Lock status: Assumed that lock is not held upon entry.
  402.  *
  403.  * Returns:     Nothing
  404.  *
  405.  * Notes:       This is called for block device requests in order to
  406.  *              mark some number of sectors as complete.
  407.  * 
  408.  * We are guaranteeing that the request queue will be goosed
  409.  * at some point during this call.
  410.  */
  411. Scsi_Cmnd *scsi_end_request(Scsi_Cmnd * SCpnt, int uptodate, int sectors)
  412. {
  413. return __scsi_end_request(SCpnt, uptodate, sectors, 1, 1);
  414. }
  415. /*
  416.  * Function:    scsi_release_buffers()
  417.  *
  418.  * Purpose:     Completion processing for block device I/O requests.
  419.  *
  420.  * Arguments:   SCpnt   - command that we are bailing.
  421.  *
  422.  * Lock status: Assumed that no lock is held upon entry.
  423.  *
  424.  * Returns:     Nothing
  425.  *
  426.  * Notes:       In the event that an upper level driver rejects a
  427.  * command, we must release resources allocated during
  428.  * the __init_io() function.  Primarily this would involve
  429.  * the scatter-gather table, and potentially any bounce
  430.  * buffers.
  431.  */
  432. static void scsi_release_buffers(Scsi_Cmnd * SCpnt)
  433. {
  434. ASSERT_LOCK(&io_request_lock, 0);
  435. /*
  436.  * Free up any indirection buffers we allocated for DMA purposes. 
  437.  */
  438. if (SCpnt->use_sg) {
  439. struct scatterlist *sgpnt;
  440. void **bbpnt;
  441. int i;
  442. sgpnt = (struct scatterlist *) SCpnt->request_buffer;
  443. bbpnt = SCpnt->bounce_buffers;
  444. if (bbpnt) {
  445. for (i = 0; i < SCpnt->use_sg; i++) {
  446. if (bbpnt[i])
  447. scsi_free(sgpnt[i].address, sgpnt[i].length);
  448. }
  449. }
  450. scsi_free(SCpnt->request_buffer, SCpnt->sglist_len);
  451. } else {
  452. if (SCpnt->request_buffer != SCpnt->request.buffer) {
  453. scsi_free(SCpnt->request_buffer, SCpnt->request_bufflen);
  454. }
  455. }
  456. /*
  457.  * Zero these out.  They now point to freed memory, and it is
  458.  * dangerous to hang onto the pointers.
  459.  */
  460. SCpnt->buffer  = NULL;
  461. SCpnt->bufflen = 0;
  462. SCpnt->request_buffer = NULL;
  463. SCpnt->request_bufflen = 0;
  464. }
  465. /*
  466.  * Function:    scsi_io_completion()
  467.  *
  468.  * Purpose:     Completion processing for block device I/O requests.
  469.  *
  470.  * Arguments:   SCpnt   - command that is finished.
  471.  *
  472.  * Lock status: Assumed that no lock is held upon entry.
  473.  *
  474.  * Returns:     Nothing
  475.  *
  476.  * Notes:       This function is matched in terms of capabilities to
  477.  *              the function that created the scatter-gather list.
  478.  *              In other words, if there are no bounce buffers
  479.  *              (the normal case for most drivers), we don't need
  480.  *              the logic to deal with cleaning up afterwards.
  481.  */
  482. void scsi_io_completion(Scsi_Cmnd * SCpnt, int good_sectors,
  483. int block_sectors)
  484. {
  485. int result = SCpnt->result;
  486. int this_count = SCpnt->bufflen >> 9;
  487. request_queue_t *q = &SCpnt->device->request_queue;
  488. struct request *req = &SCpnt->request;
  489. /*
  490.  * We must do one of several things here:
  491.  *
  492.  * Call scsi_end_request.  This will finish off the specified
  493.  * number of sectors.  If we are done, the command block will
  494.  * be released, and the queue function will be goosed.  If we
  495.  * are not done, then scsi_end_request will directly goose
  496.  * the queue.
  497.  *
  498.  * We can just use scsi_queue_next_request() here.  This
  499.  * would be used if we just wanted to retry, for example.
  500.  *
  501.  */
  502. ASSERT_LOCK(&io_request_lock, 0);
  503. /*
  504.  * Free up any indirection buffers we allocated for DMA purposes. 
  505.  * For the case of a READ, we need to copy the data out of the
  506.  * bounce buffer and into the real buffer.
  507.  */
  508. if (SCpnt->use_sg) {
  509. struct scatterlist *sgpnt;
  510. void **bbpnt;
  511. int i;
  512. sgpnt = (struct scatterlist *) SCpnt->buffer;
  513. bbpnt = SCpnt->bounce_buffers;
  514. if (bbpnt) {
  515. for (i = 0; i < SCpnt->use_sg; i++) {
  516. if (bbpnt[i]) {
  517. if (req->cmd == READ) {
  518. memcpy(bbpnt[i],
  519.        sgpnt[i].address,
  520.        sgpnt[i].length);
  521. }
  522. scsi_free(sgpnt[i].address, sgpnt[i].length);
  523. }
  524. }
  525. }
  526. scsi_free(SCpnt->buffer, SCpnt->sglist_len);
  527. } else {
  528. if (SCpnt->buffer != req->buffer) {
  529. if (PageHighMem(req->bh->b_page))
  530. BUG();
  531. if (req->cmd == READ)
  532. memcpy(req->buffer, SCpnt->buffer, SCpnt->bufflen);
  533. scsi_free(SCpnt->buffer, SCpnt->bufflen);
  534. }
  535. }
  536. /*
  537.  * Zero these out.  They now point to freed memory, and it is
  538.  * dangerous to hang onto the pointers.
  539.  */
  540. SCpnt->buffer  = NULL;
  541. SCpnt->bufflen = 0;
  542. SCpnt->request_buffer = NULL;
  543. SCpnt->request_bufflen = 0;
  544. /*
  545.  * Next deal with any sectors which we were able to correctly
  546.  * handle.
  547.  */
  548. if (good_sectors > 0) {
  549. SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, %d sectors done.n",
  550.       SCpnt->request.nr_sectors,
  551.       good_sectors));
  552. SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %dn ", SCpnt->use_sg));
  553. req->errors = 0;
  554. /*
  555.  * If multiple sectors are requested in one buffer, then
  556.  * they will have been finished off by the first command.
  557.  * If not, then we have a multi-buffer command.
  558.  *
  559.  * If block_sectors != 0, it means we had a medium error
  560.  * of some sort, and that we want to mark some number of
  561.  * sectors as not uptodate.  Thus we want to inhibit
  562.  * requeueing right here - we will requeue down below
  563.  * when we handle the bad sectors.
  564.  */
  565. SCpnt = __scsi_end_request(SCpnt, 
  566.    1, 
  567.    good_sectors,
  568.    result == 0,
  569.    1);
  570. /*
  571.  * If the command completed without error, then either finish off the
  572.  * rest of the command, or start a new one.
  573.  */
  574. if (result == 0 || SCpnt == NULL ) {
  575. return;
  576. }
  577. }
  578. /*
  579.  * Now, if we were good little boys and girls, Santa left us a request
  580.  * sense buffer.  We can extract information from this, so we
  581.  * can choose a block to remap, etc.
  582.  */
  583. if (driver_byte(result) != 0) {
  584. if (suggestion(result) == SUGGEST_REMAP) {
  585. #ifdef REMAP
  586. /*
  587.  * Not yet implemented.  A read will fail after being remapped,
  588.  * a write will call the strategy routine again.
  589.  */
  590. if (SCpnt->device->remap) {
  591. result = 0;
  592. }
  593. #endif
  594. }
  595. if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
  596. /*
  597.  * If the device is in the process of becoming ready,
  598.  * retry.
  599.  */
  600. if (SCpnt->sense_buffer[12] == 0x04 &&
  601.     SCpnt->sense_buffer[13] == 0x01) {
  602. scsi_queue_next_request(q, SCpnt);
  603. return;
  604. }
  605. if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
  606. if (SCpnt->device->removable) {
  607. /* detected disc change.  set a bit 
  608.  * and quietly refuse further access.
  609.    */
  610. SCpnt->device->changed = 1;
  611. SCpnt = scsi_end_request(SCpnt, 0, this_count);
  612. return;
  613. } else {
  614. /*
  615.   * Must have been a power glitch, or a
  616.   * bus reset.  Could not have been a
  617.   * media change, so we just retry the
  618.   * request and see what happens.  
  619.   */
  620. scsi_queue_next_request(q, SCpnt);
  621. return;
  622. }
  623. }
  624. }
  625. /* If we had an ILLEGAL REQUEST returned, then we may have
  626.  * performed an unsupported command.  The only thing this should be
  627.  * would be a ten byte read where only a six byte read was supported.
  628.  * Also, on a system where READ CAPACITY failed, we have have read
  629.  * past the end of the disk.
  630.  */
  631. switch (SCpnt->sense_buffer[2]) {
  632. case ILLEGAL_REQUEST:
  633. if (SCpnt->device->ten && SCSI_RETRY_10(SCpnt->cmnd[0])) {
  634. SCpnt->device->ten = 0;
  635. /*
  636.  * This will cause a retry with a 6-byte
  637.  * command.
  638.  */
  639. scsi_queue_next_request(q, SCpnt);
  640. result = 0;
  641. } else {
  642. SCpnt = scsi_end_request(SCpnt, 0, this_count);
  643. return;
  644. }
  645. break;
  646. case NOT_READY:
  647. printk(KERN_INFO "Device %s not ready.n",
  648.        kdevname(SCpnt->request.rq_dev));
  649. SCpnt = scsi_end_request(SCpnt, 0, this_count);
  650. return;
  651. break;
  652. case MEDIUM_ERROR:
  653. case VOLUME_OVERFLOW:
  654. printk("scsi%d: ERROR on channel %d, id %d, lun %d, CDB: ",
  655.        SCpnt->host->host_no, (int) SCpnt->channel,
  656.        (int) SCpnt->target, (int) SCpnt->lun);
  657. print_command(SCpnt->cmnd);
  658. print_sense("sd", SCpnt);
  659. SCpnt = scsi_end_request(SCpnt, 0, block_sectors);
  660. return;
  661. default:
  662. break;
  663. }
  664. } /* driver byte != 0 */
  665. if (host_byte(result) == DID_RESET) {
  666. /*
  667.  * Third party bus reset or reset for error
  668.  * recovery reasons.  Just retry the request
  669.  * and see what happens.  
  670.  */
  671. scsi_queue_next_request(q, SCpnt);
  672. return;
  673. }
  674. if (result) {
  675. struct Scsi_Device_Template *STpnt;
  676. STpnt = scsi_get_request_dev(&SCpnt->request);
  677. printk("SCSI %s error : host %d channel %d id %d lun %d return code = %xn",
  678.        (STpnt ? STpnt->name : "device"),
  679.        SCpnt->device->host->host_no,
  680.        SCpnt->device->channel,
  681.        SCpnt->device->id,
  682.        SCpnt->device->lun, result);
  683. if (driver_byte(result) & DRIVER_SENSE)
  684. print_sense("sd", SCpnt);
  685. /*
  686.  * Mark a single buffer as not uptodate.  Queue the remainder.
  687.  * We sometimes get this cruft in the event that a medium error
  688.  * isn't properly reported.
  689.  */
  690. SCpnt = scsi_end_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
  691. return;
  692. }
  693. }
  694. /*
  695.  * Function:    scsi_get_request_dev()
  696.  *
  697.  * Purpose:     Find the upper-level driver that is responsible for this
  698.  *              request
  699.  *
  700.  * Arguments:   request   - I/O request we are preparing to queue.
  701.  *
  702.  * Lock status: No locks assumed to be held, but as it happens the
  703.  *              io_request_lock is held when this is called.
  704.  *
  705.  * Returns:     Nothing
  706.  *
  707.  * Notes:       The requests in the request queue may have originated
  708.  *              from any block device driver.  We need to find out which
  709.  *              one so that we can later form the appropriate command.
  710.  */
  711. struct Scsi_Device_Template *scsi_get_request_dev(struct request *req)
  712. {
  713. struct Scsi_Device_Template *spnt;
  714. kdev_t dev = req->rq_dev;
  715. int major = MAJOR(dev);
  716. ASSERT_LOCK(&io_request_lock, 1);
  717. for (spnt = scsi_devicelist; spnt; spnt = spnt->next) {
  718. /*
  719.  * Search for a block device driver that supports this
  720.  * major.
  721.  */
  722. if (spnt->blk && spnt->major == major) {
  723. return spnt;
  724. }
  725. /*
  726.  * I am still not entirely satisfied with this solution,
  727.  * but it is good enough for now.  Disks have a number of
  728.  * major numbers associated with them, the primary
  729.  * 8, which we test above, and a secondary range of 7
  730.  * different consecutive major numbers.   If this ever
  731.  * becomes insufficient, then we could add another function
  732.  * to the structure, and generalize this completely.
  733.  */
  734. if( spnt->min_major != 0 
  735.     && spnt->max_major != 0
  736.     && major >= spnt->min_major
  737.     && major <= spnt->max_major )
  738. {
  739. return spnt;
  740. }
  741. }
  742. return NULL;
  743. }
  744. /*
  745.  * Function:    scsi_request_fn()
  746.  *
  747.  * Purpose:     Generic version of request function for SCSI hosts.
  748.  *
  749.  * Arguments:   q       - Pointer to actual queue.
  750.  *
  751.  * Returns:     Nothing
  752.  *
  753.  * Lock status: IO request lock assumed to be held when called.
  754.  *
  755.  * Notes:       The theory is that this function is something which individual
  756.  *              drivers could also supply if they wished to.   The problem
  757.  *              is that we have 30 some odd low-level drivers in the kernel
  758.  *              tree already, and it would be most difficult to retrofit
  759.  *              this crap into all of them.   Thus this function has the job
  760.  *              of acting as a generic queue manager for all of those existing
  761.  *              drivers.
  762.  */
  763. void scsi_request_fn(request_queue_t * q)
  764. {
  765. struct request *req;
  766. Scsi_Cmnd *SCpnt;
  767. Scsi_Request *SRpnt;
  768. Scsi_Device *SDpnt;
  769. struct Scsi_Host *SHpnt;
  770. struct Scsi_Device_Template *STpnt;
  771. ASSERT_LOCK(&io_request_lock, 1);
  772. SDpnt = (Scsi_Device *) q->queuedata;
  773. if (!SDpnt) {
  774. panic("Missing device");
  775. }
  776. SHpnt = SDpnt->host;
  777. /*
  778.  * To start with, we keep looping until the queue is empty, or until
  779.  * the host is no longer able to accept any more requests.
  780.  */
  781. while (1 == 1) {
  782. /*
  783.  * Check this again - each time we loop through we will have
  784.  * released the lock and grabbed it again, so each time
  785.  * we need to check to see if the queue is plugged or not.
  786.  */
  787. if (SHpnt->in_recovery || q->plugged)
  788. return;
  789. /*
  790.  * If the device cannot accept another request, then quit.
  791.  */
  792. if (SDpnt->device_blocked) {
  793. break;
  794. }
  795. if ((SHpnt->can_queue > 0 && (SHpnt->host_busy >= SHpnt->can_queue))
  796.     || (SHpnt->host_blocked) 
  797.     || (SHpnt->host_self_blocked)) {
  798. /*
  799.  * If we are unable to process any commands at all for
  800.  * this device, then we consider it to be starved.
  801.  * What this means is that there are no outstanding
  802.  * commands for this device and hence we need a
  803.  * little help getting it started again
  804.  * once the host isn't quite so busy.
  805.  */
  806. if (SDpnt->device_busy == 0) {
  807. SDpnt->starved = 1;
  808. SHpnt->some_device_starved = 1;
  809. }
  810. break;
  811. } else {
  812. SDpnt->starved = 0;
  813. }
  814.   /*
  815.  * FIXME(eric)
  816.  * I am not sure where the best place to do this is.  We need
  817.  * to hook in a place where we are likely to come if in user
  818.  * space.   Technically the error handling thread should be
  819.  * doing this crap, but the error handler isn't used by
  820.  * most hosts.
  821.  */
  822. if (SDpnt->was_reset) {
  823. /*
  824.  * We need to relock the door, but we might
  825.  * be in an interrupt handler.  Only do this
  826.  * from user space, since we do not want to
  827.  * sleep from an interrupt.
  828.  *
  829.  * FIXME(eric) - have the error handler thread do
  830.  * this work.
  831.  */
  832. SDpnt->was_reset = 0;
  833. if (SDpnt->removable && !in_interrupt()) {
  834. spin_unlock_irq(&io_request_lock);
  835. scsi_ioctl(SDpnt, SCSI_IOCTL_DOORLOCK, 0);
  836. spin_lock_irq(&io_request_lock);
  837. continue;
  838. }
  839. }
  840. /*
  841.  * If we couldn't find a request that could be queued, then we
  842.  * can also quit.
  843.  */
  844. if (list_empty(&q->queue_head))
  845. break;
  846. /*
  847.  * Loop through all of the requests in this queue, and find
  848.  * one that is queueable.
  849.  */
  850. req = blkdev_entry_next_request(&q->queue_head);
  851. /*
  852.  * Find the actual device driver associated with this command.
  853.  * The SPECIAL requests are things like character device or
  854.  * ioctls, which did not originate from ll_rw_blk.  Note that
  855.  * the special field is also used to indicate the SCpnt for
  856.  * the remainder of a partially fulfilled request that can 
  857.  * come up when there is a medium error.  We have to treat
  858.  * these two cases differently.  We differentiate by looking
  859.  * at request.cmd, as this tells us the real story.
  860.  */
  861. if (req->cmd == SPECIAL) {
  862. STpnt = NULL;
  863. SCpnt = (Scsi_Cmnd *) req->special;
  864. SRpnt = (Scsi_Request *) req->special;
  865. if( SRpnt->sr_magic == SCSI_REQ_MAGIC ) {
  866. SCpnt = scsi_allocate_device(SRpnt->sr_device, 
  867.      FALSE, FALSE);
  868. if( !SCpnt ) {
  869. break;
  870. }
  871. scsi_init_cmd_from_req(SCpnt, SRpnt);
  872. }
  873. } else {
  874. SRpnt = NULL;
  875. STpnt = scsi_get_request_dev(req);
  876. if (!STpnt) {
  877. panic("Unable to find device associated with request");
  878. }
  879. /*
  880.  * Now try and find a command block that we can use.
  881.  */
  882. if( req->special != NULL ) {
  883. SCpnt = (Scsi_Cmnd *) req->special;
  884. /*
  885.  * We need to recount the number of
  886.  * scatter-gather segments here - the
  887.  * normal case code assumes this to be
  888.  * correct, as it would be a performance
  889.  * lose to always recount.  Handling
  890.  * errors is always unusual, of course.
  891.  */
  892. recount_segments(SCpnt);
  893. } else {
  894. SCpnt = scsi_allocate_device(SDpnt, FALSE, FALSE);
  895. }
  896. /*
  897.  * If so, we are ready to do something.  Bump the count
  898.  * while the queue is locked and then break out of the
  899.  * loop. Otherwise loop around and try another request.
  900.  */
  901. if (!SCpnt) {
  902. break;
  903. }
  904. }
  905. /*
  906.  * Now bump the usage count for both the host and the
  907.  * device.
  908.  */
  909. SHpnt->host_busy++;
  910. SDpnt->device_busy++;
  911. /*
  912.  * Finally, before we release the lock, we copy the
  913.  * request to the command block, and remove the
  914.  * request from the request list.   Note that we always
  915.  * operate on the queue head - there is absolutely no
  916.  * reason to search the list, because all of the commands
  917.  * in this queue are for the same device.
  918.  */
  919. blkdev_dequeue_request(req);
  920. if (req != &SCpnt->request && req != &SRpnt->sr_request ) {
  921. memcpy(&SCpnt->request, req, sizeof(struct request));
  922. /*
  923.  * We have copied the data out of the request block -
  924.  * it is now in a field in SCpnt.  Release the request
  925.  * block.
  926.  */
  927. blkdev_release_request(req);
  928. }
  929. /*
  930.  * Now it is finally safe to release the lock.  We are
  931.  * not going to noodle the request list until this
  932.  * request has been queued and we loop back to queue
  933.  * another.  
  934.  */
  935. req = NULL;
  936. spin_unlock_irq(&io_request_lock);
  937. if (SCpnt->request.cmd != SPECIAL) {
  938. /*
  939.  * This will do a couple of things:
  940.  *  1) Fill in the actual SCSI command.
  941.  *  2) Fill in any other upper-level specific fields
  942.  * (timeout).
  943.  *
  944.  * If this returns 0, it means that the request failed
  945.  * (reading past end of disk, reading offline device,
  946.  * etc).   This won't actually talk to the device, but
  947.  * some kinds of consistency checking may cause the
  948.  * request to be rejected immediately.
  949.  */
  950. if (STpnt == NULL) {
  951. STpnt = scsi_get_request_dev(req);
  952. }
  953. /* 
  954.  * This sets up the scatter-gather table (allocating if
  955.  * required).  Hosts that need bounce buffers will also
  956.  * get those allocated here.  
  957.  */
  958. if (!SDpnt->scsi_init_io_fn(SCpnt)) {
  959. /*
  960.  * probably we ran out of sgtable memory, or
  961.  * __init_io() wanted to revert to a single
  962.  * segment request. this would require bouncing
  963.  * on highmem i/o, so mark the device as
  964.  * starved and continue later instead
  965.  */
  966. spin_lock_irq(&io_request_lock);
  967. SHpnt->host_busy--;
  968. SDpnt->device_busy--;
  969. if (SDpnt->device_busy == 0) {
  970. SDpnt->starved = 1;
  971. SHpnt->some_device_starved = 1;
  972. }
  973. SCpnt->request.special = SCpnt;
  974. list_add(&SCpnt->request.queue, &q->queue_head);
  975. break;
  976. }
  977. /*
  978.  * Initialize the actual SCSI command for this request.
  979.  */
  980. if (!STpnt->init_command(SCpnt)) {
  981. scsi_release_buffers(SCpnt);
  982. SCpnt = __scsi_end_request(SCpnt, 0, 
  983.    SCpnt->request.nr_sectors, 0, 0);
  984. if( SCpnt != NULL )
  985. {
  986. panic("Should not have leftover blocksn");
  987. }
  988. spin_lock_irq(&io_request_lock);
  989. SHpnt->host_busy--;
  990. SDpnt->device_busy--;
  991. continue;
  992. }
  993. }
  994. /*
  995.  * Finally, initialize any error handling parameters, and set up
  996.  * the timers for timeouts.
  997.  */
  998. scsi_init_cmd_errh(SCpnt);
  999. /*
  1000.  * Dispatch the command to the low-level driver.
  1001.  */
  1002. scsi_dispatch_cmd(SCpnt);
  1003. /*
  1004.  * Now we need to grab the lock again.  We are about to mess
  1005.  * with the request queue and try to find another command.
  1006.  */
  1007. spin_lock_irq(&io_request_lock);
  1008. }
  1009. }
  1010. /*
  1011.  * Function:    scsi_block_requests()
  1012.  *
  1013.  * Purpose:     Utility function used by low-level drivers to prevent further
  1014.  * commands from being queued to the device.
  1015.  *
  1016.  * Arguments:   SHpnt       - Host in question
  1017.  *
  1018.  * Returns:     Nothing
  1019.  *
  1020.  * Lock status: No locks are assumed held.
  1021.  *
  1022.  * Notes:       There is no timer nor any other means by which the requests
  1023.  * get unblocked other than the low-level driver calling
  1024.  * scsi_unblock_requests().
  1025.  */
  1026. void scsi_block_requests(struct Scsi_Host * SHpnt)
  1027. {
  1028. SHpnt->host_self_blocked = TRUE;
  1029. }
  1030. /*
  1031.  * Function:    scsi_unblock_requests()
  1032.  *
  1033.  * Purpose:     Utility function used by low-level drivers to allow further
  1034.  * commands from being queued to the device.
  1035.  *
  1036.  * Arguments:   SHpnt       - Host in question
  1037.  *
  1038.  * Returns:     Nothing
  1039.  *
  1040.  * Lock status: No locks are assumed held.
  1041.  *
  1042.  * Notes:       There is no timer nor any other means by which the requests
  1043.  * get unblocked other than the low-level driver calling
  1044.  * scsi_unblock_requests().
  1045.  *
  1046.  * This is done as an API function so that changes to the
  1047.  * internals of the scsi mid-layer won't require wholesale
  1048.  * changes to drivers that use this feature.
  1049.  */
  1050. void scsi_unblock_requests(struct Scsi_Host * SHpnt)
  1051. {
  1052. Scsi_Device *SDloop;
  1053. SHpnt->host_self_blocked = FALSE;
  1054. /* Now that we are unblocked, try to start the queues. */
  1055. for (SDloop = SHpnt->host_queue; SDloop; SDloop = SDloop->next)
  1056. scsi_queue_next_request(&SDloop->request_queue, NULL);
  1057. }
  1058. /*
  1059.  * Function:    scsi_report_bus_reset()
  1060.  *
  1061.  * Purpose:     Utility function used by low-level drivers to report that
  1062.  * they have observed a bus reset on the bus being handled.
  1063.  *
  1064.  * Arguments:   SHpnt       - Host in question
  1065.  * channel     - channel on which reset was observed.
  1066.  *
  1067.  * Returns:     Nothing
  1068.  *
  1069.  * Lock status: No locks are assumed held.
  1070.  *
  1071.  * Notes:       This only needs to be called if the reset is one which
  1072.  * originates from an unknown location.  Resets originated
  1073.  * by the mid-level itself don't need to call this, but there
  1074.  * should be no harm.
  1075.  *
  1076.  * The main purpose of this is to make sure that a CHECK_CONDITION
  1077.  * is properly treated.
  1078.  */
  1079. void scsi_report_bus_reset(struct Scsi_Host * SHpnt, int channel)
  1080. {
  1081. Scsi_Device *SDloop;
  1082. for (SDloop = SHpnt->host_queue; SDloop; SDloop = SDloop->next) {
  1083. if (channel == SDloop->channel) {
  1084. SDloop->was_reset = 1;
  1085. SDloop->expecting_cc_ua = 1;
  1086. }
  1087. }
  1088. }
  1089. /*
  1090.  * FIXME(eric) - these are empty stubs for the moment.  I need to re-implement
  1091.  * host blocking from scratch. The theory is that hosts that wish to block
  1092.  * will register/deregister using these functions instead of the old way
  1093.  * of setting the wish_block flag.
  1094.  *
  1095.  * The details of the implementation remain to be settled, however the
  1096.  * stubs are here now so that the actual drivers will properly compile.
  1097.  */
  1098. void scsi_register_blocked_host(struct Scsi_Host * SHpnt)
  1099. {
  1100. }
  1101. void scsi_deregister_blocked_host(struct Scsi_Host * SHpnt)
  1102. {
  1103. }