aboutsummaryrefslogtreecommitdiffstats
path: root/mm/swap_state.c
blob: 5f7cf2a4cb55f8e9a106776b8d45ec5433c6991e (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
/*
 *  linux/mm/swap_state.c
 *
 *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
 *  Swap reorganised 29.12.95, Stephen Tweedie
 *
 *  Rewritten to use page cache, (C) 1998 Stephen Tweedie
 */
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/kernel_stat.h>
#include <linux/swap.h>
#include <linux/init.h>
#include <linux/pagemap.h>
#include <linux/buffer_head.h>
#include <linux/backing-dev.h>
#include <linux/pagevec.h>
#include <linux/migrate.h>

#include <asm/pgtable.h>

/*
 * swapper_space is a fiction, retained to simplify the path through
 * vmscan's shrink_list, to make sync_page look nicer, and to allow
 * future use of radix_tree tags in the swap cache.
 */
static const struct address_space_operations swap_aops = {
	.writepage	= swap_writepage,
	.sync_page	= block_sync_page,
	.set_page_dirty	= __set_page_dirty_nobuffers,
	.migratepage	= migrate_page,
};

static struct backing_dev_info swap_backing_dev_info = {
	.capabilities	= BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
	.unplug_io_fn	= swap_unplug_io_fn,
};

struct address_space swapper_space = {
	.page_tree	= RADIX_TREE_INIT(GFP_ATOMIC|__GFP_NOWARN),
	.tree_lock	= __RW_LOCK_UNLOCKED(swapper_space.tree_lock),
	.a_ops		= &swap_aops,
	.i_mmap_nonlinear = LIST_HEAD_INIT(swapper_space.i_mmap_nonlinear),
	.backing_dev_info = &swap_backing_dev_info,
};

#define INC_CACHE_INFO(x)	do { swap_cache_info.x++; } while (0)

static struct {
	unsigned long add_total;
	unsigned long del_total;
	unsigned long find_success;
	unsigned long find_total;
	unsigned long noent_race;
	unsigned long exist_race;
} swap_cache_info;

void show_swap_cache_info(void)
{
	printk("Swap cache: add %lu, delete %lu, find %lu/%lu, race %lu+%lu\n",
		swap_cache_info.add_total, swap_cache_info.del_total,
		swap_cache_info.find_success, swap_cache_info.find_total,
		swap_cache_info.noent_race, swap_cache_info.exist_race);
	printk("Free swap  = %lukB\n", nr_swap_pages << (PAGE_SHIFT - 10));
	printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
}

/*
 * __add_to_swap_cache resembles add_to_page_cache on swapper_space,
 * but sets SwapCache flag and private instead of mapping and index.
 */
static int __add_to_swap_cache(struct page *page, swp_entry_t entry,
			       gfp_t gfp_mask)
{
	int error;

	BUG_ON(PageSwapCache(page));
	BUG_ON(PagePrivate(page));
	error = radix_tree_preload(gfp_mask);
	if (!error) {
		write_lock_irq(&swapper_space.tree_lock);
		error = radix_tree_insert(&swapper_space.page_tree,
						entry.val, page);
		if (!error) {
			page_cache_get(page);
			SetPageLocked(page);
			SetPageSwapCache(page);
			set_page_private(page, entry.val);
			total_swapcache_pages++;
			__inc_zone_page_state(page, NR_FILE_PAGES);
		}
		write_unlock_irq(&swapper_space.tree_lock);
		radix_tree_preload_end();
	}
	return error;
}

static int add_to_swap_cache(struct page *page, swp_entry_t entry)
{
	int error;

	if (!swap_duplicate(entry)) {
		INC_CACHE_INFO(noent_race);
		return -ENOENT;
	}
	error = __add_to_swap_cache(page, entry, GFP_KERNEL);
	/*
	 * Anon pages are already on the LRU, we don't run lru_cache_add here.
	 */
	if (error) {
		swap_free(entry);
		if (error == -EEXIST)
			INC_CACHE_INFO(exist_race);
		return error;
	}
	INC_CACHE_INFO(add_total);
	return 0;
}

/*
 * This must be called only on pages that have
 * been verified to be in the swap cache.
 */
void __delete_from_swap_cache(struct page *page)
{
	BUG_ON(!PageLocked(page));
	BUG_ON(!PageSwapCache(page));
	BUG_ON(PageWriteback(page));
	BUG_ON(PagePrivate(page));

	radix_tree_delete(&swapper_space.page_tree, page_private(page));
	set_page_private(page, 0);
	ClearPageSwapCache(page);
	total_swapcache_pages--;
	__dec_zone_page_state(page, NR_FILE_PAGES);
	INC_CACHE_INFO(del_total);
}

/**
 * add_to_swap - allocate swap space for a page
 * @page: page we want to move to swap
 *
 * Allocate swap space for the page and add the page to the
 * swap cache.  Caller needs to hold the page lock. 
 */
int add_to_swap(struct page * page, gfp_t gfp_mask)
{
	swp_entry_t entry;
	int err;

	BUG_ON(!PageLocked(page));

	for (;;) {
		entry = get_swap_page();
		if (!entry.val)
			return 0;

		/*
		 * Radix-tree node allocations from PF_MEMALLOC contexts could
		 * completely exhaust the page allocator. __GFP_NOMEMALLOC
		 * stops emergency reserves from being allocated.
		 *
		 * TODO: this could cause a theoretical memory reclaim
		 * deadlock in the swap out path.
		 */
		/*
		 * Add it to the swap cache and mark it dirty
		 */
		err = __add_to_swap_cache(page, entry,
				gfp_mask|__GFP_NOMEMALLOC|__GFP_NOWARN);

		switch (err) {
		case 0:				/* Success */
			SetPageUptodate(page);
			SetPageDirty(page);
			INC_CACHE_INFO(add_total);
			return 1;
		case -EEXIST:
			/* Raced with "speculative" read_swap_cache_async */
			INC_CACHE_INFO(exist_race);
			swap_free(entry);
			continue;
		default:
			/* -ENOMEM radix-tree allocation failure */
			swap_free(entry);
			return 0;
		}
	}
}

/*
 * This must be called only on pages that have
 * been verified to be in the swap cache and locked.
 * It will never put the page into the free list,
 * the caller has a reference on the page.
 */
void delete_from_swap_cache(struct page *page)
{
	swp_entry_t entry;

	entry.val = page_private(page);

	write_lock_irq(&swapper_space.tree_lock);
	__delete_from_swap_cache(page);
	write_unlock_irq(&swapper_space.tree_lock);

	swap_free(entry);
	page_cache_release(page);
}

/*
 * Strange swizzling function only for use by shmem_writepage
 */
int move_to_swap_cache(struct page *page, swp_entry_t entry)
{
	int err = __add_to_swap_cache(page, entry, GFP_ATOMIC);
	if (!err) {
		remove_from_page_cache(page);
		page_cache_release(page);	/* pagecache ref */
		if (!swap_duplicate(entry))
			BUG();
		SetPageDirty(page);
		INC_CACHE_INFO(add_total);
	} else if (err == -EEXIST)
		INC_CACHE_INFO(exist_race);
	return err;
}

/*
 * Strange swizzling function for shmem_getpage (and shmem_unuse)
 */
int move_from_swap_cache(struct page *page, unsigned long index,
		struct address_space *mapping)
{
	int err = add_to_page_cache(page, mapping, index, GFP_ATOMIC);
	if (!err) {
		delete_from_swap_cache(page);
		/* shift page from clean_pages to dirty_pages list */
		ClearPageDirty(page);
		set_page_dirty(page);
	}
	return err;
}

/* 
 * If we are the only user, then try to free up the swap cache. 
 * 
 * Its ok to check for PageSwapCache without the page lock
 * here because we are going to recheck again inside 
 * exclusive_swap_page() _with_ the lock. 
 * 					- Marcelo
 */
static inline void free_swap_cache(struct page *page)
{
	if (PageSwapCache(page) && !TestSetPageLocked(page)) {
		remove_exclusive_swap_page(page);
		unlock_page(page);
	}
}

/* 
 * Perform a free_page(), also freeing any swap cache associated with
 * this page if it is the last user of the page.
 */
void free_page_and_swap_cache(struct page *page)
{
	free_swap_cache(page);
	page_cache_release(page);
}

/*
 * Passed an array of pages, drop them all from swapcache and then release
 * them.  They are removed from the LRU and freed if this is their last use.
 */
void free_pages_and_swap_cache(struct page **pages, int nr)
{
	struct page **pagep = pages;

	lru_add_drain();
	while (nr) {
		int todo = min(nr, PAGEVEC_SIZE);
		int i;

		for (i = 0; i < todo; i++)
			free_swap_cache(pagep[i]);
		release_pages(pagep, todo, 0);
		pagep += todo;
		nr -= todo;
	}
}

/*
 * Lookup a swap entry in the swap cache. A found page will be returned
 * unlocked and with its refcount incremented - we rely on the kernel
 * lock getting page table operations atomic even if we drop the page
 * lock before returning.
 */
struct page * lookup_swap_cache(swp_entry_t entry)
{
	struct page *page;

	page = find_get_page(&swapper_space, entry.val);

	if (page)
		INC_CACHE_INFO(find_success);

	INC_CACHE_INFO(find_total);
	return page;
}

/* 
 * Locate a page of swap in physical memory, reserving swap cache space
 * and reading the disk if it is not already cached.
 * A failure return means that either the page allocation failed or that
 * the swap entry is no longer in use.
 */
struct page *read_swap_cache_async(swp_entry_t entry,
			struct vm_area_struct *vma, unsigned long addr)
{
	struct page *found_page, *new_page = NULL;
	int err;

	do {
		/*
		 * First check the swap cache.  Since this is normally
		 * called after lookup_swap_cache() failed, re-calling
		 * that would confuse statistics.
		 */
		found_page = find_get_page(&swapper_space, entry.val);
		if (found_page)
			break;

		/*
		 * Get a new page to read into from swap.
		 */
		if (!new_page) {
			new_page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
			if (!new_page)
				break;		/* Out of memory */
		}

		/*
		 * Associate the page with swap entry in the swap cache.
		 * May fail (-ENOENT) if swap entry has been freed since
		 * our caller observed it.  May fail (-EEXIST) if there
		 * is already a page associated with this entry in the
		 * swap cache: added by a racing read_swap_cache_async,
		 * or by try_to_swap_out (or shmem_writepage) re-using
		 * the just freed swap entry for an existing page.
		 * May fail (-ENOMEM) if radix-tree node allocation failed.
		 */
		err = add_to_swap_cache(new_page, entry);
		if (!err) {
			/*
			 * Initiate read into locked page and return.
			 */
			lru_cache_add_active(new_page);
			swap_readpage(NULL, new_page);
			return new_page;
		}
	} while (err != -ENOENT && err != -ENOMEM);

	if (new_page)
		page_cache_release(new_page);
	return found_page;
}
n914' href='#n914'>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 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894


































                                                                                                                   







                                          





                                                            
                                               










                                       

                                                         

                                                                 








                                                                                         





































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                  
                            




















                                                                                                                    
                                






























                                                                                  
/*
 *  Copyright (C) 1997 Cullen Jennings
 *  Copyright (C) 1998 Elmer Joandiu, elmer@ylenurme.ee
 *  GNU General Public License applies
 * This module provides support for the Arlan 655 card made by Aironet
 */

#include <linux/config.h>
#include "arlan.h"

#if BITS_PER_LONG != 32
#  error FIXME: this driver requires a 32-bit platform
#endif

static const char *arlan_version = "C.Jennigs 97 & Elmer.Joandi@ut.ee  Oct'98, http://www.ylenurme.ee/~elmer/655/";

struct net_device *arlan_device[MAX_ARLANS];

static int SID = SIDUNKNOWN;
static int radioNodeId = radioNodeIdUNKNOWN;
static char encryptionKey[12] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
int arlan_debug = debugUNKNOWN;
static int spreadingCode = spreadingCodeUNKNOWN;
static int channelNumber = channelNumberUNKNOWN;
static int channelSet = channelSetUNKNOWN;
static int systemId = systemIdUNKNOWN;
static int registrationMode = registrationModeUNKNOWN;
static int keyStart;
static int tx_delay_ms;
static int retries = 5;
static int tx_queue_len = 1;
static int arlan_EEPROM_bad;

#ifdef ARLAN_DEBUGGING

static int testMemory = testMemoryUNKNOWN;
static int irq = irqUNKNOWN;
static int txScrambled = 1;
static int mdebug;

module_param(irq, int, 0);
module_param(mdebug, int, 0);
module_param(testMemory, int, 0);
module_param(txScrambled, int, 0);
MODULE_PARM_DESC(irq, "(unused)");
MODULE_PARM_DESC(testMemory, "(unused)");
MODULE_PARM_DESC(mdebug, "Arlan multicast debugging (0-1)");
#endif

module_param_named(debug, arlan_debug, int, 0);
module_param(spreadingCode, int, 0);
module_param(channelNumber, int, 0);
module_param(channelSet, int, 0);
module_param(systemId, int, 0);
module_param(registrationMode, int, 0);
module_param(radioNodeId, int, 0);
module_param(SID, int, 0);
module_param(keyStart, int, 0);
module_param(tx_delay_ms, int, 0);
module_param(retries, int, 0);
module_param(tx_queue_len, int, 0);
module_param_named(EEPROM_bad, arlan_EEPROM_bad, int, 0);
MODULE_PARM_DESC(debug, "Arlan debug enable (0-1)");
MODULE_PARM_DESC(retries, "Arlan maximum packet retransmisions");
#ifdef ARLAN_ENTRY_EXIT_DEBUGGING
static int arlan_entry_debug;
static int arlan_exit_debug;
static int arlan_entry_and_exit_debug;
module_param_named(entry_debug, arlan_entry_debug, int, 0);
module_param_named(exit_debug, arlan_exit_debug, int, 0);
module_param_named(entry_and_exit_debug, arlan_entry_and_exit_debug, int, 0);
MODULE_PARM_DESC(entry_debug, "Arlan driver function entry debugging");
MODULE_PARM_DESC(exit_debug, "Arlan driver function exit debugging");
MODULE_PARM_DESC(entry_and_exit_debug, "Arlan driver function entry and exit debugging");
#endif

struct arlan_conf_stru arlan_conf[MAX_ARLANS];
static int arlans_found;

static  int 	arlan_open(struct net_device *dev);
static  int 	arlan_tx(struct sk_buff *skb, struct net_device *dev);
static  irqreturn_t arlan_interrupt(int irq, void *dev_id, struct pt_regs *regs);
static  int 	arlan_close(struct net_device *dev);
static  struct net_device_stats *
		arlan_statistics		(struct net_device *dev);
static  void 	arlan_set_multicast		(struct net_device *dev);
static  int 	arlan_hw_tx			(struct net_device* dev, char *buf, int length );
static  int	arlan_hw_config			(struct net_device * dev);
static  void 	arlan_tx_done_interrupt		(struct net_device * dev, int status);
static  void	arlan_rx_interrupt		(struct net_device * dev, u_char rxStatus, u_short, u_short);
static  void	arlan_process_interrupt		(struct net_device * dev);
static	void	arlan_tx_timeout		(struct net_device *dev);

static inline long us2ticks(int us)
{
	return us * (1000000 / HZ);
}


#ifdef ARLAN_ENTRY_EXIT_DEBUGGING
#define ARLAN_DEBUG_ENTRY(name) \
	{\
	struct timeval timev;\
	do_gettimeofday(&timev);\
		if (arlan_entry_debug || arlan_entry_and_exit_debug)\
			printk("--->>>" name " %ld " "\n",((long int) timev.tv_sec * 1000000 + timev.tv_usec));\
	}
#define ARLAN_DEBUG_EXIT(name) \
	{\
	struct timeval timev;\
	do_gettimeofday(&timev);\
		if (arlan_exit_debug || arlan_entry_and_exit_debug)\
			printk("<<<---" name " %ld " "\n",((long int) timev.tv_sec * 1000000 + timev.tv_usec) );\
	}
#else
#define ARLAN_DEBUG_ENTRY(name)
#define ARLAN_DEBUG_EXIT(name)
#endif


#define arlan_interrupt_ack(dev)\
        clearClearInterrupt(dev);\
        setClearInterrupt(dev);

static inline int arlan_drop_tx(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);

	priv->stats.tx_errors++;
	if (priv->Conf->tx_delay_ms)
	{
		priv->tx_done_delayed = jiffies + priv->Conf->tx_delay_ms * HZ / 1000 + 1;
	}
	else
	{
		priv->waiting_command_mask &= ~ARLAN_COMMAND_TX;
		TXHEAD(dev).offset = 0;
		TXTAIL(dev).offset = 0;
		priv->txLast = 0;
		priv->bad = 0;
		if (!priv->under_reset && !priv->under_config)
			netif_wake_queue (dev);
	}
	return 1;
}


int arlan_command(struct net_device *dev, int command_p)
{
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	struct arlan_conf_stru *conf = priv->Conf;
	int udelayed = 0;
	int i = 0;
	unsigned long flags;

	ARLAN_DEBUG_ENTRY("arlan_command");

	if (priv->card_polling_interval)
		priv->card_polling_interval = 1;

	if (arlan_debug & ARLAN_DEBUG_CHAIN_LOCKS)
		printk(KERN_DEBUG "arlan_command, %lx commandByte %x waiting %lx incoming %x \n",
		jiffies, READSHMB(arlan->commandByte),
		       priv->waiting_command_mask, command_p);

	priv->waiting_command_mask |= command_p;

	if (priv->waiting_command_mask & ARLAN_COMMAND_RESET)
		if (time_after(jiffies, priv->lastReset + 5 * HZ))
			priv->waiting_command_mask &= ~ARLAN_COMMAND_RESET;

	if (priv->waiting_command_mask & ARLAN_COMMAND_INT_ACK)
	{
		arlan_interrupt_ack(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_INT_ACK;
	}
	if (priv->waiting_command_mask & ARLAN_COMMAND_INT_ENABLE)
	{
		setInterruptEnable(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_INT_ENABLE;
	}

	/* Card access serializing lock */
	spin_lock_irqsave(&priv->lock, flags);

	/* Check cards status and waiting */

	if (priv->waiting_command_mask & (ARLAN_COMMAND_LONG_WAIT_NOW | ARLAN_COMMAND_WAIT_NOW))
	{
		while (priv->waiting_command_mask & (ARLAN_COMMAND_LONG_WAIT_NOW | ARLAN_COMMAND_WAIT_NOW))
		{
			if (READSHMB(arlan->resetFlag) ||
				READSHMB(arlan->commandByte))	/* || 
								   (readControlRegister(dev) & ARLAN_ACCESS))
								 */
				udelay(40);
			else
				priv->waiting_command_mask &= ~(ARLAN_COMMAND_LONG_WAIT_NOW | ARLAN_COMMAND_WAIT_NOW);

			udelayed++;

			if (priv->waiting_command_mask & ARLAN_COMMAND_LONG_WAIT_NOW)
			{
				if (udelayed * 40 > 1000000)
				{
					printk(KERN_ERR "%s long wait too long \n", dev->name);
					priv->waiting_command_mask |= ARLAN_COMMAND_RESET;
					break;
				}
			}
			else if (priv->waiting_command_mask & ARLAN_COMMAND_WAIT_NOW)
			{
				if (udelayed * 40 > 1000)
				{
					printk(KERN_ERR "%s short wait too long \n", dev->name);
					goto bad_end;
				}
			}
		}
	}
	else
	{
		i = 0;
		while ((READSHMB(arlan->resetFlag) ||
			READSHMB(arlan->commandByte)) &&
			conf->pre_Command_Wait > (i++) * 10)
			udelay(10);


		if ((READSHMB(arlan->resetFlag) ||
			READSHMB(arlan->commandByte)) &&
			!(priv->waiting_command_mask & ARLAN_COMMAND_RESET))
		{
			goto card_busy_end;
		}
	}
	if (priv->waiting_command_mask & ARLAN_COMMAND_RESET)
		priv->under_reset = 1;
	if (priv->waiting_command_mask & ARLAN_COMMAND_CONF)
		priv->under_config = 1;

	/* Issuing command */
	arlan_lock_card_access(dev);
	if (priv->waiting_command_mask & ARLAN_COMMAND_POWERUP)
	{
	//     if (readControlRegister(dev) & (ARLAN_ACCESS && ARLAN_POWER))
		setPowerOn(dev);
		arlan_interrupt_lancpu(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_POWERUP;
		priv->waiting_command_mask |= ARLAN_COMMAND_RESET;
		priv->card_polling_interval = HZ / 10;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_ACTIVATE)
	{
		WRITESHMB(arlan->commandByte, ARLAN_COM_ACTIVATE);
		arlan_interrupt_lancpu(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_ACTIVATE;
		priv->card_polling_interval = HZ / 10;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_RX_ABORT)
	{
		if (priv->rx_command_given)
		{
			WRITESHMB(arlan->commandByte, ARLAN_COM_RX_ABORT);
			arlan_interrupt_lancpu(dev);
			priv->rx_command_given = 0;
		}
		priv->waiting_command_mask &= ~ARLAN_COMMAND_RX_ABORT;
		priv->card_polling_interval = 1;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_TX_ABORT)
	{
		if (priv->tx_command_given)
		{
			WRITESHMB(arlan->commandByte, ARLAN_COM_TX_ABORT);
			arlan_interrupt_lancpu(dev);
			priv->tx_command_given = 0;
		}
		priv->waiting_command_mask &= ~ARLAN_COMMAND_TX_ABORT;
		priv->card_polling_interval = 1;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_RESET)
	{
		priv->under_reset=1;
		netif_stop_queue (dev);

		arlan_drop_tx(dev);
		if (priv->tx_command_given || priv->rx_command_given)
		{
			printk(KERN_ERR "%s: Reset under tx or rx command \n", dev->name);
		}
		netif_stop_queue (dev);
		if (arlan_debug & ARLAN_DEBUG_RESET)
			printk(KERN_ERR "%s: Doing chip reset\n", dev->name);
		priv->lastReset = jiffies;
		WRITESHM(arlan->commandByte, 0, u_char);
		/* hold card in reset state */
		setHardwareReset(dev);
		/* set reset flag and then release reset */
		WRITESHM(arlan->resetFlag, 0xff, u_char);
		clearChannelAttention(dev);
		clearHardwareReset(dev);
		priv->card_polling_interval = HZ / 4;
		priv->waiting_command_mask &= ~ARLAN_COMMAND_RESET;
		priv->waiting_command_mask |= ARLAN_COMMAND_INT_RACK;
//		priv->waiting_command_mask |= ARLAN_COMMAND_INT_RENABLE; 
//		priv->waiting_command_mask |= ARLAN_COMMAND_RX;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_INT_RACK)
	{
		clearHardwareReset(dev);
		clearClearInterrupt(dev);
		setClearInterrupt(dev);
		setInterruptEnable(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_INT_RACK;
		priv->waiting_command_mask |= ARLAN_COMMAND_CONF;
		priv->under_config = 1;
		priv->under_reset = 0;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_INT_RENABLE)
	{
		setInterruptEnable(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_INT_RENABLE;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_CONF)
	{
		if (priv->tx_command_given || priv->rx_command_given)
		{
			printk(KERN_ERR "%s: Reset under tx or rx command \n", dev->name);
		}
		arlan_drop_tx(dev);
		setInterruptEnable(dev);
		arlan_hw_config(dev);
		arlan_interrupt_lancpu(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_CONF;
		priv->card_polling_interval = HZ / 10;
//		priv->waiting_command_mask |= ARLAN_COMMAND_INT_RACK;   
//		priv->waiting_command_mask |= ARLAN_COMMAND_INT_ENABLE; 
		priv->waiting_command_mask |= ARLAN_COMMAND_CONF_WAIT;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_CONF_WAIT)
	{
		if (READSHMB(arlan->configuredStatusFlag) != 0 &&
			READSHMB(arlan->diagnosticInfo) == 0xff)
		{
			priv->waiting_command_mask &= ~ARLAN_COMMAND_CONF_WAIT;
			priv->waiting_command_mask |= ARLAN_COMMAND_RX;
			priv->waiting_command_mask |= ARLAN_COMMAND_TBUSY_CLEAR;
			priv->card_polling_interval = HZ / 10;
			priv->tx_command_given = 0;
			priv->under_config = 0;
		}
		else
		{
			priv->card_polling_interval = 1;
			if (arlan_debug & ARLAN_DEBUG_TIMING)
				printk(KERN_ERR "configure delayed \n");
		}
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_RX)
	{
		if (!registrationBad(dev))
		{
			setInterruptEnable(dev);
			memset_io(arlan->commandParameter, 0, 0xf);
			WRITESHMB(arlan->commandByte, ARLAN_COM_INT | ARLAN_COM_RX_ENABLE);
			WRITESHMB(arlan->commandParameter[0], conf->rxParameter);
			arlan_interrupt_lancpu(dev);
			priv->rx_command_given = 0; // mnjah, bad
			priv->waiting_command_mask &= ~ARLAN_COMMAND_RX;
			priv->card_polling_interval = 1;
		}
		else
			priv->card_polling_interval = 2;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_TBUSY_CLEAR)
	{
		if ( !registrationBad(dev) &&
		     (netif_queue_stopped(dev) || !netif_running(dev)) )
			{
				priv->waiting_command_mask &= ~ARLAN_COMMAND_TBUSY_CLEAR;
				netif_wake_queue (dev);
			}
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_TX)
	{
		if (!test_and_set_bit(0, (void *) &priv->tx_command_given))
		{
			if (time_after(jiffies, 
				       priv->tx_last_sent + us2ticks(conf->rx_tweak1))
			    || time_before(jiffies,
					   priv->last_rx_int_ack_time + us2ticks(conf->rx_tweak2)))
			{
				setInterruptEnable(dev);
				memset_io(arlan->commandParameter, 0, 0xf);
				WRITESHMB(arlan->commandByte, ARLAN_COM_TX_ENABLE | ARLAN_COM_INT);
				memcpy_toio(arlan->commandParameter, &TXLAST(dev), 14);
//				for ( i=1 ; i < 15 ; i++) printk("%02x:",READSHMB(arlan->commandParameter[i]));
				priv->tx_last_sent = jiffies;
				arlan_interrupt_lancpu(dev);
				priv->tx_command_given = 1;
				priv->waiting_command_mask &= ~ARLAN_COMMAND_TX;
				priv->card_polling_interval = 1;
			}
			else
			{
				priv->tx_command_given = 0;
				priv->card_polling_interval = 1;
			}
		} 
		else if (arlan_debug & ARLAN_DEBUG_CHAIN_LOCKS)
			printk(KERN_ERR "tx command when tx chain locked \n");
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_NOOPINT)
	{
		{
			WRITESHMB(arlan->commandByte, ARLAN_COM_NOP | ARLAN_COM_INT);
		}
		arlan_interrupt_lancpu(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_NOOPINT;
		priv->card_polling_interval = HZ / 3;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_NOOP)
	{
		WRITESHMB(arlan->commandByte, ARLAN_COM_NOP);
		arlan_interrupt_lancpu(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_NOOP;
		priv->card_polling_interval = HZ / 3;
	}
	else if (priv->waiting_command_mask & ARLAN_COMMAND_SLOW_POLL)
	{
		WRITESHMB(arlan->commandByte, ARLAN_COM_GOTO_SLOW_POLL);
		arlan_interrupt_lancpu(dev);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_SLOW_POLL;
		priv->card_polling_interval = HZ / 3;
	} 
	else if (priv->waiting_command_mask & ARLAN_COMMAND_POWERDOWN)
	{
		setPowerOff(dev);
		if (arlan_debug & ARLAN_DEBUG_CARD_STATE)
			printk(KERN_WARNING "%s: Arlan Going Standby\n", dev->name);
		priv->waiting_command_mask &= ~ARLAN_COMMAND_POWERDOWN;
		priv->card_polling_interval = 3 * HZ;
	}
	arlan_unlock_card_access(dev);
	for (i = 0; READSHMB(arlan->commandByte) && i < 20; i++)
		udelay(10);
	if (READSHMB(arlan->commandByte))
		if (arlan_debug & ARLAN_DEBUG_CARD_STATE)
			printk(KERN_ERR "card busy leaving command %lx\n", priv->waiting_command_mask);

	spin_unlock_irqrestore(&priv->lock, flags);
	ARLAN_DEBUG_EXIT("arlan_command");
	priv->last_command_buff_free_time = jiffies;
	return 0;

card_busy_end:
	if (time_after(jiffies, priv->last_command_buff_free_time + HZ))
		priv->waiting_command_mask |= ARLAN_COMMAND_CLEAN_AND_RESET;

	if (arlan_debug & ARLAN_DEBUG_CARD_STATE)
		printk(KERN_ERR "%s arlan_command card busy end \n", dev->name);
	spin_unlock_irqrestore(&priv->lock, flags);
	ARLAN_DEBUG_EXIT("arlan_command");
	return 1;

bad_end:
	printk(KERN_ERR "%s arlan_command bad end \n", dev->name);

	spin_unlock_irqrestore(&priv->lock, flags);
	ARLAN_DEBUG_EXIT("arlan_command");

	return -1;
}

static inline void arlan_command_process(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);

	int times = 0;
	while (priv->waiting_command_mask && times < 8)
	{
		if (priv->waiting_command_mask)
		{
			if (arlan_command(dev, 0))
				break;
			times++;
		}
		/* if long command, we won't repeat trying */ ;
		if (priv->card_polling_interval > 1)
			break;
		times++;
	}
}


static inline void arlan_retransmit_now(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);


	ARLAN_DEBUG_ENTRY("arlan_retransmit_now");
	if (TXLAST(dev).offset == 0)
	{
		if (TXHEAD(dev).offset)
		{
			priv->txLast = 0;
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk(KERN_DEBUG "TX buff switch to head \n");

		}
		else if (TXTAIL(dev).offset)
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk(KERN_DEBUG "TX buff switch to tail \n");
			priv->txLast = 1;
		}
		else
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk(KERN_ERR "ReTransmit buff empty");
		netif_wake_queue (dev);
		return;

	}
	arlan_command(dev, ARLAN_COMMAND_TX);

	priv->Conf->driverRetransmissions++;
	priv->retransmissions++;

	IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk("Retransmit %d bytes \n", TXLAST(dev).length);

	ARLAN_DEBUG_EXIT("arlan_retransmit_now");
}



static void arlan_registration_timer(unsigned long data)
{
	struct net_device *dev = (struct net_device *) data;
	struct arlan_private *priv = netdev_priv(dev);
	int bh_mark_needed = 0;
	int next_tick = 1;
	long lostTime = ((long)jiffies - (long)priv->registrationLastSeen)
			* (1000/HZ);

	if (registrationBad(dev))
	{
		priv->registrationLostCount++;
		if (lostTime > 7000 && lostTime < 7200)
		{
			printk(KERN_NOTICE "%s registration Lost \n", dev->name);
		}
		if (lostTime / priv->reRegisterExp > 2000)
			arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_CONF);
		if (lostTime / (priv->reRegisterExp) > 3500)
			arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_RESET);
		if (priv->reRegisterExp < 400)
			priv->reRegisterExp += 2;
		if (lostTime > 7200)
		{
			next_tick = HZ;
			arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_RESET);
		}
	}
	else
	{
		if (priv->Conf->registrationMode && lostTime > 10000 &&
			priv->registrationLostCount)
		{
			printk(KERN_NOTICE "%s registration is back after %ld milliseconds\n",
			       dev->name, lostTime);
		}
		priv->registrationLastSeen = jiffies;
		priv->registrationLostCount = 0;
		priv->reRegisterExp = 1;
		if (!netif_running(dev) )
			netif_wake_queue(dev);
		if (time_after(priv->tx_last_sent,priv->tx_last_cleared) &&
		    time_after(jiffies, priv->tx_last_sent * 5*HZ) ){
			arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_RESET);		
			priv->tx_last_cleared = jiffies;
		}
	}


	if (!registrationBad(dev) && priv->ReTransmitRequested)
	{
		IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
			printk(KERN_ERR "Retransmit from timer \n");
		priv->ReTransmitRequested = 0;
		arlan_retransmit_now(dev);
	}
	if (!registrationBad(dev) &&
		time_after(jiffies, priv->tx_done_delayed) &&
		priv->tx_done_delayed != 0)
	{
		TXLAST(dev).offset = 0;
		if (priv->txLast)
			priv->txLast = 0;
		else if (TXTAIL(dev).offset)
			priv->txLast = 1;
		if (TXLAST(dev).offset)
		{
			arlan_retransmit_now(dev);
			dev->trans_start = jiffies;
		}
		if (!(TXHEAD(dev).offset && TXTAIL(dev).offset))
		{
			netif_wake_queue (dev);
		}
		priv->tx_done_delayed = 0;
		bh_mark_needed = 1;
	}
	if (bh_mark_needed)
	{
		netif_wake_queue (dev);
	}
	arlan_process_interrupt(dev);

	if (next_tick < priv->card_polling_interval)
		next_tick = priv->card_polling_interval;

	priv->timer.expires = jiffies + next_tick;

	add_timer(&priv->timer);
}


#ifdef ARLAN_DEBUGGING

static void arlan_print_registers(struct net_device *dev, int line)
{
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem *arlan = priv->card;

	u_char hostcpuLock, lancpuLock, controlRegister, cntrlRegImage,
		txStatus, rxStatus, interruptInProgress, commandByte;


	ARLAN_DEBUG_ENTRY("arlan_print_registers");
	READSHM(interruptInProgress, arlan->interruptInProgress, u_char);
	READSHM(hostcpuLock, arlan->hostcpuLock, u_char);
	READSHM(lancpuLock, arlan->lancpuLock, u_char);
	READSHM(controlRegister, arlan->controlRegister, u_char);
	READSHM(cntrlRegImage, arlan->cntrlRegImage, u_char);
	READSHM(txStatus, arlan->txStatus, u_char);
	READSHM(rxStatus, arlan->rxStatus, u_char);
	READSHM(commandByte, arlan->commandByte, u_char);

	printk(KERN_WARNING "line %04d IP %02x HL %02x LL %02x CB %02x CR %02x CRI %02x TX %02x RX %02x\n",
		line, interruptInProgress, hostcpuLock, lancpuLock, commandByte,
		controlRegister, cntrlRegImage, txStatus, rxStatus);

	ARLAN_DEBUG_EXIT("arlan_print_registers");
}
#endif


static int arlan_hw_tx(struct net_device *dev, char *buf, int length)
{
	int i;

	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	struct arlan_conf_stru *conf = priv->Conf;

	int tailStarts = 0x800;
	int headEnds = 0x0;


	ARLAN_DEBUG_ENTRY("arlan_hw_tx");
	if (TXHEAD(dev).offset)
		headEnds = (((TXHEAD(dev).offset + TXHEAD(dev).length - offsetof(struct arlan_shmem, txBuffer)) / 64) + 1) * 64;
	if (TXTAIL(dev).offset)
		tailStarts = 0x800 - (((TXTAIL(dev).offset - offsetof(struct arlan_shmem, txBuffer)) / 64) + 2) * 64;


	if (!TXHEAD(dev).offset && length < tailStarts)
	{
		IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
			printk(KERN_ERR "TXHEAD insert, tailStart %d\n", tailStarts);

		TXHEAD(dev).offset =
			offsetof(struct arlan_shmem, txBuffer);
		TXHEAD(dev).length = length - ARLAN_FAKE_HDR_LEN;
		for (i = 0; i < 6; i++)
			TXHEAD(dev).dest[i] = buf[i];
		TXHEAD(dev).clear = conf->txClear;
		TXHEAD(dev).retries = conf->txRetries;	/* 0 is use default */
		TXHEAD(dev).routing = conf->txRouting;
		TXHEAD(dev).scrambled = conf->txScrambled;
		memcpy_toio((char __iomem *)arlan + TXHEAD(dev).offset, buf + ARLAN_FAKE_HDR_LEN, TXHEAD(dev).length);
	}
	else if (!TXTAIL(dev).offset && length < (0x800 - headEnds))
	{
		IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
			printk(KERN_ERR "TXTAIL insert, headEnd %d\n", headEnds);

		TXTAIL(dev).offset =
			offsetof(struct arlan_shmem, txBuffer) + 0x800 - (length / 64 + 2) * 64;
		TXTAIL(dev).length = length - ARLAN_FAKE_HDR_LEN;
		for (i = 0; i < 6; i++)
			TXTAIL(dev).dest[i] = buf[i];
		TXTAIL(dev).clear = conf->txClear;
		TXTAIL(dev).retries = conf->txRetries;
		TXTAIL(dev).routing = conf->txRouting;
		TXTAIL(dev).scrambled = conf->txScrambled;
		memcpy_toio(((char __iomem *)arlan + TXTAIL(dev).offset), buf + ARLAN_FAKE_HDR_LEN, TXTAIL(dev).length);
	}
	else
	{
		netif_stop_queue (dev);
		IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
			printk(KERN_ERR "TX TAIL & HEAD full, return, tailStart %d headEnd %d\n", tailStarts, headEnds);
		return -1;
	}
	priv->out_bytes += length;
	priv->out_bytes10 += length;
	if (conf->measure_rate < 1)
		conf->measure_rate = 1;
	if (time_after(jiffies, priv->out_time + conf->measure_rate * HZ))
	{
		conf->out_speed = priv->out_bytes / conf->measure_rate;
		priv->out_bytes = 0;
		priv->out_time = jiffies;
	}
	if (time_after(jiffies, priv->out_time10 + conf->measure_rate * 10*HZ))
	{
		conf->out_speed10 = priv->out_bytes10 / (10 * conf->measure_rate);
		priv->out_bytes10 = 0;
		priv->out_time10 = jiffies;
	}
	if (TXHEAD(dev).offset && TXTAIL(dev).offset)
	{
		netif_stop_queue (dev);
		return 0;
	}
	else
		netif_start_queue (dev);


	IFDEBUG(ARLAN_DEBUG_HEADER_DUMP)
		printk(KERN_WARNING "%s Transmit t %2x:%2x:%2x:%2x:%2x:%2x f %2x:%2x:%2x:%2x:%2x:%2x \n", dev->name,
		   (unsigned char) buf[0], (unsigned char) buf[1], (unsigned char) buf[2], (unsigned char) buf[3],
		   (unsigned char) buf[4], (unsigned char) buf[5], (unsigned char) buf[6], (unsigned char) buf[7],
		   (unsigned char) buf[8], (unsigned char) buf[9], (unsigned char) buf[10], (unsigned char) buf[11]);

	IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk(KERN_ERR "TX command prepare for buffer %d\n", priv->txLast);

	arlan_command(dev, ARLAN_COMMAND_TX);

	priv->tx_last_sent = jiffies;

	IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk("%s TX Qued %d bytes \n", dev->name, length);

	ARLAN_DEBUG_EXIT("arlan_hw_tx");

	return 0;
}


static int arlan_hw_config(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	struct arlan_conf_stru *conf = priv->Conf;

	ARLAN_DEBUG_ENTRY("arlan_hw_config");

	printk(KERN_NOTICE "%s arlan configure called \n", dev->name);
	if (arlan_EEPROM_bad)
		printk(KERN_NOTICE "arlan configure with eeprom bad option \n");


	WRITESHM(arlan->spreadingCode, conf->spreadingCode, u_char);
	WRITESHM(arlan->channelSet, conf->channelSet, u_char);

	if (arlan_EEPROM_bad)
		WRITESHM(arlan->defaultChannelSet, conf->channelSet, u_char);

	WRITESHM(arlan->channelNumber, conf->channelNumber, u_char);

	WRITESHM(arlan->scramblingDisable, conf->scramblingDisable, u_char);
	WRITESHM(arlan->txAttenuation, conf->txAttenuation, u_char);

	WRITESHM(arlan->systemId, conf->systemId, u_int);

	WRITESHM(arlan->maxRetries, conf->maxRetries, u_char);
	WRITESHM(arlan->receiveMode, conf->receiveMode, u_char);
	WRITESHM(arlan->priority, conf->priority, u_char);
	WRITESHM(arlan->rootOrRepeater, conf->rootOrRepeater, u_char);
	WRITESHM(arlan->SID, conf->SID, u_int);

	WRITESHM(arlan->registrationMode, conf->registrationMode, u_char);

	WRITESHM(arlan->registrationFill, conf->registrationFill, u_char);
	WRITESHM(arlan->localTalkAddress, conf->localTalkAddress, u_char);
	WRITESHM(arlan->codeFormat, conf->codeFormat, u_char);
	WRITESHM(arlan->numChannels, conf->numChannels, u_char);
	WRITESHM(arlan->channel1, conf->channel1, u_char);
	WRITESHM(arlan->channel2, conf->channel2, u_char);
	WRITESHM(arlan->channel3, conf->channel3, u_char);
	WRITESHM(arlan->channel4, conf->channel4, u_char);
	WRITESHM(arlan->radioNodeId, conf->radioNodeId, u_short);
	WRITESHM(arlan->SID, conf->SID, u_int);
	WRITESHM(arlan->waitTime, conf->waitTime, u_short);
	WRITESHM(arlan->lParameter, conf->lParameter, u_short);
	memcpy_toio(&(arlan->_15), &(conf->_15), 3);
	WRITESHM(arlan->_15, conf->_15, u_short);
	WRITESHM(arlan->headerSize, conf->headerSize, u_short);
	if (arlan_EEPROM_bad)
		WRITESHM(arlan->hardwareType, conf->hardwareType, u_char);
	WRITESHM(arlan->radioType, conf->radioType, u_char);
	if (arlan_EEPROM_bad)
		WRITESHM(arlan->radioModule, conf->radioType, u_char);

	memcpy_toio(arlan->encryptionKey + keyStart, encryptionKey, 8);
	memcpy_toio(arlan->name, conf->siteName, 16);

	WRITESHMB(arlan->commandByte, ARLAN_COM_INT | ARLAN_COM_CONF);	/* do configure */
	memset_io(arlan->commandParameter, 0, 0xf);	/* 0xf */
	memset_io(arlan->commandParameter + 1, 0, 2);
	if (conf->writeEEPROM)
	{
		  memset_io(arlan->commandParameter, conf->writeEEPROM, 1);
//		conf->writeEEPROM=0;
	}
	if (conf->registrationMode && conf->registrationInterrupts)
		memset_io(arlan->commandParameter + 3, 1, 1);
	else
		memset_io(arlan->commandParameter + 3, 0, 1);

	priv->irq_test_done = 0;

	if (conf->tx_queue_len)
		dev->tx_queue_len = conf->tx_queue_len;
	udelay(100);

	ARLAN_DEBUG_EXIT("arlan_hw_config");
	return 0;
}


static int arlan_read_card_configuration(struct net_device *dev)
{
	u_char tlx415;
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	struct arlan_conf_stru *conf = priv->Conf;

	ARLAN_DEBUG_ENTRY("arlan_read_card_configuration");

	if (radioNodeId == radioNodeIdUNKNOWN)
	{
		READSHM(conf->radioNodeId, arlan->radioNodeId, u_short);
	}
	else
		conf->radioNodeId = radioNodeId;
		
	if (SID == SIDUNKNOWN)
	{
		READSHM(conf->SID, arlan->SID, u_int);
	}
	else conf->SID = SID;
		
	if (spreadingCode == spreadingCodeUNKNOWN)
	{
		  READSHM(conf->spreadingCode, arlan->spreadingCode, u_char);
	}
	else
		conf->spreadingCode = spreadingCode;
		
	if (channelSet == channelSetUNKNOWN)
	{
		READSHM(conf->channelSet, arlan->channelSet, u_char);
	}
	else conf->channelSet = channelSet;

	if (channelNumber == channelNumberUNKNOWN)
	{
		READSHM(conf->channelNumber, arlan->channelNumber, u_char);
	}
	else conf->channelNumber = channelNumber;
	
	READSHM(conf->scramblingDisable, arlan->scramblingDisable, u_char);
	READSHM(conf->txAttenuation, arlan->txAttenuation, u_char);
	
	if (systemId == systemIdUNKNOWN)
	{
		READSHM(conf->systemId, arlan->systemId, u_int);
	} 
	else conf->systemId = systemId;
	
	READSHM(conf->maxDatagramSize, arlan->maxDatagramSize, u_short);
	READSHM(conf->maxFrameSize, arlan->maxFrameSize, u_short);
	READSHM(conf->maxRetries, arlan->maxRetries, u_char);
	READSHM(conf->receiveMode, arlan->receiveMode, u_char);
	READSHM(conf->priority, arlan->priority, u_char);
	READSHM(conf->rootOrRepeater, arlan->rootOrRepeater, u_char);

	if (SID == SIDUNKNOWN)
	{
		  READSHM(conf->SID, arlan->SID, u_int);
	}
	else conf->SID = SID;
	
	if (registrationMode == registrationModeUNKNOWN)
	{
		  READSHM(conf->registrationMode, arlan->registrationMode, u_char);
	}
	else conf->registrationMode = registrationMode;
	
	READSHM(conf->registrationFill, arlan->registrationFill, u_char);
	READSHM(conf->localTalkAddress, arlan->localTalkAddress, u_char);
	READSHM(conf->codeFormat, arlan->codeFormat, u_char);
	READSHM(conf->numChannels, arlan->numChannels, u_char);
	READSHM(conf->channel1, arlan->channel1, u_char);
	READSHM(conf->channel2, arlan->channel2, u_char);
	READSHM(conf->channel3, arlan->channel3, u_char);
	READSHM(conf->channel4, arlan->channel4, u_char);
	READSHM(conf->waitTime, arlan->waitTime, u_short);
	READSHM(conf->lParameter, arlan->lParameter, u_short);
	READSHM(conf->_15, arlan->_15, u_short);
	READSHM(conf->headerSize, arlan->headerSize, u_short);
	READSHM(conf->hardwareType, arlan->hardwareType, u_char);
	READSHM(conf->radioType, arlan->radioModule, u_char);
	
	if (conf->radioType == 0)
		conf->radioType = 0xc;

	WRITESHM(arlan->configStatus, 0xA5, u_char);
	READSHM(tlx415, arlan->configStatus, u_char);
	
	if (tlx415 != 0xA5)
		printk(KERN_INFO "%s tlx415 chip \n", dev->name);
	
	conf->txClear = 0;
	conf->txRetries = 1;
	conf->txRouting = 1;
	conf->txScrambled = 0;
	conf->rxParameter = 1;
	conf->txTimeoutMs = 4000;
	conf->waitCardTimeout = 100000;
	conf->receiveMode = ARLAN_RCV_CLEAN;
	memcpy_fromio(conf->siteName, arlan->name, 16);
	conf->siteName[16] = '\0';
	conf->retries = retries;
	conf->tx_delay_ms = tx_delay_ms;
	conf->ReTransmitPacketMaxSize = 200;
	conf->waitReTransmitPacketMaxSize = 200;
	conf->txAckTimeoutMs = 900;
	conf->fastReTransCount = 3;

	ARLAN_DEBUG_EXIT("arlan_read_card_configuration");

	return 0;
}


static int lastFoundAt = 0xbe000;


/*
 * This is the real probe routine. Linux has a history of friendly device
 * probes on the ISA bus. A good device probes avoids doing writes, and
 * verifies that the correct device exists and functions.
 */
#define ARLAN_SHMEM_SIZE	0x2000
static int __init arlan_check_fingerprint(unsigned long memaddr)
{
	static const char probeText[] = "TELESYSTEM SLW INC.    ARLAN \0";
	volatile struct arlan_shmem __iomem *arlan = (struct arlan_shmem *) memaddr;
	unsigned long paddr = virt_to_phys((void *) memaddr);
	char tempBuf[49];

	ARLAN_DEBUG_ENTRY("arlan_check_fingerprint");

	if (!request_mem_region(paddr, ARLAN_SHMEM_SIZE, "arlan")) {
		// printk(KERN_WARNING "arlan: memory region %lx excluded from probing \n",paddr);
		return -ENODEV;
	}

	memcpy_fromio(tempBuf, arlan->textRegion, 29);
	tempBuf[30] = 0;

	/* check for card at this address */
	if (0 != strncmp(tempBuf, probeText, 29)){
 		release_mem_region(paddr, ARLAN_SHMEM_SIZE);
		return -ENODEV;
	}

//   printk(KERN_INFO "arlan found at 0x%x \n",memaddr);
	ARLAN_DEBUG_EXIT("arlan_check_fingerprint");

	return 0;
}

static int arlan_change_mtu(struct net_device *dev, int new_mtu)
{
	struct arlan_private *priv = netdev_priv(dev);
	struct arlan_conf_stru *conf = priv->Conf;

	ARLAN_DEBUG_ENTRY("arlan_change_mtu");
	if (new_mtu > 2032)
		return -EINVAL;
	dev->mtu = new_mtu;
	if (new_mtu < 256)
		new_mtu = 256;	/* cards book suggests 1600 */
	conf->maxDatagramSize = new_mtu;
	conf->maxFrameSize = new_mtu + 48;

	arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_CONF);
	printk(KERN_NOTICE "%s mtu changed to %d \n", dev->name, new_mtu);

	ARLAN_DEBUG_EXIT("arlan_change_mtu");

	return 0;
}

static int arlan_mac_addr(struct net_device *dev, void *p)
{
	struct sockaddr *addr = p;


	ARLAN_DEBUG_ENTRY("arlan_mac_addr");
	return -EINVAL;

	if (!netif_running(dev))
		return -EBUSY;
	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);

	ARLAN_DEBUG_EXIT("arlan_mac_addr");
	return 0;
}



static int __init arlan_setup_device(struct net_device *dev, int num)
{
	struct arlan_private *ap = netdev_priv(dev);
	int err;

	ARLAN_DEBUG_ENTRY("arlan_setup_device");

	ap->conf = (struct arlan_shmem *)(ap+1);

	dev->tx_queue_len = tx_queue_len;
	dev->open = arlan_open;
	dev->stop = arlan_close;
	dev->hard_start_xmit = arlan_tx;
	dev->get_stats = arlan_statistics;
	dev->set_multicast_list = arlan_set_multicast;
	dev->change_mtu = arlan_change_mtu;
	dev->set_mac_address = arlan_mac_addr;
	dev->tx_timeout = arlan_tx_timeout;
	dev->watchdog_timeo = 3*HZ;
	
	ap->irq_test_done = 0;
	ap->Conf = &arlan_conf[num];

	ap->Conf->pre_Command_Wait = 40;
	ap->Conf->rx_tweak1 = 30;
	ap->Conf->rx_tweak2 = 0;


	err = register_netdev(dev);
	if (err) {
		release_mem_region(virt_to_phys((void *) dev->mem_start), 
			   ARLAN_SHMEM_SIZE);
		free_netdev(dev);
		return err;
	}
	arlan_device[num] = dev;
	ARLAN_DEBUG_EXIT("arlan_setup_device");
	return 0;
}

static int __init arlan_probe_here(struct net_device *dev, 
				   unsigned long memaddr)
{
	struct arlan_private *ap = netdev_priv(dev);

	ARLAN_DEBUG_ENTRY("arlan_probe_here");

	if (arlan_check_fingerprint(memaddr))
		return -ENODEV;

	printk(KERN_NOTICE "%s: Arlan found at %x, \n ", dev->name, 
	       (int) virt_to_phys((void*)memaddr));

	ap->card = (void *) memaddr;
	dev->mem_start = memaddr;
	dev->mem_end = memaddr + ARLAN_SHMEM_SIZE-1;

	if (dev->irq < 2)
	{
		READSHM(dev->irq, ap->card->irqLevel, u_char);
	} else if (dev->irq == 2)
		dev->irq = 9;

	arlan_read_card_configuration(dev);

	ARLAN_DEBUG_EXIT("arlan_probe_here");
	return 0;
}


static int arlan_open(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	int ret = 0;

	ARLAN_DEBUG_ENTRY("arlan_open");

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


	priv->bad = 0;
	priv->lastReset = 0;
	priv->reset = 0;
	memcpy_fromio(dev->dev_addr, arlan->lanCardNodeId, 6);
	memset(dev->broadcast, 0xff, 6);
	dev->tx_queue_len = tx_queue_len;
	priv->interrupt_processing_active = 0;
	spin_lock_init(&priv->lock);

	netif_start_queue (dev);

	priv->registrationLostCount = 0;
	priv->registrationLastSeen = jiffies;
	priv->txLast = 0;
	priv->tx_command_given = 0;
	priv->rx_command_given = 0;
	
	priv->reRegisterExp = 1;
	priv->tx_last_sent = jiffies - 1;
	priv->tx_last_cleared = jiffies;
	priv->Conf->writeEEPROM = 0;
	priv->Conf->registrationInterrupts = 1;

	init_timer(&priv->timer);
	priv->timer.expires = jiffies + HZ / 10;
	priv->timer.data = (unsigned long) dev;
	priv->timer.function = &arlan_registration_timer;	/* timer handler */

	arlan_command(dev, ARLAN_COMMAND_POWERUP | ARLAN_COMMAND_LONG_WAIT_NOW);
	mdelay(200);
	add_timer(&priv->timer);

	ARLAN_DEBUG_EXIT("arlan_open");
	return 0;
}


static void arlan_tx_timeout (struct net_device *dev)
{
	printk(KERN_ERR "%s: arlan transmit timed out, kernel decided\n", dev->name);
	/* Try to restart the adaptor. */
	arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_RESET);
	// dev->trans_start = jiffies;
	// netif_start_queue (dev);
}


static int arlan_tx(struct sk_buff *skb, struct net_device *dev)
{
	short length;
	unsigned char *buf;

	ARLAN_DEBUG_ENTRY("arlan_tx");
	
	length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
	buf = skb->data;

	if (length + 0x12 > 0x800) {
		printk(KERN_ERR "TX RING overflow \n");
		netif_stop_queue (dev);
	}

	if (arlan_hw_tx(dev, buf, length) == -1)
		goto bad_end;

	dev->trans_start = jiffies;

	dev_kfree_skb(skb);

	arlan_process_interrupt(dev);
	ARLAN_DEBUG_EXIT("arlan_tx");
	return 0;

bad_end:
	arlan_process_interrupt(dev);
	netif_stop_queue (dev);
	ARLAN_DEBUG_EXIT("arlan_tx");
	return 1;
}


static inline int DoNotReTransmitCrap(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);

	if (TXLAST(dev).length < priv->Conf->ReTransmitPacketMaxSize)
		return 1;
	return 0;

}

static inline int DoNotWaitReTransmitCrap(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);

	if (TXLAST(dev).length < priv->Conf->waitReTransmitPacketMaxSize)
		return 1;
	return 0;
}

static inline void arlan_queue_retransmit(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);

	ARLAN_DEBUG_ENTRY("arlan_queue_retransmit");

	if (DoNotWaitReTransmitCrap(dev))
	{
		  arlan_drop_tx(dev);
	} else
		priv->ReTransmitRequested++;

	ARLAN_DEBUG_EXIT("arlan_queue_retransmit");
}

static inline void RetryOrFail(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);

	ARLAN_DEBUG_ENTRY("RetryOrFail");

	if (priv->retransmissions > priv->Conf->retries ||
	    DoNotReTransmitCrap(dev))
	{
		arlan_drop_tx(dev);
	}
	else if (priv->bad <= priv->Conf->fastReTransCount)
	{
		arlan_retransmit_now(dev);
	}
	else arlan_queue_retransmit(dev);

	ARLAN_DEBUG_EXIT("RetryOrFail");
}


static void arlan_tx_done_interrupt(struct net_device *dev, int status)
{
	struct arlan_private *priv = netdev_priv(dev);

	ARLAN_DEBUG_ENTRY("arlan_tx_done_interrupt");

	priv->tx_last_cleared = jiffies;
	priv->tx_command_given = 0;
	switch (status)
	{
		case 1:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
				printk("arlan intr: transmit OK\n");
			priv->stats.tx_packets++;
			priv->bad = 0;
			priv->reset = 0;
			priv->retransmissions = 0;
			if (priv->Conf->tx_delay_ms)
			{
				priv->tx_done_delayed = jiffies + (priv->Conf->tx_delay_ms * HZ) / 1000 + 1;
			}
			else
			{
				TXLAST(dev).offset = 0;
				if (priv->txLast)
					priv->txLast = 0;
				else if (TXTAIL(dev).offset)
					priv->txLast = 1;
				if (TXLAST(dev).offset)
				{
					arlan_retransmit_now(dev);
					dev->trans_start = jiffies;
				}
				if (!TXHEAD(dev).offset || !TXTAIL(dev).offset)
				{
					netif_wake_queue (dev);
				}
			}
		}
		break;
		
		case 2:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
				printk("arlan intr: transmit timed out\n");
			priv->bad += 1;
			//arlan_queue_retransmit(dev);
			RetryOrFail(dev);
		}
		break;

		case 3:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
				printk("arlan intr: transmit max retries\n");
			priv->bad += 1;
			priv->reset = 0;
			//arlan_queue_retransmit(dev);
			RetryOrFail(dev);
		}
		break;
		
		case 4:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
				printk("arlan intr: transmit aborted\n");
			priv->bad += 1;
			arlan_queue_retransmit(dev);
			//RetryOrFail(dev);
		}
		break;

		case 5:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
				printk("arlan intr: transmit not registered\n");
			priv->bad += 1;
			//debug=101;
			arlan_queue_retransmit(dev);
		}
		break;

		case 6:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN) 
				printk("arlan intr: transmit destination full\n");
			priv->bad += 1;
			priv->reset = 0;
			//arlan_drop_tx(dev);
			arlan_queue_retransmit(dev);
		}
		break;

		case 7:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
				printk("arlan intr: transmit unknown ack\n");
			priv->bad += 1;
			priv->reset = 0;
			arlan_queue_retransmit(dev);
		}
		break;
		
		case 8:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
				printk("arlan intr: transmit dest mail box full\n");
			priv->bad += 1;
			priv->reset = 0;
			//arlan_drop_tx(dev);
			arlan_queue_retransmit(dev);
		}
		break;

		case 9:
		{
			IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
				printk("arlan intr: transmit root dest not reg.\n");
			priv->bad += 1;
			priv->reset = 1;
			//arlan_drop_tx(dev);
			arlan_queue_retransmit(dev);
		}
		break;

		default:
		{
			printk(KERN_ERR "arlan intr: transmit status unknown\n");
			priv->bad += 1;
			priv->reset = 1;
			arlan_drop_tx(dev);
		}
	}

	ARLAN_DEBUG_EXIT("arlan_tx_done_interrupt");
}


static void arlan_rx_interrupt(struct net_device *dev, u_char rxStatus, u_short rxOffset, u_short pkt_len)
{
	char *skbtmp;
	int i = 0;

	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	struct arlan_conf_stru *conf = priv->Conf;


	ARLAN_DEBUG_ENTRY("arlan_rx_interrupt");
	// by spec,   not                WRITESHMB(arlan->rxStatus,0x00);
	// prohibited here              arlan_command(dev, ARLAN_COMMAND_RX);

	if (pkt_len < 10 || pkt_len > 2048)
	{
		printk(KERN_WARNING "%s: got too short or long packet, len %d \n", dev->name, pkt_len);
		return;
	}
	if (rxOffset + pkt_len > 0x2000)
	{
		printk("%s: got too long packet, len %d offset %x\n", dev->name, pkt_len, rxOffset);
		return;
	}
	priv->in_bytes += pkt_len;
	priv->in_bytes10 += pkt_len;
	if (conf->measure_rate < 1)
		conf->measure_rate = 1;
	if (time_after(jiffies, priv->in_time + conf->measure_rate * HZ))
	{
		conf->in_speed = priv->in_bytes / conf->measure_rate;
		priv->in_bytes = 0;
		priv->in_time = jiffies;
	}
	if (time_after(jiffies, priv->in_time10 + conf->measure_rate * 10*HZ))
	{
		conf->in_speed10 = priv->in_bytes10 / (10 * conf->measure_rate);
		priv->in_bytes10 = 0;
		priv->in_time10 = jiffies;
	}
	DEBUGSHM(1, "arlan rcv pkt rxStatus= %d ", arlan->rxStatus, u_char);
	switch (rxStatus)
	{
		case 1:
		case 2:
		case 3:
		{
			/* Malloc up new buffer. */
			struct sk_buff *skb;

			DEBUGSHM(50, "arlan recv pkt offs=%d\n", arlan->rxOffset, u_short);
			DEBUGSHM(1, "arlan rxFrmType = %d \n", arlan->rxFrmType, u_char);
			DEBUGSHM(1, KERN_INFO "arlan rx scrambled = %d \n", arlan->scrambled, u_char);

			/* here we do multicast filtering to avoid slow 8-bit memcopy */
#ifdef ARLAN_MULTICAST
			if (!(dev->flags & IFF_ALLMULTI) &&
				!(dev->flags & IFF_PROMISC) &&
				dev->mc_list)
			{
				char hw_dst_addr[6];
				struct dev_mc_list *dmi = dev->mc_list;
				int i;

				memcpy_fromio(hw_dst_addr, arlan->ultimateDestAddress, 6);
				if (hw_dst_addr[0] == 0x01)
				{
					if (mdebug)
						if (hw_dst_addr[1] == 0x00)
							printk(KERN_ERR "%s mcast 0x0100 \n", dev->name);
						else if (hw_dst_addr[1] == 0x40)
							printk(KERN_ERR "%s m/bcast 0x0140 \n", dev->name);
					while (dmi)
					{							if (dmi->dmi_addrlen == 6)
						{
							if (arlan_debug & ARLAN_DEBUG_HEADER_DUMP)
								printk(KERN_ERR "%s mcl %2x:%2x:%2x:%2x:%2x:%2x \n", dev->name,
										 dmi->dmi_addr[0], dmi->dmi_addr[1], dmi->dmi_addr[2],
										 dmi->dmi_addr[3], dmi->dmi_addr[4], dmi->dmi_addr[5]);
							for (i = 0; i < 6; i++)
								if (dmi->dmi_addr[i] != hw_dst_addr[i])
									break;
							if (i == 6)
								break;
						}
						else
							printk(KERN_ERR "%s: invalid multicast address length given.\n", dev->name);
						dmi = dmi->next;
					}
					/* we reach here if multicast filtering is on and packet 
					 * is multicast and not for receive */
					goto end_of_interrupt;
				}
			}
#endif				// ARLAN_MULTICAST
			/* multicast filtering ends here */
			pkt_len += ARLAN_FAKE_HDR_LEN;

			skb = dev_alloc_skb(pkt_len + 4);
			if (skb == NULL)
			{
				printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n", dev->name);
				priv->stats.rx_dropped++;
				break;
			}
			skb_reserve(skb, 2);
			skb->dev = dev;
			skbtmp = skb_put(skb, pkt_len);

			memcpy_fromio(skbtmp + ARLAN_FAKE_HDR_LEN, ((char __iomem *) arlan) + rxOffset, pkt_len - ARLAN_FAKE_HDR_LEN);
			memcpy_fromio(skbtmp, arlan->ultimateDestAddress, 6);
			memcpy_fromio(skbtmp + 6, arlan->rxSrc, 6);
			WRITESHMB(arlan->rxStatus, 0x00);
			arlan_command(dev, ARLAN_COMMAND_RX);

			IFDEBUG(ARLAN_DEBUG_HEADER_DUMP)
			{
				char immedDestAddress[6];
				char immedSrcAddress[6];
				memcpy_fromio(immedDestAddress, arlan->immedDestAddress, 6);
				memcpy_fromio(immedSrcAddress, arlan->immedSrcAddress, 6);

				printk(KERN_WARNING "%s t %2x:%2x:%2x:%2x:%2x:%2x f %2x:%2x:%2x:%2x:%2x:%2x imd %2x:%2x:%2x:%2x:%2x:%2x ims %2x:%2x:%2x:%2x:%2x:%2x\n", dev->name,
					(unsigned char) skbtmp[0], (unsigned char) skbtmp[1], (unsigned char) skbtmp[2], (unsigned char) skbtmp[3],
					(unsigned char) skbtmp[4], (unsigned char) skbtmp[5], (unsigned char) skbtmp[6], (unsigned char) skbtmp[7],
					(unsigned char) skbtmp[8], (unsigned char) skbtmp[9], (unsigned char) skbtmp[10], (unsigned char) skbtmp[11],
					immedDestAddress[0], immedDestAddress[1], immedDestAddress[2],
					immedDestAddress[3], immedDestAddress[4], immedDestAddress[5],
					immedSrcAddress[0], immedSrcAddress[1], immedSrcAddress[2],
					immedSrcAddress[3], immedSrcAddress[4], immedSrcAddress[5]);
			}
			skb->protocol = eth_type_trans(skb, dev);
			IFDEBUG(ARLAN_DEBUG_HEADER_DUMP)
				if (skb->protocol != 0x608 && skb->protocol != 0x8)
				{
					for (i = 0; i <= 22; i++)
						printk("%02x:", (u_char) skbtmp[i + 12]);
					printk(KERN_ERR "\n");
					printk(KERN_WARNING "arlan kernel pkt type trans %x \n", skb->protocol);
				}
			netif_rx(skb);
			dev->last_rx = jiffies;
			priv->stats.rx_packets++;
			priv->stats.rx_bytes += pkt_len;
		}
		break;
		
		default:
			printk(KERN_ERR "arlan intr: received unknown status\n");
			priv->stats.rx_crc_errors++;
			break;
	}
	ARLAN_DEBUG_EXIT("arlan_rx_interrupt");
}

static void arlan_process_interrupt(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	u_char rxStatus = READSHMB(arlan->rxStatus);
	u_char txStatus = READSHMB(arlan->txStatus);
	u_short rxOffset = READSHMS(arlan->rxOffset);
	u_short pkt_len = READSHMS(arlan->rxLength);
	int interrupt_count = 0;

	ARLAN_DEBUG_ENTRY("arlan_process_interrupt");

	if (test_and_set_bit(0, (void *) &priv->interrupt_processing_active))
	{
		if (arlan_debug & ARLAN_DEBUG_CHAIN_LOCKS)
			printk(KERN_ERR "interrupt chain reentering \n");
		goto end_int_process;
	}
	while ((rxStatus || txStatus || priv->interrupt_ack_requested)
			&& (interrupt_count < 5))
	{
		if (rxStatus)
			priv->last_rx_int_ack_time = jiffies;

		arlan_command(dev, ARLAN_COMMAND_INT_ACK);
		arlan_command(dev, ARLAN_COMMAND_INT_ENABLE);
		
		IFDEBUG(ARLAN_DEBUG_INTERRUPT)
			printk(KERN_ERR "%s:  got IRQ rx %x tx %x comm %x rxOff %x rxLen %x \n",
					dev->name, rxStatus, txStatus, READSHMB(arlan->commandByte),
					rxOffset, pkt_len);

		if (rxStatus == 0 && txStatus == 0)
		{
			if (priv->irq_test_done)
			{
				if (!registrationBad(dev))
					IFDEBUG(ARLAN_DEBUG_INTERRUPT) printk(KERN_ERR "%s unknown interrupt(nop? regLost ?) reason tx %d rx %d ",
										    dev->name, txStatus, rxStatus);
			} else {
				IFDEBUG(ARLAN_DEBUG_INTERRUPT)
					printk(KERN_INFO "%s irq $%d test OK \n", dev->name, dev->irq);

			}
			priv->interrupt_ack_requested = 0;
			goto ends;
		}
		if (txStatus != 0)
		{
			WRITESHMB(arlan->txStatus, 0x00);
			arlan_tx_done_interrupt(dev, txStatus);
			goto ends;
		}
		if (rxStatus == 1 || rxStatus == 2)
		{		/* a packet waiting */
			arlan_rx_interrupt(dev, rxStatus, rxOffset, pkt_len);
			goto ends;
		}
		if (rxStatus > 2 && rxStatus < 0xff)
		{
			WRITESHMB(arlan->rxStatus, 0x00);
			printk(KERN_ERR "%s unknown rxStatus reason tx %d rx %d ",
				dev->name, txStatus, rxStatus);
			goto ends;
		}
		if (rxStatus == 0xff)
		{
			WRITESHMB(arlan->rxStatus, 0x00);
			arlan_command(dev, ARLAN_COMMAND_RX);
			if (registrationBad(dev))
				netif_device_detach(dev);
			if (!registrationBad(dev))
			{
				priv->registrationLastSeen = jiffies;
				if (!netif_queue_stopped(dev) && !priv->under_reset && !priv->under_config)
					netif_wake_queue (dev);
			}
			goto ends;
		}
ends:

		arlan_command_process(dev);

		rxStatus = READSHMB(arlan->rxStatus);
		txStatus = READSHMB(arlan->txStatus);
		rxOffset = READSHMS(arlan->rxOffset);
		pkt_len = READSHMS(arlan->rxLength);


		priv->irq_test_done = 1;

		interrupt_count++;
	}
	priv->interrupt_processing_active = 0;

end_int_process:
	arlan_command_process(dev);

	ARLAN_DEBUG_EXIT("arlan_process_interrupt");
	return;
}

static irqreturn_t arlan_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct net_device *dev = dev_id;
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	u_char rxStatus = READSHMB(arlan->rxStatus);
	u_char txStatus = READSHMB(arlan->txStatus);

	ARLAN_DEBUG_ENTRY("arlan_interrupt");


	if (!rxStatus && !txStatus)
		priv->interrupt_ack_requested++;

	arlan_process_interrupt(dev);
	
	priv->irq_test_done = 1;

	ARLAN_DEBUG_EXIT("arlan_interrupt");
	return IRQ_HANDLED;

}


static int arlan_close(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);

	ARLAN_DEBUG_ENTRY("arlan_close");

	del_timer_sync(&priv->timer);

	arlan_command(dev, ARLAN_COMMAND_POWERDOWN);

	IFDEBUG(ARLAN_DEBUG_STARTUP)
		printk(KERN_NOTICE "%s: Closing device\n", dev->name);

	netif_stop_queue(dev);
	free_irq(dev->irq, dev);

	ARLAN_DEBUG_EXIT("arlan_close");
	return 0;
}

#ifdef ARLAN_DEBUGGING
static long alignLong(volatile u_char * ptr)
{
	long ret;
	memcpy_fromio(&ret, (void *) ptr, 4);
	return ret;
}
#endif

/*
 * Get the current statistics.
 * This may be called with the card open or closed.
 */

static struct net_device_stats *arlan_statistics(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;


	ARLAN_DEBUG_ENTRY("arlan_statistics");

	/* Update the statistics from the device registers. */

	READSHM(priv->stats.collisions, arlan->numReTransmissions, u_int);
	READSHM(priv->stats.rx_crc_errors, arlan->numCRCErrors, u_int);
	READSHM(priv->stats.rx_dropped, arlan->numFramesDiscarded, u_int);
	READSHM(priv->stats.rx_fifo_errors, arlan->numRXBufferOverflows, u_int);
	READSHM(priv->stats.rx_frame_errors, arlan->numReceiveFramesLost, u_int);
	READSHM(priv->stats.rx_over_errors, arlan->numRXOverruns, u_int);
	READSHM(priv->stats.rx_packets, arlan->numDatagramsReceived, u_int);
	READSHM(priv->stats.tx_aborted_errors, arlan->numAbortErrors, u_int);
	READSHM(priv->stats.tx_carrier_errors, arlan->numStatusTimeouts, u_int);
	READSHM(priv->stats.tx_dropped, arlan->numDatagramsDiscarded, u_int);
	READSHM(priv->stats.tx_fifo_errors, arlan->numTXUnderruns, u_int);
	READSHM(priv->stats.tx_packets, arlan->numDatagramsTransmitted, u_int);
	READSHM(priv->stats.tx_window_errors, arlan->numHoldOffs, u_int);

	ARLAN_DEBUG_EXIT("arlan_statistics");

	return &priv->stats;
}


static void arlan_set_multicast(struct net_device *dev)
{
	struct arlan_private *priv = netdev_priv(dev);
	volatile struct arlan_shmem __iomem *arlan = priv->card;
	struct arlan_conf_stru *conf = priv->Conf;
	int board_conf_needed = 0;


	ARLAN_DEBUG_ENTRY("arlan_set_multicast");

	if (dev->flags & IFF_PROMISC)
	{
		unsigned char recMode;
		READSHM(recMode, arlan->receiveMode, u_char);
		conf->receiveMode = (ARLAN_RCV_PROMISC | ARLAN_RCV_CONTROL);
		if (conf->receiveMode != recMode)
			board_conf_needed = 1;
	}
	else
	{
		/* turn off promiscuous mode  */
		unsigned char recMode;
		READSHM(recMode, arlan->receiveMode, u_char);
		conf->receiveMode = ARLAN_RCV_CLEAN | ARLAN_RCV_CONTROL;
		if (conf->receiveMode != recMode)
			board_conf_needed = 1;
	}
	if (board_conf_needed)
		arlan_command(dev, ARLAN_COMMAND_CONF);

	ARLAN_DEBUG_EXIT("arlan_set_multicast");
}


struct net_device * __init arlan_probe(int unit)
{
	struct net_device *dev;
	int err;
	int m;

	ARLAN_DEBUG_ENTRY("arlan_probe");

	if (arlans_found == MAX_ARLANS)
		return ERR_PTR(-ENODEV);

	/* 
	 * Reserve space for local data and a copy of the shared memory
	 * that is used by the /proc interface.
	 */
	dev = alloc_etherdev(sizeof(struct arlan_private)
			     + sizeof(struct arlan_shmem));
	if (!dev)
		return ERR_PTR(-ENOMEM);

	SET_MODULE_OWNER(dev);

	if (unit >= 0) {
		sprintf(dev->name, "eth%d", unit);
		netdev_boot_setup_check(dev);
		
		if (dev->mem_start) {
			if (arlan_probe_here(dev, dev->mem_start) == 0)
				goto found;
			goto not_found;
		}
			
	}


	for (m = (int)phys_to_virt(lastFoundAt) + ARLAN_SHMEM_SIZE; 
	     m <= (int)phys_to_virt(0xDE000); 
	     m += ARLAN_SHMEM_SIZE)
	{
		if (arlan_probe_here(dev, m) == 0)
		{
			lastFoundAt = (int)virt_to_phys((void*)m);
			goto found;
		}
	}

	if (lastFoundAt == 0xbe000)
		printk(KERN_ERR "arlan: No Arlan devices found \n");

 not_found:
	free_netdev(dev);
	return ERR_PTR(-ENODEV);

 found:
	err = arlan_setup_device(dev, arlans_found);
	if (err)
		dev = ERR_PTR(err);
	else if (!arlans_found++)
		printk(KERN_INFO "Arlan driver %s\n", arlan_version);

	return dev;
}

#ifdef  MODULE
int __init init_module(void)
{
	int i = 0;

	ARLAN_DEBUG_ENTRY("init_module");

	if (channelSet != channelSetUNKNOWN || channelNumber != channelNumberUNKNOWN || systemId != systemIdUNKNOWN)
		return -EINVAL;

	for (i = 0; i < MAX_ARLANS; i++) {
		struct net_device *dev = arlan_probe(i);

		if (IS_ERR(dev)) 
			return PTR_ERR(dev);
	}
	init_arlan_proc();
	printk(KERN_INFO "Arlan driver %s\n", arlan_version);
	ARLAN_DEBUG_EXIT("init_module");
	return 0;
}


void __exit cleanup_module(void)
{
	int i = 0;
	struct net_device *dev;

	ARLAN_DEBUG_ENTRY("cleanup_module");

	IFDEBUG(ARLAN_DEBUG_SHUTDOWN)
		printk(KERN_INFO "arlan: unloading module\n");

	cleanup_arlan_proc();

	for (i = 0; i < MAX_ARLANS; i++)
	{
		dev = arlan_device[i];
		if (dev) {
			arlan_command(dev, ARLAN_COMMAND_POWERDOWN );

			unregister_netdev(dev);
			release_mem_region(virt_to_phys((void *) dev->mem_start), 
					   ARLAN_SHMEM_SIZE);
			free_netdev(dev);
			arlan_device[i] = NULL;
		}
	}

	ARLAN_DEBUG_EXIT("cleanup_module");
}


#endif
MODULE_LICENSE("GPL");