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