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