aboutsummaryrefslogtreecommitdiffstats
path: root/fs/binfmt_flat.c
blob: 33764fd6db66122ff36b52fba3ebf95f26d4a5d7 (plain) (blame)
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
/****************************************************************************/
/*
 *  linux/fs/binfmt_flat.c
 *
 *	Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com>
 *	Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
 *	Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com>
 *	Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com>
 *  based heavily on:
 *
 *  linux/fs/binfmt_aout.c:
 *      Copyright (C) 1991, 1992, 1996  Linus Torvalds
 *  linux/fs/binfmt_flat.c for 2.0 kernel
 *	    Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>
 *	JAN/99 -- coded full program relocation (gerg@snapgear.com)
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/a.out.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/stat.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/user.h>
#include <linux/slab.h>
#include <linux/binfmts.h>
#include <linux/personality.h>
#include <linux/init.h>
#include <linux/flat.h>
#include <linux/syscalls.h>

#include <asm/byteorder.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/unaligned.h>
#include <asm/cacheflush.h>

/****************************************************************************/

#if 0
#define DEBUG 1
#endif

#ifdef DEBUG
#define	DBG_FLT(a...)	printk(a)
#else
#define	DBG_FLT(a...)
#endif

#define RELOC_FAILED 0xff00ff01		/* Relocation incorrect somewhere */
#define UNLOADED_LIB 0x7ff000ff		/* Placeholder for unused library */

struct lib_info {
	struct {
		unsigned long start_code;		/* Start of text segment */
		unsigned long start_data;		/* Start of data segment */
		unsigned long start_brk;		/* End of data segment */
		unsigned long text_len;			/* Length of text segment */
		unsigned long entry;			/* Start address for this module */
		unsigned long build_date;		/* When this one was compiled */
		short loaded;				/* Has this library been loaded? */
	} lib_list[MAX_SHARED_LIBS];
};

#ifdef CONFIG_BINFMT_SHARED_FLAT
static int load_flat_shared_library(int id, struct lib_info *p);
#endif

static int load_flat_binary(struct linux_binprm *, struct pt_regs * regs);
static int flat_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);

static struct linux_binfmt flat_format = {
	.module		= THIS_MODULE,
	.load_binary	= load_flat_binary,
	.core_dump	= flat_core_dump,
	.min_coredump	= PAGE_SIZE
};

/****************************************************************************/
/*
 * Routine writes a core dump image in the current directory.
 * Currently only a stub-function.
 */

static int flat_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
{
	printk("Process %s:%d received signr %d and should have core dumped\n",
			current->comm, current->pid, (int) signr);
	return(1);
}

/****************************************************************************/
/*
 * create_flat_tables() parses the env- and arg-strings in new user
 * memory and creates the pointer tables from them, and puts their
 * addresses on the "stack", returning the new stack pointer value.
 */

static unsigned long create_flat_tables(
	unsigned long pp,
	struct linux_binprm * bprm)
{
	unsigned long *argv,*envp;
	unsigned long * sp;
	char * p = (char*)pp;
	int argc = bprm->argc;
	int envc = bprm->envc;
	char uninitialized_var(dummy);

	sp = (unsigned long *) ((-(unsigned long)sizeof(char *))&(unsigned long) p);

	sp -= envc+1;
	envp = sp;
	sp -= argc+1;
	argv = sp;

	flat_stack_align(sp);
	if (flat_argvp_envp_on_stack()) {
		--sp; put_user((unsigned long) envp, sp);
		--sp; put_user((unsigned long) argv, sp);
	}

	put_user(argc,--sp);
	current->mm->arg_start = (unsigned long) p;
	while (argc-->0) {
		put_user((unsigned long) p, argv++);
		do {
			get_user(dummy, p); p++;
		} while (dummy);
	}
	put_user((unsigned long) NULL, argv);
	current->mm->arg_end = current->mm->env_start = (unsigned long) p;
	while (envc-->0) {
		put_user((unsigned long)p, envp); envp++;
		do {
			get_user(dummy, p); p++;
		} while (dummy);
	}
	put_user((unsigned long) NULL, envp);
	current->mm->env_end = (unsigned long) p;
	return (unsigned long)sp;
}

/****************************************************************************/

#ifdef CONFIG_BINFMT_ZFLAT

#include <linux/zlib.h>

#define LBUFSIZE	4000

/* gzip flag byte */
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define COMMENT      0x10 /* bit 4 set: file comment present */
#define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
#define RESERVED     0xC0 /* bit 6,7:   reserved */

static int decompress_exec(
	struct linux_binprm *bprm,
	unsigned long offset,
	char *dst,
	long len,
	int fd)
{
	unsigned char *buf;
	z_stream strm;
	loff_t fpos;
	int ret, retval;

	DBG_FLT("decompress_exec(offset=%x,buf=%x,len=%x)\n",(int)offset, (int)dst, (int)len);

	memset(&strm, 0, sizeof(strm));
	strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
	if (strm.workspace == NULL) {
		DBG_FLT("binfmt_flat: no memory for decompress workspace\n");
		return -ENOMEM;
	}
	buf = kmalloc(LBUFSIZE, GFP_KERNEL);
	if (buf == NULL) {
		DBG_FLT("binfmt_flat: no memory for read buffer\n");
		retval = -ENOMEM;
		goto out_free;
	}

	/* Read in first chunk of data and parse gzip header. */
	fpos = offset;
	ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);

	strm.next_in = buf;
	strm.avail_in = ret;
	strm.total_in = 0;

	retval = -ENOEXEC;

	/* Check minimum size -- gzip header */
	if (ret < 10) {
		DBG_FLT("binfmt_flat: file too small?\n");
		goto out_free_buf;
	}

	/* Check gzip magic number */
	if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) {
		DBG_FLT("binfmt_flat: unknown compression magic?\n");
		goto out_free_buf;
	}

	/* Check gzip method */
	if (buf[2] != 8) {
		DBG_FLT("binfmt_flat: unknown compression method?\n");
		goto out_free_buf;
	}
	/* Check gzip flags */
	if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) ||
	    (buf[3] & RESERVED)) {
		DBG_FLT("binfmt_flat: unknown flags?\n");
		goto out_free_buf;
	}

	ret = 10;
	if (buf[3] & EXTRA_FIELD) {
		ret += 2 + buf[10] + (buf[11] << 8);
		if (unlikely(LBUFSIZE == ret)) {
			DBG_FLT("binfmt_flat: buffer overflow (EXTRA)?\n");
			goto out_free_buf;
		}
	}
	if (buf[3] & ORIG_NAME) {
		for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
			;
		if (unlikely(LBUFSIZE == ret)) {
			DBG_FLT("binfmt_flat: buffer overflow (ORIG_NAME)?\n");
			goto out_free_buf;
		}
	}
	if (buf[3] & COMMENT) {
		for (;  ret < LBUFSIZE && (buf[ret] != 0); ret++)
			;
		if (unlikely(LBUFSIZE == ret)) {
			DBG_FLT("binfmt_flat: buffer overflow (COMMENT)?\n");
			goto out_free_buf;
		}
	}

	strm.next_in += ret;
	strm.avail_in -= ret;

	strm.next_out = dst;
	strm.avail_out = len;
	strm.total_out = 0;

	if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
		DBG_FLT("binfmt_flat: zlib init failed?\n");
		goto out_free_buf;
	}

	while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
		if (ret <= 0)
			break;
		if (ret >= (unsigned long) -4096)
			break;
		len -= ret;

		strm.next_in = buf;
		strm.avail_in = ret;
		strm.total_in = 0;
	}

	if (ret < 0) {
		DBG_FLT("binfmt_flat: decompression failed (%d), %s\n",
			ret, strm.msg);
		goto out_zlib;
	}

	retval = 0;
out_zlib:
	zlib_inflateEnd(&strm);
out_free_buf:
	kfree(buf);
out_free:
	kfree(strm.workspace);
	return retval;
}

#endif /* CONFIG_BINFMT_ZFLAT */

/****************************************************************************/

static unsigned long
calc_reloc(unsigned long r, struct lib_info *p, int curid, int internalp)
{
	unsigned long addr;
	int id;
	unsigned long start_brk;
	unsigned long start_data;
	unsigned long text_len;
	unsigned long start_code;

#ifdef CONFIG_BINFMT_SHARED_FLAT
	if (r == 0)
		id = curid;	/* Relocs of 0 are always self referring */
	else {
		id = (r >> 24) & 0xff;	/* Find ID for this reloc */
		r &= 0x00ffffff;	/* Trim ID off here */
	}
	if (id >= MAX_SHARED_LIBS) {
		printk("BINFMT_FLAT: reference 0x%x to shared library %d",
				(unsigned) r, id);
		goto failed;
	}
	if (curid != id) {
		if (internalp) {
			printk("BINFMT_FLAT: reloc address 0x%x not in same module "
					"(%d != %d)", (unsigned) r, curid, id);
			goto failed;
		} else if ( ! p->lib_list[id].loaded &&
				load_flat_shared_library(id, p) > (unsigned long) -4096) {
			printk("BINFMT_FLAT: failed to load library %d", id);
			goto failed;
		}
		/* Check versioning information (i.e. time stamps) */
		if (p->lib_list[id].build_date && p->lib_list[curid].build_date &&
				p->lib_list[curid].build_date < p->lib_list[id].build_date) {
			printk("BINFMT_FLAT: library %d is younger than %d", id, curid);
			goto failed;
		}
	}
#else
	id = 0;
#endif

	start_brk = p->lib_list[id].start_brk;
	start_data = p->lib_list[id].start_data;
	start_code = p->lib_list[id].start_code;
	text_len = p->lib_list[id].text_len;

	if (!flat_reloc_valid(r, start_brk - start_data + text_len)) {
		printk("BINFMT_FLAT: reloc outside program 0x%x (0 - 0x%x/0x%x)",
		       (int) r,(int)(start_brk-start_code),(int)text_len);
		goto failed;
	}

	if (r < text_len)			/* In text segment */
		addr = r + start_code;
	else					/* In data segment */
		addr = r - text_len + start_data;

	/* Range checked already above so doing the range tests is redundant...*/
	return(addr);

failed:
	printk(", killing %s!\n", current->comm);
	send_sig(SIGSEGV, current, 0);

	return RELOC_FAILED;
}

/****************************************************************************/

void old_reloc(unsigned long rl)
{
#ifdef DEBUG
	char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
#endif
	flat_v2_reloc_t	r;
	unsigned long *ptr;
	
	r.value = rl;
#if defined(CONFIG_COLDFIRE)
	ptr = (unsigned long *) (current->mm->start_code + r.reloc.offset);
#else
	ptr = (unsigned long *) (current->mm->start_data + r.reloc.offset);
#endif

#ifdef DEBUG
	printk("Relocation of variable at DATASEG+%x "
		"(address %p, currently %x) into segment %s\n",
		r.reloc.offset, ptr, (int)*ptr, segment[r.reloc.type]);
#endif
	
	switch (r.reloc.type) {
	case OLD_FLAT_RELOC_TYPE_TEXT:
		*ptr += current->mm->start_code;
		break;
	case OLD_FLAT_RELOC_TYPE_DATA:
		*ptr += current->mm->start_data;
		break;
	case OLD_FLAT_RELOC_TYPE_BSS:
		*ptr += current->mm->end_data;
		break;
	default:
		printk("BINFMT_FLAT: Unknown relocation type=%x\n", r.reloc.type);
		break;
	}

#ifdef DEBUG
	printk("Relocation became %x\n", (int)*ptr);
#endif
}		

/****************************************************************************/

static int load_flat_file(struct linux_binprm * bprm,
		struct lib_info *libinfo, int id, unsigned long *extra_stack)
{
	struct flat_hdr * hdr;
	unsigned long textpos = 0, datapos = 0, result;
	unsigned long realdatastart = 0;
	unsigned long text_len, data_len, bss_len, stack_len, flags;
	unsigned long len, reallen, memp = 0;
	unsigned long extra, rlim;
	unsigned long *reloc = 0, *rp;
	struct inode *inode;
	int i, rev, relocs = 0;
	loff_t fpos;
	unsigned long start_code, end_code;
	int ret;

	hdr = ((struct flat_hdr *) bprm->buf);		/* exec-header */
	inode = bprm->file->f_path.dentry->d_inode;

	text_len  = ntohl(hdr->data_start);
	data_len  = ntohl(hdr->data_end) - ntohl(hdr->data_start);
	bss_len   = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
	stack_len = ntohl(hdr->stack_size);
	if (extra_stack) {
		stack_len += *extra_stack;
		*extra_stack = stack_len;
	}
	relocs    = ntohl(hdr->reloc_count);
	flags     = ntohl(hdr->flags);
	rev       = ntohl(hdr->rev);

	if (strncmp(hdr->magic, "bFLT", 4)) {
		/*
		 * because a lot of people do not manage to produce good
		 * flat binaries,  we leave this printk to help them realise
		 * the problem.  We only print the error if its not a script file
		 */
		if (strncmp(hdr->magic, "#!", 2))
			printk("BINFMT_FLAT: bad header magic\n");
		ret = -ENOEXEC;
		goto err;
	}

	if (flags & FLAT_FLAG_KTRACE)
		printk("BINFMT_FLAT: Loading file: %s\n", bprm->filename);

	if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
		printk("BINFMT_FLAT: bad flat file version 0x%x (supported "
			"0x%lx and 0x%lx)\n",
			rev, FLAT_VERSION, OLD_FLAT_VERSION);
		ret = -ENOEXEC;
		goto err;
	}
	
	/* Don't allow old format executables to use shared libraries */
	if (rev == OLD_FLAT_VERSION && id != 0) {
		printk("BINFMT_FLAT: shared libraries are not available before rev 0x%x\n",
				(int) FLAT_VERSION);
		ret = -ENOEXEC;
		goto err;
	}

	/*
	 * fix up the flags for the older format,  there were all kinds
	 * of endian hacks,  this only works for the simple cases
	 */
	if (rev == OLD_FLAT_VERSION && flat_old_ram_flag(flags))
		flags = FLAT_FLAG_RAM;

#ifndef CONFIG_BINFMT_ZFLAT
	if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
		printk("Support for ZFLAT executables is not enabled.\n");
		ret = -ENOEXEC;
		goto err;
	}
#endif

	/*
	 * Check initial limits. This avoids letting people circumvent
	 * size limits imposed on them by creating programs with large
	 * arrays in the data or bss.
	 */
	rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
	if (rlim >= RLIM_INFINITY)
		rlim = ~0;
	if (data_len + bss_len > rlim) {
		ret = -ENOMEM;
		goto err;
	}

	/* Flush all traces of the currently running executable */
	if (id == 0) {
		result = flush_old_exec(bprm);
		if (result) {
			ret = result;
			goto err;
		}

		/* OK, This is the point of no return */
		set_personality(PER_LINUX_32BIT);
	}

	/*
	 * calculate the extra space we need to map in
	 */
	extra = max_t(unsigned long, bss_len + stack_len,
			relocs * sizeof(unsigned long));

	/*
	 * there are a couple of cases here,  the separate code/data
	 * case,  and then the fully copied to RAM case which lumps
	 * it all together.
	 */
	if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) {
		/*
		 * this should give us a ROM ptr,  but if it doesn't we don't
		 * really care
		 */
		DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");

		down_write(&current->mm->mmap_sem);
		textpos = do_mmap(bprm->file, 0, text_len, PROT_READ|PROT_EXEC, MAP_PRIVATE, 0);
		up_write(&current->mm->mmap_sem);
		if (!textpos  || textpos >= (unsigned long) -4096) {
			if (!textpos)
				textpos = (unsigned long) -ENOMEM;
			printk("Unable to mmap process text, errno %d\n", (int)-textpos);
			ret = textpos;
			goto err;
		}

		len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
		down_write(&current->mm->mmap_sem);
		realdatastart = do_mmap(0, 0, len,
			PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
		/* Remap to use all availabe slack region space */
		if (realdatastart && (realdatastart < (unsigned long)-4096)) {
			reallen = ksize((void *)realdatastart);
			if (reallen > len) {
				realdatastart = do_mremap(realdatastart, len,
					reallen, MREMAP_FIXED, realdatastart);
			}
		}
		up_write(&current->mm->mmap_sem);

		if (realdatastart == 0 || realdatastart >= (unsigned long)-4096) {
			if (!realdatastart)
				realdatastart = (unsigned long) -ENOMEM;
			printk("Unable to allocate RAM for process data, errno %d\n",
					(int)-realdatastart);
			do_munmap(current->mm, textpos, text_len);
			ret = realdatastart;
			goto err;
		}
		datapos = realdatastart + MAX_SHARED_LIBS * sizeof(unsigned long);

		DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
				(int)(data_len + bss_len + stack_len), (int)datapos);

		fpos = ntohl(hdr->data_start);
#ifdef CONFIG_BINFMT_ZFLAT
		if (flags & FLAT_FLAG_GZDATA) {
			result = decompress_exec(bprm, fpos, (char *) datapos, 
						 data_len + (relocs * sizeof(unsigned long)), 0);
		} else
#endif
		{
			result = bprm->file->f_op->read(bprm->file, (char *) datapos,
					data_len + (relocs * sizeof(unsigned long)), &fpos);
		}
		if (result >= (unsigned long)-4096) {
			printk("Unable to read data+bss, errno %d\n", (int)-result);
			do_munmap(current->mm, textpos, text_len);
			do_munmap(current->mm, realdatastart, data_len + extra);
			ret = result;
			goto err;
		}

		reloc = (unsigned long *) (datapos+(ntohl(hdr->reloc_start)-text_len));
		memp = realdatastart;

	} else {

		len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
		down_write(&current->mm->mmap_sem);
		textpos = do_mmap(0, 0, len,
			PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
		/* Remap to use all availabe slack region space */
		if (textpos && (textpos < (unsigned long) -4096)) {
			reallen = ksize((void *)textpos);
			if (reallen > len) {
				textpos = do_mremap(textpos, len, reallen,
					MREMAP_FIXED, textpos);
			}
		}
		up_write(&current->mm->mmap_sem);

		if (!textpos  || textpos >= (unsigned long) -4096) {
			if (!textpos)
				textpos = (unsigned long) -ENOMEM;
			printk("Unable to allocate RAM for process text/data, errno %d\n",
					(int)-textpos);
			ret = textpos;
			goto err;
		}

		realdatastart = textpos + ntohl(hdr->data_start);
		datapos = realdatastart + MAX_SHARED_LIBS * sizeof(unsigned long);
		reloc = (unsigned long *) (textpos + ntohl(hdr->reloc_start) +
				MAX_SHARED_LIBS * sizeof(unsigned long));
		memp = textpos;

#ifdef CONFIG_BINFMT_ZFLAT
		/*
		 * load it all in and treat it like a RAM load from now on
		 */
		if (flags & FLAT_FLAG_GZIP) {
			result = decompress_exec(bprm, sizeof (struct flat_hdr),
					 (((char *) textpos) + sizeof (struct flat_hdr)),
					 (text_len + data_len + (relocs * sizeof(unsigned long))
						  - sizeof (struct flat_hdr)),
					 0);
			memmove((void *) datapos, (void *) realdatastart,
					data_len + (relocs * sizeof(unsigned long)));
		} else if (flags & FLAT_FLAG_GZDATA) {
			fpos = 0;
			result = bprm->file->f_op->read(bprm->file,
					(char *) textpos, text_len, &fpos);
			if (result < (unsigned long) -4096)
				result = decompress_exec(bprm, text_len, (char *) datapos,
						 data_len + (relocs * sizeof(unsigned long)), 0);
		}
		else
#endif
		{
			fpos = 0;
			result = bprm->file->f_op->read(bprm->file,
					(char *) textpos, text_len, &fpos);
			if (result < (unsigned long) -4096) {
				fpos = ntohl(hdr->data_start);
				result = bprm->file->f_op->read(bprm->file, (char *) datapos,
					data_len + (relocs * sizeof(unsigned long)), &fpos);
			}
		}
		if (result >= (unsigned long)-4096) {
			printk("Unable to read code+data+bss, errno %d\n",(int)-result);
			do_munmap(current->mm, textpos, text_len + data_len + extra +
				MAX_SHARED_LIBS * sizeof(unsigned long));
			ret = result;
			goto err;
		}
	}

	if (flags & FLAT_FLAG_KTRACE)
		printk("Mapping is %x, Entry point is %x, data_start is %x\n",
			(int)textpos, 0x00ffffff&ntohl(hdr->entry), ntohl(hdr->data_start));

	/* The main program needs a little extra setup in the task structure */
	start_code = textpos + sizeof (struct flat_hdr);
	end_code = textpos + text_len;
	if (id == 0) {
		current->mm->start_code = start_code;
		current->mm->end_code = end_code;
		current->mm->start_data = datapos;
		current->mm->end_data = datapos + data_len;
		/*
		 * set up the brk stuff, uses any slack left in data/bss/stack
		 * allocation.  We put the brk after the bss (between the bss
		 * and stack) like other platforms.
		 */
		current->mm->start_brk = datapos + data_len + bss_len;
		current->mm->brk = (current->mm->start_brk + 3) & ~3;
		current->mm->context.end_brk = memp + ksize((void *) memp) - stack_len;
	}

	if (flags & FLAT_FLAG_KTRACE)
		printk("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
			id ? "Lib" : "Load", bprm->filename,
			(int) start_code, (int) end_code,
			(int) datapos,
			(int) (datapos + data_len),
			(int) (datapos + data_len),
			(int) (((datapos + data_len + bss_len) + 3) & ~3));

	text_len -= sizeof(struct flat_hdr); /* the real code len */

	/* Store the current module values into the global library structure */
	libinfo->lib_list[id].start_code = start_code;
	libinfo->lib_list[id].start_data = datapos;
	libinfo->lib_list[id].start_brk = datapos + data_len + bss_len;
	libinfo->lib_list[id].text_len = text_len;
	libinfo->lib_list[id].loaded = 1;
	libinfo->lib_list[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
	libinfo->lib_list[id].build_date = ntohl(hdr->build_date);
	
	/*
	 * We just load the allocations into some temporary memory to
	 * help simplify all this mumbo jumbo
	 *
	 * We've got two different sections of relocation entries.
	 * The first is the GOT which resides at the begining of the data segment
	 * and is terminated with a -1.  This one can be relocated in place.
	 * The second is the extra relocation entries tacked after the image's
	 * data segment. These require a little more processing as the entry is
	 * really an offset into the image which contains an offset into the
	 * image.
	 */
	if (flags & FLAT_FLAG_GOTPIC) {
		for (rp = (unsigned long *)datapos; *rp != 0xffffffff; rp++) {
			unsigned long addr;
			if (*rp) {
				addr = calc_reloc(*rp, libinfo, id, 0);
				if (addr == RELOC_FAILED) {
					ret = -ENOEXEC;
					goto err;
				}
				*rp = addr;
			}
		}
	}

	/*
	 * Now run through the relocation entries.
	 * We've got to be careful here as C++ produces relocatable zero
	 * entries in the constructor and destructor tables which are then
	 * tested for being not zero (which will always occur unless we're
	 * based from address zero).  This causes an endless loop as __start
	 * is at zero.  The solution used is to not relocate zero addresses.
	 * This has the negative side effect of not allowing a global data
	 * reference to be statically initialised to _stext (I've moved
	 * __start to address 4 so that is okay).
	 */
	if (rev > OLD_FLAT_VERSION) {
		unsigned long persistent = 0;
		for (i=0; i < relocs; i++) {
			unsigned long addr, relval;

			/* Get the address of the pointer to be
			   relocated (of course, the address has to be
			   relocated first).  */
			relval = ntohl(reloc[i]);
			if (flat_set_persistent (relval, &persistent))
				continue;
			addr = flat_get_relocate_addr(relval);
			rp = (unsigned long *) calc_reloc(addr, libinfo, id, 1);
			if (rp == (unsigned long *)RELOC_FAILED) {
				ret = -ENOEXEC;
				goto err;
			}

			/* Get the pointer's value.  */
			addr = flat_get_addr_from_rp(rp, relval, flags,
							&persistent);
			if (addr != 0) {
				/*
				 * Do the relocation.  PIC relocs in the data section are
				 * already in target order
				 */
				if ((flags & FLAT_FLAG_GOTPIC) == 0)
					addr = ntohl(addr);
				addr = calc_reloc(addr, libinfo, id, 0);
				if (addr == RELOC_FAILED) {
					ret = -ENOEXEC;
					goto err;
				}

				/* Write back the relocated pointer.  */
				flat_put_addr_at_rp(rp, addr, relval);
			}
		}
	} else {
		for (i=0; i < relocs; i++)
			old_reloc(ntohl(reloc[i]));
	}
	
	flush_icache_range(start_code, end_code);

	/* zero the BSS,  BRK and stack areas */
	memset((void*)(datapos + data_len), 0, bss_len + 
			(memp + ksize((void *) memp) - stack_len -	/* end brk */
			libinfo->lib_list[id].start_brk) +		/* start brk */
			stack_len);

	return 0;
err:
	return ret;
}


/****************************************************************************/
#ifdef CONFIG_BINFMT_SHARED_FLAT

/*
 * Load a shared library into memory.  The library gets its own data
 * segment (including bss) but not argv/argc/environ.
 */

static int load_flat_shared_library(int id, struct lib_info *libs)
{
	struct linux_binprm bprm;
	int res;
	char buf[16];

	/* Create the file name */
	sprintf(buf, "/lib/lib%d.so", id);

	/* Open the file up */
	bprm.filename = buf;
	bprm.file = open_exec(bprm.filename);
	res = PTR_ERR(bprm.file);
	if (IS_ERR(bprm.file))
		return res;

	res = prepare_binprm(&bprm);

	if (res <= (unsigned long)-4096)
		res = load_flat_file(&bprm, libs, id, NULL);
	if (bprm.file) {
		allow_write_access(bprm.file);
		fput(bprm.file);
		bprm.file = NULL;
	}
	return(res);
}

#endif /* CONFIG_BINFMT_SHARED_FLAT */
/****************************************************************************/

/*
 * These are the functions used to load flat style executables and shared
 * libraries.  There is no binary dependent code anywhere else.
 */

static int load_flat_binary(struct linux_binprm * bprm, struct pt_regs * regs)
{
	struct lib_info libinfo;
	unsigned long p = bprm->p;
	unsigned long stack_len;
	unsigned long start_addr;
	unsigned long *sp;
	int res;
	int i, j;

	memset(&libinfo, 0, sizeof(libinfo));
	/*
	 * We have to add the size of our arguments to our stack size
	 * otherwise it's too easy for users to create stack overflows
	 * by passing in a huge argument list.  And yes,  we have to be
	 * pedantic and include space for the argv/envp array as it may have
	 * a lot of entries.
	 */
#define TOP_OF_ARGS (PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *))
	stack_len = TOP_OF_ARGS - bprm->p;             /* the strings */
	stack_len += (bprm->argc + 1) * sizeof(char *); /* the argv array */
	stack_len += (bprm->envc + 1) * sizeof(char *); /* the envp array */

	
	res = load_flat_file(bprm, &libinfo, 0, &stack_len);
	if (res > (unsigned long)-4096)
		return res;
	
	/* Update data segment pointers for all libraries */
	for (i=0; i<MAX_SHARED_LIBS; i++)
		if (libinfo.lib_list[i].loaded)
			for (j=0; j<MAX_SHARED_LIBS; j++)
				(-(j+1))[(unsigned long *)(libinfo.lib_list[i].start_data)] =
					(libinfo.lib_list[j].loaded)?
						libinfo.lib_list[j].start_data:UNLOADED_LIB;

	compute_creds(bprm);
 	current->flags &= ~PF_FORKNOEXEC;

	set_binfmt(&flat_format);

	p = ((current->mm->context.end_brk + stack_len + 3) & ~3) - 4;
	DBG_FLT("p=%x\n", (int)p);

	/* copy the arg pages onto the stack, this could be more efficient :-) */
	for (i = TOP_OF_ARGS - 1; i >= bprm->p; i--)
		* (char *) --p =
			((char *) page_address(bprm->page[i/PAGE_SIZE]))[i % PAGE_SIZE];

	sp = (unsigned long *) create_flat_tables(p, bprm);
	
	/* Fake some return addresses to ensure the call chain will
	 * initialise library in order for us.  We are required to call
	 * lib 1 first, then 2, ... and finally the main program (id 0).
	 */
	start_addr = libinfo.lib_list[0].entry;

#ifdef CONFIG_BINFMT_SHARED_FLAT
	for (i = MAX_SHARED_LIBS-1; i>0; i--) {
		if (libinfo.lib_list[i].loaded) {
			/* Push previos first to call address */
			--sp;	put_user(start_addr, sp);
			start_addr = libinfo.lib_list[i].entry;
		}
	}
#endif
	
	/* Stash our initial stack pointer into the mm structure */
	current->mm->start_stack = (unsigned long )sp;

	
	DBG_FLT("start_thread(regs=0x%x, entry=0x%x, start_stack=0x%x)\n",
		(int)regs, (int)start_addr, (int)current->mm->start_stack);
	
	start_thread(regs, start_addr, current->mm->start_stack);

	if (current->ptrace & PT_PTRACED)
		send_sig(SIGTRAP, current, 0);

	return 0;
}

/****************************************************************************/

static int __init init_flat_binfmt(void)
{
	return register_binfmt(&flat_format);
}

static void __exit exit_flat_binfmt(void)
{
	unregister_binfmt(&flat_format);
}

/****************************************************************************/

core_initcall(init_flat_binfmt);
module_exit(exit_flat_binfmt);

/****************************************************************************/
[i].timestamp, delta, jiffies);) if (dev->ffL.tcq_rd == dev->ffL.tcq_st) dev->ffL.tcq_rd = dev->ffL.tcq_ed; else dev->ffL.tcq_rd -= 2; *(u_short *)(dev->seg_ram + dev->ffL.tcq_rd) = i+1; if (!(skb = dev->desc_tbl[i].txskb) || !(iavcc_r = dev->desc_tbl[i].iavcc)) printk("Fatal err, desc table vcc or skb is NULL\n"); else iavcc_r->vc_desc_cnt--; dev->desc_tbl[i].timestamp = 0; dev->desc_tbl[i].iavcc = NULL; dev->desc_tbl[i].txskb = NULL; } i++; } /* while */ } if (dev->ffL.tcq_rd == dev->host_tcq_wr) return 0xFFFF; /* Get the next available descriptor number from TCQ */ desc_num = *(u_short *)(dev->seg_ram + dev->ffL.tcq_rd); while (!desc_num || (dev->desc_tbl[desc_num -1]).timestamp) { dev->ffL.tcq_rd += 2; if (dev->ffL.tcq_rd > dev->ffL.tcq_ed) dev->ffL.tcq_rd = dev->ffL.tcq_st; if (dev->ffL.tcq_rd == dev->host_tcq_wr) return 0xFFFF; desc_num = *(u_short *)(dev->seg_ram + dev->ffL.tcq_rd); } /* get system time */ dev->desc_tbl[desc_num -1].timestamp = jiffies; return desc_num; } static void clear_lockup (struct atm_vcc *vcc, IADEV *dev) { u_char foundLockUp; vcstatus_t *vcstatus; u_short *shd_tbl; u_short tempCellSlot, tempFract; struct main_vc *abr_vc = (struct main_vc *)dev->MAIN_VC_TABLE_ADDR; struct ext_vc *eabr_vc = (struct ext_vc *)dev->EXT_VC_TABLE_ADDR; u_int i; if (vcc->qos.txtp.traffic_class == ATM_ABR) { vcstatus = (vcstatus_t *) &(dev->testTable[vcc->vci]->vc_status); vcstatus->cnt++; foundLockUp = 0; if( vcstatus->cnt == 0x05 ) { abr_vc += vcc->vci; eabr_vc += vcc->vci; if( eabr_vc->last_desc ) { if( (abr_vc->status & 0x07) == ABR_STATE /* 0x2 */ ) { /* Wait for 10 Micro sec */ udelay(10); if ((eabr_vc->last_desc)&&((abr_vc->status & 0x07)==ABR_STATE)) foundLockUp = 1; } else { tempCellSlot = abr_vc->last_cell_slot; tempFract = abr_vc->fraction; if((tempCellSlot == dev->testTable[vcc->vci]->lastTime) && (tempFract == dev->testTable[vcc->vci]->fract)) foundLockUp = 1; dev->testTable[vcc->vci]->lastTime = tempCellSlot; dev->testTable[vcc->vci]->fract = tempFract; } } /* last descriptor */ vcstatus->cnt = 0; } /* vcstatus->cnt */ if (foundLockUp) { IF_ABR(printk("LOCK UP found\n");) writew(0xFFFD, dev->seg_reg+MODE_REG_0); /* Wait for 10 Micro sec */ udelay(10); abr_vc->status &= 0xFFF8; abr_vc->status |= 0x0001; /* state is idle */ shd_tbl = (u_short *)dev->ABR_SCHED_TABLE_ADDR; for( i = 0; ((i < dev->num_vc) && (shd_tbl[i])); i++ ); if (i < dev->num_vc) shd_tbl[i] = vcc->vci; else IF_ERR(printk("ABR Seg. may not continue on VC %x\n",vcc->vci);) writew(T_ONLINE, dev->seg_reg+MODE_REG_0); writew(~(TRANSMIT_DONE|TCQ_NOT_EMPTY), dev->seg_reg+SEG_MASK_REG); writew(TRANSMIT_DONE, dev->seg_reg+SEG_INTR_STATUS_REG); vcstatus->cnt = 0; } /* foundLockUp */ } /* if an ABR VC */ } /* ** Conversion of 24-bit cellrate (cells/sec) to 16-bit floating point format. ** ** +----+----+------------------+-------------------------------+ ** | R | NZ | 5-bit exponent | 9-bit mantissa | ** +----+----+------------------+-------------------------------+ ** ** R = reserved (written as 0) ** NZ = 0 if 0 cells/sec; 1 otherwise ** ** if NZ = 1, rate = 1.mmmmmmmmm x 2^(eeeee) cells/sec */ static u16 cellrate_to_float(u32 cr) { #define NZ 0x4000 #define M_BITS 9 /* Number of bits in mantissa */ #define E_BITS 5 /* Number of bits in exponent */ #define M_MASK 0x1ff #define E_MASK 0x1f u16 flot; u32 tmp = cr & 0x00ffffff; int i = 0; if (cr == 0) return 0; while (tmp != 1) { tmp >>= 1; i++; } if (i == M_BITS) flot = NZ | (i << M_BITS) | (cr & M_MASK); else if (i < M_BITS) flot = NZ | (i << M_BITS) | ((cr << (M_BITS - i)) & M_MASK); else flot = NZ | (i << M_BITS) | ((cr >> (i - M_BITS)) & M_MASK); return flot; } #if 0 /* ** Conversion of 16-bit floating point format to 24-bit cellrate (cells/sec). */ static u32 float_to_cellrate(u16 rate) { u32 exp, mantissa, cps; if ((rate & NZ) == 0) return 0; exp = (rate >> M_BITS) & E_MASK; mantissa = rate & M_MASK; if (exp == 0) return 1; cps = (1 << M_BITS) | mantissa; if (exp == M_BITS) cps = cps; else if (exp > M_BITS) cps <<= (exp - M_BITS); else cps >>= (M_BITS - exp); return cps; } #endif static void init_abr_vc (IADEV *dev, srv_cls_param_t *srv_p) { srv_p->class_type = ATM_ABR; srv_p->pcr = dev->LineRate; srv_p->mcr = 0; srv_p->icr = 0x055cb7; srv_p->tbe = 0xffffff; srv_p->frtt = 0x3a; srv_p->rif = 0xf; srv_p->rdf = 0xb; srv_p->nrm = 0x4; srv_p->trm = 0x7; srv_p->cdf = 0x3; srv_p->adtf = 50; } static int ia_open_abr_vc(IADEV *dev, srv_cls_param_t *srv_p, struct atm_vcc *vcc, u8 flag) { f_vc_abr_entry *f_abr_vc; r_vc_abr_entry *r_abr_vc; u32 icr; u8 trm, nrm, crm; u16 adtf, air, *ptr16; f_abr_vc =(f_vc_abr_entry *)dev->MAIN_VC_TABLE_ADDR; f_abr_vc += vcc->vci; switch (flag) { case 1: /* FFRED initialization */ #if 0 /* sanity check */ if (srv_p->pcr == 0) return INVALID_PCR; if (srv_p->pcr > dev->LineRate) srv_p->pcr = dev->LineRate; if ((srv_p->mcr + dev->sum_mcr) > dev->LineRate) return MCR_UNAVAILABLE; if (srv_p->mcr > srv_p->pcr) return INVALID_MCR; if (!(srv_p->icr)) srv_p->icr = srv_p->pcr; if ((srv_p->icr < srv_p->mcr) || (srv_p->icr > srv_p->pcr)) return INVALID_ICR; if ((srv_p->tbe < MIN_TBE) || (srv_p->tbe > MAX_TBE)) return INVALID_TBE; if ((srv_p->frtt < MIN_FRTT) || (srv_p->frtt > MAX_FRTT)) return INVALID_FRTT; if (srv_p->nrm > MAX_NRM) return INVALID_NRM; if (srv_p->trm > MAX_TRM) return INVALID_TRM; if (srv_p->adtf > MAX_ADTF) return INVALID_ADTF; else if (srv_p->adtf == 0) srv_p->adtf = 1; if (srv_p->cdf > MAX_CDF) return INVALID_CDF; if (srv_p->rif > MAX_RIF) return INVALID_RIF; if (srv_p->rdf > MAX_RDF) return INVALID_RDF; #endif memset ((caddr_t)f_abr_vc, 0, sizeof(*f_abr_vc)); f_abr_vc->f_vc_type = ABR; nrm = 2 << srv_p->nrm; /* (2 ** (srv_p->nrm +1)) */ /* i.e 2**n = 2 << (n-1) */ f_abr_vc->f_nrm = nrm << 8 | nrm; trm = 100000/(2 << (16 - srv_p->trm)); if ( trm == 0) trm = 1; f_abr_vc->f_nrmexp =(((srv_p->nrm +1) & 0x0f) << 12)|(MRM << 8) | trm; crm = srv_p->tbe / nrm; if (crm == 0) crm = 1; f_abr_vc->f_crm = crm & 0xff; f_abr_vc->f_pcr = cellrate_to_float(srv_p->pcr); icr = min( srv_p->icr, (srv_p->tbe > srv_p->frtt) ? ((srv_p->tbe/srv_p->frtt)*1000000) : (1000000/(srv_p->frtt/srv_p->tbe))); f_abr_vc->f_icr = cellrate_to_float(icr); adtf = (10000 * srv_p->adtf)/8192; if (adtf == 0) adtf = 1; f_abr_vc->f_cdf = ((7 - srv_p->cdf) << 12 | adtf) & 0xfff; f_abr_vc->f_mcr = cellrate_to_float(srv_p->mcr); f_abr_vc->f_acr = f_abr_vc->f_icr; f_abr_vc->f_status = 0x0042; break; case 0: /* RFRED initialization */ ptr16 = (u_short *)(dev->reass_ram + REASS_TABLE*dev->memSize); *(ptr16 + vcc->vci) = NO_AAL5_PKT | REASS_ABR; r_abr_vc = (r_vc_abr_entry*)(dev->reass_ram+ABR_VC_TABLE*dev->memSize); r_abr_vc += vcc->vci; r_abr_vc->r_status_rdf = (15 - srv_p->rdf) & 0x000f; air = srv_p->pcr << (15 - srv_p->rif); if (air == 0) air = 1; r_abr_vc->r_air = cellrate_to_float(air); dev->testTable[vcc->vci]->vc_status = VC_ACTIVE | VC_ABR; dev->sum_mcr += srv_p->mcr; dev->n_abr++; break; default: break; } return 0; } static int ia_cbr_setup (IADEV *dev, struct atm_vcc *vcc) { u32 rateLow=0, rateHigh, rate; int entries; struct ia_vcc *ia_vcc; int idealSlot =0, testSlot, toBeAssigned, inc; u32 spacing; u16 *SchedTbl, *TstSchedTbl; u16 cbrVC, vcIndex; u32 fracSlot = 0; u32 sp_mod = 0; u32 sp_mod2 = 0; /* IpAdjustTrafficParams */ if (vcc->qos.txtp.max_pcr <= 0) { IF_ERR(printk("PCR for CBR not defined\n");) return -1; } rate = vcc->qos.txtp.max_pcr; entries = rate / dev->Granularity; IF_CBR(printk("CBR: CBR entries=0x%x for rate=0x%x & Gran=0x%x\n", entries, rate, dev->Granularity);) if (entries < 1) IF_CBR(printk("CBR: Bandwidth smaller than granularity of CBR table\n");) rateLow = entries * dev->Granularity; rateHigh = (entries + 1) * dev->Granularity; if (3*(rate - rateLow) > (rateHigh - rate)) entries++; if (entries > dev->CbrRemEntries) { IF_CBR(printk("CBR: Not enough bandwidth to support this PCR.\n");) IF_CBR(printk("Entries = 0x%x, CbrRemEntries = 0x%x.\n", entries, dev->CbrRemEntries);) return -EBUSY; } ia_vcc = INPH_IA_VCC(vcc); ia_vcc->NumCbrEntry = entries; dev->sum_mcr += entries * dev->Granularity; /* IaFFrednInsertCbrSched */ // Starting at an arbitrary location, place the entries into the table // as smoothly as possible cbrVC = 0; spacing = dev->CbrTotEntries / entries; sp_mod = dev->CbrTotEntries % entries; // get modulo toBeAssigned = entries; fracSlot = 0; vcIndex = vcc->vci; IF_CBR(printk("Vci=0x%x,Spacing=0x%x,Sp_mod=0x%x\n",vcIndex,spacing,sp_mod);) while (toBeAssigned) { // If this is the first time, start the table loading for this connection // as close to entryPoint as possible. if (toBeAssigned == entries) { idealSlot = dev->CbrEntryPt; dev->CbrEntryPt += 2; // Adding 2 helps to prevent clumping if (dev->CbrEntryPt >= dev->CbrTotEntries) dev->CbrEntryPt -= dev->CbrTotEntries;// Wrap if necessary } else { idealSlot += (u32)(spacing + fracSlot); // Point to the next location // in the table that would be smoothest fracSlot = ((sp_mod + sp_mod2) / entries); // get new integer part sp_mod2 = ((sp_mod + sp_mod2) % entries); // calc new fractional part } if (idealSlot >= (int)dev->CbrTotEntries) idealSlot -= dev->CbrTotEntries; // Continuously check around this ideal value until a null // location is encountered. SchedTbl = (u16*)(dev->seg_ram+CBR_SCHED_TABLE*dev->memSize); inc = 0; testSlot = idealSlot; TstSchedTbl = (u16*)(SchedTbl+testSlot); //set index and read in value IF_CBR(printk("CBR Testslot 0x%x AT Location 0x%x, NumToAssign=%d\n", testSlot, (u32)TstSchedTbl,toBeAssigned);) memcpy((caddr_t)&cbrVC,(caddr_t)TstSchedTbl,sizeof(cbrVC)); while (cbrVC) // If another VC at this location, we have to keep looking { inc++; testSlot = idealSlot - inc; if (testSlot < 0) { // Wrap if necessary testSlot += dev->CbrTotEntries; IF_CBR(printk("Testslot Wrap. STable Start=0x%x,Testslot=%d\n", (u32)SchedTbl,testSlot);) } TstSchedTbl = (u16 *)(SchedTbl + testSlot); // set table index memcpy((caddr_t)&cbrVC,(caddr_t)TstSchedTbl,sizeof(cbrVC)); if (!cbrVC) break; testSlot = idealSlot + inc; if (testSlot >= (int)dev->CbrTotEntries) { // Wrap if necessary testSlot -= dev->CbrTotEntries; IF_CBR(printk("TotCbrEntries=%d",dev->CbrTotEntries);) IF_CBR(printk(" Testslot=0x%x ToBeAssgned=%d\n", testSlot, toBeAssigned);) } // set table index and read in value TstSchedTbl = (u16*)(SchedTbl + testSlot); IF_CBR(printk("Reading CBR Tbl from 0x%x, CbrVal=0x%x Iteration %d\n", (u32)TstSchedTbl,cbrVC,inc);) memcpy((caddr_t)&cbrVC,(caddr_t)TstSchedTbl,sizeof(cbrVC)); } /* while */ // Move this VCI number into this location of the CBR Sched table. memcpy((caddr_t)TstSchedTbl, (caddr_t)&vcIndex,sizeof(TstSchedTbl)); dev->CbrRemEntries--; toBeAssigned--; } /* while */ /* IaFFrednCbrEnable */ dev->NumEnabledCBR++; if (dev->NumEnabledCBR == 1) { writew((CBR_EN | UBR_EN | ABR_EN | (0x23 << 2)), dev->seg_reg+STPARMS); IF_CBR(printk("CBR is enabled\n");) } return 0; } static void ia_cbrVc_close (struct atm_vcc *vcc) { IADEV *iadev; u16 *SchedTbl, NullVci = 0; u32 i, NumFound; iadev = INPH_IA_DEV(vcc->dev); iadev->NumEnabledCBR--; SchedTbl = (u16*)(iadev->seg_ram+CBR_SCHED_TABLE*iadev->memSize); if (iadev->NumEnabledCBR == 0) { writew((UBR_EN | ABR_EN | (0x23 << 2)), iadev->seg_reg+STPARMS); IF_CBR (printk("CBR support disabled\n");) } NumFound = 0; for (i=0; i < iadev->CbrTotEntries; i++) { if (*SchedTbl == vcc->vci) { iadev->CbrRemEntries++; *SchedTbl = NullVci; IF_CBR(NumFound++;) } SchedTbl++; } IF_CBR(printk("Exit ia_cbrVc_close, NumRemoved=%d\n",NumFound);) } static int ia_avail_descs(IADEV *iadev) { int tmp = 0; ia_hack_tcq(iadev); if (iadev->host_tcq_wr >= iadev->ffL.tcq_rd) tmp = (iadev->host_tcq_wr - iadev->ffL.tcq_rd) / 2; else tmp = (iadev->ffL.tcq_ed - iadev->ffL.tcq_rd + 2 + iadev->host_tcq_wr - iadev->ffL.tcq_st) / 2; return tmp; } static int ia_pkt_tx (struct atm_vcc *vcc, struct sk_buff *skb); static int ia_que_tx (IADEV *iadev) { struct sk_buff *skb; int num_desc; struct atm_vcc *vcc; struct ia_vcc *iavcc; num_desc = ia_avail_descs(iadev); while (num_desc && (skb = skb_dequeue(&iadev->tx_backlog))) { if (!(vcc = ATM_SKB(skb)->vcc)) { dev_kfree_skb_any(skb); printk("ia_que_tx: Null vcc\n"); break; } if (!test_bit(ATM_VF_READY,&vcc->flags)) { dev_kfree_skb_any(skb); printk("Free the SKB on closed vci %d \n", vcc->vci); break; } iavcc = INPH_IA_VCC(vcc); if (ia_pkt_tx (vcc, skb)) { skb_queue_head(&iadev->tx_backlog, skb); } num_desc--; } return 0; } static void ia_tx_poll (IADEV *iadev) { struct atm_vcc *vcc = NULL; struct sk_buff *skb = NULL, *skb1 = NULL; struct ia_vcc *iavcc; IARTN_Q * rtne; ia_hack_tcq(iadev); while ( (rtne = ia_deque_rtn_q(&iadev->tx_return_q))) { skb = rtne->data.txskb; if (!skb) { printk("ia_tx_poll: skb is null\n"); goto out; } vcc = ATM_SKB(skb)->vcc; if (!vcc) { printk("ia_tx_poll: vcc is null\n"); dev_kfree_skb_any(skb); goto out; } iavcc = INPH_IA_VCC(vcc); if (!iavcc) { printk("ia_tx_poll: iavcc is null\n"); dev_kfree_skb_any(skb); goto out; } skb1 = skb_dequeue(&iavcc->txing_skb); while (skb1 && (skb1 != skb)) { if (!(IA_SKB_STATE(skb1) & IA_TX_DONE)) { printk("IA_tx_intr: Vci %d lost pkt!!!\n", vcc->vci); } IF_ERR(printk("Release the SKB not match\n");) if ((vcc->pop) && (skb1->len != 0)) { vcc->pop(vcc, skb1); IF_EVENT(printk("Tansmit Done - skb 0x%lx return\n", (long)skb1);) } else dev_kfree_skb_any(skb1); skb1 = skb_dequeue(&iavcc->txing_skb); } if (!skb1) { IF_EVENT(printk("IA: Vci %d - skb not found requed\n",vcc->vci);) ia_enque_head_rtn_q (&iadev->tx_return_q, rtne); break; } if ((vcc->pop) && (skb->len != 0)) { vcc->pop(vcc, skb); IF_EVENT(printk("Tx Done - skb 0x%lx return\n",(long)skb);) } else dev_kfree_skb_any(skb); kfree(rtne); } ia_que_tx(iadev); out: return; } #if 0 static void ia_eeprom_put (IADEV *iadev, u32 addr, u_short val) { u32 t; int i; /* * Issue a command to enable writes to the NOVRAM */ NVRAM_CMD (EXTEND + EWEN); NVRAM_CLR_CE; /* * issue the write command */ NVRAM_CMD(IAWRITE + addr); /* * Send the data, starting with D15, then D14, and so on for 16 bits */ for (i=15; i>=0; i--) { NVRAM_CLKOUT (val & 0x8000); val <<= 1; } NVRAM_CLR_CE; CFG_OR(NVCE); t = readl(iadev->reg+IPHASE5575_EEPROM_ACCESS); while (!(t & NVDO)) t = readl(iadev->reg+IPHASE5575_EEPROM_ACCESS); NVRAM_CLR_CE; /* * disable writes again */ NVRAM_CMD(EXTEND + EWDS) NVRAM_CLR_CE; CFG_AND(~NVDI); } #endif static u16 ia_eeprom_get (IADEV *iadev, u32 addr) { u_short val; u32 t; int i; /* * Read the first bit that was clocked with the falling edge of the * the last command data clock */ NVRAM_CMD(IAREAD + addr); /* * Now read the rest of the bits, the next bit read is D14, then D13, * and so on. */ val = 0; for (i=15; i>=0; i--) { NVRAM_CLKIN(t); val |= (t << i); } NVRAM_CLR_CE; CFG_AND(~NVDI); return val; } static void ia_hw_type(IADEV *iadev) { u_short memType = ia_eeprom_get(iadev, 25); iadev->memType = memType; if ((memType & MEM_SIZE_MASK) == MEM_SIZE_1M) { iadev->num_tx_desc = IA_TX_BUF; iadev->tx_buf_sz = IA_TX_BUF_SZ; iadev->num_rx_desc = IA_RX_BUF; iadev->rx_buf_sz = IA_RX_BUF_SZ; } else if ((memType & MEM_SIZE_MASK) == MEM_SIZE_512K) { if (IA_TX_BUF == DFL_TX_BUFFERS) iadev->num_tx_desc = IA_TX_BUF / 2; else iadev->num_tx_desc = IA_TX_BUF; iadev->tx_buf_sz = IA_TX_BUF_SZ; if (IA_RX_BUF == DFL_RX_BUFFERS) iadev->num_rx_desc = IA_RX_BUF / 2; else iadev->num_rx_desc = IA_RX_BUF; iadev->rx_buf_sz = IA_RX_BUF_SZ; } else { if (IA_TX_BUF == DFL_TX_BUFFERS) iadev->num_tx_desc = IA_TX_BUF / 8; else iadev->num_tx_desc = IA_TX_BUF; iadev->tx_buf_sz = IA_TX_BUF_SZ; if (IA_RX_BUF == DFL_RX_BUFFERS) iadev->num_rx_desc = IA_RX_BUF / 8; else iadev->num_rx_desc = IA_RX_BUF; iadev->rx_buf_sz = IA_RX_BUF_SZ; } iadev->rx_pkt_ram = TX_PACKET_RAM + (iadev->num_tx_desc * iadev->tx_buf_sz); IF_INIT(printk("BUF: tx=%d,sz=%d rx=%d sz= %d rx_pkt_ram=%d\n", iadev->num_tx_desc, iadev->tx_buf_sz, iadev->num_rx_desc, iadev->rx_buf_sz, iadev->rx_pkt_ram);) #if 0 if ((memType & FE_MASK) == FE_SINGLE_MODE) { iadev->phy_type = PHY_OC3C_S; else if ((memType & FE_MASK) == FE_UTP_OPTION) iadev->phy_type = PHY_UTP155; else iadev->phy_type = PHY_OC3C_M; #endif iadev->phy_type = memType & FE_MASK; IF_INIT(printk("memType = 0x%x iadev->phy_type = 0x%x\n", memType,iadev->phy_type);) if (iadev->phy_type == FE_25MBIT_PHY) iadev->LineRate = (u32)(((25600000/8)*26)/(27*53)); else if (iadev->phy_type == FE_DS3_PHY) iadev->LineRate = (u32)(((44736000/8)*26)/(27*53)); else if (iadev->phy_type == FE_E3_PHY) iadev->LineRate = (u32)(((34368000/8)*26)/(27*53)); else iadev->LineRate = (u32)(ATM_OC3_PCR); IF_INIT(printk("iadev->LineRate = %d \n", iadev->LineRate);) } static void IaFrontEndIntr(IADEV *iadev) { volatile IA_SUNI *suni; volatile ia_mb25_t *mb25; volatile suni_pm7345_t *suni_pm7345; u32 intr_status; u_int frmr_intr; if(iadev->phy_type & FE_25MBIT_PHY) { mb25 = (ia_mb25_t*)iadev->phy; iadev->carrier_detect = Boolean(mb25->mb25_intr_status & MB25_IS_GSB); } else if (iadev->phy_type & FE_DS3_PHY) { suni_pm7345 = (suni_pm7345_t *)iadev->phy; /* clear FRMR interrupts */ frmr_intr = suni_pm7345->suni_ds3_frm_intr_stat; iadev->carrier_detect = Boolean(!(suni_pm7345->suni_ds3_frm_stat & SUNI_DS3_LOSV)); } else if (iadev->phy_type & FE_E3_PHY ) { suni_pm7345 = (suni_pm7345_t *)iadev->phy; frmr_intr = suni_pm7345->suni_e3_frm_maint_intr_ind; iadev->carrier_detect = Boolean(!(suni_pm7345->suni_e3_frm_fram_intr_ind_stat&SUNI_E3_LOS)); } else { suni = (IA_SUNI *)iadev->phy; intr_status = suni->suni_rsop_status & 0xff; iadev->carrier_detect = Boolean(!(suni->suni_rsop_status & SUNI_LOSV)); } if (iadev->carrier_detect) printk("IA: SUNI carrier detected\n"); else printk("IA: SUNI carrier lost signal\n"); return; } static void ia_mb25_init (IADEV *iadev) { volatile ia_mb25_t *mb25 = (ia_mb25_t*)iadev->phy; #if 0 mb25->mb25_master_ctrl = MB25_MC_DRIC | MB25_MC_DREC | MB25_MC_ENABLED; #endif mb25->mb25_master_ctrl = MB25_MC_DRIC | MB25_MC_DREC; mb25->mb25_diag_control = 0; /* * Initialize carrier detect state */ iadev->carrier_detect = Boolean(mb25->mb25_intr_status & MB25_IS_GSB); return; } static void ia_suni_pm7345_init (IADEV *iadev) { volatile suni_pm7345_t *suni_pm7345 = (suni_pm7345_t *)iadev->phy; if (iadev->phy_type & FE_DS3_PHY) { iadev->carrier_detect = Boolean(!(suni_pm7345->suni_ds3_frm_stat & SUNI_DS3_LOSV)); suni_pm7345->suni_ds3_frm_intr_enbl = 0x17; suni_pm7345->suni_ds3_frm_cfg = 1; suni_pm7345->suni_ds3_tran_cfg = 1; suni_pm7345->suni_config = 0; suni_pm7345->suni_splr_cfg = 0; suni_pm7345->suni_splt_cfg = 0; } else { iadev->carrier_detect = Boolean(!(suni_pm7345->suni_e3_frm_fram_intr_ind_stat & SUNI_E3_LOS)); suni_pm7345->suni_e3_frm_fram_options = 0x4; suni_pm7345->suni_e3_frm_maint_options = 0x20; suni_pm7345->suni_e3_frm_fram_intr_enbl = 0x1d; suni_pm7345->suni_e3_frm_maint_intr_enbl = 0x30; suni_pm7345->suni_e3_tran_stat_diag_options = 0x0; suni_pm7345->suni_e3_tran_fram_options = 0x1; suni_pm7345->suni_config = SUNI_PM7345_E3ENBL; suni_pm7345->suni_splr_cfg = 0x41; suni_pm7345->suni_splt_cfg = 0x41; } /* * Enable RSOP loss of signal interrupt. */ suni_pm7345->suni_intr_enbl = 0x28; /* * Clear error counters */ suni_pm7345->suni_id_reset = 0; /* * Clear "PMCTST" in master test register. */ suni_pm7345->suni_master_test = 0; suni_pm7345->suni_rxcp_ctrl = 0x2c; suni_pm7345->suni_rxcp_fctrl = 0x81; suni_pm7345->suni_rxcp_idle_pat_h1 = suni_pm7345->suni_rxcp_idle_pat_h2 = suni_pm7345->suni_rxcp_idle_pat_h3 = 0; suni_pm7345->suni_rxcp_idle_pat_h4 = 1; suni_pm7345->suni_rxcp_idle_mask_h1 = 0xff; suni_pm7345->suni_rxcp_idle_mask_h2 = 0xff; suni_pm7345->suni_rxcp_idle_mask_h3 = 0xff; suni_pm7345->suni_rxcp_idle_mask_h4 = 0xfe; suni_pm7345->suni_rxcp_cell_pat_h1 = suni_pm7345->suni_rxcp_cell_pat_h2 = suni_pm7345->suni_rxcp_cell_pat_h3 = 0; suni_pm7345->suni_rxcp_cell_pat_h4 = 1; suni_pm7345->suni_rxcp_cell_mask_h1 = suni_pm7345->suni_rxcp_cell_mask_h2 = suni_pm7345->suni_rxcp_cell_mask_h3 = suni_pm7345->suni_rxcp_cell_mask_h4 = 0xff; suni_pm7345->suni_txcp_ctrl = 0xa4; suni_pm7345->suni_txcp_intr_en_sts = 0x10; suni_pm7345->suni_txcp_idle_pat_h5 = 0x55; suni_pm7345->suni_config &= ~(SUNI_PM7345_LLB | SUNI_PM7345_CLB | SUNI_PM7345_DLB | SUNI_PM7345_PLB); #ifdef __SNMP__ suni_pm7345->suni_rxcp_intr_en_sts |= SUNI_OOCDE; #endif /* __SNMP__ */ return; } /***************************** IA_LIB END *****************************/ #ifdef CONFIG_ATM_IA_DEBUG static int tcnter = 0; static void xdump( u_char* cp, int length, char* prefix ) { int col, count; u_char prntBuf[120]; u_char* pBuf = prntBuf; count = 0; while(count < length){ pBuf += sprintf( pBuf, "%s", prefix ); for(col = 0;count + col < length && col < 16; col++){ if (col != 0 && (col % 4) == 0) pBuf += sprintf( pBuf, " " ); pBuf += sprintf( pBuf, "%02X ", cp[count + col] ); } while(col++ < 16){ /* pad end of buffer with blanks */ if ((col % 4) == 0) sprintf( pBuf, " " ); pBuf += sprintf( pBuf, " " ); } pBuf += sprintf( pBuf, " " ); for(col = 0;count + col < length && col < 16; col++){ if (isprint((int)cp[count + col])) pBuf += sprintf( pBuf, "%c", cp[count + col] ); else pBuf += sprintf( pBuf, "." ); } sprintf( pBuf, "\n" ); // SPrint(prntBuf); printk(prntBuf); count += col; pBuf = prntBuf; } } /* close xdump(... */ #endif /* CONFIG_ATM_IA_DEBUG */ static struct atm_dev *ia_boards = NULL; #define ACTUAL_RAM_BASE \ RAM_BASE*((iadev->mem)/(128 * 1024)) #define ACTUAL_SEG_RAM_BASE \ IPHASE5575_FRAG_CONTROL_RAM_BASE*((iadev->mem)/(128 * 1024)) #define ACTUAL_REASS_RAM_BASE \ IPHASE5575_REASS_CONTROL_RAM_BASE*((iadev->mem)/(128 * 1024)) /*-- some utilities and memory allocation stuff will come here -------------*/ static void desc_dbg(IADEV *iadev) { u_short tcq_wr_ptr, tcq_st_ptr, tcq_ed_ptr; u32 i; void __iomem *tmp; // regval = readl((u32)ia_cmds->maddr); tcq_wr_ptr = readw(iadev->seg_reg+TCQ_WR_PTR); printk("B_tcq_wr = 0x%x desc = %d last desc = %d\n", tcq_wr_ptr, readw(iadev->seg_ram+tcq_wr_ptr), readw(iadev->seg_ram+tcq_wr_ptr-2)); printk(" host_tcq_wr = 0x%x host_tcq_rd = 0x%x \n", iadev->host_tcq_wr, iadev->ffL.tcq_rd); tcq_st_ptr = readw(iadev->seg_reg+TCQ_ST_ADR); tcq_ed_ptr = readw(iadev->seg_reg+TCQ_ED_ADR); printk("tcq_st_ptr = 0x%x tcq_ed_ptr = 0x%x \n", tcq_st_ptr, tcq_ed_ptr); i = 0; while (tcq_st_ptr != tcq_ed_ptr) { tmp = iadev->seg_ram+tcq_st_ptr; printk("TCQ slot %d desc = %d Addr = %p\n", i++, readw(tmp), tmp); tcq_st_ptr += 2; } for(i=0; i <iadev->num_tx_desc; i++) printk("Desc_tbl[%d] = %d \n", i, iadev->desc_tbl[i].timestamp); } /*----------------------------- Recieving side stuff --------------------------*/ static void rx_excp_rcvd(struct atm_dev *dev) { #if 0 /* closing the receiving size will cause too many excp int */ IADEV *iadev; u_short state; u_short excpq_rd_ptr; //u_short *ptr; int vci, error = 1; iadev = INPH_IA_DEV(dev); state = readl(iadev->reass_reg + STATE_REG) & 0xffff; while((state & EXCPQ_EMPTY) != EXCPQ_EMPTY) { printk("state = %x \n", state); excpq_rd_ptr = readw(iadev->reass_reg + EXCP_Q_RD_PTR) & 0xffff; printk("state = %x excpq_rd_ptr = %x \n", state, excpq_rd_ptr); if (excpq_rd_ptr == *(u16*)(iadev->reass_reg + EXCP_Q_WR_PTR)) IF_ERR(printk("excpq_rd_ptr is wrong!!!\n");) // TODO: update exception stat vci = readw(iadev->reass_ram+excpq_rd_ptr); error = readw(iadev->reass_ram+excpq_rd_ptr+2) & 0x0007; // pwang_test excpq_rd_ptr += 4; if (excpq_rd_ptr > (readw(iadev->reass_reg + EXCP_Q_ED_ADR)& 0xffff)) excpq_rd_ptr = readw(iadev->reass_reg + EXCP_Q_ST_ADR)& 0xffff; writew( excpq_rd_ptr, iadev->reass_reg + EXCP_Q_RD_PTR); state = readl(iadev->reass_reg + STATE_REG) & 0xffff; } #endif } static void free_desc(struct atm_dev *dev, int desc) { IADEV *iadev; iadev = INPH_IA_DEV(dev); writew(desc, iadev->reass_ram+iadev->rfL.fdq_wr); iadev->rfL.fdq_wr +=2; if (iadev->rfL.fdq_wr > iadev->rfL.fdq_ed) iadev->rfL.fdq_wr = iadev->rfL.fdq_st; writew(iadev->rfL.fdq_wr, iadev->reass_reg+FREEQ_WR_PTR); } static int rx_pkt(struct atm_dev *dev) { IADEV *iadev; struct atm_vcc *vcc; unsigned short status; struct rx_buf_desc __iomem *buf_desc_ptr; int desc; struct dle* wr_ptr; int len; struct sk_buff *skb; u_int buf_addr, dma_addr; iadev = INPH_IA_DEV(dev); if (iadev->rfL.pcq_rd == (readw(iadev->reass_reg+PCQ_WR_PTR)&0xffff)) { printk(KERN_ERR DEV_LABEL "(itf %d) Receive queue empty\n", dev->number); return -EINVAL; } /* mask 1st 3 bits to get the actual descno. */ desc = readw(iadev->reass_ram+iadev->rfL.pcq_rd) & 0x1fff; IF_RX(printk("reass_ram = %p iadev->rfL.pcq_rd = 0x%x desc = %d\n", iadev->reass_ram, iadev->rfL.pcq_rd, desc); printk(" pcq_wr_ptr = 0x%x\n", readw(iadev->reass_reg+PCQ_WR_PTR)&0xffff);) /* update the read pointer - maybe we shud do this in the end*/ if ( iadev->rfL.pcq_rd== iadev->rfL.pcq_ed) iadev->rfL.pcq_rd = iadev->rfL.pcq_st; else iadev->rfL.pcq_rd += 2; writew(iadev->rfL.pcq_rd, iadev->reass_reg+PCQ_RD_PTR); /* get the buffer desc entry. update stuff. - doesn't seem to be any update necessary */ buf_desc_ptr = iadev->RX_DESC_BASE_ADDR; /* make the ptr point to the corresponding buffer desc entry */ buf_desc_ptr += desc; if (!desc || (desc > iadev->num_rx_desc) || ((buf_desc_ptr->vc_index & 0xffff) > iadev->num_vc)) { free_desc(dev, desc); IF_ERR(printk("IA: bad descriptor desc = %d \n", desc);) return -1; } vcc = iadev->rx_open[buf_desc_ptr->vc_index & 0xffff]; if (!vcc) { free_desc(dev, desc); printk("IA: null vcc, drop PDU\n"); return -1; } /* might want to check the status bits for errors */ status = (u_short) (buf_desc_ptr->desc_mode); if (status & (RX_CER | RX_PTE | RX_OFL)) { atomic_inc(&vcc->stats->rx_err); IF_ERR(printk("IA: bad packet, dropping it");) if (status & RX_CER) { IF_ERR(printk(" cause: packet CRC error\n");) } else if (status & RX_PTE) { IF_ERR(printk(" cause: packet time out\n");) } else { IF_ERR(printk(" cause: buffer over flow\n");) } goto out_free_desc; } /* build DLE. */ buf_addr = (buf_desc_ptr->buf_start_hi << 16) | buf_desc_ptr->buf_start_lo; dma_addr = (buf_desc_ptr->dma_start_hi << 16) | buf_desc_ptr->dma_start_lo; len = dma_addr - buf_addr; if (len > iadev->rx_buf_sz) { printk("Over %d bytes sdu received, dropped!!!\n", iadev->rx_buf_sz); atomic_inc(&vcc->stats->rx_err); goto out_free_desc; } if (!(skb = atm_alloc_charge(vcc, len, GFP_ATOMIC))) { if (vcc->vci < 32) printk("Drop control packets\n"); goto out_free_desc; } skb_put(skb,len); // pwang_test ATM_SKB(skb)->vcc = vcc; ATM_DESC(skb) = desc; skb_queue_tail(&iadev->rx_dma_q, skb); /* Build the DLE structure */ wr_ptr = iadev->rx_dle_q.write; wr_ptr->sys_pkt_addr = pci_map_single(iadev->pci, skb->data, len, PCI_DMA_FROMDEVICE); wr_ptr->local_pkt_addr = buf_addr; wr_ptr->bytes = len; /* We don't know this do we ?? */ wr_ptr->mode = DMA_INT_ENABLE; /* shud take care of wrap around here too. */ if(++wr_ptr == iadev->rx_dle_q.end) wr_ptr = iadev->rx_dle_q.start; iadev->rx_dle_q.write = wr_ptr; udelay(1); /* Increment transaction counter */ writel(1, iadev->dma+IPHASE5575_RX_COUNTER); out: return 0; out_free_desc: free_desc(dev, desc); goto out; } static void rx_intr(struct atm_dev *dev) { IADEV *iadev; u_short status; u_short state, i; iadev = INPH_IA_DEV(dev); status = readl(iadev->reass_reg+REASS_INTR_STATUS_REG) & 0xffff; IF_EVENT(printk("rx_intr: status = 0x%x\n", status);) if (status & RX_PKT_RCVD) { /* do something */ /* Basically recvd an interrupt for receving a packet. A descriptor would have been written to the packet complete queue. Get all the descriptors and set up dma to move the packets till the packet complete queue is empty.. */ state = readl(iadev->reass_reg + STATE_REG) & 0xffff; IF_EVENT(printk("Rx intr status: RX_PKT_RCVD %08x\n", status);) while(!(state & PCQ_EMPTY)) { rx_pkt(dev); state = readl(iadev->reass_reg + STATE_REG) & 0xffff; } iadev->rxing = 1; } if (status & RX_FREEQ_EMPT) { if (iadev->rxing) { iadev->rx_tmp_cnt = iadev->rx_pkt_cnt; iadev->rx_tmp_jif = jiffies; iadev->rxing = 0; } else if ((time_after(jiffies, iadev->rx_tmp_jif + 50)) && ((iadev->rx_pkt_cnt - iadev->rx_tmp_cnt) == 0)) { for (i = 1; i <= iadev->num_rx_desc; i++) free_desc(dev, i); printk("Test logic RUN!!!!\n"); writew( ~(RX_FREEQ_EMPT|RX_EXCP_RCVD),iadev->reass_reg+REASS_MASK_REG); iadev->rxing = 1; } IF_EVENT(printk("Rx intr status: RX_FREEQ_EMPT %08x\n", status);) } if (status & RX_EXCP_RCVD) { /* probably need to handle the exception queue also. */ IF_EVENT(printk("Rx intr status: RX_EXCP_RCVD %08x\n", status);) rx_excp_rcvd(dev); } if (status & RX_RAW_RCVD) { /* need to handle the raw incoming cells. This deepnds on whether we have programmed to receive the raw cells or not. Else ignore. */ IF_EVENT(printk("Rx intr status: RX_RAW_RCVD %08x\n", status);) } } static void rx_dle_intr(struct atm_dev *dev) { IADEV *iadev; struct atm_vcc *vcc; struct sk_buff *skb; int desc; u_short state; struct dle *dle, *cur_dle; u_int dle_lp; int len; iadev = INPH_IA_DEV(dev); /* free all the dles done, that is just update our own dle read pointer - do we really need to do this. Think not. */ /* DMA is done, just get all the recevie buffers from the rx dma queue and push them up to the higher layer protocol. Also free the desc associated with the buffer. */ dle = iadev->rx_dle_q.read; dle_lp = readl(iadev->dma+IPHASE5575_RX_LIST_ADDR) & (sizeof(struct dle)*DLE_ENTRIES - 1); cur_dle = (struct dle*)(iadev->rx_dle_q.start + (dle_lp >> 4)); while(dle != cur_dle) { /* free the DMAed skb */ skb = skb_dequeue(&iadev->rx_dma_q); if (!skb) goto INCR_DLE; desc = ATM_DESC(skb); free_desc(dev, desc); if (!(len = skb->len)) { printk("rx_dle_intr: skb len 0\n"); dev_kfree_skb_any(skb); } else { struct cpcs_trailer *trailer; u_short length; struct ia_vcc *ia_vcc; pci_unmap_single(iadev->pci, iadev->rx_dle_q.write->sys_pkt_addr, len, PCI_DMA_FROMDEVICE); /* no VCC related housekeeping done as yet. lets see */ vcc = ATM_SKB(skb)->vcc; if (!vcc) { printk("IA: null vcc\n"); dev_kfree_skb_any(skb); goto INCR_DLE; } ia_vcc = INPH_IA_VCC(vcc); if (ia_vcc == NULL) { atomic_inc(&vcc->stats->rx_err); dev_kfree_skb_any(skb); atm_return(vcc, atm_guess_pdu2truesize(len)); goto INCR_DLE; } // get real pkt length pwang_test trailer = (struct cpcs_trailer*)((u_char *)skb->data + skb->len - sizeof(*trailer)); length = swap(trailer->length); if ((length > iadev->rx_buf_sz) || (length > (skb->len - sizeof(struct cpcs_trailer)))) { atomic_inc(&vcc->stats->rx_err); IF_ERR(printk("rx_dle_intr: Bad AAL5 trailer %d (skb len %d)", length, skb->len);) dev_kfree_skb_any(skb); atm_return(vcc, atm_guess_pdu2truesize(len)); goto INCR_DLE; } skb_trim(skb, length); /* Display the packet */ IF_RXPKT(printk("\nDmad Recvd data: len = %d \n", skb->len); xdump(skb->data, skb->len, "RX: "); printk("\n");) IF_RX(printk("rx_dle_intr: skb push");) vcc->push(vcc,skb); atomic_inc(&vcc->stats->rx); iadev->rx_pkt_cnt++; } INCR_DLE: if (++dle == iadev->rx_dle_q.end) dle = iadev->rx_dle_q.start; } iadev->rx_dle_q.read = dle; /* if the interrupts are masked because there were no free desc available, unmask them now. */ if (!iadev->rxing) { state = readl(iadev->reass_reg + STATE_REG) & 0xffff; if (!(state & FREEQ_EMPTY)) { state = readl(iadev->reass_reg + REASS_MASK_REG) & 0xffff; writel(state & ~(RX_FREEQ_EMPT |/* RX_EXCP_RCVD |*/ RX_PKT_RCVD), iadev->reass_reg+REASS_MASK_REG); iadev->rxing++; } } } static int open_rx(struct atm_vcc *vcc) { IADEV *iadev; u_short __iomem *vc_table; u_short __iomem *reass_ptr; IF_EVENT(printk("iadev: open_rx %d.%d\n", vcc->vpi, vcc->vci);) if (vcc->qos.rxtp.traffic_class == ATM_NONE) return 0; iadev = INPH_IA_DEV(vcc->dev); if (vcc->qos.rxtp.traffic_class == ATM_ABR) { if (iadev->phy_type & FE_25MBIT_PHY) { printk("IA: ABR not support\n"); return -EINVAL; } } /* Make only this VCI in the vc table valid and let all others be invalid entries */ vc_table = iadev->reass_ram+RX_VC_TABLE*iadev->memSize; vc_table += vcc->vci; /* mask the last 6 bits and OR it with 3 for 1K VCs */ *vc_table = vcc->vci << 6; /* Also keep a list of open rx vcs so that we can attach them with incoming PDUs later. */ if ((vcc->qos.rxtp.traffic_class == ATM_ABR) || (vcc->qos.txtp.traffic_class == ATM_ABR)) { srv_cls_param_t srv_p; init_abr_vc(iadev, &srv_p); ia_open_abr_vc(iadev, &srv_p, vcc, 0); } else { /* for UBR later may need to add CBR logic */ reass_ptr = iadev->reass_ram+REASS_TABLE*iadev->memSize; reass_ptr += vcc->vci; *reass_ptr = NO_AAL5_PKT; } if (iadev->rx_open[vcc->vci]) printk(KERN_CRIT DEV_LABEL "(itf %d): VCI %d already open\n", vcc->dev->number, vcc->vci); iadev->rx_open[vcc->vci] = vcc; return 0; } static int rx_init(struct atm_dev *dev) { IADEV *iadev; struct rx_buf_desc __iomem *buf_desc_ptr; unsigned long rx_pkt_start = 0; void *dle_addr; struct abr_vc_table *abr_vc_table; u16 *vc_table; u16 *reass_table; int i,j, vcsize_sel; u_short freeq_st_adr; u_short *freeq_start; iadev = INPH_IA_DEV(dev); // spin_lock_init(&iadev->rx_lock); /* Allocate 4k bytes - more aligned than needed (4k boundary) */ dle_addr = pci_alloc_consistent(iadev->pci, DLE_TOTAL_SIZE, &iadev->rx_dle_dma); if (!dle_addr) { printk(KERN_ERR DEV_LABEL "can't allocate DLEs\n"); goto err_out; } iadev->rx_dle_q.start = (struct dle *)dle_addr; iadev->rx_dle_q.read = iadev->rx_dle_q.start; iadev->rx_dle_q.write = iadev->rx_dle_q.start; iadev->rx_dle_q.end = (struct dle*)((unsigned long)dle_addr+sizeof(struct dle)*DLE_ENTRIES); /* the end of the dle q points to the entry after the last DLE that can be used. */ /* write the upper 20 bits of the start address to rx list address register */ /* We know this is 32bit bus addressed so the following is safe */ writel(iadev->rx_dle_dma & 0xfffff000, iadev->dma + IPHASE5575_RX_LIST_ADDR); IF_INIT(printk("Tx Dle list addr: 0x%08x value: 0x%0x\n", (u32)(iadev->dma+IPHASE5575_TX_LIST_ADDR), *(u32*)(iadev->dma+IPHASE5575_TX_LIST_ADDR)); printk("Rx Dle list addr: 0x%08x value: 0x%0x\n", (u32)(iadev->dma+IPHASE5575_RX_LIST_ADDR), *(u32*)(iadev->dma+IPHASE5575_RX_LIST_ADDR));) writew(0xffff, iadev->reass_reg+REASS_MASK_REG); writew(0, iadev->reass_reg+MODE_REG); writew(RESET_REASS, iadev->reass_reg+REASS_COMMAND_REG); /* Receive side control memory map ------------------------------- Buffer descr 0x0000 (736 - 23K) VP Table 0x5c00 (256 - 512) Except q 0x5e00 (128 - 512) Free buffer q 0x6000 (1K - 2K) Packet comp q 0x6800 (1K - 2K) Reass Table 0x7000 (1K - 2K) VC Table 0x7800 (1K - 2K) ABR VC Table 0x8000 (1K - 32K) */ /* Base address for Buffer Descriptor Table */ writew(RX_DESC_BASE >> 16, iadev->reass_reg+REASS_DESC_BASE); /* Set the buffer size register */ writew(iadev->rx_buf_sz, iadev->reass_reg+BUF_SIZE); /* Initialize each entry in the Buffer Descriptor Table */ iadev->RX_DESC_BASE_ADDR = iadev->reass_ram+RX_DESC_BASE*iadev->memSize; buf_desc_ptr = iadev->RX_DESC_BASE_ADDR; memset_io(buf_desc_ptr, 0, sizeof(*buf_desc_ptr)); buf_desc_ptr++; rx_pkt_start = iadev->rx_pkt_ram; for(i=1; i<=iadev->num_rx_desc; i++) { memset_io(buf_desc_ptr, 0, sizeof(*buf_desc_ptr)); buf_desc_ptr->buf_start_hi = rx_pkt_start >> 16; buf_desc_ptr->buf_start_lo = rx_pkt_start & 0x0000ffff; buf_desc_ptr++; rx_pkt_start += iadev->rx_buf_sz; } IF_INIT(printk("Rx Buffer desc ptr: 0x%0x\n", (u32)(buf_desc_ptr));) i = FREE_BUF_DESC_Q*iadev->memSize; writew(i >> 16, iadev->reass_reg+REASS_QUEUE_BASE); writew(i, iadev->reass_reg+FREEQ_ST_ADR); writew(i+iadev->num_rx_desc*sizeof(u_short), iadev->reass_reg+FREEQ_ED_ADR); writew(i, iadev->reass_reg+FREEQ_RD_PTR); writew(i+iadev->num_rx_desc*sizeof(u_short), iadev->reass_reg+FREEQ_WR_PTR); /* Fill the FREEQ with all the free descriptors. */ freeq_st_adr = readw(iadev->reass_reg+FREEQ_ST_ADR); freeq_start = (u_short *)(iadev->reass_ram+freeq_st_adr); for(i=1; i<=iadev->num_rx_desc; i++) { *freeq_start = (u_short)i; freeq_start++; } IF_INIT(printk("freeq_start: 0x%0x\n", (u32)freeq_start);) /* Packet Complete Queue */ i = (PKT_COMP_Q * iadev->memSize) & 0xffff; writew(i, iadev->reass_reg+PCQ_ST_ADR); writew(i+iadev->num_vc*sizeof(u_short), iadev->reass_reg+PCQ_ED_ADR); writew(i, iadev->reass_reg+PCQ_RD_PTR); writew(i, iadev->reass_reg+PCQ_WR_PTR); /* Exception Queue */ i = (EXCEPTION_Q * iadev->memSize) & 0xffff; writew(i, iadev->reass_reg+EXCP_Q_ST_ADR); writew(i + NUM_RX_EXCP * sizeof(RX_ERROR_Q), iadev->reass_reg+EXCP_Q_ED_ADR); writew(i, iadev->reass_reg+EXCP_Q_RD_PTR); writew(i, iadev->reass_reg+EXCP_Q_WR_PTR); /* Load local copy of FREEQ and PCQ ptrs */ iadev->rfL.fdq_st = readw(iadev->reass_reg+FREEQ_ST_ADR) & 0xffff; iadev->rfL.fdq_ed = readw(iadev->reass_reg+FREEQ_ED_ADR) & 0xffff ; iadev->rfL.fdq_rd = readw(iadev->reass_reg+FREEQ_RD_PTR) & 0xffff; iadev->rfL.fdq_wr = readw(iadev->reass_reg+FREEQ_WR_PTR) & 0xffff; iadev->rfL.pcq_st = readw(iadev->reass_reg+PCQ_ST_ADR) & 0xffff; iadev->rfL.pcq_ed = readw(iadev->reass_reg+PCQ_ED_ADR) & 0xffff; iadev->rfL.pcq_rd = readw(iadev->reass_reg+PCQ_RD_PTR) & 0xffff; iadev->rfL.pcq_wr = readw(iadev->reass_reg+PCQ_WR_PTR) & 0xffff; IF_INIT(printk("INIT:pcq_st:0x%x pcq_ed:0x%x pcq_rd:0x%x pcq_wr:0x%x", iadev->rfL.pcq_st, iadev->rfL.pcq_ed, iadev->rfL.pcq_rd, iadev->rfL.pcq_wr);) /* just for check - no VP TBL */ /* VP Table */ /* writew(0x0b80, iadev->reass_reg+VP_LKUP_BASE); */ /* initialize VP Table for invalid VPIs - I guess we can write all 1s or 0x000f in the entire memory space or something similar. */ /* This seems to work and looks right to me too !!! */ i = REASS_TABLE * iadev->memSize; writew((i >> 3), iadev->reass_reg+REASS_TABLE_BASE); /* initialize Reassembly table to I don't know what ???? */ reass_table = (u16 *)(iadev->reass_ram+i); j = REASS_TABLE_SZ * iadev->memSize; for(i=0; i < j; i++) *reass_table++ = NO_AAL5_PKT; i = 8*1024; vcsize_sel = 0; while (i != iadev->num_vc) { i /= 2; vcsize_sel++; } i = RX_VC_TABLE * iadev->memSize; writew(((i>>3) & 0xfff8) | vcsize_sel, iadev->reass_reg+VC_LKUP_BASE); vc_table = (u16 *)(iadev->reass_ram+RX_VC_TABLE*iadev->memSize); j = RX_VC_TABLE_SZ * iadev->memSize; for(i = 0; i < j; i++) { /* shift the reassembly pointer by 3 + lower 3 bits of vc_lkup_base register (=3 for 1K VCs) and the last byte is those low 3 bits. Shall program this later. */ *vc_table = (i << 6) | 15; /* for invalid VCI */ vc_table++; } /* ABR VC table */ i = ABR_VC_TABLE * iadev->memSize; writew(i >> 3, iadev->reass_reg+ABR_LKUP_BASE); i = ABR_VC_TABLE * iadev->memSize; abr_vc_table = (struct abr_vc_table *)(iadev->reass_ram+i); j = REASS_TABLE_SZ * iadev->memSize; memset ((char*)abr_vc_table, 0, j * sizeof(*abr_vc_table)); for(i = 0; i < j; i++) { abr_vc_table->rdf = 0x0003; abr_vc_table->air = 0x5eb1; abr_vc_table++; } /* Initialize other registers */ /* VP Filter Register set for VC Reassembly only */ writew(0xff00, iadev->reass_reg+VP_FILTER); writew(0, iadev->reass_reg+XTRA_RM_OFFSET); writew(0x1, iadev->reass_reg+PROTOCOL_ID); /* Packet Timeout Count related Registers : Set packet timeout to occur in about 3 seconds Set Packet Aging Interval count register to overflow in about 4 us */ writew(0xF6F8, iadev->reass_reg+PKT_TM_CNT ); i = (j >> 6) & 0xFF; j += 2 * (j - 1); i |= ((j << 2) & 0xFF00); writew(i, iadev->reass_reg+TMOUT_RANGE); /* initiate the desc_tble */ for(i=0; i<iadev->num_tx_desc;i++) iadev->desc_tbl[i].timestamp = 0; /* to clear the interrupt status register - read it */ readw(iadev->reass_reg+REASS_INTR_STATUS_REG); /* Mask Register - clear it */ writew(~(RX_FREEQ_EMPT|RX_PKT_RCVD), iadev->reass_reg+REASS_MASK_REG); skb_queue_head_init(&iadev->rx_dma_q); iadev->rx_free_desc_qhead = NULL; iadev->rx_open = kzalloc(4 * iadev->num_vc, GFP_KERNEL); if (!iadev->rx_open) { printk(KERN_ERR DEV_LABEL "itf %d couldn't get free page\n", dev->number); goto err_free_dle; } iadev->rxing = 1; iadev->rx_pkt_cnt = 0; /* Mode Register */ writew(R_ONLINE, iadev->reass_reg+MODE_REG); return 0; err_free_dle: pci_free_consistent(iadev->pci, DLE_TOTAL_SIZE, iadev->rx_dle_q.start, iadev->rx_dle_dma); err_out: return -ENOMEM; } /* The memory map suggested in appendix A and the coding for it. Keeping it around just in case we change our mind later. Buffer descr 0x0000 (128 - 4K) UBR sched 0x1000 (1K - 4K) UBR Wait q 0x2000 (1K - 4K) Commn queues 0x3000 Packet Ready, Trasmit comp(0x3100) (128 - 256) each extended VC 0x4000 (1K - 8K) ABR sched 0x6000 and ABR wait queue (1K - 2K) each CBR sched 0x7000 (as needed) VC table 0x8000 (1K - 32K) */ static void tx_intr(struct atm_dev *dev) { IADEV *iadev; unsigned short status; unsigned long flags; iadev = INPH_IA_DEV(dev); status = readl(iadev->seg_reg+SEG_INTR_STATUS_REG); if (status & TRANSMIT_DONE){ IF_EVENT(printk("Tansmit Done Intr logic run\n");) spin_lock_irqsave(&iadev->tx_lock, flags); ia_tx_poll(iadev); spin_unlock_irqrestore(&iadev->tx_lock, flags); writew(TRANSMIT_DONE, iadev->seg_reg+SEG_INTR_STATUS_REG); if (iadev->close_pending) wake_up(&iadev->close_wait); } if (status & TCQ_NOT_EMPTY) { IF_EVENT(printk("TCQ_NOT_EMPTY int received\n");) } } static void tx_dle_intr(struct atm_dev *dev) { IADEV *iadev; struct dle *dle, *cur_dle; struct sk_buff *skb; struct atm_vcc *vcc; struct ia_vcc *iavcc; u_int dle_lp; unsigned long flags; iadev = INPH_IA_DEV(dev); spin_lock_irqsave(&iadev->tx_lock, flags); dle = iadev->tx_dle_q.read; dle_lp = readl(iadev->dma+IPHASE5575_TX_LIST_ADDR) & (sizeof(struct dle)*DLE_ENTRIES - 1); cur_dle = (struct dle*)(iadev->tx_dle_q.start + (dle_lp >> 4)); while (dle != cur_dle) { /* free the DMAed skb */ skb = skb_dequeue(&iadev->tx_dma_q); if (!skb) break; /* Revenge of the 2 dle (skb + trailer) used in ia_pkt_tx() */ if (!((dle - iadev->tx_dle_q.start)%(2*sizeof(struct dle)))) { pci_unmap_single(iadev->pci, dle->sys_pkt_addr, skb->len, PCI_DMA_TODEVICE); } vcc = ATM_SKB(skb)->vcc; if (!vcc) { printk("tx_dle_intr: vcc is null\n"); spin_unlock_irqrestore(&iadev->tx_lock, flags); dev_kfree_skb_any(skb); return; } iavcc = INPH_IA_VCC(vcc); if (!iavcc) { printk("tx_dle_intr: iavcc is null\n"); spin_unlock_irqrestore(&iadev->tx_lock, flags); dev_kfree_skb_any(skb); return; } if (vcc->qos.txtp.pcr >= iadev->rate_limit) { if ((vcc->pop) && (skb->len != 0)) { vcc->pop(vcc, skb); } else { dev_kfree_skb_any(skb); } } else { /* Hold the rate-limited skb for flow control */ IA_SKB_STATE(skb) |= IA_DLED; skb_queue_tail(&iavcc->txing_skb, skb); } IF_EVENT(printk("tx_dle_intr: enque skb = 0x%x \n", (u32)skb);) if (++dle == iadev->tx_dle_q.end) dle = iadev->tx_dle_q.start; } iadev->tx_dle_q.read = dle; spin_unlock_irqrestore(&iadev->tx_lock, flags); } static int open_tx(struct atm_vcc *vcc) { struct ia_vcc *ia_vcc; IADEV *iadev; struct main_vc *vc; struct ext_vc *evc; int ret; IF_EVENT(printk("iadev: open_tx entered vcc->vci = %d\n", vcc->vci);) if (vcc->qos.txtp.traffic_class == ATM_NONE) return 0; iadev = INPH_IA_DEV(vcc->dev); if (iadev->phy_type & FE_25MBIT_PHY) { if (vcc->qos.txtp.traffic_class == ATM_ABR) { printk("IA: ABR not support\n"); return -EINVAL; } if (vcc->qos.txtp.traffic_class == ATM_CBR) { printk("IA: CBR not support\n"); return -EINVAL; } } ia_vcc = INPH_IA_VCC(vcc); memset((caddr_t)ia_vcc, 0, sizeof(*ia_vcc)); if (vcc->qos.txtp.max_sdu > (iadev->tx_buf_sz - sizeof(struct cpcs_trailer))){ printk("IA: SDU size over (%d) the configured SDU size %d\n", vcc->qos.txtp.max_sdu,iadev->tx_buf_sz); vcc->dev_data = NULL; kfree(ia_vcc); return -EINVAL; } ia_vcc->vc_desc_cnt = 0; ia_vcc->txing = 1; /* find pcr */ if (vcc->qos.txtp.max_pcr == ATM_MAX_PCR) vcc->qos.txtp.pcr = iadev->LineRate; else if ((vcc->qos.txtp.max_pcr == 0)&&( vcc->qos.txtp.pcr <= 0)) vcc->qos.txtp.pcr = iadev->LineRate; else if ((vcc->qos.txtp.max_pcr > vcc->qos.txtp.pcr) && (vcc->qos.txtp.max_pcr> 0)) vcc->qos.txtp.pcr = vcc->qos.txtp.max_pcr; if (vcc->qos.txtp.pcr > iadev->LineRate) vcc->qos.txtp.pcr = iadev->LineRate; ia_vcc->pcr = vcc->qos.txtp.pcr; if (ia_vcc->pcr > (iadev->LineRate / 6) ) ia_vcc->ltimeout = HZ / 10; else if (ia_vcc->pcr > (iadev->LineRate / 130)) ia_vcc->ltimeout = HZ; else if (ia_vcc->pcr <= 170) ia_vcc->ltimeout = 16 * HZ; else ia_vcc->ltimeout = 2700 * HZ / ia_vcc->pcr; if (ia_vcc->pcr < iadev->rate_limit) skb_queue_head_init (&ia_vcc->txing_skb); if (ia_vcc->pcr < iadev->rate_limit) { struct sock *sk = sk_atm(vcc); if (vcc->qos.txtp.max_sdu != 0) { if (ia_vcc->pcr > 60000) sk->sk_sndbuf = vcc->qos.txtp.max_sdu * 5; else if (ia_vcc->pcr > 2000) sk->sk_sndbuf = vcc->qos.txtp.max_sdu * 4; else sk->sk_sndbuf = vcc->qos.txtp.max_sdu * 3; } else sk->sk_sndbuf = 24576; } vc = (struct main_vc *)iadev->MAIN_VC_TABLE_ADDR; evc = (struct ext_vc *)iadev->EXT_VC_TABLE_ADDR; vc += vcc->vci; evc += vcc->vci; memset((caddr_t)vc, 0, sizeof(*vc)); memset((caddr_t)evc, 0, sizeof(*evc)); /* store the most significant 4 bits of vci as the last 4 bits of first part of atm header. store the last 12 bits of vci as first 12 bits of the second part of the atm header. */ evc->atm_hdr1 = (vcc->vci >> 12) & 0x000f; evc->atm_hdr2 = (vcc->vci & 0x0fff) << 4; /* check the following for different traffic classes */ if (vcc->qos.txtp.traffic_class == ATM_UBR) { vc->type = UBR; vc->status = CRC_APPEND; vc->acr = cellrate_to_float(iadev->LineRate); if (vcc->qos.txtp.pcr > 0) vc->acr = cellrate_to_float(vcc->qos.txtp.pcr); IF_UBR(printk("UBR: txtp.pcr = 0x%x f_rate = 0x%x\n", vcc->qos.txtp.max_pcr,vc->acr);) } else if (vcc->qos.txtp.traffic_class == ATM_ABR) { srv_cls_param_t srv_p; IF_ABR(printk("Tx ABR VCC\n");) init_abr_vc(iadev, &srv_p); if (vcc->qos.txtp.pcr > 0) srv_p.pcr = vcc->qos.txtp.pcr; if (vcc->qos.txtp.min_pcr > 0) { int tmpsum = iadev->sum_mcr+iadev->sum_cbr+vcc->qos.txtp.min_pcr; if (tmpsum > iadev->LineRate) return -EBUSY; srv_p.mcr = vcc->qos.txtp.min_pcr; iadev->sum_mcr += vcc->qos.txtp.min_pcr; } else srv_p.mcr = 0; if (vcc->qos.txtp.icr) srv_p.icr = vcc->qos.txtp.icr; if (vcc->qos.txtp.tbe) srv_p.tbe = vcc->qos.txtp.tbe; if (vcc->qos.txtp.frtt) srv_p.frtt = vcc->qos.txtp.frtt; if (vcc->qos.txtp.rif) srv_p.rif = vcc->qos.txtp.rif; if (vcc->qos.txtp.rdf) srv_p.rdf = vcc->qos.txtp.rdf; if (vcc->qos.txtp.nrm_pres) srv_p.nrm = vcc->qos.txtp.nrm; if (vcc->qos.txtp.trm_pres) srv_p.trm = vcc->qos.txtp.trm; if (vcc->qos.txtp.adtf_pres) srv_p.adtf = vcc->qos.txtp.adtf; if (vcc->qos.txtp.cdf_pres) srv_p.cdf = vcc->qos.txtp.cdf; if (srv_p.icr > srv_p.pcr) srv_p.icr = srv_p.pcr; IF_ABR(printk("ABR:vcc->qos.txtp.max_pcr = %d mcr = %d\n", srv_p.pcr, srv_p.mcr);) ia_open_abr_vc(iadev, &srv_p, vcc, 1); } else if (vcc->qos.txtp.traffic_class == ATM_CBR) { if (iadev->phy_type & FE_25MBIT_PHY) { printk("IA: CBR not support\n"); return -EINVAL; } if (vcc->qos.txtp.max_pcr > iadev->LineRate) { IF_CBR(printk("PCR is not availble\n");) return -1; } vc->type = CBR; vc->status = CRC_APPEND; if ((ret = ia_cbr_setup (iadev, vcc)) < 0) { return ret; } } else printk("iadev: Non UBR, ABR and CBR traffic not supportedn"); iadev->testTable[vcc->vci]->vc_status |= VC_ACTIVE; IF_EVENT(printk("ia open_tx returning \n");) return 0; } static int tx_init(struct atm_dev *dev) { IADEV *iadev; struct tx_buf_desc *buf_desc_ptr; unsigned int tx_pkt_start; void *dle_addr; int i; u_short tcq_st_adr; u_short *tcq_start; u_short prq_st_adr; u_short *prq_start; struct main_vc *vc; struct ext_vc *evc; u_short tmp16; u32 vcsize_sel; iadev = INPH_IA_DEV(dev); spin_lock_init(&iadev->tx_lock); IF_INIT(printk("Tx MASK REG: 0x%0x\n", readw(iadev->seg_reg+SEG_MASK_REG));) /* Allocate 4k (boundary aligned) bytes */ dle_addr = pci_alloc_consistent(iadev->pci, DLE_TOTAL_SIZE, &iadev->tx_dle_dma); if (!dle_addr) { printk(KERN_ERR DEV_LABEL "can't allocate DLEs\n"); goto err_out; } iadev->tx_dle_q.start = (struct dle*)dle_addr; iadev->tx_dle_q.read = iadev->tx_dle_q.start; iadev->tx_dle_q.write = iadev->tx_dle_q.start; iadev->tx_dle_q.end = (struct dle*)((unsigned long)dle_addr+sizeof(struct dle)*DLE_ENTRIES); /* write the upper 20 bits of the start address to tx list address register */ writel(iadev->tx_dle_dma & 0xfffff000, iadev->dma + IPHASE5575_TX_LIST_ADDR); writew(0xffff, iadev->seg_reg+SEG_MASK_REG); writew(0, iadev->seg_reg+MODE_REG_0); writew(RESET_SEG, iadev->seg_reg+SEG_COMMAND_REG); iadev->MAIN_VC_TABLE_ADDR = iadev->seg_ram+MAIN_VC_TABLE*iadev->memSize; iadev->EXT_VC_TABLE_ADDR = iadev->seg_ram+EXT_VC_TABLE*iadev->memSize; iadev->ABR_SCHED_TABLE_ADDR=iadev->seg_ram+ABR_SCHED_TABLE*iadev->memSize; /* Transmit side control memory map --------------------------------