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
 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
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
/* 3c527.c: 3Com Etherlink/MC32 driver for Linux
 *
 *	(c) Copyright 1998 Red Hat Software Inc
 *	Written by Alan Cox.
 *
 *	Based on skeleton.c written 1993-94 by Donald Becker and ne2.c
 *	(for the MCA stuff) written by Wim Dumon.
 *
 *	Thanks to 3Com for making this possible by providing me with the
 *	documentation.
 *
 *	This software may be used and distributed according to the terms
 *	of the GNU Public License, incorporated herein by reference.
 *
 */

static const char *version =
	"3c527.c:v0.07 2000/01/18 Alan Cox (alan@redhat.com)\n";

/**
 * DOC: Traps for the unwary
 *
 *	The diagram (Figure 1-1) and the POS summary disagree with the
 *	"Interrupt Level" section in the manual.
 *
 *	The documentation in places seems to miss things. In actual fact
 *	I've always eventually found everything is documented, it just
 *	requires careful study.
 *
 * DOC: Theory Of Operation
 *
 *	The 3com 3c527 is a 32bit MCA bus mastering adapter with a large
 *	amount of on board intelligence that housekeeps a somewhat dumber
 *	Intel NIC. For performance we want to keep the transmit queue deep
 *	as the card can transmit packets while fetching others from main
 *	memory by bus master DMA. Transmission and reception are driven by
 *	ring buffers. When updating the ring we are required to do some
 *	housekeeping work using the mailboxes and the command register.
 *
 *	The mailboxes provide a method for sending control requests to the
 *	card. The transmit mail box is used to update the transmit ring 
 *	pointers and the receive mail box to update the receive ring
 *	pointers. The exec mailbox allows a variety of commands to be
 *	executed. Each command must complete before the next is executed.
 *	Primarily we use the exec mailbox for controlling the multicast lists.
 *	We have to do a certain amount of interesting hoop jumping as the 
 *	multicast list changes can occur in interrupt state when the card
 *	has an exec command pending. We defer such events until the command
 *	completion interrupt.
 *
 *	The control register is used to pass status information. It tells us
 *	the transmit and receive status for packets and allows us to control
 *	the card operation mode. You must stop the card when emptying the
 *	receive ring, or you will race with the ring buffer and lose packets.
 */

#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/mca.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/malloc.h>
#include <linux/string.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <linux/errno.h>
#include <linux/init.h>

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>

#include "3c527.h"

/*
 * The name of the card. Is used for messages and in the requests for
 * io regions, irqs and dma channels
 */
static const char* cardname = "3c527";

/* use 0 for production, 1 for verification, >2 for debug */
#ifndef NET_DEBUG
#define NET_DEBUG 2
#endif
static unsigned int mc32_debug = NET_DEBUG;

/* The number of low I/O ports used by the ethercard. */
#define NETCARD_IO_EXTENT	8


struct mc32_mailbox
{
	u16	mbox __attribute((packed));
	u16	data[1] __attribute((packed));
};

/* Information that need to be kept for each board. */

#define TX_RING_MAX	16	/* Typically the card supports 37 */
#define RX_RING_MAX	32	/*        "     "       "         */

struct mc32_local 
{
	struct net_device_stats net_stats;
	int slot;
	volatile struct mc32_mailbox *rx_box;
	volatile struct mc32_mailbox *tx_box;
	volatile struct mc32_mailbox *exec_box;
	volatile u16 *stats;
	u16 tx_chain;
	u16 rx_chain;
	u16 tx_len;
	u16 rx_len;
	u32 base;
	u16 rx_halted;
	u16 tx_halted;
	u16 exec_pending;
	u16 mc_reload_wait;	/* a multicast load request is pending */
	atomic_t tx_count;		/* buffers left */
	wait_queue_head_t event;
	struct sk_buff *tx_skb[TX_RING_MAX];	/* Transmit ring */
	u16 tx_skb_top;
	u16 tx_skb_end;
	struct sk_buff *rx_skb[RX_RING_MAX];	/* Receive ring */
	void *rx_ptr[RX_RING_MAX];		/* Data pointers */
	u32 mc_list_valid;			/* True when the mclist is set */
};

/* The station (ethernet) address prefix, used for a sanity check. */
#define SA_ADDR0 0x02
#define SA_ADDR1 0x60
#define SA_ADDR2 0xAC

struct mca_adapters_t {
	unsigned int	id;
	char		*name;
};

const struct mca_adapters_t mc32_adapters[] = {
	{ 0x0041, "3COM EtherLink MC/32" },
	{ 0x8EF5, "IBM High Performance Lan Adapter" },
	{ 0x0000, NULL }
};


/* Index to functions, as function prototypes. */

extern int mc32_probe(struct net_device *dev);

static int	mc32_probe1(struct net_device *dev, int ioaddr);
static int	mc32_open(struct net_device *dev);
static void	mc32_timeout(struct net_device *dev);
static int	mc32_send_packet(struct sk_buff *skb, struct net_device *dev);
static void	mc32_interrupt(int irq, void *dev_id, struct pt_regs *regs);
static int	mc32_close(struct net_device *dev);
static struct	net_device_stats *mc32_get_stats(struct net_device *dev);
static void	mc32_set_multicast_list(struct net_device *dev);
static void	mc32_reset_multicast_list(struct net_device *dev);


/**
 * mc32_probe:
 * @dev: device to probe
 *
 * Because MCA bus is a real bus and we can scan for cards we could do a
 * single scan for all boards here. Right now we use the passed in device
 * structure and scan for only one board. This needs fixing for modules
 * in paticular.
 */

int __init mc32_probe(struct net_device *dev)
{
	static int current_mca_slot = -1;
	int i;
	int adapter_found = 0;

	/* Do not check any supplied i/o locations. 
	   POS registers usually don't fail :) */

	/* MCA cards have POS registers.  
	   Autodetecting MCA cards is extremely simple. 
	   Just search for the card. */

	for(i = 0; (mc32_adapters[i].name != NULL) && !adapter_found; i++) {
		current_mca_slot = 
			mca_find_unused_adapter(mc32_adapters[i].id, 0);

		if((current_mca_slot != MCA_NOTFOUND) && !adapter_found) {
			if(!mc32_probe1(dev, current_mca_slot))
			{
				mca_set_adapter_name(current_mca_slot, 
						mc32_adapters[i].name);
				mca_mark_as_used(current_mca_slot);
				return 0;
			}
			
		}
	}
	return -ENODEV;
}

/**
 * mc32_probe1:
 * @dev:  Device structure to fill in
 * @slot: The MCA bus slot being used by this card
 *
 * Decode the slot data and configure the card structures. Having done this we
 * can reset the card and configure it. The card does a full self test cycle
 * in firmware so we have to wait for it to return and post us either a 
 * failure case or some addresses we use to find the board internals.
 */
 
static int __init mc32_probe1(struct net_device *dev, int slot)
{
	static unsigned version_printed = 0;
	int i;
	u8 POS;
	u32 base;
	struct mc32_local *lp;
	static u16 mca_io_bases[]={
		0x7280,0x7290,
		0x7680,0x7690,
		0x7A80,0x7A90,
		0x7E80,0x7E90
	};
	static u32 mca_mem_bases[]={
		0x00C0000,
		0x00C4000,
		0x00C8000,
		0x00CC000,
		0x00D0000,
		0x00D4000,
		0x00D8000,
		0x00DC000
	};
	static char *failures[]={
		"Processor instruction",
		"Processor data bus",
		"Processor data bus",
		"Processor data bus",
		"Adapter bus",
		"ROM checksum",
		"Base RAM",
		"Extended RAM",
		"82586 internal loopback",
		"82586 initialisation failure",
		"Adapter list configuration error"
	};
	
	/* Time to play MCA games */

	if (mc32_debug  &&  version_printed++ == 0)
		printk(KERN_DEBUG "%s", version);

	printk(KERN_INFO "%s: %s found in slot %d:", dev->name, cardname, slot);

	POS = mca_read_stored_pos(slot, 2);
	
	if(!(POS&1))
	{
		printk(" disabled.\n");
		return -ENODEV;
	}

	/*
	 * Don't allocate the private data here, it is done later
	 * This makes it easier to free the memory when this driver
	 * is used as a module.
	 */

	if(dev==NULL)
	{
		dev = init_etherdev(0, 0);
		if (dev == NULL)
			return -ENOMEM;
	}

	/* Fill in the 'dev' fields. */
	dev->base_addr = mca_io_bases[(POS>>1)&7];
	dev->mem_start = mca_mem_bases[(POS>>4)&7];
	
	POS = mca_read_stored_pos(slot, 4);
	if(!(POS&1))
	{
		printk("memory window disabled.\n");
		return -ENODEV;
	}

	POS = mca_read_stored_pos(slot, 5);
	
	i=(POS>>4)&3;
	if(i==3)
	{
		printk("invalid memory window.\n");
		return -ENODEV;
	}
	
	i*=16384;
	i+=16384;
	
	dev->mem_end=dev->mem_start + i;
	
	dev->irq = ((POS>>2)&3)+9;
	
	printk("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
		dev->base_addr, dev->irq, dev->mem_start, i/1024);
	
	
	/* We ought to set the cache line size here.. */
	
	
	/*
	 *	Go PROM browsing
	 */
	 
	printk("%s: Address ", dev->name);
	 
	/* Retrieve and print the ethernet address. */
	for (i = 0; i < 6; i++)
	{
		mca_write_pos(slot, 6, i+12);
		mca_write_pos(slot, 7, 0);
	
		printk(" %2.2x", dev->dev_addr[i] = mca_read_pos(slot,3));
	}

	mca_write_pos(slot, 6, 0);
	mca_write_pos(slot, 7, 0);

	POS = mca_read_stored_pos(slot, 4);
	
	if(POS&2)
		printk(" : BNC port selected.\n");
	else 
		printk(" : AUI port selected.\n");
		
	POS=inb(dev->base_addr+HOST_CTRL);
	POS|=HOST_CTRL_ATTN|HOST_CTRL_RESET;
	POS&=~HOST_CTRL_INTE;
	outb(POS, dev->base_addr+HOST_CTRL);
	/* Reset adapter */
	udelay(100);
	/* Reset off */
	POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
	outb(POS, dev->base_addr+HOST_CTRL);
	
	udelay(300);
	
	/*
	 *	Grab the IRQ
	 */

	if(request_irq(dev->irq, &mc32_interrupt, 0, cardname, dev))
	{
		printk("%s: unable to get IRQ %d.\n",
				   dev->name, dev->irq);
		return -EAGAIN;
	}

	/* Initialize the device structure. */
	if (dev->priv == NULL) {
		dev->priv = kmalloc(sizeof(struct mc32_local), GFP_KERNEL);
		if (dev->priv == NULL)
		{
			free_irq(dev->irq, dev);
			return -ENOMEM;
		}
	}

	memset(dev->priv, 0, sizeof(struct mc32_local));
	lp = (struct mc32_local *)dev->priv;
	lp->slot = slot;

	i=0;

	base = inb(dev->base_addr);
	
	while(base==0xFF)
	{
		i++;
		if(i==1000)
		{
			printk("%s: failed to boot adapter.\n", dev->name);
			free_irq(dev->irq, dev);
			return -ENODEV;
		}
		udelay(1000);
		if(inb(dev->base_addr+2)&(1<<5))
			base = inb(dev->base_addr);
	}

	if(base>0)
	{
		if(base < 0x0C)
			printk("%s: %s%s.\n", dev->name, failures[base-1],
				base<0x0A?" test failure":"");
		else
			printk("%s: unknown failure %d.\n", dev->name, base);
		free_irq(dev->irq, dev);
		return -ENODEV;
	}
	
	base=0;
	for(i=0;i<4;i++)
	{
		int n=0;
	
		while(!(inb(dev->base_addr+2)&(1<<5)))
		{
			n++;
			udelay(50);
			if(n>100)
			{
				printk(KERN_ERR "%s: mailbox read fail (%d).\n", dev->name, i);
				free_irq(dev->irq, dev);
				return -ENODEV;
			}
		}

		base|=(inb(dev->base_addr)<<(8*i));
	}
	
	lp->exec_box=bus_to_virt(dev->mem_start+base);
	
	base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];
	
	lp->base = dev->mem_start+base;
	
	lp->rx_box=bus_to_virt(lp->base + lp->exec_box->data[2]);
	lp->tx_box=bus_to_virt(lp->base + lp->exec_box->data[3]);
	
	lp->stats = bus_to_virt(lp->base + lp->exec_box->data[5]);

	/*
	 *	Descriptor chains (card relative)
	 */
	 
	lp->tx_chain 		= lp->exec_box->data[8];
	lp->rx_chain 		= lp->exec_box->data[10];
	lp->tx_len 		= lp->exec_box->data[9];
	lp->rx_len 		= lp->exec_box->data[11];
	init_waitqueue_head(&lp->event);
	
	printk("%s: %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
		dev->name, lp->rx_len, lp->tx_len, lp->base);
	
	dev->open		= mc32_open;
	dev->stop		= mc32_close;
	dev->hard_start_xmit	= mc32_send_packet;
	dev->get_stats		= mc32_get_stats;
	dev->set_multicast_list = mc32_set_multicast_list;
	dev->tx_timeout		= mc32_timeout;
	dev->watchdog_timeo	= HZ*5;	/* Board does all the work */
	
	lp->rx_halted		= 1;
	lp->tx_halted		= 1;

	/* Fill in the fields of the device structure with ethernet values. */
	ether_setup(dev);
	return 0;
}


/**
 *	mc32_ring_poll:
 *	@dev:	The device to wait for
 *	
 *	Wait until a command we issues to the control register is completed.
 *	This actually takes very little time at all, which is fortunate as
 *	we often have to busy wait it.
 */
 
static void mc32_ring_poll(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
}



/**
 *	mc32_command_nowait:
 *	@dev: The 3c527 to issue the command to
 *	@cmd: The command word to write to the mailbox
 *	@data: A data block if the command expects one
 *	@len: Length of the data block
 *
 *	Send a command from interrupt state. If there is a command currently
 *	being executed then we return an error of -1. It simply isnt viable
 *	to wait around as commands may be slow. Providing we get in then
 *	we send the command and busy wait for the board to acknowledge that
 *	a command request is pending. We do not wait for the command to 
 *	complete, just for the card to admit to noticing it.  
 */

static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	int ioaddr = dev->base_addr;
	
	if(lp->exec_pending)
		return -1;
		
	lp->exec_pending=3;
	lp->exec_box->mbox=0;
	lp->exec_box->mbox=cmd;
	memcpy((void *)lp->exec_box->data, data, len);
	barrier();	/* the memcpy forgot the volatile so be sure */

	/* Send the command */
	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
	outb(1<<6, ioaddr+HOST_CMD);	
	return 0;
}


/**
 *	mc32_command: 
 *	@dev: The 3c527 card to issue the command to
 *	@cmd: The command word to write to the mailbox
 *	@data: A data block if the command expects one
 *	@len: Length of the data block
 *
 *	Sends exec commands in a user context. This permits us to wait around
 *	for the replies and also to wait for the command buffer to complete
 *	from a previous command before we execute our command. After our 
 *	command completes we will complete any pending multicast reload
 *	we blocked off by hogging the exec buffer.
 *
 *	You feed the card a command, you wait, it interrupts you get a 
 *	reply. All well and good. The complication arises because you use
 *	commands for filter list changes which come in at bh level from things
 *	like IPV6 group stuff.
 *
 *	We have a simple state machine
 *
 *	0	- nothing issued
 *
 *	1	- command issued, wait reply
 *
 *	2	- reply waiting - reader then goes to state 0
 *
 *	3	- command issued, trash reply. In which case the irq
 *		  takes it back to state 0
 *
 *	Send command and block for results. On completion spot and reissue
 *	multicasts
 */
  
static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	int ioaddr = dev->base_addr;
	unsigned long flags;
	int ret = 0;
	
	/*
	 *	Wait for a command
	 */
	 
	save_flags(flags);
	cli();
	 
	while(lp->exec_pending)
		sleep_on(&lp->event);
		
	/*
	 *	Issue mine
	 */

	lp->exec_pending=1;
	
	restore_flags(flags);
	
	lp->exec_box->mbox=0;
	lp->exec_box->mbox=cmd;
	memcpy((void *)lp->exec_box->data, data, len);
	barrier();	/* the memcpy forgot the volatile so be sure */

	/* Send the command */
	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
	outb(1<<6, ioaddr+HOST_CMD);	
	
	save_flags(flags);
	cli();
	while(lp->exec_pending!=2)
		sleep_on(&lp->event);
	lp->exec_pending=0;
	restore_flags(flags);
	
	 
	if(lp->exec_box->data[0]&(1<<13))
		ret = -1;
	/*
	 *	A multicast set got blocked - do it now
	 */
		
	if(lp->mc_reload_wait)
		mc32_reset_multicast_list(dev);

	return ret;
}


/**
 *	mc32_rx_abort:
 *	@dev: 3c527 to abort
 *
 *	Peforms a receive abort sequence on the card. In fact after some
 *	experimenting we now simply tell the card to suspend reception. When
 *	issuing aborts occasionally odd things happened.
 */
 
static void mc32_rx_abort(struct net_device *dev)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	int ioaddr = dev->base_addr;

	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
	
	lp->rx_box->mbox=0;
	outb(3<<3, ioaddr+HOST_CMD);	/* Suspend reception */
}

 
/**
 *	mc32_rx_begin:
 *	@dev: 3c527 to enable
 *
 *	We wait for any pending command to complete and then issue 
 *	a start reception command to the board itself. At this point 
 *	receive handling continues as it was before.
 */
 
static void mc32_rx_begin(struct net_device *dev)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	int ioaddr = dev->base_addr;
	
	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
	
	lp->rx_box->mbox=0;
	outb(1<<3, ioaddr+HOST_CMD);	/* GO */
	mc32_ring_poll(dev);	
	
	lp->rx_halted=0;
}

/**
 *	mc32_tx_abort:
 *	@dev: 3c527 to abort
 *
 *	Peforms a receive abort sequence on the card. In fact after some
 *	experimenting we now simply tell the card to suspend transmits . When
 *	issuing aborts occasionally odd things happened. In theory we want
 *	an abort to be sure we can recycle our buffers. As it happens we
 *	just have to be careful to shut the card down on close, and
 *	boot it carefully from scratch on setup.
 */
 
static void mc32_tx_abort(struct net_device *dev)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	int ioaddr = dev->base_addr;
	
	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
	
	lp->tx_box->mbox=0;
	outb(3, ioaddr+HOST_CMD);	/* Suspend */
	
	/* Ring empty */
	
	atomic_set(&lp->tx_count, lp->tx_len);
	
	/* Flush */
	if(lp->tx_skb_top!=lp->tx_skb_end)
	{
		int i;
		if(lp->tx_skb_top<=lp->tx_skb_end)
		{
			for(i=lp->tx_skb_top;i<lp->tx_skb_end;i++)
			{
				dev_kfree_skb(lp->tx_skb[i]);
				lp->tx_skb[i]=NULL;
			}
		}
		else
		{
			for(i=lp->tx_skb_end;i<TX_RING_MAX;i++)
			{
				dev_kfree_skb(lp->tx_skb[i]);
				lp->tx_skb[i]=NULL;
			}
			for(i=0;i<lp->tx_skb_top;i++)
			{
				dev_kfree_skb(lp->tx_skb[i]);
				lp->tx_skb[i]=NULL;
			}
		}
	}
	lp->tx_skb_top=lp->tx_skb_end=0;
}

/**
 *	mc32_tx_begin:
 *	@dev: 3c527 to enable
 *
 *	We wait for any pending command to complete and then issue 
 *	a start transmit command to the board itself. At this point 
 *	transmit handling continues as it was before. The ring must
 *	be setup before you do this and must have an end marker in it.
 *	It turns out we can avoid issuing this specific command when
 *	doing our setup so we avoid it.
 */
 
static void mc32_tx_begin(struct net_device *dev)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	int ioaddr = dev->base_addr;
	
	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
	
	lp->tx_box->mbox=0;
#if 0	
	outb(5, ioaddr+HOST_CMD);	/* GO */
	printk("TX=>5\n");
	mc32_ring_poll(dev);	
	if(lp->tx_box->mbox&(1<<13))
		printk("TX begin error!\n");
#endif		
	lp->tx_halted=0;
}

	
/**
 *	mc32_load_rx_ring:
 *	@dev: 3c527 to build the ring for
 *
 *	The card setups up the receive ring for us. We are required to
 *	use the ring it provides although we can change the size of the
 *	ring.
 *
 *	We allocate an sk_buff for each ring entry in turn and set the entry
 *	up for a single non s/g buffer. The first buffer we mark with the
 *	end marker bits. Finally we clear the rx mailbox.
 */
 
static int mc32_load_rx_ring(struct net_device *dev)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	int i;
	u16 base;
	volatile struct skb_header *p;
	
	base = lp->rx_box->data[0];
	
	/* Fix me - should use card size - also fix flush ! */ 

	for(i=0;i<RX_RING_MAX;i++)
	{
		lp->rx_skb[i]=alloc_skb(1532, GFP_KERNEL);
		if(lp->rx_skb[i]==NULL)
		{
			for(;i>=0;i--)
				kfree_skb(lp->rx_skb[i]);
			return -ENOBUFS;
		}
		lp->rx_ptr[i]=lp->rx_skb[i]->data+18;
		
		p=bus_to_virt(lp->base+base);
		p->control=0;
		p->data = virt_to_bus(lp->rx_ptr[i]);
		p->status=0;
		p->length = 1532;
		base = p->next;
	}
	p->control = (1<<6);
	lp->rx_box->mbox = 0;
	return 0;
}	

/**
 *	mc32_flush_rx_ring:
 *	@lp: Local data of 3c527 to flush the rx ring of
 *
 *	Free the buffer for each ring slot. Because of the receive 
 *	algorithm we use the ring will always be loaded will a full set
 *	of buffers.
 */

static void mc32_flush_rx_ring(struct mc32_local *lp)
{
	int i;
	for(i=0;i<RX_RING_MAX;i++)
		kfree_skb(lp->rx_skb[i]);
}

/**
 *	mc32_flush_tx_ring:
 *	@lp: Local data of 3c527 to flush the tx ring of
 *
 *	We have to consider two cases here. We want to free the pending
 *	buffers only. If the ring buffer head is past the start then the
 *	ring segment we wish to free wraps through zero.
 */

static void mc32_flush_tx_ring(struct mc32_local *lp)
{
	int i;
	
	if(lp->tx_skb_top <= lp->tx_skb_end)
	{
		for(i=lp->tx_skb_top;i<lp->tx_skb_end;i++)
			dev_kfree_skb(lp->tx_skb[i]);
	}
	else
	{
		for(i=0;i<lp->tx_skb_end;i++)
			dev_kfree_skb(lp->tx_skb[i]);
		for(i=lp->tx_skb_top;i<TX_RING_MAX;i++)
			dev_kfree_skb(lp->tx_skb[i]);
	}
}
 	
/**
 *	mc32_open
 *	@dev: device to open
 *
 *	The user is trying to bring the card into ready state. This requires
 *	a brief dialogue with the card. Firstly we enable interrupts and then
 *	'indications'. Without these enabled the card doesn't bother telling
 *	us what it has done. This had me puzzled for a week.
 *
 *	We then load the network address and multicast filters. Turn on the
 *	workaround mode. This works around a bug in the 82586 - it asks the
 *	firmware to do so. It has a performance hit but is needed on busy
 *	[read most] lans. We load the ring with buffers then we kick it
 *	all off.
 */

static int mc32_open(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	u16 zero_word=0;
	u8 one=1;
	u8 regs;
	
	/*
	 *	Interrupts enabled
	 */

	regs=inb(ioaddr+HOST_CTRL);
	regs|=HOST_CTRL_INTE;
	outb(regs, ioaddr+HOST_CTRL);
	

	/*
	 *	Send the indications on command
	 */

	mc32_command(dev, 4, &one, 2);

	 	
	/*
	 *	Send the command sequence "abort, resume" for RX and TX.
	 *	The abort cleans up the buffer chains if needed.
	 */

	mc32_rx_abort(dev);
	mc32_tx_abort(dev);
	
	/* Set Network Address */
	mc32_command(dev, 1, dev->dev_addr, 6);
	
	/* Set the filters */
	mc32_set_multicast_list(dev);
	
	/* Issue the 82586 workaround command - this is for "busy lans",
	   but basically means for all lans now days - has a performance
	   cost but best set */
	   
	mc32_command(dev, 0x0D, &zero_word, 2); /* 82586 bug workaround on */
	
	/* Load the ring we just initialised */
	
	if(mc32_load_rx_ring(dev))
	{
		mc32_close(dev);
		return -ENOBUFS;
	}
	
	/* And the resume command goes last */
	
	mc32_rx_begin(dev);
	mc32_tx_begin(dev);

	netif_start_queue(dev);	
	MOD_INC_USE_COUNT;

	return 0;
}

/**
 *	mc32_timeout:
 *	@dev: 3c527 that timed out
 *
 *	Handle a timeout on transmit from the 3c527. This normally means
 *	bad things as the hardware handles cable timeouts and mess for
 *	us.
 *
 */

static void mc32_timeout(struct net_device *dev)
{
	printk(KERN_WARNING "%s: transmit timed out?\n", dev->name);
	/* Try to restart the adaptor. */
	netif_wake_queue(dev);
}
 
/**
 *	mc32_send_packet:
 *	@skb: buffer to transmit
 *	@dev: 3c527 to send it out of
 *
 *	Transmit a buffer. This normally means throwing the buffer onto
 *	the transmit queue as the queue is quite large. If the queue is
 *	full then we set tx_busy and return. Once the interrupt handler
 *	gets messages telling it to reclaim transmit queue entries we will
 *	clear tx_busy and the kernel will start calling this again.
 *
 *	We use cli rather than spinlocks. Since I have no access to an SMP
 *	MCA machine I don't plan to change it. It is probably the top 
 *	performance hit for this driver on SMP however.
 */
 
static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	unsigned long flags;
		
	u16 tx_head;
	volatile struct skb_header *p, *np;

	netif_stop_queue(dev);
	
	save_flags(flags);
	cli();
		
	if(atomic_read(&lp->tx_count)==0)
	{
		restore_flags(flags);
		return 1;
	}

	tx_head = lp->tx_box->data[0];
	atomic_dec(&lp->tx_count);
	/* We will need this to flush the buffer out */
	
	lp->tx_skb[lp->tx_skb_end] = skb;
	lp->tx_skb_end++;
	lp->tx_skb_end&=(TX_RING_MAX-1);
	
	/* P is the last sending/sent buffer as a pointer */
	p=(struct skb_header *)bus_to_virt(lp->base+tx_head);
	
	/* NP is the buffer we will be loading */
	np=(struct skb_header *)bus_to_virt(lp->base+p->next);
		
	np->control	|= (1<<6);	/* EOL */
	wmb();
		
	np->length	= skb->len;
		
	if(np->length < 60)
		np->length = 60;
			
	np->data	= virt_to_bus(skb->data);
	np->status	= 0;
	np->control	= (1<<7)|(1<<6);	/* EOP EOL */
	wmb();
		
	p->status	= 0;
	p->control	&= ~(1<<6);
	
	lp->tx_box->mbox=0;
	restore_flags(flags);
	
	netif_wake_queue(dev);
	return 0;
}

/**
 *	mc32_update_stats:
 *	@dev: 3c527 to service
 *
 *	When the board signals us that its statistics need attention we
 *	should query the table and clear it. In actual fact we currently
 *	track all our statistics in software and I haven't implemented it yet.
 */
 
static void mc32_update_stats(struct net_device *dev)
{
}

/**
 *	mc32_rx_ring:
 *	@dev: 3c527 that needs its receive ring processing
 *
 *	We have received one or more indications from the card that
 *	a receive has completed. The ring buffer thus contains dirty
 *	entries. Firstly we tell the card to stop receiving, then We walk 
 *	the ring from the first filled entry, which is pointed to by the 
 *	card rx mailbox and for each completed packet we will either copy 
 *	it and pass it up the stack or if the packet is near MTU sized we 
 *	allocate another buffer and flip the old one up the stack.
 *
 *	We must succeed in keeping a buffer on the ring. If neccessary we
 *	will toss a received packet rather than lose a ring entry. Once the
 *	first packet that is unused is found we reload the mailbox with the
 *	buffer so that the card knows it can use the buffers again. Finally
 *	we set it receiving again. 
 *
 *	We must stop reception during the ring walk. I thought it would be
 *	neat to avoid it by clever tricks, but it turns out the event order
 *	on the card means you have to play by the manual.
 */
 
static void mc32_rx_ring(struct net_device *dev)
{
	struct mc32_local *lp=dev->priv;
	int ioaddr = dev->base_addr;
	int x=0;
	volatile struct skb_header *p;
	u16 base;
	u16 top;

	/* Halt RX before walking the ring */
	
	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
	outb(3<<3, ioaddr+HOST_CMD);
	while(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR);
	
	top = base = lp->rx_box->data[0];
	do
	{
		p=(struct skb_header *)bus_to_virt(base+lp->base);
		if(!(p->status & (1<<7)))
			break;
		if(p->status & (1<<6))
		{
			u16 length = p->length;
			struct sk_buff *skb=dev_alloc_skb(length+2);
			if(skb!=NULL)
			{
				skb_reserve(skb,2);
				/*printk("Frame at %p\n", bus_to_virt(p->data)); */
				memcpy(skb_put(skb, length),
					bus_to_virt(p->data), length);
				skb->protocol=eth_type_trans(skb,dev);
				skb->dev=dev;
				lp->net_stats.rx_packets++;
				lp->net_stats.rx_bytes+=skb->len;
				netif_rx(skb);
			}
			else
				lp->net_stats.rx_dropped++;
		}
		else
		{
			lp->net_stats.rx_errors++;
			switch(p->status&0x0F)
			{
				case 1:
					lp->net_stats.rx_crc_errors++;break;
				case 2:
					lp->net_stats.rx_fifo_errors++;break;
				case 3:
					lp->net_stats.rx_frame_errors++;break;
				case 4:
					lp->net_stats.rx_missed_errors++;break;
				case 5:
					lp->net_stats.rx_length_errors++;break;
			}
		}
		p->length = 1532;
		p->control &= ~(1<<6);
		p->status = 0;
		base = p->next;
	}
	while(x++<48);
	
	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
	lp->rx_box->mbox=0;
	lp->rx_box->data[0] = top;
	outb(1<<3, ioaddr+HOST_CMD);	
}


/**
 *	mc32_interrupt:
 *	@irq: Interrupt number
 *	@dev_id: 3c527 that requires servicing
 *	@regs: Registers (unused)
 *
 *	The 3c527 interrupts us for four reasons. The command register 
 *	contains the message it wishes to send us packed into a single
 *	byte field. We keep reading status entries until we have processed
 *	all the transmit and control items, but simply count receive
 *	reports. When the receive reports are in we can call the mc32_rx_ring
 *	and empty the ring. This saves the overhead of multiple command requests
 */
 
static void mc32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
{
	struct net_device *dev = dev_id;
	struct mc32_local *lp;
	int ioaddr, status, boguscount = 0;
	int rx_event = 0;
	
	if (dev == NULL) {
		printk(KERN_WARNING "%s: irq %d for unknown device.\n", cardname, irq);
		return;
	}
	ioaddr = dev->base_addr;
	lp = (struct mc32_local *)dev->priv;

	/* See whats cooking */
	
	while((inb(ioaddr+2)&(1<<5)) && boguscount++<2000)
	{
		status=inb(ioaddr+HOST_CMD);

#ifdef DEBUG_IRQ		
		printk("Status TX%d RX%d EX%d OV%d\n",
			(status&7), (status>>3)&7, (status>>6)&1,
			(status>>7)&1);
#endif
			
		switch(status&7)
		{
			case 0:
				break;
			case 6: /* TX fail */
				lp->net_stats.tx_errors++;
			case 2:	/* TX ok */
				lp->net_stats.tx_packets++;
				/* Packets are sent in order - this is
				   basically a FIFO queue of buffers matching
				   the card ring */
				lp->net_stats.tx_bytes+=lp->tx_skb[lp->tx_skb_top]->len;
				dev_kfree_skb_irq(lp->tx_skb[lp->tx_skb_top]);
				lp->tx_skb[lp->tx_skb_top]=NULL;
				lp->tx_skb_top++;
				lp->tx_skb_top&=(TX_RING_MAX-1);
				atomic_inc(&lp->tx_count);
				netif_wake_queue(dev);
				break;
			case 3: /* Halt */
			case 4: /* Abort */
				lp->tx_halted=1;
				wake_up(&lp->event);
				break;
			case 5:
				lp->tx_halted=0;
				wake_up(&lp->event);
				break;
			default:
				printk("%s: strange tx ack %d\n", 
					dev->name, status&7);
		}
		status>>=3;
		switch(status&7)
		{
			case 0:
				break;
			case 2:	/* RX */
				rx_event=1;
				break;
			case 3:
			case 4:
				lp->rx_halted=1;
				wake_up(&lp->event);
				break;
			case 5:
				lp->rx_halted=0;
				wake_up(&lp->event);
				break;
			case 6:
				/* Out of RX buffers stat */
				/* Must restart */
				lp->net_stats.rx_dropped++;
				rx_event = 1; 	/* To restart */
				break;
			default:
				printk("%s: strange rx ack %d\n", 
					dev->name, status&7);
			
		}
		status>>=3;
		if(status&1)
		{
			/* 0=no 1=yes 2=replied, get cmd, 3 = wait reply & dump it */
			if(lp->exec_pending!=3)
				lp->exec_pending=2;
			else
				lp->exec_pending=0;
			wake_up(&lp->event);
		}
		if(status&2)
		{
			/*
			 *	Update the stats as soon as
			 *	we have it flagged and can 
			 *	send an immediate reply (CRR set)
			 */
			 
			if(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR)
			{
				mc32_update_stats(dev);
				outb(0, ioaddr+HOST_CMD);
			}
		}
	}
	
	/*
	 *	Process and restart the receive ring.
	 */
	 
	if(rx_event)
		mc32_rx_ring(dev);
	return;
}


/**
 *	mc32_close:
 *	@dev: 3c527 card to shut down
 *
 *	The 3c527 is a bus mastering device. We must be careful how we
 *	shut it down. It may also be running shared interrupt so we have
 *	to be sure to silence it properly
 *
 *	We abort any receive and transmits going on and then wait until
 *	any pending exec commands have completed in other code threads.
 *	In theory we can't get here while that is true, in practice I am
 *	paranoid
 *
 *	We turn off the interrupt enable for the board to be sure it can't
 *	intefere with other devices.
 */
 
static int mc32_close(struct net_device *dev)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	int ioaddr = dev->base_addr;
	u8 regs;
	u16 one=1;

	netif_stop_queue(dev);
	
	/*
	 *	Send the indications on command (handy debug check)
	 */

	mc32_command(dev, 4, &one, 2);

	/* Abort RX and Abort TX */
	
	mc32_rx_abort(dev);	
	mc32_tx_abort(dev);
	
	/* Catch any waiting commands */
	
	while(lp->exec_pending==1)
		sleep_on(&lp->event);
		
	/* Ok the card is now stopping */	
	
	regs=inb(ioaddr+HOST_CTRL);
	regs&=~HOST_CTRL_INTE;
	outb(regs, ioaddr+HOST_CTRL);

	mc32_flush_rx_ring(lp);
	mc32_flush_tx_ring(lp);
	
	/* Update the statistics here. */

	MOD_DEC_USE_COUNT;

	return 0;

}

/**
 *	mc32_get_stats:
 *	@dev: The 3c527 card to handle
 *
 *	As we currently handle our statistics in software this one is
 *	easy to handle. With hardware statistics it will get messy
 *	as the get_stats call will need to send exec mailbox messages and
 *	need to lock out the multicast reloads.
 */

static struct net_device_stats *mc32_get_stats(struct net_device *dev)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	return &lp->net_stats;
}

/**
 *	do_mc32_set_multicast_list:
 *	@dev: 3c527 device to load the list on
 *	@retry: indicates this is not the first call. 
 *
 * Actually set or clear the multicast filter for this adaptor. The locking
 * issues are handled by this routine. We have to track state as it may take
 * multiple calls to get the command sequence completed. We just keep trying
 * to schedule the loads until we manage to process them all.
 *
 * num_addrs == -1	Promiscuous mode, receive all packets
 *
 * num_addrs == 0	Normal mode, clear multicast list
 *
 * num_addrs > 0	Multicast mode, receive normal and MC packets,
 *			and do best-effort filtering.
 */

static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
{
	struct mc32_local *lp = (struct mc32_local *)dev->priv;
	u16 filt;

	if (dev->flags&IFF_PROMISC)
		/* Enable promiscuous mode */
		filt = 1;
	else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > 10)
	{
		dev->flags|=IFF_PROMISC;
		filt = 1;
	}
	else if(dev->mc_count)
	{
		unsigned char block[62];
		unsigned char *bp;
		struct dev_mc_list *dmc=dev->mc_list;
		
		int i;
		
		filt = 0;
		
		if(retry==0)
			lp->mc_list_valid = 0;
		if(!lp->mc_list_valid)
		{
			block[1]=0;
			block[0]=dev->mc_count;
			bp=block+2;
		
			for(i=0;i<dev->mc_count;i++)
			{
				memcpy(bp, dmc->dmi_addr, 6);
				bp+=6;
				dmc=dmc->next;
			}
			if(mc32_command_nowait(dev, 2, block, 2+6*dev->mc_count)==-1)
			{
				lp->mc_reload_wait = 1;
				return;
			}
			lp->mc_list_valid=1;
		}
	}
	else 
	{
		filt = 0;
	}
	if(mc32_command_nowait(dev, 0, &filt, 2)==-1)
	{
		lp->mc_reload_wait = 1;
	}
}

/**
 *	mc32_set_multicast_list:
 *	@dev: The 3c527 to use
 *
 *	Commence loading the multicast list. This is called when the kernel
 *	changes the lists. It will override any pending list we are trying to
 *	load.
 */
 
static void mc32_set_multicast_list(struct net_device *dev)
{
	do_mc32_set_multicast_list(dev,0);
}

/**
 *	mc32_reset_multicast_list:
 *	@dev: The 3c527 to use
 *
 *	Attempt the next step in loading the multicast lists. If this attempt
 *	fails to complete then it will be scheduled and this function called
 *	again later from elsewhere.
 */
 

static void mc32_reset_multicast_list(struct net_device *dev)
{
	do_mc32_set_multicast_list(dev,1);
}

#ifdef MODULE

static char devicename[9] = { 0, };
static struct net_device this_device = {
	devicename, /* will be inserted by linux/drivers/net/mc32_init.c */
	0, 0, 0, 0,
	0, 0,  /* I/O address, IRQ */
	0, 0, 0, NULL, mc32_probe };


/**
 *	init_module:
 *
 *	Probe and locate a 3c527 card. This really should probe and locate
 *	all the 3c527 cards in the machine not just one of them. Yes you can
 *	insmod multiple modules for now but its a hack.
 */
 
int init_module(void)
{
	int result;

	if ((result = register_netdev(&this_device)) != 0)
		return result;

	return 0;
}

/**
 *	cleanup_module:
 *
 *	Unloading time. We release the MCA bus resources and the interrupt
 *	at which point everything is ready to unload. The card must be stopped
 *	at this point or we would not have been called. When we unload we
 *	leave the card stopped but not totally shut down. When the card is
 *	initialized it must be rebooted or the rings reloaded before any
 *	transmit operations are allowed to start scribbling into memory.
 */
 
void cleanup_module(void)
{
	int slot;
	
	/* No need to check MOD_IN_USE, as sys_delete_module() checks. */
	unregister_netdev(&this_device);

	/*
	 * If we don't do this, we can't re-insmod it later.
	 */
	 
	if (this_device.priv)
	{
		struct mc32_local *lp=this_device.priv;
		slot = lp->slot;
		mca_mark_as_unused(slot);
		mca_set_adapter_name(slot, NULL);
		kfree_s(this_device.priv, sizeof(struct mc32_local));
	}
	free_irq(this_device.irq, &this_device);
}

#endif /* MODULE */