1 /* mac8390.c: New driver for 8390-based Nubus (or Nubus-alike)
2    Ethernet cards on Linux */
3 /* Based on the former daynaport.c driver, by Alan Cox.  Some code
4    taken from or inspired by skeleton.c by Donald Becker, acenic.c by
5    Jes Sorensen, and ne2k-pci.c by Donald Becker and Paul Gortmaker.
6 
7    This software may be used and distributed according to the terms of
8    the GNU Public License, incorporated herein by reference.  */
9 
10 /* 2000-02-28: support added for Dayna and Kinetics cards by
11    A.G.deWijn@phys.uu.nl */
12 /* 2000-04-04: support added for Dayna2 by bart@etpmod.phys.tue.nl */
13 /* 2001-04-18: support for DaynaPort E/LC-M by rayk@knightsmanor.org */
14 /* 2001-05-15: support for Cabletron ported from old daynaport driver
15  * and fixed access to Sonic Sys card which masquerades as a Farallon
16  * by rayk@knightsmanor.org */
17 /* 2002-12-30: Try to support more cards, some clues from NetBSD driver */
18 /* 2003-12-26: Make sure Asante cards always work. */
19 
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/interrupt.h>
27 #include <linux/ptrace.h>
28 #include <linux/ioport.h>
29 #include <linux/nubus.h>
30 #include <linux/in.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/bitops.h>
38 #include <linux/io.h>
39 
40 #include <asm/dma.h>
41 #include <asm/hwtest.h>
42 #include <asm/macints.h>
43 
44 static char version[] =
45 	"v0.4 2001-05-15 David Huggins-Daines <dhd@debian.org> and others\n";
46 
47 #define EI_SHIFT(x)	(ei_local->reg_offset[x])
48 #define ei_inb(port)	in_8(port)
49 #define ei_outb(val, port)	out_8(port, val)
50 #define ei_inb_p(port)	in_8(port)
51 #define ei_outb_p(val, port)	out_8(port, val)
52 
53 #include "lib8390.c"
54 
55 #define WD_START_PG			0x00	/* First page of TX buffer */
56 #define CABLETRON_RX_START_PG		0x00    /* First page of RX buffer */
57 #define CABLETRON_RX_STOP_PG		0x30    /* Last page +1 of RX ring */
58 #define CABLETRON_TX_START_PG		CABLETRON_RX_STOP_PG
59 						/* First page of TX buffer */
60 
61 /*
62  * Unfortunately it seems we have to hardcode these for the moment
63  * Shouldn't the card know about this?
64  * Does anyone know where to read it off the card?
65  * Do we trust the data provided by the card?
66  */
67 
68 #define DAYNA_8390_BASE		0x80000
69 #define DAYNA_8390_MEM		0x00000
70 
71 #define CABLETRON_8390_BASE	0x90000
72 #define CABLETRON_8390_MEM	0x00000
73 
74 #define INTERLAN_8390_BASE	0xE0000
75 #define INTERLAN_8390_MEM	0xD0000
76 
77 enum mac8390_type {
78 	MAC8390_NONE = -1,
79 	MAC8390_APPLE,
80 	MAC8390_ASANTE,
81 	MAC8390_FARALLON,
82 	MAC8390_CABLETRON,
83 	MAC8390_DAYNA,
84 	MAC8390_INTERLAN,
85 	MAC8390_KINETICS,
86 };
87 
88 static const char *cardname[] = {
89 	"apple",
90 	"asante",
91 	"farallon",
92 	"cabletron",
93 	"dayna",
94 	"interlan",
95 	"kinetics",
96 };
97 
98 static const int word16[] = {
99 	1, /* apple */
100 	1, /* asante */
101 	1, /* farallon */
102 	1, /* cabletron */
103 	0, /* dayna */
104 	1, /* interlan */
105 	0, /* kinetics */
106 };
107 
108 /* on which cards do we use NuBus resources? */
109 static const int useresources[] = {
110 	1, /* apple */
111 	1, /* asante */
112 	1, /* farallon */
113 	0, /* cabletron */
114 	0, /* dayna */
115 	0, /* interlan */
116 	0, /* kinetics */
117 };
118 
119 enum mac8390_access {
120 	ACCESS_UNKNOWN = 0,
121 	ACCESS_32,
122 	ACCESS_16,
123 };
124 
125 extern int mac8390_memtest(struct net_device *dev);
126 static int mac8390_initdev(struct net_device *dev, struct nubus_board *board,
127 			   enum mac8390_type type);
128 
129 static int mac8390_open(struct net_device *dev);
130 static int mac8390_close(struct net_device *dev);
131 static void mac8390_no_reset(struct net_device *dev);
132 static void interlan_reset(struct net_device *dev);
133 
134 /* Sane (32-bit chunk memory read/write) - Some Farallon and Apple do this*/
135 static void sane_get_8390_hdr(struct net_device *dev,
136 			      struct e8390_pkt_hdr *hdr, int ring_page);
137 static void sane_block_input(struct net_device *dev, int count,
138 			     struct sk_buff *skb, int ring_offset);
139 static void sane_block_output(struct net_device *dev, int count,
140 			      const unsigned char *buf, const int start_page);
141 
142 /* dayna_memcpy to and from card */
143 static void dayna_memcpy_fromcard(struct net_device *dev, void *to,
144 				int from, int count);
145 static void dayna_memcpy_tocard(struct net_device *dev, int to,
146 			      const void *from, int count);
147 
148 /* Dayna - Dayna/Kinetics use this */
149 static void dayna_get_8390_hdr(struct net_device *dev,
150 			       struct e8390_pkt_hdr *hdr, int ring_page);
151 static void dayna_block_input(struct net_device *dev, int count,
152 			      struct sk_buff *skb, int ring_offset);
153 static void dayna_block_output(struct net_device *dev, int count,
154 			       const unsigned char *buf, int start_page);
155 
156 /* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
157 static void slow_sane_get_8390_hdr(struct net_device *dev,
158 				   struct e8390_pkt_hdr *hdr, int ring_page);
159 static void slow_sane_block_input(struct net_device *dev, int count,
160 				  struct sk_buff *skb, int ring_offset);
161 static void slow_sane_block_output(struct net_device *dev, int count,
162 				   const unsigned char *buf, int start_page);
163 static void word_memcpy_tocard(unsigned long tp, const void *fp, int count);
164 static void word_memcpy_fromcard(void *tp, unsigned long fp, int count);
165 
166 static enum mac8390_type mac8390_ident(struct nubus_rsrc *fres)
167 {
168 	switch (fres->dr_sw) {
169 	case NUBUS_DRSW_3COM:
170 		switch (fres->dr_hw) {
171 		case NUBUS_DRHW_APPLE_SONIC_NB:
172 		case NUBUS_DRHW_APPLE_SONIC_LC:
173 		case NUBUS_DRHW_SONNET:
174 			return MAC8390_NONE;
175 		default:
176 			return MAC8390_APPLE;
177 		}
178 
179 	case NUBUS_DRSW_APPLE:
180 		switch (fres->dr_hw) {
181 		case NUBUS_DRHW_ASANTE_LC:
182 			return MAC8390_NONE;
183 		case NUBUS_DRHW_CABLETRON:
184 			return MAC8390_CABLETRON;
185 		default:
186 			return MAC8390_APPLE;
187 		}
188 
189 	case NUBUS_DRSW_ASANTE:
190 		return MAC8390_ASANTE;
191 
192 	case NUBUS_DRSW_TECHWORKS:
193 	case NUBUS_DRSW_DAYNA2:
194 	case NUBUS_DRSW_DAYNA_LC:
195 		if (fres->dr_hw == NUBUS_DRHW_CABLETRON)
196 			return MAC8390_CABLETRON;
197 		else
198 			return MAC8390_APPLE;
199 
200 	case NUBUS_DRSW_FARALLON:
201 		return MAC8390_FARALLON;
202 
203 	case NUBUS_DRSW_KINETICS:
204 		switch (fres->dr_hw) {
205 		case NUBUS_DRHW_INTERLAN:
206 			return MAC8390_INTERLAN;
207 		default:
208 			return MAC8390_KINETICS;
209 		}
210 
211 	case NUBUS_DRSW_DAYNA:
212 		/*
213 		 * These correspond to Dayna Sonic cards
214 		 * which use the macsonic driver
215 		 */
216 		if (fres->dr_hw == NUBUS_DRHW_SMC9194 ||
217 		    fres->dr_hw == NUBUS_DRHW_INTERLAN)
218 			return MAC8390_NONE;
219 		else
220 			return MAC8390_DAYNA;
221 	}
222 	return MAC8390_NONE;
223 }
224 
225 static enum mac8390_access mac8390_testio(unsigned long membase)
226 {
227 	u32 outdata = 0xA5A0B5B0;
228 	u32 indata = 0;
229 
230 	/* Try writing 32 bits */
231 	nubus_writel(outdata, membase);
232 	/* Now read it back */
233 	indata = nubus_readl(membase);
234 	if (outdata == indata)
235 		return ACCESS_32;
236 
237 	outdata = 0xC5C0D5D0;
238 	indata = 0;
239 
240 	/* Write 16 bit output */
241 	word_memcpy_tocard(membase, &outdata, 4);
242 	/* Now read it back */
243 	word_memcpy_fromcard(&indata, membase, 4);
244 	if (outdata == indata)
245 		return ACCESS_16;
246 
247 	return ACCESS_UNKNOWN;
248 }
249 
250 static int mac8390_memsize(unsigned long membase)
251 {
252 	unsigned long flags;
253 	int i, j;
254 
255 	local_irq_save(flags);
256 	/* Check up to 32K in 4K increments */
257 	for (i = 0; i < 8; i++) {
258 		volatile unsigned short *m = (unsigned short *)(membase + (i * 0x1000));
259 
260 		/* Unwriteable - we have a fully decoded card and the
261 		   RAM end located */
262 		if (hwreg_present(m) == 0)
263 			break;
264 
265 		/* write a distinctive byte */
266 		*m = 0xA5A0 | i;
267 		/* check that we read back what we wrote */
268 		if (*m != (0xA5A0 | i))
269 			break;
270 
271 		/* check for partial decode and wrap */
272 		for (j = 0; j < i; j++) {
273 			volatile unsigned short *p = (unsigned short *)(membase + (j * 0x1000));
274 			if (*p != (0xA5A0 | j))
275 				break;
276 		}
277 	}
278 	local_irq_restore(flags);
279 	/*
280 	 * in any case, we stopped once we tried one block too many,
281 	 * or once we reached 32K
282 	 */
283 	return i * 0x1000;
284 }
285 
286 static bool mac8390_rsrc_init(struct net_device *dev,
287 			      struct nubus_rsrc *fres,
288 			      enum mac8390_type cardtype)
289 {
290 	struct nubus_board *board = fres->board;
291 	struct nubus_dir dir;
292 	struct nubus_dirent ent;
293 	int offset;
294 	volatile unsigned short *i;
295 
296 	dev->irq = SLOT2IRQ(board->slot);
297 	/* This is getting to be a habit */
298 	dev->base_addr = board->slot_addr | ((board->slot & 0xf) << 20);
299 
300 	/*
301 	 * Get some Nubus info - we will trust the card's idea
302 	 * of where its memory and registers are.
303 	 */
304 
305 	if (nubus_get_func_dir(fres, &dir) == -1) {
306 		dev_err(&board->dev,
307 			"Unable to get Nubus functional directory\n");
308 		return false;
309 	}
310 
311 	/* Get the MAC address */
312 	if (nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent) == -1) {
313 		dev_info(&board->dev, "MAC address resource not found\n");
314 		return false;
315 	}
316 
317 	nubus_get_rsrc_mem(dev->dev_addr, &ent, 6);
318 
319 	if (useresources[cardtype] == 1) {
320 		nubus_rewinddir(&dir);
321 		if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS,
322 				    &ent) == -1) {
323 			dev_err(&board->dev,
324 				"Memory offset resource not found\n");
325 			return false;
326 		}
327 		nubus_get_rsrc_mem(&offset, &ent, 4);
328 		dev->mem_start = dev->base_addr + offset;
329 		/* yes, this is how the Apple driver does it */
330 		dev->base_addr = dev->mem_start + 0x10000;
331 		nubus_rewinddir(&dir);
332 		if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH,
333 				    &ent) == -1) {
334 			dev_info(&board->dev,
335 				 "Memory length resource not found, probing\n");
336 			offset = mac8390_memsize(dev->mem_start);
337 		} else {
338 			nubus_get_rsrc_mem(&offset, &ent, 4);
339 		}
340 		dev->mem_end = dev->mem_start + offset;
341 	} else {
342 		switch (cardtype) {
343 		case MAC8390_KINETICS:
344 		case MAC8390_DAYNA: /* it's the same */
345 			dev->base_addr = (int)(board->slot_addr +
346 					       DAYNA_8390_BASE);
347 			dev->mem_start = (int)(board->slot_addr +
348 					       DAYNA_8390_MEM);
349 			dev->mem_end = dev->mem_start +
350 				       mac8390_memsize(dev->mem_start);
351 			break;
352 		case MAC8390_INTERLAN:
353 			dev->base_addr = (int)(board->slot_addr +
354 					       INTERLAN_8390_BASE);
355 			dev->mem_start = (int)(board->slot_addr +
356 					       INTERLAN_8390_MEM);
357 			dev->mem_end = dev->mem_start +
358 				       mac8390_memsize(dev->mem_start);
359 			break;
360 		case MAC8390_CABLETRON:
361 			dev->base_addr = (int)(board->slot_addr +
362 					       CABLETRON_8390_BASE);
363 			dev->mem_start = (int)(board->slot_addr +
364 					       CABLETRON_8390_MEM);
365 			/* The base address is unreadable if 0x00
366 			 * has been written to the command register
367 			 * Reset the chip by writing E8390_NODMA +
368 			 *   E8390_PAGE0 + E8390_STOP just to be
369 			 *   sure
370 			 */
371 			i = (void *)dev->base_addr;
372 			*i = 0x21;
373 			dev->mem_end = dev->mem_start +
374 				       mac8390_memsize(dev->mem_start);
375 			break;
376 
377 		default:
378 			dev_err(&board->dev,
379 				"No known base address for card type\n");
380 			return false;
381 		}
382 	}
383 
384 	return true;
385 }
386 
387 static int mac8390_device_probe(struct nubus_board *board)
388 {
389 	struct net_device *dev;
390 	int err = -ENODEV;
391 	struct nubus_rsrc *fres;
392 	enum mac8390_type cardtype = MAC8390_NONE;
393 
394 	dev = ____alloc_ei_netdev(0);
395 	if (!dev)
396 		return -ENOMEM;
397 
398 	SET_NETDEV_DEV(dev, &board->dev);
399 
400 	for_each_board_func_rsrc(board, fres) {
401 		if (fres->category != NUBUS_CAT_NETWORK ||
402 		    fres->type != NUBUS_TYPE_ETHERNET)
403 			continue;
404 
405 		cardtype = mac8390_ident(fres);
406 		if (cardtype == MAC8390_NONE)
407 			continue;
408 
409 		if (mac8390_rsrc_init(dev, fres, cardtype))
410 			break;
411 	}
412 	if (!fres)
413 		goto out;
414 
415 	err = mac8390_initdev(dev, board, cardtype);
416 	if (err)
417 		goto out;
418 
419 	err = register_netdev(dev);
420 	if (err)
421 		goto out;
422 
423 	nubus_set_drvdata(board, dev);
424 	return 0;
425 
426 out:
427 	free_netdev(dev);
428 	return err;
429 }
430 
431 static int mac8390_device_remove(struct nubus_board *board)
432 {
433 	struct net_device *dev = nubus_get_drvdata(board);
434 
435 	unregister_netdev(dev);
436 	free_netdev(dev);
437 	return 0;
438 }
439 
440 static struct nubus_driver mac8390_driver = {
441 	.probe = mac8390_device_probe,
442 	.remove = mac8390_device_remove,
443 	.driver = {
444 		.name = KBUILD_MODNAME,
445 		.owner = THIS_MODULE,
446 	}
447 };
448 
449 MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
450 MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
451 MODULE_LICENSE("GPL");
452 
453 static int __init mac8390_init(void)
454 {
455 	return nubus_driver_register(&mac8390_driver);
456 }
457 module_init(mac8390_init);
458 
459 static void __exit mac8390_exit(void)
460 {
461 	nubus_driver_unregister(&mac8390_driver);
462 }
463 module_exit(mac8390_exit);
464 
465 static const struct net_device_ops mac8390_netdev_ops = {
466 	.ndo_open 		= mac8390_open,
467 	.ndo_stop		= mac8390_close,
468 	.ndo_start_xmit		= __ei_start_xmit,
469 	.ndo_tx_timeout		= __ei_tx_timeout,
470 	.ndo_get_stats		= __ei_get_stats,
471 	.ndo_set_rx_mode	= __ei_set_multicast_list,
472 	.ndo_validate_addr	= eth_validate_addr,
473 	.ndo_set_mac_address 	= eth_mac_addr,
474 #ifdef CONFIG_NET_POLL_CONTROLLER
475 	.ndo_poll_controller	= __ei_poll,
476 #endif
477 };
478 
479 static int mac8390_initdev(struct net_device *dev, struct nubus_board *board,
480 			   enum mac8390_type type)
481 {
482 	static u32 fwrd4_offsets[16] = {
483 		0,      4,      8,      12,
484 		16,     20,     24,     28,
485 		32,     36,     40,     44,
486 		48,     52,     56,     60
487 	};
488 	static u32 back4_offsets[16] = {
489 		60,     56,     52,     48,
490 		44,     40,     36,     32,
491 		28,     24,     20,     16,
492 		12,     8,      4,      0
493 	};
494 	static u32 fwrd2_offsets[16] = {
495 		0,      2,      4,      6,
496 		8,     10,     12,     14,
497 		16,    18,     20,     22,
498 		24,    26,     28,     30
499 	};
500 
501 	int access_bitmode = 0;
502 
503 	/* Now fill in our stuff */
504 	dev->netdev_ops = &mac8390_netdev_ops;
505 
506 	/* GAR, ei_status is actually a macro even though it looks global */
507 	ei_status.name = cardname[type];
508 	ei_status.word16 = word16[type];
509 
510 	/* Cabletron's TX/RX buffers are backwards */
511 	if (type == MAC8390_CABLETRON) {
512 		ei_status.tx_start_page = CABLETRON_TX_START_PG;
513 		ei_status.rx_start_page = CABLETRON_RX_START_PG;
514 		ei_status.stop_page = CABLETRON_RX_STOP_PG;
515 		ei_status.rmem_start = dev->mem_start;
516 		ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
517 	} else {
518 		ei_status.tx_start_page = WD_START_PG;
519 		ei_status.rx_start_page = WD_START_PG + TX_PAGES;
520 		ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
521 		ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
522 		ei_status.rmem_end = dev->mem_end;
523 	}
524 
525 	/* Fill in model-specific information and functions */
526 	switch (type) {
527 	case MAC8390_FARALLON:
528 	case MAC8390_APPLE:
529 		switch (mac8390_testio(dev->mem_start)) {
530 		case ACCESS_UNKNOWN:
531 			dev_err(&board->dev,
532 				"Don't know how to access card memory\n");
533 			return -ENODEV;
534 
535 		case ACCESS_16:
536 			/* 16 bit card, register map is reversed */
537 			ei_status.reset_8390 = mac8390_no_reset;
538 			ei_status.block_input = slow_sane_block_input;
539 			ei_status.block_output = slow_sane_block_output;
540 			ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
541 			ei_status.reg_offset = back4_offsets;
542 			break;
543 
544 		case ACCESS_32:
545 			/* 32 bit card, register map is reversed */
546 			ei_status.reset_8390 = mac8390_no_reset;
547 			ei_status.block_input = sane_block_input;
548 			ei_status.block_output = sane_block_output;
549 			ei_status.get_8390_hdr = sane_get_8390_hdr;
550 			ei_status.reg_offset = back4_offsets;
551 			access_bitmode = 1;
552 			break;
553 		}
554 		break;
555 
556 	case MAC8390_ASANTE:
557 		/* Some Asante cards pass the 32 bit test
558 		 * but overwrite system memory when run at 32 bit.
559 		 * so we run them all at 16 bit.
560 		 */
561 		ei_status.reset_8390 = mac8390_no_reset;
562 		ei_status.block_input = slow_sane_block_input;
563 		ei_status.block_output = slow_sane_block_output;
564 		ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
565 		ei_status.reg_offset = back4_offsets;
566 		break;
567 
568 	case MAC8390_CABLETRON:
569 		/* 16 bit card, register map is short forward */
570 		ei_status.reset_8390 = mac8390_no_reset;
571 		ei_status.block_input = slow_sane_block_input;
572 		ei_status.block_output = slow_sane_block_output;
573 		ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
574 		ei_status.reg_offset = fwrd2_offsets;
575 		break;
576 
577 	case MAC8390_DAYNA:
578 	case MAC8390_KINETICS:
579 		/* 16 bit memory, register map is forward */
580 		/* dayna and similar */
581 		ei_status.reset_8390 = mac8390_no_reset;
582 		ei_status.block_input = dayna_block_input;
583 		ei_status.block_output = dayna_block_output;
584 		ei_status.get_8390_hdr = dayna_get_8390_hdr;
585 		ei_status.reg_offset = fwrd4_offsets;
586 		break;
587 
588 	case MAC8390_INTERLAN:
589 		/* 16 bit memory, register map is forward */
590 		ei_status.reset_8390 = interlan_reset;
591 		ei_status.block_input = slow_sane_block_input;
592 		ei_status.block_output = slow_sane_block_output;
593 		ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
594 		ei_status.reg_offset = fwrd4_offsets;
595 		break;
596 
597 	default:
598 		dev_err(&board->dev, "Unsupported card type\n");
599 		return -ENODEV;
600 	}
601 
602 	__NS8390_init(dev, 0);
603 
604 	/* Good, done, now spit out some messages */
605 	dev_info(&board->dev, "%s (type %s)\n", board->name, cardname[type]);
606 	dev_info(&board->dev, "MAC %pM, IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
607 		 dev->dev_addr, dev->irq,
608 		 (unsigned int)(dev->mem_end - dev->mem_start) >> 10,
609 		 dev->mem_start, access_bitmode ? 32 : 16);
610 	return 0;
611 }
612 
613 static int mac8390_open(struct net_device *dev)
614 {
615 	int err;
616 
617 	__ei_open(dev);
618 	err = request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev);
619 	if (err)
620 		pr_err("%s: unable to get IRQ %d\n", dev->name, dev->irq);
621 	return err;
622 }
623 
624 static int mac8390_close(struct net_device *dev)
625 {
626 	free_irq(dev->irq, dev);
627 	__ei_close(dev);
628 	return 0;
629 }
630 
631 static void mac8390_no_reset(struct net_device *dev)
632 {
633 	struct ei_device *ei_local = netdev_priv(dev);
634 
635 	ei_status.txing = 0;
636 	netif_info(ei_local, hw, dev, "reset not supported\n");
637 }
638 
639 static void interlan_reset(struct net_device *dev)
640 {
641 	unsigned char *target = nubus_slot_addr(IRQ2SLOT(dev->irq));
642 	struct ei_device *ei_local = netdev_priv(dev);
643 
644 	netif_info(ei_local, hw, dev, "Need to reset the NS8390 t=%lu...",
645 		   jiffies);
646 	ei_status.txing = 0;
647 	target[0xC0000] = 0;
648 	if (netif_msg_hw(ei_local))
649 		pr_cont("reset complete\n");
650 }
651 
652 /* dayna_memcpy_fromio/dayna_memcpy_toio */
653 /* directly from daynaport.c by Alan Cox */
654 static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from,
655 				  int count)
656 {
657 	volatile unsigned char *ptr;
658 	unsigned char *target = to;
659 	from <<= 1;	/* word, skip overhead */
660 	ptr = (unsigned char *)(dev->mem_start+from);
661 	/* Leading byte? */
662 	if (from & 2) {
663 		*target++ = ptr[-1];
664 		ptr += 2;
665 		count--;
666 	}
667 	while (count >= 2) {
668 		*(unsigned short *)target = *(unsigned short volatile *)ptr;
669 		ptr += 4;			/* skip cruft */
670 		target += 2;
671 		count -= 2;
672 	}
673 	/* Trailing byte? */
674 	if (count)
675 		*target = *ptr;
676 }
677 
678 static void dayna_memcpy_tocard(struct net_device *dev, int to,
679 				const void *from, int count)
680 {
681 	volatile unsigned short *ptr;
682 	const unsigned char *src = from;
683 	to <<= 1;	/* word, skip overhead */
684 	ptr = (unsigned short *)(dev->mem_start+to);
685 	/* Leading byte? */
686 	if (to & 2) {		/* avoid a byte write (stomps on other data) */
687 		ptr[-1] = (ptr[-1]&0xFF00)|*src++;
688 		ptr++;
689 		count--;
690 	}
691 	while (count >= 2) {
692 		*ptr++ = *(unsigned short *)src;	/* Copy and */
693 		ptr++;			/* skip cruft */
694 		src += 2;
695 		count -= 2;
696 	}
697 	/* Trailing byte? */
698 	if (count) {
699 		/* card doesn't like byte writes */
700 		*ptr = (*ptr & 0x00FF) | (*src << 8);
701 	}
702 }
703 
704 /* sane block input/output */
705 static void sane_get_8390_hdr(struct net_device *dev,
706 			      struct e8390_pkt_hdr *hdr, int ring_page)
707 {
708 	unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
709 	memcpy_fromio(hdr, (void __iomem *)dev->mem_start + hdr_start, 4);
710 	/* Fix endianness */
711 	hdr->count = swab16(hdr->count);
712 }
713 
714 static void sane_block_input(struct net_device *dev, int count,
715 			     struct sk_buff *skb, int ring_offset)
716 {
717 	unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
718 	unsigned long xfer_start = xfer_base + dev->mem_start;
719 
720 	if (xfer_start + count > ei_status.rmem_end) {
721 		/* We must wrap the input move. */
722 		int semi_count = ei_status.rmem_end - xfer_start;
723 		memcpy_fromio(skb->data,
724 			      (void __iomem *)dev->mem_start + xfer_base,
725 			      semi_count);
726 		count -= semi_count;
727 		memcpy_fromio(skb->data + semi_count,
728 			      (void __iomem *)ei_status.rmem_start, count);
729 	} else {
730 		memcpy_fromio(skb->data,
731 			      (void __iomem *)dev->mem_start + xfer_base,
732 			      count);
733 	}
734 }
735 
736 static void sane_block_output(struct net_device *dev, int count,
737 			      const unsigned char *buf, int start_page)
738 {
739 	long shmem = (start_page - WD_START_PG)<<8;
740 
741 	memcpy_toio((void __iomem *)dev->mem_start + shmem, buf, count);
742 }
743 
744 /* dayna block input/output */
745 static void dayna_get_8390_hdr(struct net_device *dev,
746 			       struct e8390_pkt_hdr *hdr, int ring_page)
747 {
748 	unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
749 
750 	dayna_memcpy_fromcard(dev, hdr, hdr_start, 4);
751 	/* Fix endianness */
752 	hdr->count = (hdr->count & 0xFF) << 8 | (hdr->count >> 8);
753 }
754 
755 static void dayna_block_input(struct net_device *dev, int count,
756 			      struct sk_buff *skb, int ring_offset)
757 {
758 	unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
759 	unsigned long xfer_start = xfer_base+dev->mem_start;
760 
761 	/* Note the offset math is done in card memory space which is word
762 	   per long onto our space. */
763 
764 	if (xfer_start + count > ei_status.rmem_end) {
765 		/* We must wrap the input move. */
766 		int semi_count = ei_status.rmem_end - xfer_start;
767 		dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
768 		count -= semi_count;
769 		dayna_memcpy_fromcard(dev, skb->data + semi_count,
770 				      ei_status.rmem_start - dev->mem_start,
771 				      count);
772 	} else {
773 		dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
774 	}
775 }
776 
777 static void dayna_block_output(struct net_device *dev, int count,
778 			       const unsigned char *buf,
779 			       int start_page)
780 {
781 	long shmem = (start_page - WD_START_PG)<<8;
782 
783 	dayna_memcpy_tocard(dev, shmem, buf, count);
784 }
785 
786 /* Cabletron block I/O */
787 static void slow_sane_get_8390_hdr(struct net_device *dev,
788 				   struct e8390_pkt_hdr *hdr,
789 				   int ring_page)
790 {
791 	unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
792 	word_memcpy_fromcard(hdr, dev->mem_start + hdr_start, 4);
793 	/* Register endianism - fix here rather than 8390.c */
794 	hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
795 }
796 
797 static void slow_sane_block_input(struct net_device *dev, int count,
798 				  struct sk_buff *skb, int ring_offset)
799 {
800 	unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
801 	unsigned long xfer_start = xfer_base+dev->mem_start;
802 
803 	if (xfer_start + count > ei_status.rmem_end) {
804 		/* We must wrap the input move. */
805 		int semi_count = ei_status.rmem_end - xfer_start;
806 		word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
807 				     semi_count);
808 		count -= semi_count;
809 		word_memcpy_fromcard(skb->data + semi_count,
810 				     ei_status.rmem_start, count);
811 	} else {
812 		word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
813 				     count);
814 	}
815 }
816 
817 static void slow_sane_block_output(struct net_device *dev, int count,
818 				   const unsigned char *buf, int start_page)
819 {
820 	long shmem = (start_page - WD_START_PG)<<8;
821 
822 	word_memcpy_tocard(dev->mem_start + shmem, buf, count);
823 }
824 
825 static void word_memcpy_tocard(unsigned long tp, const void *fp, int count)
826 {
827 	volatile unsigned short *to = (void *)tp;
828 	const unsigned short *from = fp;
829 
830 	count++;
831 	count /= 2;
832 
833 	while (count--)
834 		*to++ = *from++;
835 }
836 
837 static void word_memcpy_fromcard(void *tp, unsigned long fp, int count)
838 {
839 	unsigned short *to = tp;
840 	const volatile unsigned short *from = (const void *)fp;
841 
842 	count++;
843 	count /= 2;
844 
845 	while (count--)
846 		*to++ = *from++;
847 }
848 
849 
850