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