1 /*
2 * scsi_merge.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 * Support for dynamic DMA mapping: Jakub Jelinek (jakub@redhat.com).
9 */
10
11 /*
12 * This file contains queue management functions that are used by SCSI.
13 * Typically this is used for several purposes. First, we need to ensure
14 * that commands do not grow so large that they cannot be handled all at
15 * once by a host adapter. The various flavors of merge functions included
16 * here serve this purpose.
17 *
18 * Note that it would be quite trivial to allow the low-level driver the
19 * flexibility to define it's own queue handling functions. For the time
20 * being, the hooks are not present. Right now we are just using the
21 * data in the host template as an indicator of how we should be handling
22 * queues, and we select routines that are optimized for that purpose.
23 *
24 * Some hosts do not impose any restrictions on the size of a request.
25 * In such cases none of the merge functions in this file are called,
26 * and we allow ll_rw_blk to merge requests in the default manner.
27 * This isn't guaranteed to be optimal, but it should be pretty darned
28 * good. If someone comes up with ideas of better ways of managing queues
29 * to improve on the default behavior, then certainly fit it into this
30 * scheme in whatever manner makes the most sense. Please note that
31 * since each device has it's own queue, we have considerable flexibility
32 * in queue management.
33 */
34
35 #define __NO_VERSION__
36 #include <linux/config.h>
37 #include <linux/module.h>
38
39 #include <linux/sched.h>
40 #include <linux/timer.h>
41 #include <linux/string.h>
42 #include <linux/malloc.h>
43 #include <linux/ioport.h>
44 #include <linux/kernel.h>
45 #include <linux/stat.h>
46 #include <linux/blk.h>
47 #include <linux/interrupt.h>
48 #include <linux/delay.h>
49 #include <linux/smp_lock.h>
50
51
52 #define __KERNEL_SYSCALLS__
53
54 #include <linux/unistd.h>
55
56 #include <asm/system.h>
57 #include <asm/irq.h>
58 #include <asm/dma.h>
59 #include <asm/io.h>
60
61 #include "scsi.h"
62 #include "hosts.h"
63 #include "constants.h"
64 #include <scsi/scsi_ioctl.h>
65
66 /*
67 * This means that bounce buffers cannot be allocated in chunks > PAGE_SIZE.
68 * Ultimately we should get away from using a dedicated DMA bounce buffer
69 * pool, and we should instead try and use kmalloc() instead. If we can
70 * eliminate this pool, then this restriction would no longer be needed.
71 */
72 #define DMA_SEGMENT_SIZE_LIMITED
73
74 #ifdef CONFIG_SCSI_DEBUG_QUEUES
75 /*
76 * Enable a bunch of additional consistency checking. Turn this off
77 * if you are benchmarking.
78 */
79 static int dump_stats(struct request *req,
80 int use_clustering,
81 int dma_host,
82 int segments)
83 {
84 struct buffer_head *bh;
85
86 /*
87 * Dump the information that we have. We know we have an
88 * inconsistency.
89 */
90 printk("nr_segments is %x\n", req->nr_segments);
91 printk("counted segments is %x\n", segments);
92 printk("Flags %d %d\n", use_clustering, dma_host);
93 for (bh = req->bh; bh->b_reqnext != NULL; bh = bh->b_reqnext)
94 {
95 printk("Segment 0x%p, blocks %d, addr 0x%lx\n",
96 bh,
97 bh->b_size >> 9,
98 virt_to_phys(bh->b_data - 1));
99 }
100 panic("Ththththaats all folks. Too dangerous to continue.\n");
101 }
102
103
104 /*
105 * Simple sanity check that we will use for the first go around
106 * in order to ensure that we are doing the counting correctly.
107 * This can be removed for optimization.
108 */
109 #define SANITY_CHECK(req, _CLUSTER, _DMA) \
110 if( req->nr_segments != __count_segments(req, _CLUSTER, _DMA, NULL) ) \
111 { \
112 printk("Incorrect segment count at 0x%p", current_text_addr()); \
113 dump_stats(req, _CLUSTER, _DMA, __count_segments(req, _CLUSTER, _DMA, NULL)); \
114 }
115 #else
116 #define SANITY_CHECK(req, _CLUSTER, _DMA)
117 #endif
118
119 static void dma_exhausted(Scsi_Cmnd * SCpnt, int i)
120 {
121 int jj;
122 struct scatterlist *sgpnt;
123 int consumed = 0;
124
125 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
126
127 /*
128 * Now print out a bunch of stats. First, start with the request
129 * size.
130 */
131 printk("dma_free_sectors:%d\n", scsi_dma_free_sectors);
132 printk("use_sg:%d\ti:%d\n", SCpnt->use_sg, i);
133 printk("request_bufflen:%d\n", SCpnt->request_bufflen);
134 /*
135 * Now dump the scatter-gather table, up to the point of failure.
136 */
137 for(jj=0; jj < SCpnt->use_sg; jj++)
138 {
139 printk("[%d]\tlen:%d\taddr:%p\talt:%p\n",
140 jj,
141 sgpnt[jj].length,
142 sgpnt[jj].address,
143 sgpnt[jj].alt_address);
144 if( sgpnt[jj].alt_address != NULL )
145 {
146 consumed = (sgpnt[jj].length >> 9);
147 }
148 }
149 printk("Total %d sectors consumed\n", consumed);
150 panic("DMA pool exhausted");
151 }
152
153 /*
154 * FIXME(eric) - the original disk code disabled clustering for MOD
155 * devices. I have no idea why we thought this was a good idea - my
156 * guess is that it was an attempt to limit the size of requests to MOD
157 * devices.
158 */
159 #define CLUSTERABLE_DEVICE(SH,SD) (SH->use_clustering && \
160 SD->type != TYPE_MOD)
161
162 /*
163 * This entire source file deals with the new queueing code.
164 */
165
166 /*
167 * Function: __count_segments()
168 *
169 * Purpose: Prototype for queue merge function.
170 *
171 * Arguments: q - Queue for which we are merging request.
172 * req - request into which we wish to merge.
173 * use_clustering - 1 if this host wishes to use clustering
174 * dma_host - 1 if this host has ISA DMA issues (bus doesn't
175 * expose all of the address lines, so that DMA cannot
176 * be done from an arbitrary address).
177 * remainder - used to track the residual size of the last
178 * segment. Comes in handy when we want to limit the
179 * size of bounce buffer segments to PAGE_SIZE.
180 *
181 * Returns: Count of the number of SG segments for the request.
182 *
183 * Lock status:
184 *
185 * Notes: This is only used for diagnostic purposes.
186 */
187 __inline static int __count_segments(struct request *req,
188 int use_clustering,
189 int dma_host,
190 int * remainder)
191 {
192 int ret = 1;
193 int reqsize = 0;
194 struct buffer_head *bh;
195 struct buffer_head *bhnext;
196
197 if( remainder != NULL ) {
198 reqsize = *remainder;
199 }
200
201 /*
202 * Add in the size increment for the first buffer.
203 */
204 bh = req->bh;
205 #ifdef DMA_SEGMENT_SIZE_LIMITED
206 if( reqsize + bh->b_size > PAGE_SIZE ) {
207 ret++;
208 reqsize = bh->b_size;
209 } else {
210 reqsize += bh->b_size;
211 }
212 #else
213 reqsize += bh->b_size;
214 #endif
215
216 for (bh = req->bh, bhnext = bh->b_reqnext;
217 bhnext != NULL;
218 bh = bhnext, bhnext = bh->b_reqnext) {
219 if (use_clustering) {
220 /*
221 * See if we can do this without creating another
222 * scatter-gather segment. In the event that this is a
223 * DMA capable host, make sure that a segment doesn't span
224 * the DMA threshold boundary.
225 */
226 if (dma_host &&
227 virt_to_phys(bhnext->b_data) - 1 == ISA_DMA_THRESHOLD) {
228 ret++;
229 reqsize = bhnext->b_size;
230 } else if (CONTIGUOUS_BUFFERS(bh, bhnext)) {
231 /*
232 * This one is OK. Let it go.
233 */
234 #ifdef DMA_SEGMENT_SIZE_LIMITED
235 /* Note scsi_malloc is only able to hand out
236 * chunks of memory in sizes of PAGE_SIZE or
237 * less. Thus we need to keep track of
238 * the size of the piece that we have
239 * seen so far, and if we have hit
240 * the limit of PAGE_SIZE, then we are
241 * kind of screwed and we need to start
242 * another segment.
243 */
244 if( dma_host
245 && virt_to_phys(bh->b_data) - 1 >= ISA_DMA_THRESHOLD
246 && reqsize + bhnext->b_size > PAGE_SIZE )
247 {
248 ret++;
249 reqsize = bhnext->b_size;
250 continue;
251 }
252 #endif
253 reqsize += bhnext->b_size;
254 continue;
255 }
256 ret++;
257 reqsize = bhnext->b_size;
258 } else {
259 ret++;
260 reqsize = bhnext->b_size;
261 }
262 }
263 if( remainder != NULL ) {
264 *remainder = reqsize;
265 }
266 return ret;
267 }
268
269 /*
270 * Function: recount_segments()
271 *
272 * Purpose: Recount the number of scatter-gather segments for this request.
273 *
274 * Arguments: req - request that needs recounting.
275 *
276 * Returns: Count of the number of SG segments for the request.
277 *
278 * Lock status: Irrelevant.
279 *
280 * Notes: This is only used when we have partially completed requests
281 * and the bit that is leftover is of an indeterminate size.
282 * This can come up if you get a MEDIUM_ERROR, for example,
283 * as we will have "completed" all of the sectors up to and
284 * including the bad sector, and the leftover bit is what
285 * we have to do now. This tends to be a rare occurrence, so
286 * we aren't busting our butts to instantiate separate versions
287 * of this function for the 4 different flag values. We
288 * probably should, however.
289 */
290 void
291 recount_segments(Scsi_Cmnd * SCpnt)
292 {
293 struct request *req;
294 struct Scsi_Host *SHpnt;
295 Scsi_Device * SDpnt;
296
297 req = &SCpnt->request;
298 SHpnt = SCpnt->host;
299 SDpnt = SCpnt->device;
300
301 req->nr_segments = __count_segments(req,
302 CLUSTERABLE_DEVICE(SHpnt, SDpnt),
303 SHpnt->unchecked_isa_dma, NULL);
304 }
305
306 #define MERGEABLE_BUFFERS(X,Y) \
307 (((((long)(X)->b_data+(X)->b_size)|((long)(Y)->b_data)) & \
308 (DMA_CHUNK_SIZE - 1)) == 0)
309
310 #ifdef DMA_CHUNK_SIZE
311 static inline int scsi_new_mergeable(request_queue_t * q,
312 struct request * req,
313 struct Scsi_Host *SHpnt,
314 int max_segments)
315 {
316 /*
317 * pci_map_sg will be able to merge these two
318 * into a single hardware sg entry, check if
319 * we'll have enough memory for the sg list.
320 * scsi.c allocates for this purpose
321 * min(64,sg_tablesize) entries.
322 */
323 if (req->nr_segments >= max_segments ||
324 req->nr_segments >= SHpnt->sg_tablesize)
325 return 0;
326 req->nr_segments++;
327 q->elevator.nr_segments++;
328 return 1;
329 }
330
331 static inline int scsi_new_segment(request_queue_t * q,
332 struct request * req,
333 struct Scsi_Host *SHpnt,
334 int max_segments)
335 {
336 /*
337 * pci_map_sg won't be able to map these two
338 * into a single hardware sg entry, so we have to
339 * check if things fit into sg_tablesize.
340 */
341 if (req->nr_hw_segments >= SHpnt->sg_tablesize ||
342 req->nr_segments >= SHpnt->sg_tablesize)
343 return 0;
344 if (req->nr_segments >= max_segments)
345 return 0;
346 req->nr_hw_segments++;
347 req->nr_segments++;
348 q->elevator.nr_segments++;
349 return 1;
350 }
351 #else
352 static inline int scsi_new_segment(request_queue_t * q,
353 struct request * req,
354 struct Scsi_Host *SHpnt,
355 int max_segments)
356 {
357 if (req->nr_segments < SHpnt->sg_tablesize &&
358 req->nr_segments < max_segments) {
359 /*
360 * This will form the start of a new segment. Bump the
361 * counter.
362 */
363 req->nr_segments++;
364 q->elevator.nr_segments++;
365 return 1;
366 } else {
367 return 0;
368 }
369 }
370 #endif
371
372 /*
373 * Function: __scsi_merge_fn()
374 *
375 * Purpose: Prototype for queue merge function.
376 *
377 * Arguments: q - Queue for which we are merging request.
378 * req - request into which we wish to merge.
379 * bh - Block which we may wish to merge into request
380 * use_clustering - 1 if this host wishes to use clustering
381 * dma_host - 1 if this host has ISA DMA issues (bus doesn't
382 * expose all of the address lines, so that DMA cannot
383 * be done from an arbitrary address).
384 *
385 * Returns: 1 if it is OK to merge the block into the request. 0
386 * if it is not OK.
387 *
388 * Lock status: io_request_lock is assumed to be held here.
389 *
390 * Notes: Some drivers have limited scatter-gather table sizes, and
391 * thus they cannot queue an infinitely large command. This
392 * function is called from ll_rw_blk before it attempts to merge
393 * a new block into a request to make sure that the request will
394 * not become too large.
395 *
396 * This function is not designed to be directly called. Instead
397 * it should be referenced from other functions where the
398 * use_clustering and dma_host parameters should be integer
399 * constants. The compiler should thus be able to properly
400 * optimize the code, eliminating stuff that is irrelevant.
401 * It is more maintainable to do this way with a single function
402 * than to have 4 separate functions all doing roughly the
403 * same thing.
404 */
405 __inline static int __scsi_back_merge_fn(request_queue_t * q,
406 struct request *req,
407 struct buffer_head *bh,
408 int max_segments,
409 int use_clustering,
410 int dma_host)
411 {
412 unsigned int count;
413 unsigned int segment_size = 0;
414 Scsi_Device *SDpnt;
415 struct Scsi_Host *SHpnt;
416
417 SDpnt = (Scsi_Device *) q->queuedata;
418 SHpnt = SDpnt->host;
419
420 if (max_segments > 64)
421 max_segments = 64;
422
423 if (use_clustering) {
424 /*
425 * See if we can do this without creating another
426 * scatter-gather segment. In the event that this is a
427 * DMA capable host, make sure that a segment doesn't span
428 * the DMA threshold boundary.
429 */
430 if (dma_host &&
431 virt_to_phys(req->bhtail->b_data) - 1 == ISA_DMA_THRESHOLD) {
432 goto new_end_segment;
433 }
434 if (CONTIGUOUS_BUFFERS(req->bhtail, bh)) {
435 #ifdef DMA_SEGMENT_SIZE_LIMITED
436 if( dma_host
437 && virt_to_phys(bh->b_data) - 1 >= ISA_DMA_THRESHOLD ) {
438 segment_size = 0;
439 count = __count_segments(req, use_clustering, dma_host, &segment_size);
440 if( segment_size + bh->b_size > PAGE_SIZE ) {
441 goto new_end_segment;
442 }
443 }
444 #endif
445 /*
446 * This one is OK. Let it go.
447 */
448 return 1;
449 }
450 }
451 new_end_segment:
452 #ifdef DMA_CHUNK_SIZE
453 if (MERGEABLE_BUFFERS(req->bhtail, bh))
454 return scsi_new_mergeable(q, req, SHpnt, max_segments);
455 #endif
456 return scsi_new_segment(q, req, SHpnt, max_segments);
457 }
458
459 __inline static int __scsi_front_merge_fn(request_queue_t * q,
460 struct request *req,
461 struct buffer_head *bh,
462 int max_segments,
463 int use_clustering,
464 int dma_host)
465 {
466 unsigned int count;
467 unsigned int segment_size = 0;
468 Scsi_Device *SDpnt;
469 struct Scsi_Host *SHpnt;
470
471 SDpnt = (Scsi_Device *) q->queuedata;
472 SHpnt = SDpnt->host;
473
474 if (max_segments > 64)
475 max_segments = 64;
476
477 if (use_clustering) {
478 /*
479 * See if we can do this without creating another
480 * scatter-gather segment. In the event that this is a
481 * DMA capable host, make sure that a segment doesn't span
482 * the DMA threshold boundary.
483 */
484 if (dma_host &&
485 virt_to_phys(bh->b_data) - 1 == ISA_DMA_THRESHOLD) {
486 goto new_start_segment;
487 }
488 if (CONTIGUOUS_BUFFERS(bh, req->bh)) {
489 #ifdef DMA_SEGMENT_SIZE_LIMITED
490 if( dma_host
491 && virt_to_phys(bh->b_data) - 1 >= ISA_DMA_THRESHOLD ) {
492 segment_size = bh->b_size;
493 count = __count_segments(req, use_clustering, dma_host, &segment_size);
494 if( count != req->nr_segments ) {
495 goto new_start_segment;
496 }
497 }
498 #endif
499 /*
500 * This one is OK. Let it go.
501 */
502 return 1;
503 }
504 }
505 new_start_segment:
506 #ifdef DMA_CHUNK_SIZE
507 if (MERGEABLE_BUFFERS(bh, req->bh))
508 return scsi_new_mergeable(q, req, SHpnt, max_segments);
509 #endif
510 return scsi_new_segment(q, req, SHpnt, max_segments);
511 }
512
513 /*
514 * Function: scsi_merge_fn_()
515 *
516 * Purpose: queue merge function.
517 *
518 * Arguments: q - Queue for which we are merging request.
519 * req - request into which we wish to merge.
520 * bh - Block which we may wish to merge into request
521 *
522 * Returns: 1 if it is OK to merge the block into the request. 0
523 * if it is not OK.
524 *
525 * Lock status: io_request_lock is assumed to be held here.
526 *
527 * Notes: Optimized for different cases depending upon whether
528 * ISA DMA is in use and whether clustering should be used.
529 */
530 #define MERGEFCT(_FUNCTION, _BACK_FRONT, _CLUSTER, _DMA) \
531 static int _FUNCTION(request_queue_t * q, \
532 struct request * req, \
533 struct buffer_head * bh, \
534 int max_segments) \
535 { \
536 int ret; \
537 SANITY_CHECK(req, _CLUSTER, _DMA); \
538 ret = __scsi_ ## _BACK_FRONT ## _merge_fn(q, \
539 req, \
540 bh, \
541 max_segments, \
542 _CLUSTER, \
543 _DMA); \
544 return ret; \
545 }
546
547 /* Version with use_clustering 0 and dma_host 1 is not necessary,
548 * since the only use of dma_host above is protected by use_clustering.
549 */
550 MERGEFCT(scsi_back_merge_fn_, back, 0, 0)
551 MERGEFCT(scsi_back_merge_fn_c, back, 1, 0)
552 MERGEFCT(scsi_back_merge_fn_dc, back, 1, 1)
553
554 MERGEFCT(scsi_front_merge_fn_, front, 0, 0)
555 MERGEFCT(scsi_front_merge_fn_c, front, 1, 0)
556 MERGEFCT(scsi_front_merge_fn_dc, front, 1, 1)
557
558 /*
559 * Function: __scsi_merge_requests_fn()
560 *
561 * Purpose: Prototype for queue merge function.
562 *
563 * Arguments: q - Queue for which we are merging request.
564 * req - request into which we wish to merge.
565 * next - 2nd request that we might want to combine with req
566 * use_clustering - 1 if this host wishes to use clustering
567 * dma_host - 1 if this host has ISA DMA issues (bus doesn't
568 * expose all of the address lines, so that DMA cannot
569 * be done from an arbitrary address).
570 *
571 * Returns: 1 if it is OK to merge the two requests. 0
572 * if it is not OK.
573 *
574 * Lock status: io_request_lock is assumed to be held here.
575 *
576 * Notes: Some drivers have limited scatter-gather table sizes, and
577 * thus they cannot queue an infinitely large command. This
578 * function is called from ll_rw_blk before it attempts to merge
579 * a new block into a request to make sure that the request will
580 * not become too large.
581 *
582 * This function is not designed to be directly called. Instead
583 * it should be referenced from other functions where the
584 * use_clustering and dma_host parameters should be integer
585 * constants. The compiler should thus be able to properly
586 * optimize the code, eliminating stuff that is irrelevant.
587 * It is more maintainable to do this way with a single function
588 * than to have 4 separate functions all doing roughly the
589 * same thing.
590 */
591 __inline static int __scsi_merge_requests_fn(request_queue_t * q,
592 struct request *req,
593 struct request *next,
594 int max_segments,
595 int use_clustering,
596 int dma_host)
597 {
598 Scsi_Device *SDpnt;
599 struct Scsi_Host *SHpnt;
600
601 SDpnt = (Scsi_Device *) q->queuedata;
602 SHpnt = SDpnt->host;
603
604 if (max_segments > 64)
605 max_segments = 64;
606
607 #ifdef DMA_CHUNK_SIZE
608 /* If it would not fit into prepared memory space for sg chain,
609 * then don't allow the merge.
610 */
611 if (req->nr_segments + next->nr_segments - 1 > max_segments ||
612 req->nr_segments + next->nr_segments - 1 > SHpnt->sg_tablesize) {
613 return 0;
614 }
615 if (req->nr_hw_segments + next->nr_hw_segments - 1 > SHpnt->sg_tablesize) {
616 return 0;
617 }
618 #else
619 /*
620 * If the two requests together are too large (even assuming that we
621 * can merge the boundary requests into one segment, then don't
622 * allow the merge.
623 */
624 if (req->nr_segments + next->nr_segments - 1 > SHpnt->sg_tablesize) {
625 return 0;
626 }
627 #endif
628 /*
629 * The main question is whether the two segments at the boundaries
630 * would be considered one or two.
631 */
632 if (use_clustering) {
633 /*
634 * See if we can do this without creating another
635 * scatter-gather segment. In the event that this is a
636 * DMA capable host, make sure that a segment doesn't span
637 * the DMA threshold boundary.
638 */
639 if (dma_host &&
640 virt_to_phys(req->bhtail->b_data) - 1 == ISA_DMA_THRESHOLD) {
641 goto dont_combine;
642 }
643 #ifdef DMA_SEGMENT_SIZE_LIMITED
644 /*
645 * We currently can only allocate scatter-gather bounce
646 * buffers in chunks of PAGE_SIZE or less.
647 */
648 if (dma_host
649 && CONTIGUOUS_BUFFERS(req->bhtail, next->bh)
650 && virt_to_phys(req->bhtail->b_data) - 1 >= ISA_DMA_THRESHOLD )
651 {
652 int segment_size = 0;
653 int count = 0;
654
655 count = __count_segments(req, use_clustering, dma_host, &segment_size);
656 count += __count_segments(next, use_clustering, dma_host, &segment_size);
657 if( count != req->nr_segments + next->nr_segments ) {
658 goto dont_combine;
659 }
660 }
661 #endif
662 if (CONTIGUOUS_BUFFERS(req->bhtail, next->bh)) {
663 /*
664 * This one is OK. Let it go.
665 */
666 req->nr_segments += next->nr_segments - 1;
667 q->elevator.nr_segments--;
668 #ifdef DMA_CHUNK_SIZE
669 req->nr_hw_segments += next->nr_hw_segments - 1;
670 #endif
671 return 1;
672 }
673 }
674 dont_combine:
675 #ifdef DMA_CHUNK_SIZE
676 if (req->nr_segments + next->nr_segments > max_segments ||
677 req->nr_segments + next->nr_segments > SHpnt->sg_tablesize) {
678 return 0;
679 }
680 /* If dynamic DMA mapping can merge last segment in req with
681 * first segment in next, then the check for hw segments was
682 * done above already, so we can always merge.
683 */
684 if (MERGEABLE_BUFFERS (req->bhtail, next->bh)) {
685 req->nr_hw_segments += next->nr_hw_segments - 1;
686 } else if (req->nr_hw_segments + next->nr_hw_segments > SHpnt->sg_tablesize) {
687 return 0;
688 } else {
689 req->nr_hw_segments += next->nr_hw_segments;
690 }
691 req->nr_segments += next->nr_segments;
692 return 1;
693 #else
694 /*
695 * We know that the two requests at the boundary should not be combined.
696 * Make sure we can fix something that is the sum of the two.
697 * A slightly stricter test than we had above.
698 */
699 if (req->nr_segments + next->nr_segments > max_segments ||
700 req->nr_segments + next->nr_segments > SHpnt->sg_tablesize) {
701 return 0;
702 } else {
703 /*
704 * This will form the start of a new segment. Bump the
705 * counter.
706 */
707 req->nr_segments += next->nr_segments;
708 return 1;
709 }
710 #endif
711 }
712
713 /*
714 * Function: scsi_merge_requests_fn_()
715 *
716 * Purpose: queue merge function.
717 *
718 * Arguments: q - Queue for which we are merging request.
719 * req - request into which we wish to merge.
720 * bh - Block which we may wish to merge into request
721 *
722 * Returns: 1 if it is OK to merge the block into the request. 0
723 * if it is not OK.
724 *
725 * Lock status: io_request_lock is assumed to be held here.
726 *
727 * Notes: Optimized for different cases depending upon whether
728 * ISA DMA is in use and whether clustering should be used.
729 */
730 #define MERGEREQFCT(_FUNCTION, _CLUSTER, _DMA) \
731 static int _FUNCTION(request_queue_t * q, \
732 struct request * req, \
733 struct request * next, \
734 int max_segments) \
735 { \
736 int ret; \
737 SANITY_CHECK(req, _CLUSTER, _DMA); \
738 ret = __scsi_merge_requests_fn(q, req, next, max_segments, _CLUSTER, _DMA); \
739 return ret; \
740 }
741
742 /* Version with use_clustering 0 and dma_host 1 is not necessary,
743 * since the only use of dma_host above is protected by use_clustering.
744 */
745 MERGEREQFCT(scsi_merge_requests_fn_, 0, 0)
746 MERGEREQFCT(scsi_merge_requests_fn_c, 1, 0)
747 MERGEREQFCT(scsi_merge_requests_fn_dc, 1, 1)
748 /*
749 * Function: __init_io()
750 *
751 * Purpose: Prototype for io initialize function.
752 *
753 * Arguments: SCpnt - Command descriptor we wish to initialize
754 * sg_count_valid - 1 if the sg count in the req is valid.
755 * use_clustering - 1 if this host wishes to use clustering
756 * dma_host - 1 if this host has ISA DMA issues (bus doesn't
757 * expose all of the address lines, so that DMA cannot
758 * be done from an arbitrary address).
759 *
760 * Returns: 1 on success.
761 *
762 * Lock status:
763 *
764 * Notes: Only the SCpnt argument should be a non-constant variable.
765 * This function is designed in such a way that it will be
766 * invoked from a series of small stubs, each of which would
767 * be optimized for specific circumstances.
768 *
769 * The advantage of this is that hosts that don't do DMA
770 * get versions of the function that essentially don't have
771 * any of the DMA code. Same goes for clustering - in the
772 * case of hosts with no need for clustering, there is no point
773 * in a whole bunch of overhead.
774 *
775 * Finally, in the event that a host has set can_queue to SG_ALL
776 * implying that there is no limit to the length of a scatter
777 * gather list, the sg count in the request won't be valid
778 * (mainly because we don't need queue management functions
779 * which keep the tally uptodate.
780 */
781 __inline static int __init_io(Scsi_Cmnd * SCpnt,
782 int sg_count_valid,
783 int use_clustering,
784 int dma_host)
785 {
786 struct buffer_head * bh;
787 struct buffer_head * bhprev;
788 char * buff;
789 int count;
790 int i;
791 struct request * req;
792 int sectors;
793 struct scatterlist * sgpnt;
794 int this_count;
795
796 /*
797 * FIXME(eric) - don't inline this - it doesn't depend on the
798 * integer flags. Come to think of it, I don't think this is even
799 * needed any more. Need to play with it and see if we hit the
800 * panic. If not, then don't bother.
801 */
802 if (!SCpnt->request.bh) {
803 /*
804 * Case of page request (i.e. raw device), or unlinked buffer
805 * Typically used for swapping, but this isn't how we do
806 * swapping any more.
807 */
808 panic("I believe this is dead code. If we hit this, I was wrong");
809 #if 0
810 SCpnt->request_bufflen = SCpnt->request.nr_sectors << 9;
811 SCpnt->request_buffer = SCpnt->request.buffer;
812 SCpnt->use_sg = 0;
813 /*
814 * FIXME(eric) - need to handle DMA here.
815 */
816 #endif
817 return 1;
818 }
819 req = &SCpnt->request;
820 /*
821 * First we need to know how many scatter gather segments are needed.
822 */
823 if (!sg_count_valid) {
824 count = __count_segments(req, use_clustering, dma_host, NULL);
825 } else {
826 count = req->nr_segments;
827 }
828
829 /*
830 * If the dma pool is nearly empty, then queue a minimal request
831 * with a single segment. Typically this will satisfy a single
832 * buffer.
833 */
834 if (dma_host && scsi_dma_free_sectors <= 10) {
835 this_count = SCpnt->request.current_nr_sectors;
836 goto single_segment;
837 }
838 /*
839 * Don't bother with scatter-gather if there is only one segment.
840 */
841 if (count == 1) {
842 this_count = SCpnt->request.nr_sectors;
843 goto single_segment;
844 }
845 SCpnt->use_sg = count;
846
847 /*
848 * Allocate the actual scatter-gather table itself.
849 * scsi_malloc can only allocate in chunks of 512 bytes
850 */
851 SCpnt->sglist_len = (SCpnt->use_sg
852 * sizeof(struct scatterlist) + 511) & ~511;
853
854 sgpnt = (struct scatterlist *) scsi_malloc(SCpnt->sglist_len);
855
856 /*
857 * Now fill the scatter-gather table.
858 */
859 if (!sgpnt) {
860 /*
861 * If we cannot allocate the scatter-gather table, then
862 * simply write the first buffer all by itself.
863 */
864 printk("Warning - running *really* short on DMA buffers\n");
865 this_count = SCpnt->request.current_nr_sectors;
866 goto single_segment;
867 }
868 /*
869 * Next, walk the list, and fill in the addresses and sizes of
870 * each segment.
871 */
872 memset(sgpnt, 0, SCpnt->sglist_len);
873 SCpnt->request_buffer = (char *) sgpnt;
874 SCpnt->request_bufflen = 0;
875 bhprev = NULL;
876
877 for (count = 0, bh = SCpnt->request.bh;
878 bh; bh = bh->b_reqnext) {
879 if (use_clustering && bhprev != NULL) {
880 if (dma_host &&
881 virt_to_phys(bhprev->b_data) - 1 == ISA_DMA_THRESHOLD) {
882 /* Nothing - fall through */
883 } else if (CONTIGUOUS_BUFFERS(bhprev, bh)) {
884 /*
885 * This one is OK. Let it go. Note that we
886 * do not have the ability to allocate
887 * bounce buffer segments > PAGE_SIZE, so
888 * for now we limit the thing.
889 */
890 if( dma_host ) {
891 #ifdef DMA_SEGMENT_SIZE_LIMITED
892 if( virt_to_phys(bh->b_data) - 1 < ISA_DMA_THRESHOLD
893 || sgpnt[count - 1].length + bh->b_size <= PAGE_SIZE ) {
894 sgpnt[count - 1].length += bh->b_size;
895 bhprev = bh;
896 continue;
897 }
898 #else
899 sgpnt[count - 1].length += bh->b_size;
900 bhprev = bh;
901 continue;
902 #endif
903 } else {
904 sgpnt[count - 1].length += bh->b_size;
905 SCpnt->request_bufflen += bh->b_size;
906 bhprev = bh;
907 continue;
908 }
909 }
910 }
911 count++;
912 sgpnt[count - 1].address = bh->b_data;
913 sgpnt[count - 1].length += bh->b_size;
914 if (!dma_host) {
915 SCpnt->request_bufflen += bh->b_size;
916 }
917 bhprev = bh;
918 }
919
920 /*
921 * Verify that the count is correct.
922 */
923 if (count != SCpnt->use_sg) {
924 printk("Incorrect number of segments after building list\n");
925 #ifdef CONFIG_SCSI_DEBUG_QUEUES
926 dump_stats(req, use_clustering, dma_host, count);
927 #endif
928 }
929 if (!dma_host) {
930 return 1;
931 }
932 /*
933 * Now allocate bounce buffers, if needed.
934 */
935 SCpnt->request_bufflen = 0;
936 for (i = 0; i < count; i++) {
937 sectors = (sgpnt[i].length >> 9);
938 SCpnt->request_bufflen += sgpnt[i].length;
939 if (virt_to_phys(sgpnt[i].address) + sgpnt[i].length - 1 >
940 ISA_DMA_THRESHOLD) {
941 if( scsi_dma_free_sectors - sectors <= 10 ) {
942 /*
943 * If this would nearly drain the DMA
944 * pool, mpty, then let's stop here.
945 * Don't make this request any larger.
946 * This is kind of a safety valve that
947 * we use - we could get screwed later
948 * on if we run out completely.
949 */
950 SCpnt->request_bufflen -= sgpnt[i].length;
951 SCpnt->use_sg = i;
952 if (i == 0) {
953 goto big_trouble;
954 }
955 break;
956 }
957
958 sgpnt[i].alt_address = sgpnt[i].address;
959 sgpnt[i].address =
960 (char *) scsi_malloc(sgpnt[i].length);
961 /*
962 * If we cannot allocate memory for this DMA bounce
963 * buffer, then queue just what we have done so far.
964 */
965 if (sgpnt[i].address == NULL) {
966 printk("Warning - running low on DMA memory\n");
967 SCpnt->request_bufflen -= sgpnt[i].length;
968 SCpnt->use_sg = i;
969 if (i == 0) {
970 goto big_trouble;
971 }
972 break;
973 }
974 if (SCpnt->request.cmd == WRITE) {
975 memcpy(sgpnt[i].address, sgpnt[i].alt_address,
976 sgpnt[i].length);
977 }
978 }
979 }
980 return 1;
981
982 big_trouble:
983 /*
984 * We come here in the event that we get one humongous
985 * request, where we need a bounce buffer, and the buffer is
986 * more than we can allocate in a single call to
987 * scsi_malloc(). In addition, we only come here when it is
988 * the 0th element of the scatter-gather table that gets us
989 * into this trouble. As a fallback, we fall back to
990 * non-scatter-gather, and ask for a single segment. We make
991 * a half-hearted attempt to pick a reasonably large request
992 * size mainly so that we don't thrash the thing with
993 * iddy-biddy requests.
994 */
995
996 /*
997 * The original number of sectors in the 0th element of the
998 * scatter-gather table.
999 */
1000 sectors = sgpnt[0].length >> 9;
1001
1002 /*
1003 * Free up the original scatter-gather table. Note that since
1004 * it was the 0th element that got us here, we don't have to
1005 * go in and free up memory from the other slots.
1006 */
1007 SCpnt->request_bufflen = 0;
1008 SCpnt->use_sg = 0;
1009 scsi_free(SCpnt->request_buffer, SCpnt->sglist_len);
1010
1011 /*
1012 * Make an attempt to pick up as much as we reasonably can.
1013 * Just keep adding sectors until the pool starts running kind of
1014 * low. The limit of 30 is somewhat arbitrary - the point is that
1015 * it would kind of suck if we dropped down and limited ourselves to
1016 * single-block requests if we had hundreds of free sectors.
1017 */
1018 if( scsi_dma_free_sectors > 30 ) {
1019 for (this_count = 0, bh = SCpnt->request.bh;
1020 bh; bh = bh->b_reqnext) {
1021 if( scsi_dma_free_sectors - this_count < 30
1022 || this_count == sectors )
1023 {
1024 break;
1025 }
1026 this_count += bh->b_size >> 9;
1027 }
1028
1029 } else {
1030 /*
1031 * Yow! Take the absolute minimum here.
1032 */
1033 this_count = SCpnt->request.current_nr_sectors;
1034 }
1035
1036 /*
1037 * Now drop through into the single-segment case.
1038 */
1039
1040 single_segment:
1041 /*
1042 * Come here if for any reason we choose to do this as a single
1043 * segment. Possibly the entire request, or possibly a small
1044 * chunk of the entire request.
1045 */
1046 bh = SCpnt->request.bh;
1047 buff = SCpnt->request.buffer;
1048
1049 if (dma_host) {
1050 /*
1051 * Allocate a DMA bounce buffer. If the allocation fails, fall
1052 * back and allocate a really small one - enough to satisfy
1053 * the first buffer.
1054 */
1055 if (virt_to_phys(SCpnt->request.bh->b_data)
1056 + (this_count << 9) - 1 > ISA_DMA_THRESHOLD) {
1057 buff = (char *) scsi_malloc(this_count << 9);
1058 if (!buff) {
1059 printk("Warning - running low on DMA memory\n");
1060 this_count = SCpnt->request.current_nr_sectors;
1061 buff = (char *) scsi_malloc(this_count << 9);
1062 if (!buff) {
1063 dma_exhausted(SCpnt, 0);
1064 }
1065 }
1066 if (SCpnt->request.cmd == WRITE)
1067 memcpy(buff, (char *) SCpnt->request.buffer, this_count << 9);
1068 }
1069 }
1070 SCpnt->request_bufflen = this_count << 9;
1071 SCpnt->request_buffer = buff;
1072 SCpnt->use_sg = 0;
1073 return 1;
1074 }
1075
1076 #define INITIO(_FUNCTION, _VALID, _CLUSTER, _DMA) \
1077 static int _FUNCTION(Scsi_Cmnd * SCpnt) \
1078 { \
1079 return __init_io(SCpnt, _VALID, _CLUSTER, _DMA); \
1080 }
1081
1082 /*
1083 * ll_rw_blk.c now keeps track of the number of segments in
1084 * a request. Thus we don't have to do it any more here.
1085 * We always force "_VALID" to 1. Eventually clean this up
1086 * and get rid of the extra argument.
1087 */
1088 INITIO(scsi_init_io_v, 1, 0, 0)
1089 INITIO(scsi_init_io_vd, 1, 0, 1)
1090 INITIO(scsi_init_io_vc, 1, 1, 0)
1091 INITIO(scsi_init_io_vdc, 1, 1, 1)
1092
1093 /*
1094 * Function: initialize_merge_fn()
1095 *
1096 * Purpose: Initialize merge function for a host
1097 *
1098 * Arguments: SHpnt - Host descriptor.
1099 *
1100 * Returns: Nothing.
1101 *
1102 * Lock status:
1103 *
1104 * Notes:
1105 */
1106 void initialize_merge_fn(Scsi_Device * SDpnt)
1107 {
1108 request_queue_t *q;
1109 struct Scsi_Host *SHpnt;
1110 SHpnt = SDpnt->host;
1111
1112 q = &SDpnt->request_queue;
1113
1114 /*
1115 * If the host has already selected a merge manager, then don't
1116 * pick a new one.
1117 */
1118 #if 0
1119 if (q->back_merge_fn && q->front_merge_fn)
1120 return;
1121 #endif
1122 /*
1123 * If this host has an unlimited tablesize, then don't bother with a
1124 * merge manager. The whole point of the operation is to make sure
1125 * that requests don't grow too large, and this host isn't picky.
1126 *
1127 * Note that ll_rw_blk.c is effectively maintaining a segment
1128 * count which is only valid if clustering is used, and it obviously
1129 * doesn't handle the DMA case. In the end, it
1130 * is simply easier to do it ourselves with our own functions
1131 * rather than rely upon the default behavior of ll_rw_blk.
1132 */
1133 if (!CLUSTERABLE_DEVICE(SHpnt, SDpnt) && SHpnt->unchecked_isa_dma == 0) {
1134 q->back_merge_fn = scsi_back_merge_fn_;
1135 q->front_merge_fn = scsi_front_merge_fn_;
1136 q->merge_requests_fn = scsi_merge_requests_fn_;
1137 SDpnt->scsi_init_io_fn = scsi_init_io_v;
1138 } else if (!CLUSTERABLE_DEVICE(SHpnt, SDpnt) && SHpnt->unchecked_isa_dma != 0) {
1139 q->back_merge_fn = scsi_back_merge_fn_;
1140 q->front_merge_fn = scsi_front_merge_fn_;
1141 q->merge_requests_fn = scsi_merge_requests_fn_;
1142 SDpnt->scsi_init_io_fn = scsi_init_io_vd;
1143 } else if (CLUSTERABLE_DEVICE(SHpnt, SDpnt) && SHpnt->unchecked_isa_dma == 0) {
1144 q->back_merge_fn = scsi_back_merge_fn_c;
1145 q->front_merge_fn = scsi_front_merge_fn_c;
1146 q->merge_requests_fn = scsi_merge_requests_fn_c;
1147 SDpnt->scsi_init_io_fn = scsi_init_io_vc;
1148 } else if (CLUSTERABLE_DEVICE(SHpnt, SDpnt) && SHpnt->unchecked_isa_dma != 0) {
1149 q->back_merge_fn = scsi_back_merge_fn_dc;
1150 q->front_merge_fn = scsi_front_merge_fn_dc;
1151 q->merge_requests_fn = scsi_merge_requests_fn_dc;
1152 SDpnt->scsi_init_io_fn = scsi_init_io_vdc;
1153 }
1154 }
1155
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.