]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/ed/if_ed.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / ed / if_ed.c
1 /*-
2  * Copyright (c) 1995, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
33  *   adapters. By David Greenman, 29-April-1993
34  *
35  * Currently supports the Western Digital/SMC 8003 and 8013 series,
36  *   the SMC Elite Ultra (8216), the 3Com 3c503, the NE1000 and NE2000,
37  *   and a variety of similar clones.
38  *
39  */
40
41 #include "opt_ed.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sockio.h>
46 #include <sys/mbuf.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/syslog.h>
51
52 #include <sys/bus.h>
53
54 #include <machine/bus.h>
55 #include <sys/rman.h>
56 #include <machine/resource.h>
57
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_arp.h>
61 #include <net/if_dl.h>
62 #include <net/if_mib.h>
63 #include <net/if_media.h>
64 #include <net/if_types.h>
65
66 #include <net/bpf.h>
67
68 #include <dev/ed/if_edreg.h>
69 #include <dev/ed/if_edvar.h>
70 #include <sys/kdb.h>
71
72 devclass_t ed_devclass;
73
74 static void     ed_init(void *);
75 static void     ed_init_locked(struct ed_softc *);
76 static int      ed_ioctl(struct ifnet *, u_long, caddr_t);
77 static void     ed_start(struct ifnet *);
78 static void     ed_start_locked(struct ifnet *);
79 static void     ed_reset(struct ifnet *);
80 static void     ed_watchdog(struct ifnet *);
81
82 static void     ed_ds_getmcaf(struct ed_softc *, uint32_t *);
83
84 static void     ed_get_packet(struct ed_softc *, bus_size_t, u_short);
85 static void     ed_stop_hw(struct ed_softc *sc);
86
87 static __inline void ed_rint(struct ed_softc *);
88 static __inline void ed_xmit(struct ed_softc *);
89 static __inline void ed_ring_copy(struct ed_softc *, bus_size_t, char *,
90     u_short);
91
92 static void     ed_setrcr(struct ed_softc *);
93
94 /*
95  * Generic probe routine for testing for the existance of a DS8390.
96  *      Must be called after the NIC has just been reset. This routine
97  *      works by looking at certain register values that are guaranteed
98  *      to be initialized a certain way after power-up or reset. Seems
99  *      not to currently work on the 83C690.
100  *
101  * Specifically:
102  *
103  *      Register                        reset bits      set bits
104  *      Command Register (CR)           TXP, STA        RD2, STP
105  *      Interrupt Status (ISR)                          RST
106  *      Interrupt Mask (IMR)            All bits
107  *      Data Control (DCR)                              LAS
108  *      Transmit Config. (TCR)          LB1, LB0
109  *
110  * We only look at the CR and ISR registers, however, because looking at
111  *      the others would require changing register pages (which would be
112  *      intrusive if this isn't an 8390).
113  *
114  * Return 1 if 8390 was found, 0 if not.
115  */
116
117 int
118 ed_probe_generic8390(struct ed_softc *sc)
119 {
120         if ((ed_nic_inb(sc, ED_P0_CR) &
121              (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
122             (ED_CR_RD2 | ED_CR_STP))
123                 return (0);
124         if ((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) != ED_ISR_RST)
125                 return (0);
126
127         return (1);
128 }
129
130 void
131 ed_disable_16bit_access(struct ed_softc *sc)
132 {
133         /*
134          * Disable 16 bit access to shared memory
135          */
136         if (sc->isa16bit && sc->vendor == ED_VENDOR_WD_SMC) {
137                 if (sc->chip_type == ED_CHIP_TYPE_WD790)
138                         ed_asic_outb(sc, ED_WD_MSR, 0x00);
139                 ed_asic_outb(sc, ED_WD_LAAR,
140                     sc->wd_laar_proto & ~ED_WD_LAAR_M16EN);
141         }
142 }
143
144 void
145 ed_enable_16bit_access(struct ed_softc *sc)
146 {
147         if (sc->isa16bit && sc->vendor == ED_VENDOR_WD_SMC) {
148                 ed_asic_outb(sc, ED_WD_LAAR,
149                      sc->wd_laar_proto | ED_WD_LAAR_M16EN);
150                 if (sc->chip_type == ED_CHIP_TYPE_WD790)
151                         ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB);
152         }
153 }
154
155 /*
156  * Allocate a port resource with the given resource id.
157  */
158 int
159 ed_alloc_port(device_t dev, int rid, int size)
160 {
161         struct ed_softc *sc = device_get_softc(dev);
162         struct resource *res;
163
164         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
165             0ul, ~0ul, size, RF_ACTIVE);
166         if (res) {
167                 sc->port_rid = rid;
168                 sc->port_res = res;
169                 sc->port_used = size;
170                 sc->port_bst = rman_get_bustag(res);
171                 sc->port_bsh = rman_get_bushandle(res);
172                 return (0);
173         }
174         return (ENOENT);
175 }
176
177 /*
178  * Allocate a memory resource with the given resource id.
179  */
180 int
181 ed_alloc_memory(device_t dev, int rid, int size)
182 {
183         struct ed_softc *sc = device_get_softc(dev);
184         struct resource *res;
185
186         res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
187             0ul, ~0ul, size, RF_ACTIVE);
188         if (res) {
189                 sc->mem_rid = rid;
190                 sc->mem_res = res;
191                 sc->mem_used = size;
192                 sc->mem_bst = rman_get_bustag(res);
193                 sc->mem_bsh = rman_get_bushandle(res);
194                 return (0);
195         }
196         return (ENOENT);
197 }
198
199 /*
200  * Allocate an irq resource with the given resource id.
201  */
202 int
203 ed_alloc_irq(device_t dev, int rid, int flags)
204 {
205         struct ed_softc *sc = device_get_softc(dev);
206         struct resource *res;
207
208         res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE | flags);
209         if (res) {
210                 sc->irq_rid = rid;
211                 sc->irq_res = res;
212                 return (0);
213         }
214         return (ENOENT);
215 }
216
217 /*
218  * Release all resources
219  */
220 void
221 ed_release_resources(device_t dev)
222 {
223         struct ed_softc *sc = device_get_softc(dev);
224
225         if (sc->port_res) {
226                 bus_release_resource(dev, SYS_RES_IOPORT,
227                     sc->port_rid, sc->port_res);
228                 sc->port_res = 0;
229         }
230         if (sc->mem_res) {
231                 bus_release_resource(dev, SYS_RES_MEMORY,
232                     sc->mem_rid, sc->mem_res);
233                 sc->mem_res = 0;
234         }
235         if (sc->irq_res) {
236                 bus_release_resource(dev, SYS_RES_IRQ,
237                     sc->irq_rid, sc->irq_res);
238                 sc->irq_res = 0;
239         }
240         if (sc->ifp)
241                 if_free(sc->ifp);
242 }
243
244 /*
245  * Install interface into kernel networking data structures
246  */
247 int
248 ed_attach(device_t dev)
249 {
250         struct ed_softc *sc = device_get_softc(dev);
251         struct ifnet *ifp;
252
253         sc->dev = dev;
254         ED_LOCK_INIT(sc);
255         ifp = sc->ifp = if_alloc(IFT_ETHER);
256         if (ifp == NULL) {
257                 device_printf(dev, "can not if_alloc()\n");
258                 ED_LOCK_DESTROY(sc);
259                 return (ENOSPC);
260         }
261
262         if (sc->readmem == NULL) {
263                 if (sc->mem_shared) {
264                         if (sc->isa16bit)
265                                 sc->readmem = ed_shmem_readmem16;
266                         else
267                                 sc->readmem = ed_shmem_readmem8;
268                 } else {
269                         sc->readmem = ed_pio_readmem;
270                 }
271         }
272         if (sc->sc_write_mbufs == NULL) {
273                 device_printf(dev, "No write mbufs routine set\n");
274                 return (ENXIO);
275         }
276
277         callout_init_mtx(&sc->tick_ch, ED_MUTEX(sc), 0);
278         /*
279          * Set interface to stopped condition (reset)
280          */
281         ed_stop_hw(sc);
282
283         /*
284          * Initialize ifnet structure
285          */
286         ifp->if_softc = sc;
287         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
288         ifp->if_start = ed_start;
289         ifp->if_ioctl = ed_ioctl;
290         ifp->if_watchdog = ed_watchdog;
291         ifp->if_init = ed_init;
292         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
293         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
294         IFQ_SET_READY(&ifp->if_snd);
295         ifp->if_linkmib = &sc->mibdata;
296         ifp->if_linkmiblen = sizeof sc->mibdata;
297         /*
298          * XXX - should do a better job.
299          */
300         if (sc->chip_type == ED_CHIP_TYPE_WD790)
301                 sc->mibdata.dot3StatsEtherChipSet =
302                         DOT3CHIPSET(dot3VendorWesternDigital,
303                                     dot3ChipSetWesternDigital83C790);
304         else
305                 sc->mibdata.dot3StatsEtherChipSet =
306                         DOT3CHIPSET(dot3VendorNational, 
307                                     dot3ChipSetNational8390);
308         sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
309
310         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
311         /*
312          * Set default state for LINK2 flag (used to disable the 
313          * tranceiver for AUI operation), based on config option.
314          * We only set this flag before we attach the device, so there's
315          * no race.  It is convenient to allow users to turn this off
316          * by default in the kernel config, but given our more advanced
317          * boot time configuration options, this might no longer be needed.
318          */
319         if (device_get_flags(dev) & ED_FLAGS_DISABLE_TRANCEIVER)
320                 ifp->if_flags |= IFF_LINK2;
321
322         /*
323          * Attach the interface
324          */
325         ether_ifattach(ifp, sc->enaddr);
326         /* device attach does transition from UNCONFIGURED to IDLE state */
327
328         sc->tx_mem = sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
329         sc->rx_mem = (sc->rec_page_stop - sc->rec_page_start) * ED_PAGE_SIZE;
330         SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
331             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
332             0, "type", CTLTYPE_STRING | CTLFLAG_RD, sc->type_str, 0,
333             "Type of chip in card");
334         SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
335             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
336             1, "TxMem", CTLTYPE_STRING | CTLFLAG_RD, &sc->tx_mem, 0,
337             "Memory set aside for transmitting packets");
338         SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
339             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
340             2, "RxMem", CTLTYPE_STRING | CTLFLAG_RD, &sc->rx_mem, 0,
341             "Memory  set aside for receiving packets");
342         SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
343             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
344             3, "Mem", CTLTYPE_STRING | CTLFLAG_RD, &sc->mem_size, 0,
345             "Total Card Memory");
346         if (bootverbose) {
347                 if (sc->type_str && (*sc->type_str != 0))
348                         device_printf(dev, "type %s ", sc->type_str);
349                 else
350                         device_printf(dev, "type unknown (0x%x) ", sc->type);
351
352 #ifdef ED_HPP
353                 if (sc->vendor == ED_VENDOR_HP)
354                         printf("(%s %s IO)",
355                             (sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS) ?
356                             "16-bit" : "32-bit",
357                             sc->hpp_mem_start ? "memory mapped" : "regular");
358                 else
359 #endif
360                         printf("%s", sc->isa16bit ? "(16 bit)" : "(8 bit)");
361
362 #if defined(ED_HPP) || defined(ED_3C503)
363                 printf("%s", (((sc->vendor == ED_VENDOR_3COM) ||
364                                     (sc->vendor == ED_VENDOR_HP)) &&
365                            (ifp->if_flags & IFF_LINK2)) ?
366                     " tranceiver disabled" : "");
367 #endif
368                 printf("\n");
369         }
370         return (0);
371 }
372
373 /*
374  * Detach the driver from the hardware and other systems in the kernel.
375  */
376 int
377 ed_detach(device_t dev)
378 {
379         struct ed_softc *sc = device_get_softc(dev);
380         struct ifnet *ifp = sc->ifp;
381
382         ED_ASSERT_UNLOCKED(sc);
383         ED_LOCK(sc);
384         if (bus_child_present(dev))
385                 ed_stop(sc);
386         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
387         ED_UNLOCK(sc);
388         callout_drain(&sc->tick_ch);
389         ether_ifdetach(ifp);
390         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
391         ed_release_resources(dev);
392         ED_LOCK_DESTROY(sc);
393         bus_generic_detach(dev);
394         return (0);
395 }
396
397 /*
398  * Reset interface.
399  */
400 static void
401 ed_reset(struct ifnet *ifp)
402 {
403         struct ed_softc *sc = ifp->if_softc;
404
405         ED_ASSERT_LOCKED(sc);
406         /*
407          * Stop interface and re-initialize.
408          */
409         ed_stop(sc);
410         ed_init_locked(sc);
411 }
412
413 static void
414 ed_stop_hw(struct ed_softc *sc)
415 {
416         int     n = 5000;
417
418         /*
419          * Stop everything on the interface, and select page 0 registers.
420          */
421         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
422
423         /*
424          * Wait for interface to enter stopped state, but limit # of checks to
425          * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
426          * just in case it's an old one.
427          */
428         if (sc->chip_type != ED_CHIP_TYPE_AX88190)
429                 while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n)
430                         continue;
431 }
432
433 /*
434  * Take interface offline.
435  */
436 void
437 ed_stop(struct ed_softc *sc)
438 {
439         ED_ASSERT_LOCKED(sc);
440         if (sc->sc_tick)
441                 callout_stop(&sc->tick_ch);
442         ed_stop_hw(sc);
443 }
444
445 /*
446  * Device timeout/watchdog routine. Entered if the device neglects to
447  *      generate an interrupt after a transmit has been started on it.
448  */
449 static void
450 ed_watchdog(struct ifnet *ifp)
451 {
452         struct ed_softc *sc = ifp->if_softc;
453
454         log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
455         ifp->if_oerrors++;
456
457         ED_LOCK(sc);
458         ed_reset(ifp);
459         ED_UNLOCK(sc);
460 }
461
462 /*
463  * Initialize device.
464  */
465 static void
466 ed_init(void *xsc)
467 {
468         struct ed_softc *sc = xsc;
469
470         ED_ASSERT_UNLOCKED(sc);
471         ED_LOCK(sc);
472         ed_init_locked(sc);
473         ED_UNLOCK(sc);
474 }
475
476 static void
477 ed_init_locked(struct ed_softc *sc)
478 {
479         struct ifnet *ifp = sc->ifp;
480         int     i;
481
482         ED_ASSERT_LOCKED(sc);
483
484         /*
485          * Initialize the NIC in the exact order outlined in the NS manual.
486          * This init procedure is "mandatory"...don't change what or when
487          * things happen.
488          */
489
490         /* reset transmitter flags */
491         sc->xmit_busy = 0;
492         ifp->if_timer = 0;
493
494         sc->txb_inuse = 0;
495         sc->txb_new = 0;
496         sc->txb_next_tx = 0;
497
498         /* This variable is used below - don't move this assignment */
499         sc->next_packet = sc->rec_page_start + 1;
500
501         /*
502          * Set interface for page 0, Remote DMA complete, Stopped
503          */
504         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
505
506         if (sc->isa16bit)
507                 /*
508                  * Set FIFO threshold to 8, No auto-init Remote DMA, byte
509                  * order=80x86, word-wide DMA xfers,
510                  */
511                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
512         else
513                 /*
514                  * Same as above, but byte-wide DMA xfers
515                  */
516                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
517
518         /*
519          * Clear Remote Byte Count Registers
520          */
521         ed_nic_outb(sc, ED_P0_RBCR0, 0);
522         ed_nic_outb(sc, ED_P0_RBCR1, 0);
523
524         /*
525          * For the moment, don't store incoming packets in memory.
526          */
527         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON);
528
529         /*
530          * Place NIC in internal loopback mode
531          */
532         ed_nic_outb(sc, ED_P0_TCR, ED_TCR_LB0);
533
534         /*
535          * Initialize transmit/receive (ring-buffer) Page Start
536          */
537         ed_nic_outb(sc, ED_P0_TPSR, sc->tx_page_start);
538         ed_nic_outb(sc, ED_P0_PSTART, sc->rec_page_start);
539         /* Set lower bits of byte addressable framing to 0 */
540         if (sc->chip_type == ED_CHIP_TYPE_WD790)
541                 ed_nic_outb(sc, 0x09, 0);
542
543         /*
544          * Initialize Receiver (ring-buffer) Page Stop and Boundry
545          */
546         ed_nic_outb(sc, ED_P0_PSTOP, sc->rec_page_stop);
547         ed_nic_outb(sc, ED_P0_BNRY, sc->rec_page_start);
548
549         /*
550          * Clear all interrupts. A '1' in each bit position clears the
551          * corresponding flag.
552          */
553         ed_nic_outb(sc, ED_P0_ISR, 0xff);
554
555         /*
556          * Enable the following interrupts: receive/transmit complete,
557          * receive/transmit error, and Receiver OverWrite.
558          *
559          * Counter overflow and Remote DMA complete are *not* enabled.
560          */
561         ed_nic_outb(sc, ED_P0_IMR,
562         ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE | ED_IMR_OVWE);
563
564         /*
565          * Program Command Register for page 1
566          */
567         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
568
569         /*
570          * Copy out our station address
571          */
572         for (i = 0; i < ETHER_ADDR_LEN; ++i)
573                 ed_nic_outb(sc, ED_P1_PAR(i), IF_LLADDR(sc->ifp)[i]);
574
575         /*
576          * Set Current Page pointer to next_packet (initialized above)
577          */
578         ed_nic_outb(sc, ED_P1_CURR, sc->next_packet);
579
580         /*
581          * Program Receiver Configuration Register and multicast filter. CR is
582          * set to page 0 on return.
583          */
584         ed_setrcr(sc);
585
586         /*
587          * Take interface out of loopback
588          */
589         ed_nic_outb(sc, ED_P0_TCR, 0);
590
591         if (sc->sc_mediachg)
592                 sc->sc_mediachg(sc);
593
594         /*
595          * Set 'running' flag, and clear output active flag.
596          */
597         ifp->if_drv_flags |= IFF_DRV_RUNNING;
598         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
599
600         /*
601          * ...and attempt to start output
602          */
603         ed_start_locked(ifp);
604
605         if (sc->sc_tick)
606                 callout_reset(&sc->tick_ch, hz, sc->sc_tick, sc);
607 }
608
609 /*
610  * This routine actually starts the transmission on the interface
611  */
612 static __inline void
613 ed_xmit(struct ed_softc *sc)
614 {
615         struct ifnet *ifp = sc->ifp;
616         unsigned short len;
617
618         len = sc->txb_len[sc->txb_next_tx];
619
620         /*
621          * Set NIC for page 0 register access
622          */
623         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
624
625         /*
626          * Set TX buffer start page
627          */
628         ed_nic_outb(sc, ED_P0_TPSR, sc->tx_page_start +
629                     sc->txb_next_tx * ED_TXBUF_SIZE);
630
631         /*
632          * Set TX length
633          */
634         ed_nic_outb(sc, ED_P0_TBCR0, len);
635         ed_nic_outb(sc, ED_P0_TBCR1, len >> 8);
636
637         /*
638          * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
639          */
640         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_TXP | ED_CR_STA);
641         sc->xmit_busy = 1;
642
643         /*
644          * Point to next transmit buffer slot and wrap if necessary.
645          */
646         sc->txb_next_tx++;
647         if (sc->txb_next_tx == sc->txb_cnt)
648                 sc->txb_next_tx = 0;
649
650         /*
651          * Set a timer just in case we never hear from the board again
652          */
653         ifp->if_timer = 2;
654 }
655
656 /*
657  * Start output on interface.
658  * We make two assumptions here:
659  *  1) that the current priority is set to splimp _before_ this code
660  *     is called *and* is returned to the appropriate priority after
661  *     return
662  *  2) that the IFF_DRV_OACTIVE flag is checked before this code is called
663  *     (i.e. that the output part of the interface is idle)
664  */
665 static void
666 ed_start(struct ifnet *ifp)
667 {
668         struct ed_softc *sc = ifp->if_softc;
669
670         ED_ASSERT_UNLOCKED(sc);
671         ED_LOCK(sc);
672         ed_start_locked(ifp);
673         ED_UNLOCK(sc);
674 }
675
676 static void
677 ed_start_locked(struct ifnet *ifp)
678 {
679         struct ed_softc *sc = ifp->if_softc;
680         struct mbuf *m0, *m;
681         bus_size_t buffer;
682         int     len;
683
684         ED_ASSERT_LOCKED(sc);
685 outloop:
686
687         /*
688          * First, see if there are buffered packets and an idle transmitter -
689          * should never happen at this point.
690          */
691         if (sc->txb_inuse && (sc->xmit_busy == 0)) {
692                 printf("ed: packets buffered, but transmitter idle\n");
693                 ed_xmit(sc);
694         }
695
696         /*
697          * See if there is room to put another packet in the buffer.
698          */
699         if (sc->txb_inuse == sc->txb_cnt) {
700
701                 /*
702                  * No room. Indicate this to the outside world and exit.
703                  */
704                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
705                 return;
706         }
707         IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
708         if (m == 0) {
709
710                 /*
711                  * We are using the !OACTIVE flag to indicate to the outside
712                  * world that we can accept an additional packet rather than
713                  * that the transmitter is _actually_ active. Indeed, the
714                  * transmitter may be active, but if we haven't filled all the
715                  * buffers with data then we still want to accept more.
716                  */
717                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
718                 return;
719         }
720
721         /*
722          * Copy the mbuf chain into the transmit buffer
723          */
724         m0 = m;
725
726         /* txb_new points to next open buffer slot */
727         buffer = sc->mem_start + (sc->txb_new * ED_TXBUF_SIZE * ED_PAGE_SIZE);
728
729         len = sc->sc_write_mbufs(sc, m, buffer);
730         if (len == 0) {
731                 m_freem(m0);
732                 goto outloop;
733         }
734
735         sc->txb_len[sc->txb_new] = max(len, (ETHER_MIN_LEN-ETHER_CRC_LEN));
736
737         sc->txb_inuse++;
738
739         /*
740          * Point to next buffer slot and wrap if necessary.
741          */
742         sc->txb_new++;
743         if (sc->txb_new == sc->txb_cnt)
744                 sc->txb_new = 0;
745
746         if (sc->xmit_busy == 0)
747                 ed_xmit(sc);
748
749         /*
750          * Tap off here if there is a bpf listener.
751          */
752         BPF_MTAP(ifp, m0);
753
754         m_freem(m0);
755
756         /*
757          * Loop back to the top to possibly buffer more packets
758          */
759         goto outloop;
760 }
761
762 /*
763  * Ethernet interface receiver interrupt.
764  */
765 static __inline void
766 ed_rint(struct ed_softc *sc)
767 {
768         struct ifnet *ifp = sc->ifp;
769         u_char  boundry;
770         u_short len;
771         struct ed_ring packet_hdr;
772         bus_size_t packet_ptr;
773
774         ED_ASSERT_LOCKED(sc);
775
776         /*
777          * Set NIC to page 1 registers to get 'current' pointer
778          */
779         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
780
781         /*
782          * 'sc->next_packet' is the logical beginning of the ring-buffer -
783          * i.e. it points to where new data has been buffered. The 'CURR'
784          * (current) register points to the logical end of the ring-buffer -
785          * i.e. it points to where additional new data will be added. We loop
786          * here until the logical beginning equals the logical end (or in
787          * other words, until the ring-buffer is empty).
788          */
789         while (sc->next_packet != ed_nic_inb(sc, ED_P1_CURR)) {
790
791                 /* get pointer to this buffer's header structure */
792                 packet_ptr = sc->mem_ring +
793                     (sc->next_packet - sc->rec_page_start) * ED_PAGE_SIZE;
794         
795                 /*
796                  * The byte count includes a 4 byte header that was added by
797                  * the NIC.
798                  */
799                 sc->readmem(sc, packet_ptr, (char *) &packet_hdr,
800                     sizeof(packet_hdr));
801                 len = packet_hdr.count;
802                 if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring)) ||
803                     len < (ETHER_MIN_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring))) {
804                         /*
805                          * Length is a wild value. There's a good chance that
806                          * this was caused by the NIC being old and buggy.
807                          * The bug is that the length low byte is duplicated in
808                          * the high byte. Try to recalculate the length based on
809                          * the pointer to the next packet.
810                          */
811                         /*
812                          * NOTE: sc->next_packet is pointing at the current packet.
813                          */
814                         len &= ED_PAGE_SIZE - 1;        /* preserve offset into page */
815                         if (packet_hdr.next_packet >= sc->next_packet)
816                                 len += (packet_hdr.next_packet -
817                                     sc->next_packet) * ED_PAGE_SIZE;
818                         else
819                                 len += 
820                                     ((packet_hdr.next_packet - sc->rec_page_start) +
821                                     (sc->rec_page_stop - sc->next_packet)) * ED_PAGE_SIZE;
822                         /*
823                          * because buffers are aligned on 256-byte boundary,
824                          * the length computed above is off by 256 in almost
825                          * all cases. Fix it...
826                          */
827                         if (len & 0xff)
828                                 len -= 256;
829                         if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN 
830                             + sizeof(struct ed_ring)))
831                                 sc->mibdata.dot3StatsFrameTooLongs++;
832                 }
833
834                 /*
835                  * Be fairly liberal about what we allow as a "reasonable" length
836                  * so that a [crufty] packet will make it to BPF (and can thus
837                  * be analyzed). Note that all that is really important is that
838                  * we have a length that will fit into one mbuf cluster or less;
839                  * the upper layer protocols can then figure out the length from
840                  * their own length field(s).
841                  * But make sure that we have at least a full ethernet header
842                  * or we would be unable to call ether_input() later.
843                  */
844                 if ((len >= sizeof(struct ed_ring) + ETHER_HDR_LEN) &&
845                     (len <= MCLBYTES) &&
846                     (packet_hdr.next_packet >= sc->rec_page_start) &&
847                     (packet_hdr.next_packet < sc->rec_page_stop)) {
848                         /*
849                          * Go get packet.
850                          */
851                         ed_get_packet(sc, packet_ptr + sizeof(struct ed_ring),
852                                       len - sizeof(struct ed_ring));
853                         ifp->if_ipackets++;
854                 } else {
855                         /*
856                          * Really BAD. The ring pointers are corrupted.
857                          */
858                         log(LOG_ERR,
859                             "%s: NIC memory corrupt - invalid packet length %d\n",
860                             ifp->if_xname, len);
861                         ifp->if_ierrors++;
862                         ed_reset(ifp);
863                         return;
864                 }
865
866                 /*
867                  * Update next packet pointer
868                  */
869                 sc->next_packet = packet_hdr.next_packet;
870
871                 /*
872                  * Update NIC boundry pointer - being careful to keep it one
873                  * buffer behind. (as recommended by NS databook)
874                  */
875                 boundry = sc->next_packet - 1;
876                 if (boundry < sc->rec_page_start)
877                         boundry = sc->rec_page_stop - 1;
878
879                 /*
880                  * Set NIC to page 0 registers to update boundry register
881                  */
882                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
883                 ed_nic_outb(sc, ED_P0_BNRY, boundry);
884
885                 /*
886                  * Set NIC to page 1 registers before looping to top (prepare
887                  * to get 'CURR' current pointer)
888                  */
889                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
890         }
891 }
892
893 /*
894  * Ethernet interface interrupt processor
895  */
896 void
897 edintr(void *arg)
898 {
899         struct ed_softc *sc = (struct ed_softc*) arg;
900         struct ifnet *ifp = sc->ifp;
901         u_char  isr;
902         int     count;
903
904         ED_LOCK(sc);
905         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
906                 ED_UNLOCK(sc);
907                 return;
908         }
909         /*
910          * Set NIC to page 0 registers
911          */
912         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
913
914         /*
915          * loop until there are no more new interrupts.  When the card
916          * goes away, the hardware will read back 0xff.  Looking at
917          * the interrupts, it would appear that 0xff is impossible,
918          * or at least extremely unlikely.
919          */
920         while ((isr = ed_nic_inb(sc, ED_P0_ISR)) != 0 && isr != 0xff) {
921
922                 /*
923                  * reset all the bits that we are 'acknowledging' by writing a
924                  * '1' to each bit position that was set (writing a '1'
925                  * *clears* the bit)
926                  */
927                 ed_nic_outb(sc, ED_P0_ISR, isr);
928
929                 /* 
930                  * XXX workaround for AX88190
931                  * We limit this to 5000 iterations.  At 1us per inb/outb,
932                  * this translates to about 15ms, which should be plenty
933                  * of time, and also gives protection in the card eject
934                  * case.
935                  */
936                 if (sc->chip_type == ED_CHIP_TYPE_AX88190) {
937                         count = 5000;           /* 15ms */
938                         while (count-- && (ed_nic_inb(sc, ED_P0_ISR) & isr)) {
939                                 ed_nic_outb(sc, ED_P0_ISR,0);
940                                 ed_nic_outb(sc, ED_P0_ISR,isr);
941                         }
942                         if (count == 0)
943                                 break;
944                 }
945
946                 /*
947                  * Handle transmitter interrupts. Handle these first because
948                  * the receiver will reset the board under some conditions.
949                  */
950                 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
951                         u_char  collisions = ed_nic_inb(sc, ED_P0_NCR) & 0x0f;
952
953                         /*
954                          * Check for transmit error. If a TX completed with an
955                          * error, we end up throwing the packet away. Really
956                          * the only error that is possible is excessive
957                          * collisions, and in this case it is best to allow
958                          * the automatic mechanisms of TCP to backoff the
959                          * flow. Of course, with UDP we're screwed, but this
960                          * is expected when a network is heavily loaded.
961                          */
962                         (void) ed_nic_inb(sc, ED_P0_TSR);
963                         if (isr & ED_ISR_TXE) {
964                                 u_char tsr;
965
966                                 /*
967                                  * Excessive collisions (16)
968                                  */
969                                 tsr = ed_nic_inb(sc, ED_P0_TSR);
970                                 if ((tsr & ED_TSR_ABT)  
971                                     && (collisions == 0)) {
972
973                                         /*
974                                          * When collisions total 16, the
975                                          * P0_NCR will indicate 0, and the
976                                          * TSR_ABT is set.
977                                          */
978                                         collisions = 16;
979                                         sc->mibdata.dot3StatsExcessiveCollisions++;
980                                         sc->mibdata.dot3StatsCollFrequencies[15]++;
981                                 }
982                                 if (tsr & ED_TSR_OWC)
983                                         sc->mibdata.dot3StatsLateCollisions++;
984                                 if (tsr & ED_TSR_CDH)
985                                         sc->mibdata.dot3StatsSQETestErrors++;
986                                 if (tsr & ED_TSR_CRS)
987                                         sc->mibdata.dot3StatsCarrierSenseErrors++;
988                                 if (tsr & ED_TSR_FU)
989                                         sc->mibdata.dot3StatsInternalMacTransmitErrors++;
990
991                                 /*
992                                  * update output errors counter
993                                  */
994                                 ifp->if_oerrors++;
995                         } else {
996
997                                 /*
998                                  * Update total number of successfully
999                                  * transmitted packets.
1000                                  */
1001                                 ifp->if_opackets++;
1002                         }
1003
1004                         /*
1005                          * reset tx busy and output active flags
1006                          */
1007                         sc->xmit_busy = 0;
1008                         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1009
1010                         /*
1011                          * clear watchdog timer
1012                          */
1013                         ifp->if_timer = 0;
1014
1015                         /*
1016                          * Add in total number of collisions on last
1017                          * transmission.
1018                          */
1019                         ifp->if_collisions += collisions;
1020                         switch(collisions) {
1021                         case 0:
1022                         case 16:
1023                                 break;
1024                         case 1:
1025                                 sc->mibdata.dot3StatsSingleCollisionFrames++;
1026                                 sc->mibdata.dot3StatsCollFrequencies[0]++;
1027                                 break;
1028                         default:
1029                                 sc->mibdata.dot3StatsMultipleCollisionFrames++;
1030                                 sc->mibdata.
1031                                         dot3StatsCollFrequencies[collisions-1]
1032                                                 ++;
1033                                 break;
1034                         }
1035
1036                         /*
1037                          * Decrement buffer in-use count if not zero (can only
1038                          * be zero if a transmitter interrupt occured while
1039                          * not actually transmitting). If data is ready to
1040                          * transmit, start it transmitting, otherwise defer
1041                          * until after handling receiver
1042                          */
1043                         if (sc->txb_inuse && --sc->txb_inuse)
1044                                 ed_xmit(sc);
1045                 }
1046
1047                 /*
1048                  * Handle receiver interrupts
1049                  */
1050                 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
1051
1052                         /*
1053                          * Overwrite warning. In order to make sure that a
1054                          * lockup of the local DMA hasn't occurred, we reset
1055                          * and re-init the NIC. The NSC manual suggests only a
1056                          * partial reset/re-init is necessary - but some chips
1057                          * seem to want more. The DMA lockup has been seen
1058                          * only with early rev chips - Methinks this bug was
1059                          * fixed in later revs. -DG
1060                          */
1061                         if (isr & ED_ISR_OVW) {
1062                                 ifp->if_ierrors++;
1063 #ifdef DIAGNOSTIC
1064                                 log(LOG_WARNING,
1065                                     "%s: warning - receiver ring buffer overrun\n",
1066                                     ifp->if_xname);
1067 #endif
1068
1069                                 /*
1070                                  * Stop/reset/re-init NIC
1071                                  */
1072                                 ed_reset(ifp);
1073                         } else {
1074
1075                                 /*
1076                                  * Receiver Error. One or more of: CRC error,
1077                                  * frame alignment error FIFO overrun, or
1078                                  * missed packet.
1079                                  */
1080                                 if (isr & ED_ISR_RXE) {
1081                                         u_char rsr;
1082                                         rsr = ed_nic_inb(sc, ED_P0_RSR);
1083                                         if (rsr & ED_RSR_CRC)
1084                                                 sc->mibdata.dot3StatsFCSErrors++;
1085                                         if (rsr & ED_RSR_FAE)
1086                                                 sc->mibdata.dot3StatsAlignmentErrors++;
1087                                         if (rsr & ED_RSR_FO)
1088                                                 sc->mibdata.dot3StatsInternalMacReceiveErrors++;
1089                                         ifp->if_ierrors++;
1090 #ifdef ED_DEBUG
1091                                         if_printf(ifp, "receive error %x\n",
1092                                                ed_nic_inb(sc, ED_P0_RSR));
1093 #endif
1094                                 }
1095
1096                                 /*
1097                                  * Go get the packet(s) XXX - Doing this on an
1098                                  * error is dubious because there shouldn't be
1099                                  * any data to get (we've configured the
1100                                  * interface to not accept packets with
1101                                  * errors).
1102                                  */
1103
1104                                 /*
1105                                  * Enable 16bit access to shared memory first
1106                                  * on WD/SMC boards.
1107                                  */
1108                                 ed_enable_16bit_access(sc);
1109                                 ed_rint(sc);
1110                                 ed_disable_16bit_access(sc);
1111                         }
1112                 }
1113
1114                 /*
1115                  * If it looks like the transmitter can take more data,
1116                  * attempt to start output on the interface. This is done
1117                  * after handling the receiver to give the receiver priority.
1118                  */
1119                 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
1120                         ed_start_locked(ifp);
1121
1122                 /*
1123                  * return NIC CR to standard state: page 0, remote DMA
1124                  * complete, start (toggling the TXP bit off, even if was just
1125                  * set in the transmit routine, is *okay* - it is 'edge'
1126                  * triggered from low to high)
1127                  */
1128                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
1129
1130                 /*
1131                  * If the Network Talley Counters overflow, read them to reset
1132                  * them. It appears that old 8390's won't clear the ISR flag
1133                  * otherwise - resulting in an infinite loop.
1134                  */
1135                 if (isr & ED_ISR_CNT) {
1136                         (void) ed_nic_inb(sc, ED_P0_CNTR0);
1137                         (void) ed_nic_inb(sc, ED_P0_CNTR1);
1138                         (void) ed_nic_inb(sc, ED_P0_CNTR2);
1139                 }
1140         }
1141         ED_UNLOCK(sc);
1142 }
1143
1144 /*
1145  * Process an ioctl request.
1146  */
1147 static int
1148 ed_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1149 {
1150         struct ed_softc *sc = ifp->if_softc;
1151         struct ifreq *ifr = (struct ifreq *)data;
1152         int     error = 0;
1153
1154         /*
1155          * XXX really needed?
1156          */
1157         if (sc == NULL) {
1158                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1159                 return (ENXIO);
1160         }
1161
1162         switch (command) {
1163         case SIOCSIFFLAGS:
1164                 /*
1165                  * If the interface is marked up and stopped, then start it.
1166                  * If we're up and already running, then it may be a mediachg.
1167                  * If it is marked down and running, then stop it.
1168                  */
1169                 ED_LOCK(sc);
1170                 if (ifp->if_flags & IFF_UP) {
1171                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1172                                 ed_init_locked(sc);
1173                         else if (sc->sc_mediachg)
1174                                 sc->sc_mediachg(sc);
1175                 } else {
1176                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1177                                 ed_stop(sc);
1178                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1179                         }
1180                 }
1181
1182                 /*
1183                  * Promiscuous flag may have changed, so reprogram the RCR.
1184                  */
1185                 ed_setrcr(sc);
1186
1187                 ED_UNLOCK(sc);
1188                 break;
1189
1190         case SIOCADDMULTI:
1191         case SIOCDELMULTI:
1192                 /*
1193                  * Multicast list has changed; set the hardware filter
1194                  * accordingly.
1195                  */
1196                 ED_LOCK(sc);
1197                 ed_setrcr(sc);
1198                 ED_UNLOCK(sc);
1199                 error = 0;
1200                 break;
1201
1202         case SIOCGIFMEDIA:
1203         case SIOCSIFMEDIA:
1204                 if (sc->sc_media_ioctl == NULL) {
1205                         error = EINVAL;
1206                         break;
1207                 }
1208                 sc->sc_media_ioctl(sc, ifr, command);
1209                 break;
1210
1211         default:
1212                 error = ether_ioctl(ifp, command, data);
1213                 break;
1214         }
1215         return (error);
1216 }
1217
1218 /*
1219  * Given a source and destination address, copy 'amount' of a packet from
1220  *      the ring buffer into a linear destination buffer. Takes into account
1221  *      ring-wrap.
1222  */
1223 static __inline void
1224 ed_ring_copy(struct ed_softc *sc, bus_size_t src, char *dst, u_short amount)
1225 {
1226         u_short tmp_amount;
1227
1228         /* does copy wrap to lower addr in ring buffer? */
1229         if (src + amount > sc->mem_end) {
1230                 tmp_amount = sc->mem_end - src;
1231                 /* copy amount up to end of NIC memory */
1232                 sc->readmem(sc, src, dst, tmp_amount);
1233                 amount -= tmp_amount;
1234                 src = sc->mem_ring;
1235                 dst += tmp_amount;
1236         }
1237         sc->readmem(sc, src, dst, amount);
1238 }
1239
1240 /*
1241  * Retreive packet from shared memory and send to the next level up via
1242  * ether_input().
1243  */
1244 static void
1245 ed_get_packet(struct ed_softc *sc, bus_size_t buf, u_short len)
1246 {
1247         struct ifnet *ifp = sc->ifp;
1248         struct ether_header *eh;
1249         struct mbuf *m;
1250
1251         /* Allocate a header mbuf */
1252         MGETHDR(m, M_DONTWAIT, MT_DATA);
1253         if (m == NULL)
1254                 return;
1255         m->m_pkthdr.rcvif = ifp;
1256         m->m_pkthdr.len = m->m_len = len;
1257
1258         /*
1259          * We always put the received packet in a single buffer -
1260          * either with just an mbuf header or in a cluster attached
1261          * to the header. The +2 is to compensate for the alignment
1262          * fixup below.
1263          */
1264         if ((len + 2) > MHLEN) {
1265                 /* Attach an mbuf cluster */
1266                 MCLGET(m, M_DONTWAIT);
1267
1268                 /* Insist on getting a cluster */
1269                 if ((m->m_flags & M_EXT) == 0) {
1270                         m_freem(m);
1271                         return;
1272                 }
1273         }
1274
1275         /*
1276          * The +2 is to longword align the start of the real packet.
1277          * This is important for NFS.
1278          */
1279         m->m_data += 2;
1280         eh = mtod(m, struct ether_header *);
1281
1282         /*
1283          * Get packet, including link layer address, from interface.
1284          */
1285         ed_ring_copy(sc, buf, (char *)eh, len);
1286
1287         m->m_pkthdr.len = m->m_len = len;
1288
1289         ED_UNLOCK(sc);
1290         (*ifp->if_input)(ifp, m);
1291         ED_LOCK(sc);
1292 }
1293
1294 /*
1295  * Supporting routines
1296  */
1297
1298 /*
1299  * Given a NIC memory source address and a host memory destination
1300  *      address, copy 'amount' from NIC to host using shared memory.
1301  *      The 'amount' is rounded up to a word - okay as long as mbufs
1302  *              are word sized.  That's what the +1 is below.
1303  * This routine accesses things as 16 bit quantities.
1304  */
1305 void
1306 ed_shmem_readmem16(struct ed_softc *sc, bus_size_t src, uint8_t *dst,
1307     uint16_t amount)
1308 {
1309         bus_space_read_region_2(sc->mem_bst, sc->mem_bsh, src, (uint16_t *)dst,
1310             (amount + 1) / 2);
1311 }
1312
1313 /*
1314  * Given a NIC memory source address and a host memory destination
1315  *      address, copy 'amount' from NIC to host using shared memory.
1316  * This routine accesses things as 8 bit quantities.
1317  */
1318 void
1319 ed_shmem_readmem8(struct ed_softc *sc, bus_size_t src, uint8_t *dst,
1320     uint16_t amount)
1321 {
1322         bus_space_read_region_1(sc->mem_bst, sc->mem_bsh, src, dst, amount);
1323 }
1324
1325 /*
1326  * Given a NIC memory source address and a host memory destination
1327  *      address, copy 'amount' from NIC to host using Programmed I/O.
1328  *      The 'amount' is rounded up to a word - okay as long as mbufs
1329  *              are word sized.
1330  *      This routine is currently Novell-specific.
1331  */
1332 void
1333 ed_pio_readmem(struct ed_softc *sc, bus_size_t src, uint8_t *dst,
1334     uint16_t amount)
1335 {
1336         /* Regular Novell cards */
1337         /* select page 0 registers */
1338         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
1339
1340         /* round up to a word */
1341         if (amount & 1)
1342                 ++amount;
1343
1344         /* set up DMA byte count */
1345         ed_nic_outb(sc, ED_P0_RBCR0, amount);
1346         ed_nic_outb(sc, ED_P0_RBCR1, amount >> 8);
1347
1348         /* set up source address in NIC mem */
1349         ed_nic_outb(sc, ED_P0_RSAR0, src);
1350         ed_nic_outb(sc, ED_P0_RSAR1, src >> 8);
1351
1352         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD0 | ED_CR_STA);
1353
1354         if (sc->isa16bit)
1355                 ed_asic_insw(sc, ED_NOVELL_DATA, dst, amount / 2);
1356         else
1357                 ed_asic_insb(sc, ED_NOVELL_DATA, dst, amount);
1358 }
1359
1360 /*
1361  * Stripped down routine for writing a linear buffer to NIC memory.
1362  *      Only used in the probe routine to test the memory. 'len' must
1363  *      be even.
1364  */
1365 void
1366 ed_pio_writemem(struct ed_softc *sc, uint8_t *src, uint16_t dst, uint16_t len)
1367 {
1368         int     maxwait = 200;  /* about 240us */
1369
1370         /* select page 0 registers */
1371         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
1372
1373         /* reset remote DMA complete flag */
1374         ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC);
1375
1376         /* set up DMA byte count */
1377         ed_nic_outb(sc, ED_P0_RBCR0, len);
1378         ed_nic_outb(sc, ED_P0_RBCR1, len >> 8);
1379
1380         /* set up destination address in NIC mem */
1381         ed_nic_outb(sc, ED_P0_RSAR0, dst);
1382         ed_nic_outb(sc, ED_P0_RSAR1, dst >> 8);
1383
1384         /* set remote DMA write */
1385         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
1386
1387         if (sc->isa16bit)
1388                 ed_asic_outsw(sc, ED_NOVELL_DATA, src, len / 2);
1389         else
1390                 ed_asic_outsb(sc, ED_NOVELL_DATA, src, len);
1391
1392         /*
1393          * Wait for remote DMA complete. This is necessary because on the
1394          * transmit side, data is handled internally by the NIC in bursts and
1395          * we can't start another remote DMA until this one completes. Not
1396          * waiting causes really bad things to happen - like the NIC
1397          * irrecoverably jamming the ISA bus.
1398          */
1399         while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) &&
1400             --maxwait)
1401                 continue;
1402 }
1403
1404 /*
1405  * Write an mbuf chain to the destination NIC memory address using
1406  *      programmed I/O.
1407  */
1408 u_short
1409 ed_pio_write_mbufs(struct ed_softc *sc, struct mbuf *m, bus_size_t dst)
1410 {
1411         struct ifnet *ifp = sc->ifp;
1412         unsigned short total_len, dma_len;
1413         struct mbuf *mp;
1414         int     maxwait = 200;  /* about 240us */
1415
1416         ED_ASSERT_LOCKED(sc);
1417
1418         /* Regular Novell cards */
1419         /* First, count up the total number of bytes to copy */
1420         for (total_len = 0, mp = m; mp; mp = mp->m_next)
1421                 total_len += mp->m_len;
1422
1423         dma_len = total_len;
1424         if (sc->isa16bit && (dma_len & 1))
1425                 dma_len++;
1426
1427         /* select page 0 registers */
1428         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA);
1429
1430         /* reset remote DMA complete flag */
1431         ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC);
1432
1433         /* set up DMA byte count */
1434         ed_nic_outb(sc, ED_P0_RBCR0, dma_len);
1435         ed_nic_outb(sc, ED_P0_RBCR1, dma_len >> 8);
1436
1437         /* set up destination address in NIC mem */
1438         ed_nic_outb(sc, ED_P0_RSAR0, dst);
1439         ed_nic_outb(sc, ED_P0_RSAR1, dst >> 8);
1440
1441         /* set remote DMA write */
1442         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
1443
1444   /*
1445    * Transfer the mbuf chain to the NIC memory.
1446    * 16-bit cards require that data be transferred as words, and only words.
1447    * So that case requires some extra code to patch over odd-length mbufs.
1448    */
1449
1450         if (!sc->isa16bit) {
1451                 /* NE1000s are easy */
1452                 while (m) {
1453                         if (m->m_len)
1454                                 ed_asic_outsb(sc, ED_NOVELL_DATA, 
1455                                     m->m_data, m->m_len);
1456                         m = m->m_next;
1457                 }
1458         } else {
1459                 /* NE2000s are a pain */
1460                 unsigned char *data;
1461                 int len, wantbyte;
1462                 unsigned char savebyte[2];
1463
1464                 wantbyte = 0;
1465
1466                 while (m) {
1467                         len = m->m_len;
1468                         if (len) {
1469                                 data = mtod(m, caddr_t);
1470                                 /* finish the last word */
1471                                 if (wantbyte) {
1472                                         savebyte[1] = *data;
1473                                         ed_asic_outw(sc, ED_NOVELL_DATA,
1474                                                      *(u_short *)savebyte);
1475                                         data++;
1476                                         len--;
1477                                         wantbyte = 0;
1478                                 }
1479                                 /* output contiguous words */
1480                                 if (len > 1) {
1481                                         ed_asic_outsw(sc, ED_NOVELL_DATA,
1482                                                       data, len >> 1);
1483                                         data += len & ~1;
1484                                         len &= 1;
1485                                 }
1486                                 /* save last byte, if necessary */
1487                                 if (len == 1) {
1488                                         savebyte[0] = *data;
1489                                         wantbyte = 1;
1490                                 }
1491                         }
1492                         m = m->m_next;
1493                 }
1494                 /* spit last byte */
1495                 if (wantbyte)
1496                         ed_asic_outw(sc, ED_NOVELL_DATA, *(u_short *)savebyte);
1497         }
1498
1499         /*
1500          * Wait for remote DMA complete. This is necessary because on the
1501          * transmit side, data is handled internally by the NIC in bursts and
1502          * we can't start another remote DMA until this one completes. Not
1503          * waiting causes really bad things to happen - like the NIC
1504          * irrecoverably jamming the ISA bus.
1505          */
1506         while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) &&
1507             --maxwait)
1508                 continue;
1509
1510         if (!maxwait) {
1511                 log(LOG_WARNING, "%s: remote transmit DMA failed to complete\n",
1512                     ifp->if_xname);
1513                 ed_reset(ifp);
1514                 return(0);
1515         }
1516         return (total_len);
1517 }
1518
1519 static void
1520 ed_setrcr(struct ed_softc *sc)
1521 {
1522         struct ifnet *ifp = sc->ifp;
1523         int     i;
1524         u_char  reg1;
1525
1526         ED_ASSERT_LOCKED(sc);
1527
1528         /* Bit 6 in AX88190 RCR register must be set. */
1529         if (sc->chip_type == ED_CHIP_TYPE_AX88190)
1530                 reg1 = ED_RCR_INTT;
1531         else
1532                 reg1 = 0x00;
1533
1534         /* set page 1 registers */
1535         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
1536
1537         if (ifp->if_flags & IFF_PROMISC) {
1538
1539                 /*
1540                  * Reconfigure the multicast filter.
1541                  */
1542                 for (i = 0; i < 8; i++)
1543                         ed_nic_outb(sc, ED_P1_MAR(i), 0xff);
1544
1545                 /*
1546                  * And turn on promiscuous mode. Also enable reception of
1547                  * runts and packets with CRC & alignment errors.
1548                  */
1549                 /* Set page 0 registers */
1550                 ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
1551
1552                 ed_nic_outb(sc, ED_P0_RCR, ED_RCR_PRO | ED_RCR_AM |
1553                             ED_RCR_AB | ED_RCR_AR | ED_RCR_SEP | reg1);
1554         } else {
1555                 /* set up multicast addresses and filter modes */
1556                 if (ifp->if_flags & IFF_MULTICAST) {
1557                         uint32_t  mcaf[2];
1558
1559                         if (ifp->if_flags & IFF_ALLMULTI) {
1560                                 mcaf[0] = 0xffffffff;
1561                                 mcaf[1] = 0xffffffff;
1562                         } else
1563                                 ed_ds_getmcaf(sc, mcaf);
1564
1565                         /*
1566                          * Set multicast filter on chip.
1567                          */
1568                         for (i = 0; i < 8; i++)
1569                                 ed_nic_outb(sc, ED_P1_MAR(i), ((u_char *) mcaf)[i]);
1570
1571                         /* Set page 0 registers */
1572                         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
1573
1574                         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_AM | ED_RCR_AB | reg1);
1575                 } else {
1576
1577                         /*
1578                          * Initialize multicast address hashing registers to
1579                          * not accept multicasts.
1580                          */
1581                         for (i = 0; i < 8; ++i)
1582                                 ed_nic_outb(sc, ED_P1_MAR(i), 0x00);
1583
1584                         /* Set page 0 registers */
1585                         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP);
1586
1587                         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_AB | reg1);
1588                 }
1589         }
1590
1591         /*
1592          * Start interface.
1593          */
1594         ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA);
1595 }
1596
1597 /*
1598  * Compute the multicast address filter from the
1599  * list of multicast addresses we need to listen to.
1600  */
1601 static void
1602 ed_ds_getmcaf(struct ed_softc *sc, uint32_t *mcaf)
1603 {
1604         uint32_t index;
1605         u_char *af = (u_char *) mcaf;
1606         struct ifmultiaddr *ifma;
1607
1608         mcaf[0] = 0;
1609         mcaf[1] = 0;
1610
1611         IF_ADDR_LOCK(sc->ifp);
1612         TAILQ_FOREACH(ifma, &sc->ifp->if_multiaddrs, ifma_link) {
1613                 if (ifma->ifma_addr->sa_family != AF_LINK)
1614                         continue;
1615                 index = ether_crc32_be(LLADDR((struct sockaddr_dl *)
1616                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
1617                 af[index >> 3] |= 1 << (index & 7);
1618         }
1619         IF_ADDR_UNLOCK(sc->ifp);
1620 }
1621
1622 int
1623 ed_isa_mem_ok(device_t dev, u_long pmem, u_int memsize)
1624 {
1625         if (pmem < 0xa0000 || pmem + memsize > 0x1000000) {
1626                 device_printf(dev, "Invalid ISA memory address range "
1627                     "configured: 0x%lx - 0x%lx\n", pmem, pmem + memsize);
1628                 return (ENXIO);
1629         }
1630         return (0);
1631 }
1632
1633 int
1634 ed_clear_memory(device_t dev)
1635 {
1636         struct ed_softc *sc = device_get_softc(dev);
1637         bus_size_t i;
1638
1639         bus_space_set_region_1(sc->mem_bst, sc->mem_bsh, sc->mem_start,
1640             0, sc->mem_size);
1641
1642         for (i = 0; i < sc->mem_size; i++) {
1643                 if (bus_space_read_1(sc->mem_bst, sc->mem_bsh,
1644                     sc->mem_start + i)) {
1645                         device_printf(dev, "failed to clear shared memory at "
1646                           "0x%jx - check configuration\n",
1647                             (uintmax_t)rman_get_start(sc->mem_res) + i);
1648                         return (ENXIO);
1649                 }
1650         }
1651         return (0);
1652 }
1653             
1654 u_short
1655 ed_shmem_write_mbufs(struct ed_softc *sc, struct mbuf *m, bus_size_t dst)
1656 {
1657         u_short len;
1658
1659         /*
1660          * Special case setup for 16 bit boards...
1661          */
1662         if (sc->isa16bit) {
1663                 switch (sc->vendor) {
1664 #ifdef ED_3C503
1665                         /*
1666                          * For 16bit 3Com boards (which have 16k of
1667                          * memory), we have the xmit buffers in a
1668                          * different page of memory ('page 0') - so
1669                          * change pages.
1670                          */
1671                 case ED_VENDOR_3COM:
1672                         ed_asic_outb(sc, ED_3COM_GACFR, ED_3COM_GACFR_RSEL);
1673                         break;
1674 #endif
1675                         /*
1676                          * Enable 16bit access to shared memory on
1677                          * WD/SMC boards.
1678                          *
1679                          * XXX - same as ed_enable_16bit_access()
1680                          */
1681                 case ED_VENDOR_WD_SMC:
1682                         ed_asic_outb(sc, ED_WD_LAAR,
1683                             sc->wd_laar_proto | ED_WD_LAAR_M16EN);
1684                         if (sc->chip_type == ED_CHIP_TYPE_WD790)
1685                                 ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB);
1686                         break;
1687                 }
1688         }
1689         for (len = 0; m != 0; m = m->m_next) {
1690                 if (sc->isa16bit)
1691                         bus_space_write_region_2(sc->mem_bst,
1692                             sc->mem_bsh, dst,
1693                             mtod(m, uint16_t *), (m->m_len + 1)/ 2);
1694                 else
1695                         bus_space_write_region_1(sc->mem_bst,
1696                             sc->mem_bsh, dst,
1697                             mtod(m, uint8_t *), m->m_len);
1698                 dst += m->m_len;
1699                 len += m->m_len;
1700         }
1701
1702         /*
1703          * Restore previous shared memory access
1704          */
1705         if (sc->isa16bit) {
1706                 switch (sc->vendor) {
1707 #ifdef ED_3C503
1708                 case ED_VENDOR_3COM:
1709                         ed_asic_outb(sc, ED_3COM_GACFR,
1710                             ED_3COM_GACFR_RSEL | ED_3COM_GACFR_MBS0);
1711                         break;
1712 #endif
1713                 case ED_VENDOR_WD_SMC:
1714                         /* XXX - same as ed_disable_16bit_access() */
1715                         if (sc->chip_type == ED_CHIP_TYPE_WD790)
1716                                 ed_asic_outb(sc, ED_WD_MSR, 0x00);
1717                         ed_asic_outb(sc, ED_WD_LAAR,
1718                             sc->wd_laar_proto & ~ED_WD_LAAR_M16EN);
1719                         break;
1720                 }
1721         }
1722         return (len);
1723 }