Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/* $Id$
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 1992 - 1997, 2000-2002 Silicon Graphics, Inc.  All rights reserved.
 */

/* Implementation of Address/Length Lists. */


#include <linux/types.h>
#include <linux/slab.h>
#include <linux/mmzone.h>
#include <asm/sn/sgi.h>
#include <asm/sn/alenlist.h>

/*
 * Logically, an Address/Length List is a list of Pairs, where each pair
 * holds an Address and a Length, all in some Address Space.  In this
 * context, "Address Space" is a particular Crosstalk Widget address
 * space, a PCI device address space, a VME bus address space, a
 * physical memory address space, etc.
 *
 * The main use for these Lists is to provide a single mechanism that
 * describes where in an address space a DMA occurs.  This allows the
 * various I/O Bus support layers to provide a single interface for
 * DMA mapping and DMA translation without regard to how the DMA target
 * was specified by upper layers.  The upper layers commonly specify a 
 * DMA target via a buf structure page list, a kernel virtual address,
 * a user virtual address, a vector of addresses (a la uio and iov), 
 * or possibly a pfn list.
 *
 * Address/Length Lists also enable drivers to take advantage of their
 * inate scatter/gather capabilities in systems where some address
 * translation may be required between bus adapters.  The driver forms
 * a List that represents physical memory targets.  This list is passed
 * to the various adapters, which apply various translations.  The final
 * list that's returned to the driver is in terms of its local address
 * address space -- addresses which can be passed off to a scatter/gather
 * capable DMA controller.
 *
 * The current implementation is intended to be useful both in kernels
 * that support interrupt threads (INTR_KTHREAD) and in systems that do
 * not support interrupt threads.  Of course, in the latter case, some
 * interfaces can be called only within a suspendable context.
 *
 * Basic operations on Address/Length Lists include:
 *	alenlist_create		Create a list
 *	alenlist_clear		Clear a list
 *	alenlist_destroy	Destroy a list
 *	alenlist_append		Append a Pair to the end of a list
 *	alenlist_replace	Replace a Pair in the middle of a list
 *	alenlist_get		Get an Address/Length Pair from a list
 *	alenlist_size		Return the number of Pairs in a list
 *	alenlist_concat		Append one list to the end of another
 *	alenlist_clone		Create a new copy of a list
 *
 * Operations that convert from upper-level specifications to Address/
 * Length Lists currently include:
 *	kvaddr_to_alenlist	Convert from a kernel virtual address
 *	uvaddr_to_alenlist	Convert from a user virtual address
 *	buf_to_alenlist		Convert from a buf structure
 *	alenlist_done		Tell system that we're done with an alenlist
 *				obtained from a conversion.
 * Additional convenience operations:
 *	alenpair_init		Create a list and initialize it with a Pair
 *	alenpair_get		Peek at the first pair on a List
 *
 * A supporting type for Address/Length Lists is an alenlist_cursor_t.  A
 * cursor marks a position in a List, and determines which Pair is fetched
 * by alenlist_get.
 *	alenlist_cursor_create	Allocate and initialize a cursor
 *	alenlist_cursor_destroy	Free space consumed by a cursor
 *	alenlist_cursor_init	(Re-)Initialize a cursor to point 
 *				to the start of a list
 *	alenlist_cursor_clone	Clone a cursor (at the current offset)
 *	alenlist_cursor_offset	Return the number of bytes into
 *				a list that this cursor marks
 * Multiple cursors can point at various points into a List.  Also, each
 * list maintains one "internal cursor" which may be updated by alenlist_clear
 * and alenlist_get.  If calling code simply wishes to scan sequentially
 * through a list starting at the beginning, and if it is the only user of
 * a list, it can rely on this internal cursor rather than managing a 
 * separate explicit cursor.
 *
 * The current implementation allows callers to allocate both cursors and
 * the lists as local stack (structure) variables.  This allows for some
 * extra efficiency at the expense of forward binary compatibility.  It 
 * is recommended that customer drivers refrain from local allocation.
 * In fact, we likely will choose to move the structures out of the public 
 * header file into a private place in order to discourage this usage.
 *
 * Currently, no locking is provided by the alenlist implementation.
 *
 * Implementation notes:
 * For efficiency, Pairs are grouped into "chunks" of, say, 32 Pairs
 * and a List consists of some number of these chunks.  Chunks are completely
 * invisible to calling code.  Chunks should be large enough to hold most
 * standard-sized DMA's, but not so large that they consume excessive space.
 *
 * It is generally expected that Lists will be constructed at one time and
 * scanned at a later time.  It is NOT expected that drivers will scan
 * a List while the List is simultaneously extended, although this is
 * theoretically possible with sufficient upper-level locking.
 *
 * In order to support demands of Real-Time drivers and in order to support
 * swapping under low-memory conditions, we support the concept of a
 * "pre-allocated fixed-sized List".  After creating a List with 
 * alenlist_create, a driver may explicitly grow the list (via "alenlist_grow")
 * to a specific number of Address/Length pairs.  It is guaranteed that future 
 * operations involving this list will never automatically grow the list 
 * (i.e. if growth is ever required, the operation will fail).  Additionally, 
 * operations that use alenlist's (e.g. DMA operations) accept a flag which 
 * causes processing to take place "in-situ"; that is, the input alenlist 
 * entries are replaced with output alenlist entries.  The combination of 
 * pre-allocated Lists and in-situ processing allows us to avoid the 
 * potential deadlock scenario where we sleep (waiting for memory) in the 
 * swap out path.
 *
 * For debugging, we track the number of allocated Lists in alenlist_count
 * the number of allocated chunks in alenlist_chunk_count, and the number
 * of allocate cursors in alenlist_cursor_count.  We also provide a debug 
 * routine, alenlist_show, which dumps the contents of an Address/Length List.
 *
 * Currently, Lists are formed by drivers on-demand.  Eventually, we may
 * associate an alenlist with a buf structure and keep it up to date as
 * we go along.  In that case, buf_to_alenlist simply returns a pointer
 * to the existing List, and increments the Lists's reference count.
 * alenlist_done would decrement the reference count and destroys the List
 * if it was the last reference.
 *
 * Eventually alenlist's may allow better support for user-level scatter/
 * gather operations (e.g. via readv/writev):  With proper support, we
 * could potentially handle a vector of reads with a single scatter/gather
 * DMA operation.  This could be especially useful on NUMA systems where
 * there's more of a reason for users to use vector I/O operations.
 *
 * Eventually, alenlist's may replace kaio lists, vhand page lists,
 * buffer cache pfdat lists, DMA page lists, etc.
 */

/* Opaque data types */

/* An Address/Length pair.  */
typedef struct alen_s {
	alenaddr_t	al_addr;
	size_t		al_length;
} alen_t;

/* 
 * Number of elements in one chunk of an Address/Length List.
 *
 * This size should be sufficient to hold at least an "average" size
 * DMA request.  Must be at least 1, and should be a power of 2,
 * for efficiency.
 */
#define ALEN_CHUNK_SZ ((512*1024)/NBPP)

/*
 * A fixed-size set of Address/Length Pairs.  Chunks of Pairs are strung together 
 * to form a complete Address/Length List.  Chunking is entirely hidden within the 
 * alenlist implementation, and it simply makes allocation and growth of lists more 
 * efficient.
 */
typedef struct alenlist_chunk_s {
	alen_t			alc_pair[ALEN_CHUNK_SZ];/* list of addr/len pairs */
	struct alenlist_chunk_s *alc_next;		/* point to next chunk of pairs */
} *alenlist_chunk_t;

/* 
 * An Address/Length List.  An Address/Length List is allocated with alenlist_create.  
 * Alternatively, a list can be allocated on the stack (local variable of type 
 * alenlist_t) and initialized with alenpair_init or with a combination of 
 * alenlist_clear and alenlist_append, etc.  Code which statically allocates these
 * structures loses forward binary compatibility!
 *
 * A statically allocated List is sufficiently large to hold ALEN_CHUNK_SZ pairs.
 */
struct alenlist_s {
	unsigned short		al_flags;
	unsigned short		al_logical_size;	/* logical size of list, in pairs */
	unsigned short		al_actual_size;		/* actual size of list, in pairs */
	struct alenlist_chunk_s	*al_last_chunk;		/* pointer to last logical chunk */
	struct alenlist_cursor_s al_cursor;		/* internal cursor */
	struct alenlist_chunk_s	al_chunk;		/* initial set of pairs */
	alenaddr_t		al_compaction_address;	/* used to compact pairs */
};

/* al_flags field */
#define AL_FIXED_SIZE	0x1	/* List is pre-allocated, and of fixed size */


zone_t *alenlist_zone = NULL;
zone_t *alenlist_chunk_zone = NULL;
zone_t *alenlist_cursor_zone = NULL;

#if DEBUG
int alenlist_count=0;		/* Currently allocated Lists */
int alenlist_chunk_count = 0;	/* Currently allocated chunks */
int alenlist_cursor_count = 0;	/* Currently allocate cursors */
#define INCR_COUNT(ptr) atomic_inc((ptr));
#define DECR_COUNT(ptr) atomic_dec((ptr));
#else
#define INCR_COUNT(ptr)
#define DECR_COUNT(ptr)
#endif /* DEBUG */

#if DEBUG
static void alenlist_show(alenlist_t);
#endif /* DEBUG */

/*
 * Initialize Address/Length List management.  One time initialization.
 */
void
alenlist_init(void)
{
	alenlist_zone = snia_kmem_zone_init(sizeof(struct alenlist_s), "alenlist");
	alenlist_chunk_zone = snia_kmem_zone_init(sizeof(struct alenlist_chunk_s), "alchunk");
	alenlist_cursor_zone = snia_kmem_zone_init(sizeof(struct alenlist_cursor_s), "alcursor");
#if DEBUG
	idbg_addfunc("alenshow", alenlist_show);
#endif /* DEBUG */
}


/*
 * Initialize an Address/Length List cursor.
 */
static void
do_cursor_init(alenlist_t alenlist, alenlist_cursor_t cursorp)
{
	cursorp->al_alenlist = alenlist;
	cursorp->al_offset = 0;
	cursorp->al_chunk = &alenlist->al_chunk;
	cursorp->al_index = 0;
	cursorp->al_bcount = 0;
}


/*
 * Create an Address/Length List, and clear it.
 * Set the cursor to the beginning.
 */
alenlist_t 
alenlist_create(unsigned flags)
{
	alenlist_t alenlist;

	alenlist = snia_kmem_zone_alloc(alenlist_zone, flags & AL_NOSLEEP ? VM_NOSLEEP : 0);
	if (alenlist) {
		INCR_COUNT(&alenlist_count);

		alenlist->al_flags = 0;
		alenlist->al_logical_size = 0;
		alenlist->al_actual_size = ALEN_CHUNK_SZ;
		alenlist->al_last_chunk = &alenlist->al_chunk;
		alenlist->al_chunk.alc_next = NULL;
		do_cursor_init(alenlist, &alenlist->al_cursor);
	}

	return(alenlist);
}


/*
 * Grow an Address/Length List so that all resources needed to contain
 * the specified number of Pairs are pre-allocated.  An Address/Length
 * List that has been explicitly "grown" will never *automatically*
 * grow, shrink, or be destroyed.
 *
 * Pre-allocation is useful for Real-Time drivers and for drivers that
 * may be used along the swap-out path and therefore cannot afford to 
 * sleep until memory is freed.
 * 
 * The cursor is set to the beginning of the list.
 */
int
alenlist_grow(alenlist_t alenlist, size_t npairs)
{
	/* 
	 * This interface should be used relatively rarely, so
	 * the implementation is kept simple: We clear the List,
	 * then append npairs bogus entries.  Finally, we mark
	 * the list as FIXED_SIZE and re-initialize the internal
	 * cursor.
	 */

	/* 
	 * Temporarily mark as non-fixed size, since we're about
	 * to shrink and expand it.
	 */
	alenlist->al_flags &= ~AL_FIXED_SIZE;

	/* Free whatever was in the alenlist. */
	alenlist_clear(alenlist);

	/* Allocate everything that we need via automatic expansion. */
	while (npairs--)
		if (alenlist_append(alenlist, 0, 0, AL_NOCOMPACT) == ALENLIST_FAILURE)
			return(ALENLIST_FAILURE);

	/* Now, mark as FIXED_SIZE */
	alenlist->al_flags |= AL_FIXED_SIZE;

	/* Clear out bogus entries */
	alenlist_clear(alenlist);

	/* Initialize internal cursor to the beginning */
	do_cursor_init(alenlist, &alenlist->al_cursor);

	return(ALENLIST_SUCCESS);
}


/*
 * Clear an Address/Length List so that it holds no pairs.
 */
void
alenlist_clear(alenlist_t alenlist)
{
	alenlist_chunk_t chunk, freechunk;

	/*
	 * If this List is not FIXED_SIZE, free all the
	 * extra chunks.
	 */
	if (!(alenlist->al_flags & AL_FIXED_SIZE)) {
		/* First, free any extension alenlist chunks */
		chunk = alenlist->al_chunk.alc_next;
		while (chunk) {
			freechunk = chunk;
			chunk = chunk->alc_next;
			snia_kmem_zone_free(alenlist_chunk_zone, freechunk);
			DECR_COUNT(&alenlist_chunk_count);
		}
		alenlist->al_actual_size = ALEN_CHUNK_SZ;
		alenlist->al_chunk.alc_next = NULL;
	}

	alenlist->al_logical_size = 0;
	alenlist->al_last_chunk = &alenlist->al_chunk;
	do_cursor_init(alenlist, &alenlist->al_cursor);
}


/*
 * Create and initialize an Address/Length Pair.
 * This is intended for degenerate lists, consisting of a single 
 * address/length pair.
 */
alenlist_t
alenpair_init(	alenaddr_t address, 
		size_t length)
{
	alenlist_t alenlist;

	alenlist = alenlist_create(0);

	alenlist->al_logical_size = 1;
	ASSERT(alenlist->al_last_chunk == &alenlist->al_chunk);
	alenlist->al_chunk.alc_pair[0].al_length = length;
	alenlist->al_chunk.alc_pair[0].al_addr = address;

	return(alenlist);
}

/*
 * Return address/length from a degenerate (1-pair) List, or
 * first pair from a larger list.  Does NOT update the internal cursor,
 * so this is an easy way to peek at a start address.
 */
int
alenpair_get(	alenlist_t alenlist,
		alenaddr_t *address,
		size_t *length)
{
	if (alenlist->al_logical_size == 0)
		return(ALENLIST_FAILURE);

	*length = alenlist->al_chunk.alc_pair[0].al_length;
	*address = alenlist->al_chunk.alc_pair[0].al_addr;
	return(ALENLIST_SUCCESS);
}


/*
 * Destroy an Address/Length List.
 */
void 
alenlist_destroy(alenlist_t alenlist)
{
	if (alenlist == NULL)
		return;

	/* 
	 * Turn off FIXED_SIZE so this List can be 
	 * automatically shrunk.
	 */
	alenlist->al_flags &= ~AL_FIXED_SIZE;

	/* Free extension chunks first */
	if (alenlist->al_chunk.alc_next)
		alenlist_clear(alenlist);

	/* Now, free the alenlist itself */
	snia_kmem_zone_free(alenlist_zone, alenlist);
	DECR_COUNT(&alenlist_count);
}

/*
 * Release an Address/Length List.
 * This is in preparation for a day when alenlist's may be longer-lived, and
 * perhaps associated with a buf structure.  We'd add a reference count, and
 * this routine would decrement the count.  For now, we create alenlist's on
 * on demand and free them when done.  If the driver is not explicitly managing
 * a List for its own use, it should call alenlist_done rather than alenlist_destroy.
 */
void
alenlist_done(alenlist_t alenlist)
{
	alenlist_destroy(alenlist);
}


/*
 * Append another address/length to the end of an Address/Length List,
 * growing the list if permitted and necessary.
 *
 * Returns: SUCCESS/FAILURE
 */
int 
alenlist_append(	alenlist_t alenlist, 		/* append to this list */
			alenaddr_t address, 		/* address to append */
			size_t length,			/* length to append */
			unsigned flags)
{
	alen_t *alenp;
	int index, last_index;

	index = alenlist->al_logical_size % ALEN_CHUNK_SZ;

	if ((alenlist->al_logical_size > 0)) {
		/*
		 * See if we can compact this new pair in with the previous entry.
		 * al_compaction_address holds that value that we'd need to see
		 * in order to compact.
		 */
		if (!(flags & AL_NOCOMPACT) &&
		    (alenlist->al_compaction_address == address)) {
			last_index = (alenlist->al_logical_size-1) % ALEN_CHUNK_SZ;
			alenp = &(alenlist->al_last_chunk->alc_pair[last_index]);
			alenp->al_length += length;
			alenlist->al_compaction_address += length;
			return(ALENLIST_SUCCESS);
		}

		/*
		 * If we're out of room in this chunk, move to a new chunk.
	 	 */
		if (index == 0) {
			if (alenlist->al_flags & AL_FIXED_SIZE) {
				alenlist->al_last_chunk = alenlist->al_last_chunk->alc_next;

				/* If we're out of space in a FIXED_SIZE List, quit. */
				if (alenlist->al_last_chunk == NULL) {
					ASSERT(alenlist->al_logical_size == alenlist->al_actual_size);
					return(ALENLIST_FAILURE);
				}
			} else {
				alenlist_chunk_t new_chunk;

				new_chunk = snia_kmem_zone_alloc(alenlist_chunk_zone, 
							flags & AL_NOSLEEP ? VM_NOSLEEP : 0);

				if (new_chunk == NULL)
					return(ALENLIST_FAILURE);

				alenlist->al_last_chunk->alc_next = new_chunk;
				new_chunk->alc_next = NULL;
				alenlist->al_last_chunk = new_chunk;
				alenlist->al_actual_size += ALEN_CHUNK_SZ;
				INCR_COUNT(&alenlist_chunk_count);
			}
		}
	}

	alenp = &(alenlist->al_last_chunk->alc_pair[index]);
	alenp->al_addr = address;
	alenp->al_length = length;
	
	alenlist->al_logical_size++;
	alenlist->al_compaction_address = address + length;

	return(ALENLIST_SUCCESS);
}


/*
 * Replace an item in an Address/Length List.  Cursor is updated so
 * that alenlist_get will get the next item in the list.  This interface 
 * is not very useful for drivers; but it is useful to bus providers 
 * that need to translate between address spaced in situ.  The old Address
 * and Length are returned.
 */
/* ARGSUSED */
int
alenlist_replace(	alenlist_t alenlist, 		/* in: replace in this list */
			alenlist_cursor_t cursorp, 	/* inout: which item to replace */
			alenaddr_t *addrp, 		/* inout: address */
			size_t *lengthp,		/* inout: length */
			unsigned flags)
{
	alen_t *alenp;
	alenlist_chunk_t chunk;
	unsigned int index;
	size_t length;
	alenaddr_t addr;

	if ((addrp == NULL) || (lengthp == NULL))
		return(ALENLIST_FAILURE);

	if (alenlist->al_logical_size == 0)
		return(ALENLIST_FAILURE);

	addr = *addrp;
	length = *lengthp;

	/* 
	 * If no cursor explicitly specified, use the Address/Length List's 
	 * internal cursor.
	 */
	if (cursorp == NULL)
		cursorp = &alenlist->al_cursor;

	chunk = cursorp->al_chunk;
	index = cursorp->al_index;

	ASSERT(cursorp->al_alenlist == alenlist);
	if (cursorp->al_alenlist != alenlist)
		return(ALENLIST_FAILURE);

	alenp = &chunk->alc_pair[index];

	/* Return old values */
	*addrp = alenp->al_length;
	*lengthp = alenp->al_addr;

	/* Set up new values */
	alenp->al_length = length;
	alenp->al_addr = addr;

	/* Update cursor to point to next item */
	cursorp->al_bcount = length;

	return(ALENLIST_SUCCESS);
}


/*
 * Initialize a cursor in order to walk an alenlist.
 * An alenlist_cursor always points to the last thing that was obtained
 * from the list.  If al_chunk is NULL, then nothing has yet been obtained.
 *
 * Note: There is an "internal cursor" associated with every Address/Length List.
 * For users that scan sequentially through a List, it is more efficient to
 * simply use the internal cursor.  The caller must insure that no other users
 * will simultaneously scan the List.  The caller can reposition the internal
 * cursor by calling alenlist_cursor_init with a NULL cursorp.
 */
int
alenlist_cursor_init(alenlist_t alenlist, size_t offset, alenlist_cursor_t cursorp)
{
	size_t byte_count;

	if (cursorp == NULL)
		cursorp = &alenlist->al_cursor;

	/* Get internal cursor's byte count for use as a hint.
	 *
	 * If the internal cursor points passed the point that we're interested in,
	 * we need to seek forward from the beginning.  Otherwise, we can seek forward
	 * from the internal cursor.
	 */
	if ((offset > 0) &&
	   ((byte_count = alenlist_cursor_offset(alenlist, (alenlist_cursor_t)NULL)) <= offset)) {
		offset -= byte_count;
		alenlist_cursor_clone(alenlist, NULL, cursorp);
	} else
		do_cursor_init(alenlist, cursorp);

	/* We could easily speed this up, but it shouldn't be used very often. */
	while (offset != 0) {
		alenaddr_t addr;
		size_t length;

		if (alenlist_get(alenlist, cursorp, offset, &addr, &length, 0) != ALENLIST_SUCCESS)
			return(ALENLIST_FAILURE);
		offset -= length;
	}
	return(ALENLIST_SUCCESS);
}


/*
 * Copy a cursor.  The source cursor is either an internal alenlist cursor
 * or an explicit cursor.
 */
int
alenlist_cursor_clone(	alenlist_t alenlist, 
			alenlist_cursor_t cursorp_in, 
			alenlist_cursor_t cursorp_out)
{
	ASSERT(cursorp_out);

	if (alenlist && cursorp_in)
		if (alenlist != cursorp_in->al_alenlist)
			return(ALENLIST_FAILURE);

	if (alenlist)
		*cursorp_out = alenlist->al_cursor; /* small structure copy */
	else if (cursorp_in)
		*cursorp_out = *cursorp_in; /* small structure copy */
	else
		return(ALENLIST_FAILURE); /* no source */

	return(ALENLIST_SUCCESS);
}

/*
 * Return the number of bytes passed so far according to the specified cursor.
 * If cursorp is NULL, use the alenlist's internal cursor.
 */
size_t
alenlist_cursor_offset(alenlist_t alenlist, alenlist_cursor_t cursorp)
{
	ASSERT(!alenlist || !cursorp || (alenlist == cursorp->al_alenlist));

	if (cursorp == NULL) {
		ASSERT(alenlist);
		cursorp = &alenlist->al_cursor;
	}

	return(cursorp->al_offset);
}

/*
 * Allocate and initialize an Address/Length List cursor.
 */
alenlist_cursor_t
alenlist_cursor_create(alenlist_t alenlist, unsigned flags)
{
	alenlist_cursor_t cursorp;

	ASSERT(alenlist != NULL);
	cursorp = snia_kmem_zone_alloc(alenlist_cursor_zone, flags & AL_NOSLEEP ? VM_NOSLEEP : 0);
	if (cursorp) {
		INCR_COUNT(&alenlist_cursor_count);
		alenlist_cursor_init(alenlist, 0, cursorp);
	}
	return(cursorp);
}

/*
 * Free an Address/Length List cursor.
 */
void
alenlist_cursor_destroy(alenlist_cursor_t cursorp)
{
	DECR_COUNT(&alenlist_cursor_count);
	snia_kmem_zone_free(alenlist_cursor_zone, cursorp);
}


/*
 * Fetch an address/length pair from an Address/Length List.  Update
 * the "cursor" so that next time this routine is called, we'll get
 * the next address range.  Never return a length that exceeds maxlength
 * (if non-zero).  If maxlength is a power of 2, never return a length 
 * that crosses a maxlength boundary.  [This may seem strange at first,
 * but it's what many drivers want.]
 *
 * Returns: SUCCESS/FAILURE
 */
int
alenlist_get(	alenlist_t alenlist, 		/* in: get from this list */
		alenlist_cursor_t cursorp, 	/* inout: which item to get */
		size_t	maxlength,		/* in: at most this length */
		alenaddr_t *addrp, 		/* out: address */
		size_t *lengthp,		/* out: length */
		unsigned flags)
{
	alen_t *alenp;
	alenlist_chunk_t chunk;
	unsigned int index;
	size_t bcount;
	size_t length;

	/* 
	 * If no cursor explicitly specified, use the Address/Length List's 
	 * internal cursor.
	 */
	if (cursorp == NULL) {
		if (alenlist->al_logical_size == 0)
			return(ALENLIST_FAILURE);
		cursorp = &alenlist->al_cursor;
	}

	chunk = cursorp->al_chunk;
	index = cursorp->al_index;
	bcount = cursorp->al_bcount;

	ASSERT(cursorp->al_alenlist == alenlist);
	if (cursorp->al_alenlist != alenlist)
		return(ALENLIST_FAILURE);

	alenp = &chunk->alc_pair[index];
	length = alenp->al_length - bcount;

	/* Bump up to next pair, if we're done with this pair. */
	if (length == 0) {
		cursorp->al_bcount = bcount = 0;
		cursorp->al_index = index = (index + 1) % ALEN_CHUNK_SZ;

		/* Bump up to next chunk, if we're done with this chunk. */
		if (index == 0) {
			if (cursorp->al_chunk == alenlist->al_last_chunk)
				return(ALENLIST_FAILURE);
			chunk = chunk->alc_next;
			ASSERT(chunk != NULL);
		} else {
			/* If in last chunk, don't go beyond end. */
			if (cursorp->al_chunk == alenlist->al_last_chunk) {
				int last_size = alenlist->al_logical_size % ALEN_CHUNK_SZ;
				if (last_size && (index >= last_size))
					return(ALENLIST_FAILURE);
			}
		}

		alenp = &chunk->alc_pair[index];
		length = alenp->al_length;
	}

	/* Constrain what we return according to maxlength */
	if (maxlength) {
		size_t maxlen1 = maxlength - 1;

		if ((maxlength & maxlen1) == 0) /* power of 2 */
			maxlength -= 
			   ((alenp->al_addr + cursorp->al_bcount) & maxlen1);

		length = min(maxlength, length);
	}

	/* Update the cursor, if desired. */
	if (!(flags & AL_LEAVE_CURSOR)) {
		cursorp->al_bcount += length;
		cursorp->al_chunk = chunk;
	}

	*lengthp = length;
	*addrp = alenp->al_addr + bcount;

	return(ALENLIST_SUCCESS);
}


/*
 * Return the number of pairs in the specified Address/Length List.
 * (For FIXED_SIZE Lists, this returns the logical size of the List, 
 * not the actual capacity of the List.)
 */
int
alenlist_size(alenlist_t alenlist)
{
	return(alenlist->al_logical_size);
}


/*
 * Concatenate two Address/Length Lists.
 */
void
alenlist_concat(alenlist_t from,
		alenlist_t to)
{
	struct alenlist_cursor_s cursor;
	alenaddr_t addr;
	size_t length;

	alenlist_cursor_init(from, 0, &cursor);

	while(alenlist_get(from, &cursor, (size_t)0, &addr, &length, 0) == ALENLIST_SUCCESS)
		alenlist_append(to, addr, length, 0);
}

/*
 * Create a copy of a list.
 * (Not all attributes of the old list are cloned.  For instance, if
 * a FIXED_SIZE list is cloned, the resulting list is NOT FIXED_SIZE.)
 */
alenlist_t
alenlist_clone(alenlist_t old_list, unsigned flags)
{
	alenlist_t new_list;

	new_list = alenlist_create(flags);
	if (new_list != NULL)
		alenlist_concat(old_list, new_list);

	return(new_list);
}


/* 
 * Convert a kernel virtual address to a Physical Address/Length List.
 */
alenlist_t
kvaddr_to_alenlist(alenlist_t alenlist, caddr_t kvaddr, size_t length, unsigned flags)
{
	alenaddr_t paddr;
	long offset;
	size_t piece_length;
	int created_alenlist;

	if (length <=0)
		return(NULL);

	/* If caller supplied a List, use it.  Otherwise, allocate one. */
	if (alenlist == NULL) {
		alenlist = alenlist_create(0);
		created_alenlist = 1;
	} else {
		alenlist_clear(alenlist);
		created_alenlist = 0;
	}

	paddr = kvtophys(kvaddr);
	offset = poff(kvaddr);

	/* Handle first page */
	piece_length = min((size_t)(NBPP - offset), length);
	if (alenlist_append(alenlist, paddr, piece_length, flags) == ALENLIST_FAILURE)
		goto failure;
	length -= piece_length;
	kvaddr += piece_length;

	/* Handle middle pages */
	while (length >= NBPP) {
		paddr = kvtophys(kvaddr);
		if (alenlist_append(alenlist, paddr, NBPP, flags) == ALENLIST_FAILURE)
			goto failure;
		length -= NBPP;
		kvaddr += NBPP;
	}

	/* Handle last page */
	if (length) {
		ASSERT(length < NBPP);
		paddr = kvtophys(kvaddr);
		if (alenlist_append(alenlist, paddr, length, flags) == ALENLIST_FAILURE)
			goto failure;
	}

	alenlist_cursor_init(alenlist, 0, NULL);
	return(alenlist);

failure:
	if (created_alenlist)
		alenlist_destroy(alenlist);
	return(NULL);
}


#if DEBUG
static void
alenlist_show(alenlist_t alenlist)
{
	struct alenlist_cursor_s cursor;
	alenaddr_t addr;
	size_t length;
	int i = 0;

	alenlist_cursor_init(alenlist, 0, &cursor);

	qprintf("Address/Length List@0x%x:\n", alenlist);
	qprintf("logical size=0x%x actual size=0x%x last_chunk at 0x%x\n", 
		alenlist->al_logical_size, alenlist->al_actual_size, 
		alenlist->al_last_chunk);
	qprintf("cursor: chunk=0x%x index=%d offset=0x%x\n",
		alenlist->al_cursor.al_chunk, 
		alenlist->al_cursor.al_index,
		alenlist->al_cursor.al_bcount);
	while(alenlist_get(alenlist, &cursor, (size_t)0, &addr, &length, 0) == ALENLIST_SUCCESS)
		qprintf("%d:\t0x%lx 0x%lx\n", ++i, addr, length);
}
#endif /* DEBUG */