aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/block/cciss.c77
-rw-r--r--drivers/block/cciss.h1
2 files changed, 46 insertions, 32 deletions
diff --git a/drivers/block/cciss.c b/drivers/block/cciss.c
index 2d128831d3c..70017071106 100644
--- a/drivers/block/cciss.c
+++ b/drivers/block/cciss.c
@@ -2292,26 +2292,12 @@ static int check_target_status(ctlr_info_t *h, CommandList_struct *c)
2292 return IO_ERROR; 2292 return IO_ERROR;
2293} 2293}
2294 2294
2295static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c) 2295static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c)
2296{ 2296{
2297 DECLARE_COMPLETION_ONSTACK(wait);
2298 u64bit buff_dma_handle;
2299 unsigned long flags;
2300 int return_status = IO_OK; 2297 int return_status = IO_OK;
2301 2298
2302resend_cmd2: 2299 if (c->err_info->CommandStatus == CMD_SUCCESS)
2303 c->waiting = &wait; 2300 return IO_OK;
2304 /* Put the request on the tail of the queue and send it */
2305 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2306 addQ(&h->reqQ, c);
2307 h->Qdepth++;
2308 start_io(h);
2309 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2310
2311 wait_for_completion(&wait);
2312
2313 if (c->err_info->CommandStatus == 0)
2314 goto command_done;
2315 2301
2316 switch (c->err_info->CommandStatus) { 2302 switch (c->err_info->CommandStatus) {
2317 case CMD_TARGET_STATUS: 2303 case CMD_TARGET_STATUS:
@@ -2322,7 +2308,7 @@ resend_cmd2:
2322 /* expected for inquiry and report lun commands */ 2308 /* expected for inquiry and report lun commands */
2323 break; 2309 break;
2324 case CMD_INVALID: 2310 case CMD_INVALID:
2325 printk(KERN_WARNING "cciss: Cmd 0x%02x is " 2311 printk(KERN_WARNING "cciss: cmd 0x%02x is "
2326 "reported invalid\n", c->Request.CDB[0]); 2312 "reported invalid\n", c->Request.CDB[0]);
2327 return_status = IO_ERROR; 2313 return_status = IO_ERROR;
2328 break; 2314 break;
@@ -2355,19 +2341,7 @@ resend_cmd2:
2355 printk(KERN_WARNING 2341 printk(KERN_WARNING
2356 "cciss%d: unsolicited abort 0x%02x\n", h->ctlr, 2342 "cciss%d: unsolicited abort 0x%02x\n", h->ctlr,
2357 c->Request.CDB[0]); 2343 c->Request.CDB[0]);
2358 if (c->retry_count < MAX_CMD_RETRIES) { 2344 return_status = IO_NEEDS_RETRY;
2359 printk(KERN_WARNING
2360 "cciss%d: retrying 0x%02x\n", h->ctlr,
2361 c->Request.CDB[0]);
2362 c->retry_count++;
2363 /* erase the old error information */
2364 memset(c->err_info, 0,
2365 sizeof(ErrorInfo_struct));
2366 return_status = IO_OK;
2367 INIT_COMPLETION(wait);
2368 goto resend_cmd2;
2369 }
2370 return_status = IO_ERROR;
2371 break; 2345 break;
2372 default: 2346 default:
2373 printk(KERN_WARNING "cciss: cmd 0x%02x returned " 2347 printk(KERN_WARNING "cciss: cmd 0x%02x returned "
@@ -2375,6 +2349,44 @@ resend_cmd2:
2375 c->err_info->CommandStatus); 2349 c->err_info->CommandStatus);
2376 return_status = IO_ERROR; 2350 return_status = IO_ERROR;
2377 } 2351 }
2352 return return_status;
2353}
2354
2355static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
2356 int attempt_retry)
2357{
2358 DECLARE_COMPLETION_ONSTACK(wait);
2359 u64bit buff_dma_handle;
2360 unsigned long flags;
2361 int return_status = IO_OK;
2362
2363resend_cmd2:
2364 c->waiting = &wait;
2365 /* Put the request on the tail of the queue and send it */
2366 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2367 addQ(&h->reqQ, c);
2368 h->Qdepth++;
2369 start_io(h);
2370 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2371
2372 wait_for_completion(&wait);
2373
2374 if (c->err_info->CommandStatus == 0 || !attempt_retry)
2375 goto command_done;
2376
2377 return_status = process_sendcmd_error(h, c);
2378
2379 if (return_status == IO_NEEDS_RETRY &&
2380 c->retry_count < MAX_CMD_RETRIES) {
2381 printk(KERN_WARNING "cciss%d: retrying 0x%02x\n", h->ctlr,
2382 c->Request.CDB[0]);
2383 c->retry_count++;
2384 /* erase the old error information */
2385 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2386 return_status = IO_OK;
2387 INIT_COMPLETION(wait);
2388 goto resend_cmd2;
2389 }
2378 2390
2379command_done: 2391command_done:
2380 /* unlock the buffers from DMA */ 2392 /* unlock the buffers from DMA */
@@ -2399,7 +2411,8 @@ static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
2399 return_status = fill_cmd(c, cmd, ctlr, buff, size, page_code, 2411 return_status = fill_cmd(c, cmd, ctlr, buff, size, page_code,
2400 scsi3addr, cmd_type); 2412 scsi3addr, cmd_type);
2401 if (return_status == IO_OK) 2413 if (return_status == IO_OK)
2402 return_status = sendcmd_withirq_core(h, c); 2414 return_status = sendcmd_withirq_core(h, c, 1);
2415
2403 cmd_free(h, c, 0); 2416 cmd_free(h, c, 0);
2404 return return_status; 2417 return return_status;
2405} 2418}
diff --git a/drivers/block/cciss.h b/drivers/block/cciss.h
index dd1926d8cd9..25cd58e2502 100644
--- a/drivers/block/cciss.h
+++ b/drivers/block/cciss.h
@@ -11,6 +11,7 @@
11 11
12#define IO_OK 0 12#define IO_OK 0
13#define IO_ERROR 1 13#define IO_ERROR 1
14#define IO_NEEDS_RETRY 3
14 15
15#define VENDOR_LEN 8 16#define VENDOR_LEN 8
16#define MODEL_LEN 16 17#define MODEL_LEN 16
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








































































































                                                                                        























































































                                                                                                                       
                                                        


























































































































































































                                                                                              



























































































































































































































































                                                                                                                    
 



































































































































































































                                                                                                                              
                                                       
































































































                                                                                                                                        
                                                                    
                                                                                                                                 



















































































































































































































































                                                                                                                                                                                           
                                                                                                 







































































































































                                                                                                                                                                  
                            









                                                                                                                             
                                














































                                                                                      
/*
 * net-3-driver for the NI5210 card (i82586 Ethernet chip)
 *
 * This is an extension to the Linux operating system, and is covered by the
 * same GNU General Public License that covers that work.
 *
 * Alphacode 0.82 (96/09/29) for Linux 2.0.0 (or later)
 * Copyrights (c) 1994,1995,1996 by M.Hipp (hippm@informatik.uni-tuebingen.de)
 *    [feel free to mail ....]
 *
 * when using as module: (no autoprobing!)
 *   compile with:
 *       gcc -O2 -fomit-frame-pointer -m486 -D__KERNEL__ -DMODULE -c ni52.c
 *   run with e.g:
 *       insmod ni52.o io=0x360 irq=9 memstart=0xd0000 memend=0xd4000
 *
 * CAN YOU PLEASE REPORT ME YOUR PERFORMANCE EXPERIENCES !!.
 *
 * If you find a bug, please report me:
 *   The kernel panic output and any kmsg from the ni52 driver
 *   the ni5210-driver-version and the linux-kernel version
 *   how many shared memory (memsize) on the netcard,
 *   bootprom: yes/no, base_addr, mem_start
 *   maybe the ni5210-card revision and the i82586 version
 *
 * autoprobe for: base_addr: 0x300,0x280,0x360,0x320,0x340
 *                mem_start: 0xd0000,0xd2000,0xc8000,0xca000,0xd4000,0xd6000,
 *                           0xd8000,0xcc000,0xce000,0xda000,0xdc000
 *
 * sources:
 *   skeleton.c from Donald Becker
 *
 * I have also done a look in the following sources: (mail me if you need them)
 *   crynwr-packet-driver by Russ Nelson
 *   Garret A. Wollman's (fourth) i82586-driver for BSD
 *   (before getting an i82596 (yes 596 not 586) manual, the existing drivers helped
 *    me a lot to understand this tricky chip.)
 *
 * Known Problems:
 *   The internal sysbus seems to be slow. So we often lose packets because of
 *   overruns while receiving from a fast remote host.
 *   This can slow down TCP connections. Maybe the newer ni5210 cards are better.
 *   my experience is, that if a machine sends with more than about 500-600K/s
 *   the fifo/sysbus overflows.
 *
 * IMPORTANT NOTE:
 *   On fast networks, it's a (very) good idea to have 16K shared memory. With
 *   8K, we can store only 4 receive frames, so it can (easily) happen that a remote
 *   machine 'overruns' our system.
 *
 * Known i82586/card problems (I'm sure, there are many more!):
 *   Running the NOP-mode, the i82586 sometimes seems to forget to report
 *   every xmit-interrupt until we restart the CU.
 *   Another MAJOR bug is, that the RU sometimes seems to ignore the EL-Bit
 *   in the RBD-Struct which indicates an end of the RBD queue.
 *   Instead, the RU fetches another (randomly selected and
 *   usually used) RBD and begins to fill it. (Maybe, this happens only if
 *   the last buffer from the previous RFD fits exact into the queue and
 *   the next RFD can't fetch an initial RBD. Anyone knows more? )
 *
 * results from ftp performance tests with Linux 1.2.5
 *   send and receive about 350-400 KByte/s (peak up to 460 kbytes/s)
 *   sending in NOP-mode: peak performance up to 530K/s (but better don't run this mode)
 */

/*
 * 29.Sept.96: virt_to_bus changes for new memory scheme
 * 19.Feb.96: more Mcast changes, module support (MH)
 *
 * 18.Nov.95: Mcast changes (AC).
 *
 * 23.April.95: fixed(?) receiving problems by configuring a RFD more
 *              than the number of RBD's. Can maybe cause other problems.
 * 18.April.95: Added MODULE support (MH)
 * 17.April.95: MC related changes in init586() and set_multicast_list().
 *              removed use of 'jiffies' in init586() (MH)
 *
 * 19.Sep.94: Added Multicast support (not tested yet) (MH)
 *
 * 18.Sep.94: Workaround for 'EL-Bug'. Removed flexible RBD-handling.
 *            Now, every RFD has exact one RBD. (MH)
 *
 * 14.Sep.94: added promiscuous mode, a few cleanups (MH)
 *
 * 19.Aug.94: changed request_irq() parameter (MH)
 *
 * 20.July.94: removed cleanup bugs, removed a 16K-mem-probe-bug (MH)
 *
 * 19.July.94: lotsa cleanups .. (MH)
 *
 * 17.July.94: some patches ... verified to run with 1.1.29 (MH)
 *
 * 4.July.94: patches for Linux 1.1.24  (MH)
 *
 * 26.March.94: patches for Linux 1.0 and iomem-auto-probe (MH)
 *
 * 30.Sep.93: Added nop-chain .. driver now runs with only one Xmit-Buff, too (MH)
 *
 * < 30.Sep.93: first versions
 */

static int debuglevel;	/* debug-printk 0: off 1: a few 2: more */
static int automatic_resume; /* experimental .. better should be zero */
static int rfdadd;	/* rfdadd=1 may be better for 8K MEM cards */
static int fifo=0x8;	/* don't change */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <asm/io.h>

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

#include "ni52.h"

#define DRV_NAME "ni52"

#define DEBUG       /* debug on */
#define SYSBUSVAL 1 /* 8 Bit */

#define ni_attn586()  {outb(0,dev->base_addr+NI52_ATTENTION);}
#define ni_reset586() {outb(0,dev->base_addr+NI52_RESET);}
#define ni_disint()   {outb(0,dev->base_addr+NI52_INTDIS);}
#define ni_enaint()   {outb(0,dev->base_addr+NI52_INTENA);}

#define make32(ptr16) (p->memtop + (short) (ptr16) )
#define make24(ptr32) ( ((char *) (ptr32)) - p->base)
#define make16(ptr32) ((unsigned short) ((unsigned long)(ptr32) - (unsigned long) p->memtop ))

/******************* how to calculate the buffers *****************************

  * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
  * --------------- in a different (more stable?) mode. Only in this mode it's
  *                 possible to configure the driver with 'NO_NOPCOMMANDS'

sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
sizeof(rfd) = 24; sizeof(rbd) = 12;
sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
sizeof(nop_cmd) = 8;

  * if you don't know the driver, better do not change these values: */

#define RECV_BUFF_SIZE 1524 /* slightly oversized */
#define XMIT_BUFF_SIZE 1524 /* slightly oversized */
#define NUM_XMIT_BUFFS 1    /* config for both, 8K and 16K shmem */
#define NUM_RECV_BUFFS_8  4 /* config for 8K shared mem */
#define NUM_RECV_BUFFS_16 9 /* config for 16K shared mem */
#define NO_NOPCOMMANDS      /* only possible with NUM_XMIT_BUFFS=1 */

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

/* different DELAYs */
#define DELAY(x) mdelay(32 * x);
#define DELAY_16(); { udelay(16); }
#define DELAY_18(); { udelay(4); }

/* wait for command with timeout: */
#define WAIT_4_SCB_CMD() \
{ int i; \
  for(i=0;i<16384;i++) { \
    if(!p->scb->cmd_cuc) break; \
    DELAY_18(); \
    if(i == 16383) { \
      printk("%s: scb_cmd timed out: %04x,%04x .. disabling i82586!!\n",dev->name,p->scb->cmd_cuc,p->scb->cus); \
       if(!p->reseted) { p->reseted = 1; ni_reset586(); } } } }

#define WAIT_4_SCB_CMD_RUC() { int i; \
  for(i=0;i<16384;i++) { \
    if(!p->scb->cmd_ruc) break; \
    DELAY_18(); \
    if(i == 16383) { \
      printk("%s: scb_cmd (ruc) timed out: %04x,%04x .. disabling i82586!!\n",dev->name,p->scb->cmd_ruc,p->scb->rus); \
       if(!p->reseted) { p->reseted = 1; ni_reset586(); } } } }

#define WAIT_4_STAT_COMPL(addr) { int i; \
   for(i=0;i<32767;i++) { \
     if((addr)->cmd_status & STAT_COMPL) break; \
     DELAY_16(); DELAY_16(); } }

#define NI52_TOTAL_SIZE 16
#define NI52_ADDR0 0x02
#define NI52_ADDR1 0x07
#define NI52_ADDR2 0x01

static int     ni52_probe1(struct net_device *dev,int ioaddr);
static irqreturn_t ni52_interrupt(int irq,void *dev_id);
static int     ni52_open(struct net_device *dev);
static int     ni52_close(struct net_device *dev);
static int     ni52_send_packet(struct sk_buff *,struct net_device *);
static struct  net_device_stats *ni52_get_stats(struct net_device *dev);
static void    set_multicast_list(struct net_device *dev);
static void    ni52_timeout(struct net_device *dev);
#if 0
static void    ni52_dump(struct net_device *,void *);
#endif

/* helper-functions */
static int     init586(struct net_device *dev);
static int     check586(struct net_device *dev,char *where,unsigned size);
static void    alloc586(struct net_device *dev);
static void    startrecv586(struct net_device *dev);
static void   *alloc_rfa(struct net_device *dev,void *ptr);
static void    ni52_rcv_int(struct net_device *dev);
static void    ni52_xmt_int(struct net_device *dev);
static void    ni52_rnr_int(struct net_device *dev);

struct priv
{
	struct net_device_stats stats;
	unsigned long base;
	char *memtop;
	long int lock;
	int reseted;
	volatile struct rfd_struct	*rfd_last,*rfd_top,*rfd_first;
	volatile struct scp_struct	*scp;	/* volatile is important */
	volatile struct iscp_struct	*iscp;	/* volatile is important */
	volatile struct scb_struct	*scb;	/* volatile is important */
	volatile struct tbd_struct	*xmit_buffs[NUM_XMIT_BUFFS];
#if (NUM_XMIT_BUFFS == 1)
	volatile struct transmit_cmd_struct *xmit_cmds[2];
	volatile struct nop_cmd_struct *nop_cmds[2];
#else
	volatile struct transmit_cmd_struct *xmit_cmds[NUM_XMIT_BUFFS];
	volatile struct nop_cmd_struct *nop_cmds[NUM_XMIT_BUFFS];
#endif
	volatile int		nop_point,num_recv_buffs;
	volatile char		*xmit_cbuffs[NUM_XMIT_BUFFS];
	volatile int		xmit_count,xmit_last;
};

/**********************************************
 * close device
 */
static int ni52_close(struct net_device *dev)
{
	free_irq(dev->irq, dev);

	ni_reset586(); /* the hard way to stop the receiver */

	netif_stop_queue(dev);

	return 0;
}

/**********************************************
 * open device
 */
static int ni52_open(struct net_device *dev)
{
	int ret;

	ni_disint();
	alloc586(dev);
	init586(dev);
	startrecv586(dev);
	ni_enaint();

	ret = request_irq(dev->irq, &ni52_interrupt,0,dev->name,dev);
	if (ret)
	{
		ni_reset586();
		return ret;
	}

	netif_start_queue(dev);

	return 0; /* most done by init */
}

/**********************************************
 * Check to see if there's an 82586 out there.
 */
static int check586(struct net_device *dev,char *where,unsigned size)
{
	struct priv pb;
	struct priv *p = /* (struct priv *) dev->priv*/ &pb;
	char *iscp_addrs[2];
	int i;

	p->base = (unsigned long) isa_bus_to_virt((unsigned long)where) + size - 0x01000000;
	p->memtop = isa_bus_to_virt((unsigned long)where) + size;
	p->scp = (struct scp_struct *)(p->base + SCP_DEFAULT_ADDRESS);
	memset((char *)p->scp,0, sizeof(struct scp_struct));
	for(i=0;i<sizeof(struct scp_struct);i++) /* memory was writeable? */
		if(((char *)p->scp)[i])
			return 0;
	p->scp->sysbus = SYSBUSVAL;				/* 1 = 8Bit-Bus, 0 = 16 Bit */
	if(p->scp->sysbus != SYSBUSVAL)
		return 0;

	iscp_addrs[0] = isa_bus_to_virt((unsigned long)where);
	iscp_addrs[1]= (char *) p->scp - sizeof(struct iscp_struct);

	for(i=0;i<2;i++)
	{
		p->iscp = (struct iscp_struct *) iscp_addrs[i];
		memset((char *)p->iscp,0, sizeof(struct iscp_struct));

		p->scp->iscp = make24(p->iscp);
		p->iscp->busy = 1;

		ni_reset586();
		ni_attn586();
		DELAY(1);	/* wait a while... */

		if(p->iscp->busy) /* i82586 clears 'busy' after successful init */
			return 0;
	}
	return 1;
}

/******************************************************************
 * set iscp at the right place, called by ni52_probe1 and open586.
 */
static void alloc586(struct net_device *dev)
{
	struct priv *p =	(struct priv *) dev->priv;

	ni_reset586();
	DELAY(1);

	p->scp	= (struct scp_struct *)	(p->base + SCP_DEFAULT_ADDRESS);
	p->scb	= (struct scb_struct *)	isa_bus_to_virt(dev->mem_start);
	p->iscp = (struct iscp_struct *) ((char *)p->scp - sizeof(struct iscp_struct));

	memset((char *) p->iscp,0,sizeof(struct iscp_struct));
	memset((char *) p->scp ,0,sizeof(struct scp_struct));

	p->scp->iscp = make24(p->iscp);
	p->scp->sysbus = SYSBUSVAL;
	p->iscp->scb_offset = make16(p->scb);

	p->iscp->busy = 1;
	ni_reset586();
	ni_attn586();

	DELAY(1);

	if(p->iscp->busy)
		printk("%s: Init-Problems (alloc).\n",dev->name);

	p->reseted = 0;

	memset((char *)p->scb,0,sizeof(struct scb_struct));
}

/* set: io,irq,memstart,memend or set it when calling insmod */
static int irq=9;
static int io=0x300;
static long memstart;	/* e.g 0xd0000 */
static long memend;	/* e.g 0xd4000 */

/**********************************************
 * probe the ni5210-card
 */
struct net_device * __init ni52_probe(int unit)
{
	struct net_device *dev = alloc_etherdev(sizeof(struct priv));
	static int ports[] = {0x300, 0x280, 0x360 , 0x320 , 0x340, 0};
	int *port;
	int err = 0;

	if (!dev)
		return ERR_PTR(-ENOMEM);

	if (unit >= 0) {
		sprintf(dev->name, "eth%d", unit);
		netdev_boot_setup_check(dev);
		io = dev->base_addr;
		irq = dev->irq;
		memstart = dev->mem_start;
		memend = dev->mem_end;
	}

	if (io > 0x1ff)	{	/* Check a single specified location. */
		err = ni52_probe1(dev, io);
	} else if (io > 0) {		/* Don't probe at all. */
		err = -ENXIO;
	} else {
		for (port = ports; *port && ni52_probe1(dev, *port) ; port++)
			;
		if (*port)
			goto got_it;
#ifdef FULL_IO_PROBE
		for (io = 0x200; io < 0x400 && ni52_probe1(dev, io); io += 8)
			;
		if (io < 0x400)
			goto got_it;
#endif
		err = -ENODEV;
	}
	if (err)
		goto out;
got_it:
	err = register_netdev(dev);
	if (err)
		goto out1;
	return dev;
out1:
	release_region(dev->base_addr, NI52_TOTAL_SIZE);
out:
	free_netdev(dev);
	return ERR_PTR(err);
}

static int __init ni52_probe1(struct net_device *dev,int ioaddr)
{
	int i, size, retval;

	dev->base_addr = ioaddr;
	dev->irq = irq;
	dev->mem_start = memstart;
	dev->mem_end = memend;

	if (!request_region(ioaddr, NI52_TOTAL_SIZE, DRV_NAME))
		return -EBUSY;

	if( !(inb(ioaddr+NI52_MAGIC1) == NI52_MAGICVAL1) ||
	    !(inb(ioaddr+NI52_MAGIC2) == NI52_MAGICVAL2)) {
		retval = -ENODEV;
		goto out;
	}

	for(i=0;i<ETH_ALEN;i++)
		dev->dev_addr[i] = inb(dev->base_addr+i);

	if(dev->dev_addr[0] != NI52_ADDR0 || dev->dev_addr[1] != NI52_ADDR1
		 || dev->dev_addr[2] != NI52_ADDR2) {
		retval = -ENODEV;
		goto out;
	}

	printk(KERN_INFO "%s: NI5210 found at %#3lx, ",dev->name,dev->base_addr);

	/*
	 * check (or search) IO-Memory, 8K and 16K
	 */
#ifdef MODULE
	size = dev->mem_end - dev->mem_start;
	if(size != 0x2000 && size != 0x4000) {
		printk("\n%s: Illegal memory size %d. Allowed is 0x2000 or 0x4000 bytes.\n",dev->name,size);
		retval = -ENODEV;
		goto out;
	}
	if(!check586(dev,(char *) dev->mem_start,size)) {
		printk("?memcheck, Can't find memory at 0x%lx with size %d!\n",dev->mem_start,size);
		retval = -ENODEV;
		goto out;
	}
#else
	if(dev->mem_start != 0) /* no auto-mem-probe */
	{
		size = 0x4000; /* check for 16K mem */
		if(!check586(dev,(char *) dev->mem_start,size)) {
			size = 0x2000; /* check for 8K mem */
			if(!check586(dev,(char *) dev->mem_start,size)) {
				printk("?memprobe, Can't find memory at 0x%lx!\n",dev->mem_start);
				retval = -ENODEV;
				goto out;
			}
		}
	}
	else
	{
		static long memaddrs[] = { 0xc8000,0xca000,0xcc000,0xce000,0xd0000,0xd2000,
					0xd4000,0xd6000,0xd8000,0xda000,0xdc000, 0 };
		for(i=0;;i++)
		{
			if(!memaddrs[i]) {
				printk("?memprobe, Can't find io-memory!\n");
				retval = -ENODEV;
				goto out;
			}
			dev->mem_start = memaddrs[i];
			size = 0x2000; /* check for 8K mem */
			if(check586(dev,(char *)dev->mem_start,size)) /* 8K-check */
				break;
			size = 0x4000; /* check for 16K mem */
			if(check586(dev,(char *)dev->mem_start,size)) /* 16K-check */
				break;
		}
	}
	dev->mem_end = dev->mem_start + size; /* set mem_end showed by 'ifconfig' */
#endif

	memset((char *) dev->priv,0,sizeof(struct priv));

	((struct priv *) (dev->priv))->memtop = isa_bus_to_virt(dev->mem_start) + size;
	((struct priv *) (dev->priv))->base =	(unsigned long) isa_bus_to_virt(dev->mem_start) + size - 0x01000000;
	alloc586(dev);

	/* set number of receive-buffs according to memsize */
	if(size == 0x2000)
		((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_8;
	else
		((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_16;

	printk("Memaddr: 0x%lx, Memsize: %d, ",dev->mem_start,size);

	if(dev->irq < 2)
	{
		unsigned long irq_mask;

		irq_mask = probe_irq_on();
		ni_reset586();
		ni_attn586();

		mdelay(20);
		dev->irq = probe_irq_off(irq_mask);
		if(!dev->irq)
		{
			printk("?autoirq, Failed to detect IRQ line!\n");
			retval = -EAGAIN;
			goto out;
		}
		printk("IRQ %d (autodetected).\n",dev->irq);
	}
	else	{
		if(dev->irq == 2)
			dev->irq = 9;
		printk("IRQ %d (assigned and not checked!).\n",dev->irq);
	}

	dev->open		= ni52_open;
	dev->stop		= ni52_close;
	dev->get_stats		= ni52_get_stats;
	dev->tx_timeout 	= ni52_timeout;
	dev->watchdog_timeo	= HZ/20;
	dev->hard_start_xmit 	= ni52_send_packet;
	dev->set_multicast_list = set_multicast_list;

	dev->if_port 		= 0;

	return 0;
out:
	release_region(ioaddr, NI52_TOTAL_SIZE);
	return retval;
}

/**********************************************
 * init the chip (ni52-interrupt should be disabled?!)
 * needs a correct 'allocated' memory
 */

static int init586(struct net_device *dev)
{
	void *ptr;
	int i,result=0;
	struct priv *p = (struct priv *) dev->priv;
	volatile struct configure_cmd_struct	*cfg_cmd;
	volatile struct iasetup_cmd_struct *ias_cmd;
	volatile struct tdr_cmd_struct *tdr_cmd;
	volatile struct mcsetup_cmd_struct *mc_cmd;
	struct dev_mc_list *dmi=dev->mc_list;
	int num_addrs=dev->mc_count;

	ptr = (void *) ((char *)p->scb + sizeof(struct scb_struct));

	cfg_cmd = (struct configure_cmd_struct *)ptr; /* configure-command */
	cfg_cmd->cmd_status	= 0;
	cfg_cmd->cmd_cmd	= CMD_CONFIGURE | CMD_LAST;
	cfg_cmd->cmd_link	= 0xffff;

	cfg_cmd->byte_cnt	= 0x0a; /* number of cfg bytes */
	cfg_cmd->fifo		= fifo; /* fifo-limit (8=tx:32/rx:64) */
	cfg_cmd->sav_bf		= 0x40; /* hold or discard bad recv frames (bit 7) */
	cfg_cmd->adr_len	= 0x2e; /* addr_len |!src_insert |pre-len |loopback */
	cfg_cmd->priority	= 0x00;
	cfg_cmd->ifs		= 0x60;
	cfg_cmd->time_low	= 0x00;
	cfg_cmd->time_high	= 0xf2;
	cfg_cmd->promisc	= 0;
	if(dev->flags & IFF_ALLMULTI) {
		int len = ((char *) p->iscp - (char *) ptr - 8) / 6;
		if(num_addrs > len)	{
			printk("%s: switching to promisc. mode\n",dev->name);
			dev->flags|=IFF_PROMISC;
		}
	}
	if(dev->flags&IFF_PROMISC)
	{
			 cfg_cmd->promisc=1;
			 dev->flags|=IFF_PROMISC;
	}
	cfg_cmd->carr_coll	= 0x00;

	p->scb->cbl_offset	= make16(cfg_cmd);
	p->scb->cmd_ruc		= 0;

	p->scb->cmd_cuc		= CUC_START; /* cmd.-unit start */
	ni_attn586();

	WAIT_4_STAT_COMPL(cfg_cmd);

	if((cfg_cmd->cmd_status & (STAT_OK|STAT_COMPL)) != (STAT_COMPL|STAT_OK))
	{
		printk("%s: configure command failed: %x\n",dev->name,cfg_cmd->cmd_status);
		return 1;
	}

	/*
	 * individual address setup
	 */

	ias_cmd = (struct iasetup_cmd_struct *)ptr;

	ias_cmd->cmd_status	= 0;
	ias_cmd->cmd_cmd	= CMD_IASETUP | CMD_LAST;
	ias_cmd->cmd_link	= 0xffff;

	memcpy((char *)&ias_cmd->iaddr,(char *) dev->dev_addr,ETH_ALEN);

	p->scb->cbl_offset = make16(ias_cmd);

	p->scb->cmd_cuc = CUC_START; /* cmd.-unit start */
	ni_attn586();

	WAIT_4_STAT_COMPL(ias_cmd);

	if((ias_cmd->cmd_status & (STAT_OK|STAT_COMPL)) != (STAT_OK|STAT_COMPL)) {
		printk("%s (ni52): individual address setup command failed: %04x\n",dev->name,ias_cmd->cmd_status);
		return 1;
	}

	/*
	 * TDR, wire check .. e.g. no resistor e.t.c
	 */

	tdr_cmd = (struct tdr_cmd_struct *)ptr;

	tdr_cmd->cmd_status	= 0;
	tdr_cmd->cmd_cmd	= CMD_TDR | CMD_LAST;
	tdr_cmd->cmd_link	= 0xffff;
	tdr_cmd->status		= 0;

	p->scb->cbl_offset = make16(tdr_cmd);
	p->scb->cmd_cuc = CUC_START; /* cmd.-unit start */
	ni_attn586();

	WAIT_4_STAT_COMPL(tdr_cmd);

	if(!(tdr_cmd->cmd_status & STAT_COMPL))
	{
		printk("%s: Problems while running the TDR.\n",dev->name);
	}
	else
	{
		DELAY_16(); /* wait for result */
		result = tdr_cmd->status;

		p->scb->cmd_cuc = p->scb->cus & STAT_MASK;
		ni_attn586(); /* ack the interrupts */

		if(result & TDR_LNK_OK)
			;
		else if(result & TDR_XCVR_PRB)
			printk("%s: TDR: Transceiver problem. Check the cable(s)!\n",dev->name);
		else if(result & TDR_ET_OPN)
			printk("%s: TDR: No correct termination %d clocks away.\n",dev->name,result & TDR_TIMEMASK);
		else if(result & TDR_ET_SRT)
		{
			if (result & TDR_TIMEMASK) /* time == 0 -> strange :-) */
				printk("%s: TDR: Detected a short circuit %d clocks away.\n",dev->name,result & TDR_TIMEMASK);
		}
		else
			printk("%s: TDR: Unknown status %04x\n",dev->name,result);
	}

	/*
	 * Multicast setup
	 */
	if(num_addrs && !(dev->flags & IFF_PROMISC) )
	{
		mc_cmd = (struct mcsetup_cmd_struct *) ptr;
		mc_cmd->cmd_status = 0;
		mc_cmd->cmd_cmd = CMD_MCSETUP | CMD_LAST;
		mc_cmd->cmd_link = 0xffff;
		mc_cmd->mc_cnt = num_addrs * 6;

		for(i=0;i<num_addrs;i++,dmi=dmi->next)
			memcpy((char *) mc_cmd->mc_list[i], dmi->dmi_addr,6);

		p->scb->cbl_offset = make16(mc_cmd);
		p->scb->cmd_cuc = CUC_START;
		ni_attn586();

		WAIT_4_STAT_COMPL(mc_cmd);

		if( (mc_cmd->cmd_status & (STAT_COMPL|STAT_OK)) != (STAT_COMPL|STAT_OK) )
			printk("%s: Can't apply multicast-address-list.\n",dev->name);
	}

	/*
	 * alloc nop/xmit-cmds
	 */
#if (NUM_XMIT_BUFFS == 1)
	for(i=0;i<2;i++)
	{
		p->nop_cmds[i] 			= (struct nop_cmd_struct *)ptr;
		p->nop_cmds[i]->cmd_cmd		= CMD_NOP;
		p->nop_cmds[i]->cmd_status 	= 0;
		p->nop_cmds[i]->cmd_link	= make16((p->nop_cmds[i]));
		ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
	}
#else
	for(i=0;i<NUM_XMIT_BUFFS;i++)
	{
		p->nop_cmds[i]			= (struct nop_cmd_struct *)ptr;
		p->nop_cmds[i]->cmd_cmd		= CMD_NOP;
		p->nop_cmds[i]->cmd_status	= 0;
		p->nop_cmds[i]->cmd_link	= make16((p->nop_cmds[i]));
		ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
	}
#endif

	ptr = alloc_rfa(dev,(void *)ptr); /* init receive-frame-area */

	/*
	 * alloc xmit-buffs / init xmit_cmds
	 */
	for(i=0;i<NUM_XMIT_BUFFS;i++)
	{
		p->xmit_cmds[i] = (struct transmit_cmd_struct *)ptr; /*transmit cmd/buff 0*/
		ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
		p->xmit_cbuffs[i] = (char *)ptr; /* char-buffs */
		ptr = (char *) ptr + XMIT_BUFF_SIZE;
		p->xmit_buffs[i] = (struct tbd_struct *)ptr; /* TBD */
		ptr = (char *) ptr + sizeof(struct tbd_struct);
		if((void *)ptr > (void *)p->iscp)
		{
			printk("%s: not enough shared-mem for your configuration!\n",dev->name);
			return 1;
		}
		memset((char *)(p->xmit_cmds[i]) ,0, sizeof(struct transmit_cmd_struct));
		memset((char *)(p->xmit_buffs[i]),0, sizeof(struct tbd_struct));
		p->xmit_cmds[i]->cmd_link = make16(p->nop_cmds[(i+1)%NUM_XMIT_BUFFS]);
		p->xmit_cmds[i]->cmd_status = STAT_COMPL;
		p->xmit_cmds[i]->cmd_cmd = CMD_XMIT | CMD_INT;
		p->xmit_cmds[i]->tbd_offset = make16((p->xmit_buffs[i]));
		p->xmit_buffs[i]->next = 0xffff;
		p->xmit_buffs[i]->buffer = make24((p->xmit_cbuffs[i]));
	}

	p->xmit_count = 0;
	p->xmit_last	= 0;
#ifndef NO_NOPCOMMANDS
	p->nop_point	= 0;
#endif

	 /*
		* 'start transmitter'
		*/
#ifndef NO_NOPCOMMANDS
	p->scb->cbl_offset = make16(p->nop_cmds[0]);
	p->scb->cmd_cuc = CUC_START;
	ni_attn586();
	WAIT_4_SCB_CMD();
#else
	p->xmit_cmds[0]->cmd_link = make16(p->xmit_cmds[0]);
	p->xmit_cmds[0]->cmd_cmd	= CMD_XMIT | CMD_SUSPEND | CMD_INT;
#endif

	/*
	 * ack. interrupts
	 */
	p->scb->cmd_cuc = p->scb->cus & STAT_MASK;
	ni_attn586();
	DELAY_16();

	ni_enaint();

	return 0;
}

/******************************************************
 * This is a helper routine for ni52_rnr_int() and init586().
 * It sets up the Receive Frame Area (RFA).
 */

static void *alloc_rfa(struct net_device *dev,void *ptr)
{
	volatile struct rfd_struct *rfd = (struct rfd_struct *)ptr;
	volatile struct rbd_struct *rbd;
	int i;
	struct priv *p = (struct priv *) dev->priv;

	memset((char *) rfd,0,sizeof(struct rfd_struct)*(p->num_recv_buffs+rfdadd));
	p->rfd_first = rfd;

	for(i = 0; i < (p->num_recv_buffs+rfdadd); i++) {
		rfd[i].next = make16(rfd + (i+1) % (p->num_recv_buffs+rfdadd) );
		rfd[i].rbd_offset = 0xffff;
	}
	rfd[p->num_recv_buffs-1+rfdadd].last = RFD_SUSP;	 /* RU suspend */

	ptr = (void *) (rfd + (p->num_recv_buffs + rfdadd) );

	rbd = (struct rbd_struct *) ptr;
	ptr = (void *) (rbd + p->num_recv_buffs);

	 /* clr descriptors */
	memset((char *) rbd,0,sizeof(struct rbd_struct)*(p->num_recv_buffs));

	for(i=0;i<p->num_recv_buffs;i++)
	{
		rbd[i].next = make16((rbd + (i+1) % p->num_recv_buffs));
		rbd[i].size = RECV_BUFF_SIZE;
		rbd[i].buffer = make24(ptr);
		ptr = (char *) ptr + RECV_BUFF_SIZE;
	}

	p->rfd_top	= p->rfd_first;
	p->rfd_last = p->rfd_first + (p->num_recv_buffs - 1 + rfdadd);

	p->scb->rfa_offset		= make16(p->rfd_first);
	p->rfd_first->rbd_offset	= make16(rbd);

	return ptr;
}


/**************************************************
 * Interrupt Handler ...
 */

static irqreturn_t ni52_interrupt(int irq,void *dev_id)
{
	struct net_device *dev = dev_id;
	unsigned short stat;
	int cnt=0;
	struct priv *p;

	if (!dev) {
		printk ("ni5210-interrupt: irq %d for unknown device.\n",irq);
		return IRQ_NONE;
	}
	p = (struct priv *) dev->priv;

	if(debuglevel > 1)
		printk("I");

	WAIT_4_SCB_CMD(); /* wait for last command	*/

	while((stat=p->scb->cus & STAT_MASK))
	{
		p->scb->cmd_cuc = stat;
		ni_attn586();

		if(stat & STAT_FR)	 /* received a frame */
			ni52_rcv_int(dev);

		if(stat & STAT_RNR) /* RU went 'not ready' */
		{
			printk("(R)");
			if(p->scb->rus & RU_SUSPEND) /* special case: RU_SUSPEND */
			{
				WAIT_4_SCB_CMD();
				p->scb->cmd_ruc = RUC_RESUME;
				ni_attn586();
				WAIT_4_SCB_CMD_RUC();
			}
			else
			{
				printk("%s: Receiver-Unit went 'NOT READY': %04x/%02x.\n",dev->name,(int) stat,(int) p->scb->rus);
				ni52_rnr_int(dev);
			}
		}

		if(stat & STAT_CX)		/* command with I-bit set complete */
			 ni52_xmt_int(dev);

#ifndef NO_NOPCOMMANDS
		if(stat & STAT_CNA)	/* CU went 'not ready' */
		{
			if(netif_running(dev))
				printk("%s: oops! CU has left active state. stat: %04x/%02x.\n",dev->name,(int) stat,(int) p->scb->cus);
		}
#endif

		if(debuglevel > 1)
			printk("%d",cnt++);

		WAIT_4_SCB_CMD(); /* wait for ack. (ni52_xmt_int can be faster than ack!!) */
		if(p->scb->cmd_cuc)	 /* timed out? */
		{
			printk("%s: Acknowledge timed out.\n",dev->name);
			ni_disint();
			break;
		}
	}

	if(debuglevel > 1)
		printk("i");
	return IRQ_HANDLED;
}

/*******************************************************
 * receive-interrupt
 */

static void ni52_rcv_int(struct net_device *dev)
{
	int status,cnt=0;
	unsigned short totlen;
	struct sk_buff *skb;
	struct rbd_struct *rbd;
	struct priv *p = (struct priv *) dev->priv;

	if(debuglevel > 0)
		printk("R");

	for(;(status = p->rfd_top->stat_high) & RFD_COMPL;)
	{
			rbd = (struct rbd_struct *) make32(p->rfd_top->rbd_offset);

			if(status & RFD_OK) /* frame received without error? */
			{
				if( (totlen = rbd->status) & RBD_LAST) /* the first and the last buffer? */
				{
					totlen &= RBD_MASK; /* length of this frame */
					rbd->status = 0;
					skb = (struct sk_buff *) dev_alloc_skb(totlen+2);
					if(skb != NULL)
					{
						skb_reserve(skb,2);
						skb_put(skb,totlen);
						skb_copy_to_linear_data(skb,(char *) p->base+(unsigned long) rbd->buffer,totlen);
						skb->protocol=eth_type_trans(skb,dev);
						netif_rx(skb);
						dev->last_rx = jiffies;
						p->stats.rx_packets++;
						p->stats.rx_bytes += totlen;
					}
					else
						p->stats.rx_dropped++;
				}
				else
				{
					int rstat;
						 /* free all RBD's until RBD_LAST is set */
					totlen = 0;
					while(!((rstat=rbd->status) & RBD_LAST))
					{
						totlen += rstat & RBD_MASK;
						if(!rstat)
						{
							printk("%s: Whoops .. no end mark in RBD list\n",dev->name);
							break;
						}
						rbd->status = 0;
						rbd = (struct rbd_struct *) make32(rbd->next);
					}
					totlen += rstat & RBD_MASK;
					rbd->status = 0;
					printk("%s: received oversized frame! length: %d\n",dev->name,totlen);
					p->stats.rx_dropped++;
			 }
		}
		else /* frame !(ok), only with 'save-bad-frames' */
		{
			printk("%s: oops! rfd-error-status: %04x\n",dev->name,status);
			p->stats.rx_errors++;
		}
		p->rfd_top->stat_high = 0;
		p->rfd_top->last = RFD_SUSP; /* maybe exchange by RFD_LAST */
		p->rfd_top->rbd_offset = 0xffff;
		p->rfd_last->last = 0;				/* delete RFD_SUSP	*/
		p->rfd_last = p->rfd_top;
		p->rfd_top = (struct rfd_struct *) make32(p->rfd_top->next); /* step to next RFD */
		p->scb->rfa_offset = make16(p->rfd_top);

		if(debuglevel > 0)
			printk("%d",cnt++);
	}

	if(automatic_resume)
	{
		WAIT_4_SCB_CMD();
		p->scb->cmd_ruc = RUC_RESUME;
		ni_attn586();
		WAIT_4_SCB_CMD_RUC();
	}

#ifdef WAIT_4_BUSY
	{
		int i;
		for(i=0;i<1024;i++)
		{
			if(p->rfd_top->status)
				break;
			DELAY_16();
			if(i == 1023)
				printk("%s: RU hasn't fetched next RFD (not busy/complete)\n",dev->name);
		}
	}
#endif

#if 0
	if(!at_least_one)
	{
		int i;
		volatile struct rfd_struct *rfds=p->rfd_top;
		volatile struct rbd_struct *rbds;
		printk("%s: received a FC intr. without having a frame: %04x %d\n",dev->name,status,old_at_least);
		for(i=0;i< (p->num_recv_buffs+4);i++)
		{
			rbds = (struct rbd_struct *) make32(rfds->rbd_offset);